|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.common.util; |
| 10 | + |
| 11 | +import org.apache.lucene.store.IndexInput; |
| 12 | +import org.apache.lucene.store.IndexOutput; |
| 13 | +import org.apache.lucene.store.RandomAccessInput; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | + |
| 17 | +/** |
| 18 | + * A bitset backed by a byte array. This will initialize and set bits in the byte array based on the index. |
| 19 | + */ |
| 20 | +public class ByteArrayBackedBitset { |
| 21 | + private final byte[] byteArray; |
| 22 | + |
| 23 | + /** |
| 24 | + * Constructor which uses an on heap list. This should be using during construction of the bitset. |
| 25 | + */ |
| 26 | + public ByteArrayBackedBitset(int capacity) { |
| 27 | + byteArray = new byte[capacity]; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Constructor which set the Lucene's RandomAccessInput to read the bitset into a read-only buffer. |
| 32 | + */ |
| 33 | + public ByteArrayBackedBitset(RandomAccessInput in, long offset, int length) throws IOException { |
| 34 | + byteArray = new byte[length]; |
| 35 | + int i = 0; |
| 36 | + while (i < length) { |
| 37 | + byteArray[i] = in.readByte(offset + i); |
| 38 | + i++; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Constructor which set the Lucene's IndexInput to read the bitset into a read-only buffer. |
| 44 | + */ |
| 45 | + public ByteArrayBackedBitset(IndexInput in, int length) throws IOException { |
| 46 | + byteArray = new byte[length]; |
| 47 | + int i = 0; |
| 48 | + while (i < length) { |
| 49 | + byteArray[i] = in.readByte(); |
| 50 | + i++; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Sets the bit at the given index to 1. |
| 56 | + * Each byte can indicate 8 bits, so the index is divided by 8 to get the byte array index. |
| 57 | + * @param index the index to set the bit |
| 58 | + */ |
| 59 | + public void set(int index) { |
| 60 | + int byteArrIndex = index >> 3; |
| 61 | + byteArray[byteArrIndex] |= (byte) (1 << (index & 7)); |
| 62 | + } |
| 63 | + |
| 64 | + public int write(IndexOutput output) throws IOException { |
| 65 | + int numBytes = 0; |
| 66 | + for (Byte bitSet : byteArray) { |
| 67 | + output.writeByte(bitSet); |
| 68 | + numBytes += Byte.BYTES; |
| 69 | + } |
| 70 | + return numBytes; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Retrieves whether the bit is set or not at the given index. |
| 75 | + * @param index the index to look up for the bit |
| 76 | + * @return true if bit is set, false otherwise |
| 77 | + */ |
| 78 | + public boolean get(int index) throws IOException { |
| 79 | + int byteArrIndex = index >> 3; |
| 80 | + return (byteArray[byteArrIndex] & (1 << (index & 7))) != 0; |
| 81 | + } |
| 82 | + |
| 83 | + public int getCurrBytesRead() { |
| 84 | + return byteArray.length; |
| 85 | + } |
| 86 | +} |
0 commit comments