19
19
20
20
import java .io .IOException ;
21
21
22
- import org .apache .lucene .store .IOContext ;
22
+ import org .apache .lucene .store .Directory ;
23
23
import org .apache .lucene .store .MockDirectoryWrapper ;
24
24
import org .apache .lucene .store .RAMDirectory ;
25
25
@@ -153,7 +153,7 @@ private void doTestWriteRead(int n) throws Exception {
153
153
assertTrue (doCompare (bv ,compare ));
154
154
}
155
155
}
156
-
156
+
157
157
/**
158
158
* Test r/w when size/count cause switching between bit-set and d-gaps file formats.
159
159
*/
@@ -165,6 +165,26 @@ public void testDgaps() throws IOException {
165
165
doTestDgaps (10000 ,40 ,43 );
166
166
doTestDgaps (100000 ,415 ,418 );
167
167
doTestDgaps (1000000 ,3123 ,3126 );
168
+ // now exercise skipping of fully populated byte in the bitset (they are omitted if bitset is sparse)
169
+ MockDirectoryWrapper d = new MockDirectoryWrapper (random , new RAMDirectory ());
170
+ d .setPreventDoubleWrite (false );
171
+ BitVector bv = new BitVector (10000 );
172
+ bv .set (0 );
173
+ for (int i = 8 ; i < 16 ; i ++) {
174
+ bv .set (i );
175
+ } // make sure we have once byte full of set bits
176
+ for (int i = 32 ; i < 40 ; i ++) {
177
+ bv .set (i );
178
+ } // get a second byte full of set bits
179
+ // add some more bits here
180
+ for (int i = 40 ; i < 10000 ; i ++) {
181
+ if (random .nextInt (1000 ) == 0 ) {
182
+ bv .set (i );
183
+ }
184
+ }
185
+ bv .write (d , "TESTBV" , newIOContext (random ));
186
+ BitVector compare = new BitVector (d , "TESTBV" , newIOContext (random ));
187
+ assertTrue (doCompare (bv ,compare ));
168
188
}
169
189
170
190
private void doTestDgaps (int size , int count1 , int count2 ) throws IOException {
@@ -183,7 +203,7 @@ private void doTestDgaps(int size, int count1, int count2) throws IOException {
183
203
assertTrue (doCompare (bv ,bv2 ));
184
204
bv = bv2 ;
185
205
bv .clear (i );
186
- assertEquals (i +1 ,size -bv .count ());
206
+ assertEquals (i +1 , size -bv .count ());
187
207
bv .write (d , "TESTBV" , newIOContext (random ));
188
208
}
189
209
// now start decreasing number of set bits
@@ -196,6 +216,54 @@ private void doTestDgaps(int size, int count1, int count2) throws IOException {
196
216
bv .write (d , "TESTBV" , newIOContext (random ));
197
217
}
198
218
}
219
+
220
+ public void testSparseWrite () throws IOException {
221
+ Directory d = newDirectory ();
222
+ final int numBits = 10240 ;
223
+ BitVector bv = new BitVector (numBits );
224
+ bv .invertAll ();
225
+ int numToClear = random .nextInt (5 );
226
+ for (int i =0 ;i <numToClear ;i ++) {
227
+ bv .clear (random .nextInt (numBits ));
228
+ }
229
+ bv .write (d , "test" , newIOContext (random ));
230
+ final long size = d .fileLength ("test" );
231
+ assertTrue ("size=" + size , size < 100 );
232
+ d .close ();
233
+ }
234
+
235
+ public void testClearedBitNearEnd () throws IOException {
236
+ Directory d = newDirectory ();
237
+ final int numBits = _TestUtil .nextInt (random , 7 , 1000 );
238
+ BitVector bv = new BitVector (numBits );
239
+ bv .invertAll ();
240
+ bv .clear (numBits -_TestUtil .nextInt (random , 1 , 7 ));
241
+ bv .write (d , "test" , newIOContext (random ));
242
+ assertEquals (numBits -1 , bv .count ());
243
+ d .close ();
244
+ }
245
+
246
+ public void testMostlySet () throws IOException {
247
+ Directory d = newDirectory ();
248
+ final int numBits = _TestUtil .nextInt (random , 30 , 1000 );
249
+ for (int numClear =0 ;numClear <20 ;numClear ++) {
250
+ BitVector bv = new BitVector (numBits );
251
+ bv .invertAll ();
252
+ int count = 0 ;
253
+ while (count < numClear ) {
254
+ final int bit = random .nextInt (numBits );
255
+ // Don't use getAndClear, so that count is recomputed
256
+ if (bv .get (bit )) {
257
+ bv .clear (bit );
258
+ count ++;
259
+ assertEquals (numBits -count , bv .count ());
260
+ }
261
+ }
262
+ }
263
+
264
+ d .close ();
265
+ }
266
+
199
267
/**
200
268
* Compare two BitVectors.
201
269
* This should really be an equals method on the BitVector itself.
@@ -211,6 +279,7 @@ private boolean doCompare(BitVector bv, BitVector compare) {
211
279
break ;
212
280
}
213
281
}
282
+ assertEquals (bv .count (), compare .count ());
214
283
return equal ;
215
284
}
216
285
}
0 commit comments