21
21
22
22
import org .apache .lucene .util .BitSet ;
23
23
import org .apache .lucene .util .FixedBitSet ;
24
+ import org .apache .lucene .util .RamUsageEstimator ;
24
25
25
26
/**
26
27
* A {@link CountedBitSet} wraps a {@link FixedBitSet} but automatically releases the internal bitset
27
28
* when all bits are set to reduce memory usage. This structure can work well for sequence numbers
28
29
* from translog as these numbers are likely to form contiguous ranges (eg. filling all bits).
29
30
*/
30
31
final class CountedBitSet extends BitSet {
32
+ static final long BASE_RAM_BYTES_USED = RamUsageEstimator .shallowSizeOfInstance (CountedBitSet .class );
31
33
private short onBits ; // Number of bits are set.
32
34
private FixedBitSet bitset ;
33
35
34
36
CountedBitSet (short numBits ) {
35
- assert numBits > 0 ;
37
+ if (numBits <= 0 ) {
38
+ throw new IllegalArgumentException ("Number of bits must be positive. Given [" + numBits + "]" );
39
+ }
36
40
this .onBits = 0 ;
37
41
this .bitset = new FixedBitSet (numBits );
38
42
}
@@ -41,7 +45,6 @@ final class CountedBitSet extends BitSet {
41
45
public boolean get (int index ) {
42
46
assert 0 <= index && index < this .length ();
43
47
assert bitset == null || onBits < bitset .length () : "Bitset should be released when all bits are set" ;
44
-
45
48
return bitset == null ? true : bitset .get (index );
46
49
}
47
50
@@ -52,7 +55,7 @@ public void set(int index) {
52
55
53
56
// Ignore set when bitset is full.
54
57
if (bitset != null ) {
55
- boolean wasOn = bitset .getAndSet (index );
58
+ final boolean wasOn = bitset .getAndSet (index );
56
59
if (wasOn == false ) {
57
60
onBits ++;
58
61
// Once all bits are set, we can simply just return YES for all indexes.
@@ -66,12 +69,12 @@ public void set(int index) {
66
69
67
70
@ Override
68
71
public void clear (int startIndex , int endIndex ) {
69
- throw new UnsupportedOperationException ("Not implemented yet" );
72
+ throw new UnsupportedOperationException ();
70
73
}
71
74
72
75
@ Override
73
76
public void clear (int index ) {
74
- throw new UnsupportedOperationException ("Not implemented yet" );
77
+ throw new UnsupportedOperationException ();
75
78
}
76
79
77
80
@ Override
@@ -86,20 +89,19 @@ public int length() {
86
89
87
90
@ Override
88
91
public int prevSetBit (int index ) {
89
- throw new UnsupportedOperationException ("Not implemented yet" );
92
+ throw new UnsupportedOperationException ();
90
93
}
91
94
92
95
@ Override
93
96
public int nextSetBit (int index ) {
94
- throw new UnsupportedOperationException ("Not implemented yet" );
97
+ throw new UnsupportedOperationException ();
95
98
}
96
99
97
100
@ Override
98
101
public long ramBytesUsed () {
99
- throw new UnsupportedOperationException ( "Not implemented yet" );
102
+ return BASE_RAM_BYTES_USED + ( bitset == null ? 0 : bitset . ramBytesUsed () );
100
103
}
101
104
102
- // Exposed for testing
103
105
boolean isInternalBitsetReleased () {
104
106
return bitset == null ;
105
107
}
0 commit comments