Skip to content

Commit 01c7491

Browse files
Merge pull request #564 from apache/quotient-filter
simplify insertion
2 parents c7b970a + 62776da commit 01c7491

File tree

1 file changed

+8
-37
lines changed

1 file changed

+8
-37
lines changed

src/main/java/org/apache/datasketches/filters/quotientfilter/QuotientFilter.java

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,6 @@ long swap_fingerprints(long index, long new_fingerprint) {
379379
return existing;
380380
}
381381

382-
// finds the first empty slot after the given slot index
383-
long find_first_empty_slot(long index) {
384-
while (!is_slot_empty(index)) {
385-
index = (index + 1) & getMask();
386-
}
387-
return index;
388-
}
389-
390382
// return the first slot to the right where the current run starting at the index parameter ends
391383
long find_new_run_location(long index) {
392384
if (!is_slot_empty(index)) {
@@ -399,14 +391,13 @@ long find_new_run_location(long index) {
399391
}
400392

401393
boolean insert_new_run(long canonical_slot, long long_fp) {
402-
long first_empty_slot = find_first_empty_slot(canonical_slot); // finds the first empty slot to the right of the canonical slot that is empty
403394
long preexisting_run_start_index = find_run_start(canonical_slot); // scans the cluster leftwards and then to the right until reaching our run's would be location
404395
long start_of_this_new_run = find_new_run_location(preexisting_run_start_index); // If there is already a run at the would-be location, find its end and insert the new run after it
405396
boolean slot_initially_empty = is_slot_empty(start_of_this_new_run);
406397

407398
// modify some metadata flags to mark the new run
408399
set_occupied(canonical_slot, true);
409-
if (first_empty_slot != canonical_slot) {
400+
if (start_of_this_new_run != canonical_slot) {
410401
set_shifted(start_of_this_new_run, true);
411402
}
412403
set_continuation(start_of_this_new_run, false);
@@ -417,26 +408,7 @@ boolean insert_new_run(long canonical_slot, long long_fp) {
417408
num_entries++;
418409
return true;
419410
}
420-
421-
// push all entries one slot to the right
422-
// if we inserted this run in the middle of a cluster
423-
long current_index = start_of_this_new_run;
424-
boolean is_this_slot_empty;
425-
boolean temp_continuation = false;
426-
do {
427-
is_this_slot_empty = is_slot_empty(current_index);
428-
long_fp = swap_fingerprints(current_index, long_fp);
429-
430-
if (current_index != start_of_this_new_run) {
431-
set_shifted(current_index, true);
432-
boolean current_continuation = is_continuation(current_index);
433-
set_continuation(current_index, temp_continuation);
434-
temp_continuation = current_continuation;
435-
}
436-
current_index = (current_index + 1) & getMask();
437-
} while (!is_this_slot_empty);
438-
num_entries++;
439-
return true;
411+
return insert_fingerprint_and_push_all_else(long_fp, start_of_this_new_run, false);
440412
}
441413

442414
boolean insert(long long_fp, long index, boolean insert_only_if_no_match) {
@@ -462,27 +434,26 @@ boolean insert(long long_fp, long index, boolean insert_only_if_no_match) {
462434
return false;
463435
}
464436
}
465-
return insert_fingerprint_and_push_all_else(long_fp, run_start_index);
437+
return insert_fingerprint_and_push_all_else(long_fp, run_start_index, true);
466438
}
467439

468-
// insert a fingerprint as the first fingerprint of the new run and push all other entries in the cluster to the right.
469-
boolean insert_fingerprint_and_push_all_else(long long_fp, long run_start_index) {
440+
// insert a fingerprint as the last fingerprint of the run and push all other entries in the cluster to the right.
441+
boolean insert_fingerprint_and_push_all_else(long long_fp, long run_start_index, boolean is_same_run) {
470442
long current_index = run_start_index;
471443
boolean is_this_slot_empty;
472-
boolean finished_first_run = false;
473444
boolean temp_continuation = false;
474445

475446
do {
476447
is_this_slot_empty = is_slot_empty(current_index);
477448
if (current_index != run_start_index) {
478449
set_shifted(current_index, true);
479450
}
480-
if (current_index != run_start_index && !finished_first_run && !is_continuation(current_index)) {
481-
finished_first_run = true;
451+
if (current_index != run_start_index && is_same_run && !is_continuation(current_index)) {
452+
is_same_run = false;
482453
set_continuation(current_index, true);
483454
long_fp = swap_fingerprints(current_index, long_fp);
484455
}
485-
else if (finished_first_run) {
456+
else if (!is_same_run) {
486457
boolean current_continuation = is_continuation(current_index);
487458
set_continuation(current_index, temp_continuation);
488459
temp_continuation = current_continuation;

0 commit comments

Comments
 (0)