2121
2222import org .apache .lucene .util .BitSet ;
2323import org .apache .lucene .util .FixedBitSet ;
24+ import org .apache .lucene .util .RamUsageEstimator ;
2425
2526/**
2627 * A {@link CountedBitSet} wraps a {@link FixedBitSet} but automatically releases the internal bitset
2728 * when all bits are set to reduce memory usage. This structure can work well for sequence numbers
2829 * from translog as these numbers are likely to form contiguous ranges (eg. filling all bits).
2930 */
3031final class CountedBitSet extends BitSet {
32+ static final long BASE_RAM_BYTES_USED = RamUsageEstimator .shallowSizeOfInstance (CountedBitSet .class );
3133 private short onBits ; // Number of bits are set.
3234 private FixedBitSet bitset ;
3335
3436 CountedBitSet (short numBits ) {
35- assert numBits > 0 ;
37+ if (numBits <= 0 ) {
38+ throw new IllegalArgumentException ("Number of bits must be positive. Given [" + numBits + "]" );
39+ }
3640 this .onBits = 0 ;
3741 this .bitset = new FixedBitSet (numBits );
3842 }
@@ -41,7 +45,6 @@ final class CountedBitSet extends BitSet {
4145 public boolean get (int index ) {
4246 assert 0 <= index && index < this .length ();
4347 assert bitset == null || onBits < bitset .length () : "Bitset should be released when all bits are set" ;
44-
4548 return bitset == null ? true : bitset .get (index );
4649 }
4750
@@ -52,7 +55,7 @@ public void set(int index) {
5255
5356 // Ignore set when bitset is full.
5457 if (bitset != null ) {
55- boolean wasOn = bitset .getAndSet (index );
58+ final boolean wasOn = bitset .getAndSet (index );
5659 if (wasOn == false ) {
5760 onBits ++;
5861 // Once all bits are set, we can simply just return YES for all indexes.
@@ -66,12 +69,12 @@ public void set(int index) {
6669
6770 @ Override
6871 public void clear (int startIndex , int endIndex ) {
69- throw new UnsupportedOperationException ("Not implemented yet" );
72+ throw new UnsupportedOperationException ();
7073 }
7174
7275 @ Override
7376 public void clear (int index ) {
74- throw new UnsupportedOperationException ("Not implemented yet" );
77+ throw new UnsupportedOperationException ();
7578 }
7679
7780 @ Override
@@ -86,20 +89,19 @@ public int length() {
8689
8790 @ Override
8891 public int prevSetBit (int index ) {
89- throw new UnsupportedOperationException ("Not implemented yet" );
92+ throw new UnsupportedOperationException ();
9093 }
9194
9295 @ Override
9396 public int nextSetBit (int index ) {
94- throw new UnsupportedOperationException ("Not implemented yet" );
97+ throw new UnsupportedOperationException ();
9598 }
9699
97100 @ Override
98101 public long ramBytesUsed () {
99- throw new UnsupportedOperationException ( "Not implemented yet" );
102+ return BASE_RAM_BYTES_USED + ( bitset == null ? 0 : bitset . ramBytesUsed () );
100103 }
101104
102- // Exposed for testing
103105 boolean isInternalBitsetReleased () {
104106 return bitset == null ;
105107 }
0 commit comments