forked from hyan/kamikaze
/
P4DDocIdSet.java
577 lines (466 loc) · 13.2 KB
/
P4DDocIdSet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
package com.kamikaze.docidset.impl;
import java.io.Serializable;
import org.apache.lucene.search.DocIdSetIterator;
import com.kamikaze.docidset.api.StatefulDSIterator;
import com.kamikaze.docidset.bitset.MyOpenBitSet;
import com.kamikaze.docidset.compression.P4DSetNoBase;
import com.kamikaze.docidset.utils.IntArray;
/**
* Doc id set wrapper around P4DSet
*
*
* @author abhasin
*
*/
public class P4DDocIdSet extends AbstractDocSet implements Serializable {
private static final long serialVersionUID = 1L;
private static final int DEFAULT_B = 5;
/**
* Utitlity Object compression.
*/
private P4DSetNoBase compressedSet = new P4DSetNoBase();
/**
* List for the base integer values of the compressed batches.
*/
private IntArray baseList = null;
public P4DDocIdSet() {
baseList = new IntArray();
compressedBits = 0;
}
public P4DDocIdSet(int batchSize) {
this();
this.BATCH_SIZE = batchSize;
this.BATCH_OVER = batchSize / 20;
}
@Override
public final boolean isCacheable() {
return true;
}
@Override
protected Object compress() {
current[0] = 0;
compressedSet.setParam(current_base, current_b, BATCH_SIZE,
current_ex_count);
baseList.add(current_base);
return compressedSet.compressAlt(current);
}
/**
* Method to decompress the entire batch
*
* @param blob MyOpenBitSet
* @return int array with decompressed segment of numbers
*/
protected int[] decompress(MyOpenBitSet blob) {
return new P4DSetNoBase().decompress(blob);
}
/**
* Binary search
*
* @param val
* @param begin
* @param end
* @return index greater than or equal to the target. -1 if the target is out
* of range.
*/
protected int binarySearchForNearest(int val, int begin, int end) {
int mid = (begin + end) / 2;
if (mid == end || (baseList.get(mid) <= val && baseList.get(mid + 1) > val))
return mid;
else if (baseList.get(mid) < val)
return binarySearchForNearest(val, mid + 1, end);
else
return binarySearchForNearest(val, begin, mid);
}
protected int binarySearchForNearestAlt(int val, int begin, int end)
{
while(true)
{
int mid = (begin+end)/2;
if(mid==end || (baseList.get(mid) <= val && baseList.get(mid + 1) > val))
return mid;
else if(baseList.get(mid) < val)
{
begin = mid+1;
}
else
{
end=mid;
}
}
}
class P4DDocIdSetIterator extends StatefulDSIterator implements Serializable {
private static final long serialVersionUID = 1L;
/**
* Address bits
*
*/
int ADDRESS_BITS = (int) (Math.log(BATCH_SIZE) / Math.log(2));
/**
* retaining Offset from the list of blobs from the iterator pov
*
*/
int cursor = -1;
/**
* Current iterating batch index.
*
*/
int bi = -1;
/**
* Current iterating offset.
*
*/
int offset = 0;
/**
* doc() returned
*
*/
int lastReturn = -1;
/**
* size of the set
*
*/
int size = size();
/**
* Reference to the blob iterating
*
*/
long[] ref = null;
/**
* Reference to the blob iterating
*
*/
int blobSize = blob.size();
P4DSetNoBase localCompressedSet = new P4DSetNoBase();
P4DDocIdSetIterator() {
super();
localCompressedSet.setParam(0, DEFAULT_B, BATCH_SIZE, BATCH_OVER);
}
@Override
public int docID() {
return lastReturn;
}
/**
* Method to allow iteration in decompressed form
public int get(OpenBitSet set, int index) {
return compressedSet.get(set, index);
}*/
/**
* Method to allow iteration in decompressed form
*/
public int get(long[] set, int index) {
return localCompressedSet.get(set, index);
}
@Override
public int nextDoc() {
// increment the cursor and check if it falls in the range for the
// number of batches, if not return false else, its within range
if (++cursor < size) {
// We are already in the array
if (bi == blobSize) {
if (offset == -1) {
lastReturn = DocIdSetIterator.NO_MORE_DOCS;
return DocIdSetIterator.NO_MORE_DOCS;
} else
lastReturn += current[offset++];
}
// if we are not in the array but on the boundary of a batch
// update local blob and set params
else if (offset == 0) {
bi = batchIndex(cursor);
if (bi < blobSize) {
lastReturn = baseList.get(bi);
ref = blob.get(bi);
localCompressedSet.updateParams(ref);
offset++;// cursor - (bi << ADDRESS_BITS);+1
} else {
// offset = 0;//cursor - (bi << ADDRESS_BITS);
lastReturn = current[offset++];
}
} else {
lastReturn += localCompressedSet.get(ref, offset);
offset = (++offset) % BATCH_SIZE;
}
return lastReturn;
}
lastReturn = DocIdSetIterator.NO_MORE_DOCS;
return DocIdSetIterator.NO_MORE_DOCS;
}
/**
* Get the index of the batch this cursor position falls into
*
* @param index
* @return
*/
private int batchIndex(int index) {
return index >> ADDRESS_BITS;
}
/**
* Next need be called after skipping.
*
*/
@Override
public int advance(int target) {
if (target <= lastReturn) target = lastReturn + 1;
// NOTE : Update lastReturn.
if (bi == blobSize || (bi + 1 < blobSize && target < baseList.get(bi + 1))) {
while (nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
if (lastReturn >= target) return lastReturn;
}
lastReturn = DocIdSetIterator.NO_MORE_DOCS;
return DocIdSetIterator.NO_MORE_DOCS;
}
// If the target is outside the compressed space
if (blobSize == 0 || target >= current[0]) {
bi = blobSize;
ref = null;
offset = findAndUpdate(current, target);
if (offset > 0) {
cursor = blobSize * BATCH_SIZE + offset - 1;
return lastReturn;
}
// We have gone over the batch boundary
else if (offset == 0) {
cursor = (blobSize + 1) * BATCH_SIZE;
return lastReturn;
}
lastReturn = DocIdSetIterator.NO_MORE_DOCS;
return DocIdSetIterator.NO_MORE_DOCS;
}
// This returns the blob where base value is less than the value looked
// for.
int index = binarySearchForNearest(target, bi, blobSize - 1);
// Move both these further, as we are in this block, so that
// doc() call works.
bi = index;
lastReturn = baseList.get(index);
ref = blob.get(index);
localCompressedSet.updateParams(ref);
// find the nearest integer in the compressed space.
offset = findAndUpdate(ref, target, lastReturn);
if (offset < 0) {
// oops we fell into the gap. This case happens when we land
// in the gap between two batches. We can optimize this
// step.
if (++index < blobSize) {
lastReturn = baseList.get(index);
ref = blob.get(index);
localCompressedSet.updateParams(ref);
}
else {
lastReturn = current[0];
ref = null;
}
bi = index;
offset = 1;
}
cursor = bi * BATCH_SIZE + offset - 1;
return lastReturn;
}
/* private void printSet(MyOpenBitSet test, int base) {
try {
int localBase = base;
for (int i = 1; i < BATCH_SIZE; i++) {
localBase += compressedSet.get(test, i);
System.out.print(localBase + ",");
}
} catch (Exception e) {
e.printStackTrace();
int localBase = base;
int testint[] = compressedSet.decompress(test);
for (int i = 1; i < BATCH_SIZE; i++) {
localBase += testint[i];
System.out.print(localBase + ",");
}
}
}*/
private void printSet(long[] test, int base) {
try {
int localBase = base;
for (int i = 1; i < BATCH_SIZE; i++) {
localBase += localCompressedSet.get(test, i);
System.out.print(localBase + ",");
}
} catch (Exception e) {
e.printStackTrace();
int localBase = base;
int testint[] = localCompressedSet.decompress(test);
for (int i = 1; i < BATCH_SIZE; i++) {
localBase += testint[i];
System.out.print(localBase + ",");
}
}
}
/**
* Find the element in the compressed set
*
* @param next
* @param target
* @param base
* @return
*/
private int findAndUpdate(long[] next, int target, int base) {
lastReturn = base;
if (lastReturn >= target)
return 1;
for (int i = 1; i < BATCH_SIZE; i++) {
// System.out.println("Getting "+i);
// System.out.flush();
lastReturn += localCompressedSet.get(next, i);
if (lastReturn >= target) {
// if(i==127)
return (i + 1) % BATCH_SIZE;
}
}
return -1;
}
/**
* Find the element in the compressed set
*
* @param next
* @param target
* @param base
* @return
private int findAndUpdate(MyOpenBitSet next, int target, int base) {
lastReturn = base;
if (lastReturn >= target)
return 1;
for (int i = 1; i < BATCH_SIZE; i++) {
// System.out.println("Getting "+i);
// System.out.flush();
lastReturn += compressedSet.get(next, i);
if (lastReturn >= target) {
// if(i==127)
return (i + 1) % BATCH_SIZE;
}
}
return -1;
}*/
/**
* Find the element in the set and update parameters.
*
*/
private int findAndUpdate(int[] array, int target) {
if(array==null)
return -1;
lastReturn = array[0];
if (lastReturn >= target)
return 1;
for (int i = 1; i < current_size; i++) {
lastReturn += array[i];
if (lastReturn >= target)
return (i + 1) % BATCH_SIZE;
}
return -1;
}
public int getCursor() {
return cursor;
}
}
public P4DDocIdSetIterator iterator() {
return new P4DDocIdSetIterator();
}
@Override
public int findWithIndex(int val) {
P4DDocIdSetIterator dcit = new P4DDocIdSetIterator();
int docid = dcit.advance(val);
if (docid == val)
return dcit.getCursor();
return -1;
}
@Override
public boolean find(int val)
{
long time = System.nanoTime();
int local = 0;
if(size()==0)
return false;
//Short Circuit case where its not in the set at all
if(val>lastAdded||val<baseList.get(0))
{
//System.out.println("Time to perform BinarySearch for:"+val+":"+(System.nanoTime() - time));
return false;
}
// We are in the set
else if(val>=current_base)
{
int i=0;
for( i=0;i<current_size;i++)
{
local+=current[i];
if(local>val)
break;
}
if(i==current_size)
return local == val;
else
return (local-current[i] == val);
}
// We are in the compressed space
else
{
if(baseList.size() == 0)
return false;
int blobIndex = binarySearchForNearest(val, 0, blob.size() - 1 );
local = baseList.get(blobIndex);
long[] ref = blob.get(blobIndex);
P4DSetNoBase localCompressedSet = new P4DSetNoBase();
localCompressedSet.setParam(0, DEFAULT_B, BATCH_SIZE, BATCH_OVER);
localCompressedSet.updateParams(ref);
int i = 0;
for(i=0;i<BATCH_SIZE;i++)
{
local+=localCompressedSet.get(ref, i);
if(local>val)
{
break;
}
}
if(i==BATCH_SIZE)
return local == val;
else
return (local-localCompressedSet.get(ref,i))==val;
}
}
private int findIn(MyOpenBitSet myOpenBitSet, int baseVal, int val) {
return -1;
}
private int findIn(int[] current, Integer baseVal, int val) {
int local = baseVal;
for (int i = 1; i < BATCH_SIZE; i++) {
local += current[i];
if (val > local) {
if (local == val)
return i;
} else
return -1;
}
return -1;
}
@Override
public void optimize()
{
//Trim the baselist to size
this.baseList.seal();
this.blob.seal();
}
@Override
public long sizeInBytes()
{
// 64 is the overhead for an int array
// blobsize * numberofelements * 1.1 (Object Overhead)
// batch_size * 4 + int array overhead
// P4dDocIdSet Overhead 110
optimize();
return (long) (baseList.length()*4 + 64 +blob.length()*BATCH_SIZE*1.1 + BATCH_SIZE*4 + 24 + 110);
}
public int totalBlobSize()
{
int total = 0;
for(int i = blob.length() - 1; i >= 0; i--)
{
long[] segment = blob.get(i);
total += segment.length;
}
return total;
}
}