diff --git a/src/collate.c b/src/collate.c index 6efdd90..990e0b6 100644 --- a/src/collate.c +++ b/src/collate.c @@ -188,8 +188,18 @@ bool ldb_import_list_variable_records(struct ldb_collate_data *collate) if (collate->table_rec_ln) rec_size = collate->table_rec_ln; else rec_size = uint32_read(rec_key + collate->rec_width - LDB_KEY_LN); - /* If record is duplicated, skip it */ - if (rec_size == last_rec_size) if (!memcmp(data, last_data, rec_size)) continue; + /* Check if key is different than the last one (always start a group on the + first record: an all-zero subkey would otherwise collide with the zeroed + last_key and the record group header would never be written) */ + new_subkey = first_record || (memcmp(rec_key+LDB_KEY_LN, last_key+LDB_KEY_LN, subkey_ln) != 0); + + /* If record is duplicated, skip it. Only a record of the same subkey can + be a duplicate: the buffer holds every subkey sharing the 4-byte main + key, sorted by key then data, so the last record of a subkey and the + first one of the next subkey can carry identical data and still be + two distinct records */ + if (!new_subkey && rec_size == last_rec_size && !memcmp(data, last_data, rec_size)) continue; + first_record = false; /* Update last record */ memcpy(last_data, data, rec_size); @@ -197,11 +207,6 @@ bool ldb_import_list_variable_records(struct ldb_collate_data *collate) uint32_t projected_size = buffer_ptr + rec_size + collate->table_key_ln + (2 * LDB_PTR_LN) + out_table.ts_ln; - /* Check if key is different than the last one (always start a group on the - first record: an all-zero subkey would otherwise collide with the zeroed - last_key and the record group header would never be written) */ - new_subkey = first_record || (memcmp(rec_key+LDB_KEY_LN, last_key+LDB_KEY_LN, subkey_ln) != 0); - first_record = false; /* If node size is exceeded, initialize buffer */ if (projected_size >= LDB_MAX_REC_LN) { @@ -398,8 +403,12 @@ bool ldb_collate_add_variable_record(struct ldb_collate_data *collate, uint8_t * memcpy(collate->data + collate->data_ptr, subkey, subkey_ln); collate->data_ptr += subkey_ln; - /* Copy record */ + /* Copy record and zero the rest of its slot. The buffer is reused for every + key (and grown with realloc), so the tail would otherwise keep bytes of a + previous record. The sort compares the whole slot, and stale tails can keep + identical records from ending up adjacent, so they escape deduplication */ memcpy(collate->data + collate->data_ptr, data, size); + memset(collate->data + collate->data_ptr + size, 0, collate->max_rec_ln - size); collate->data_ptr += collate->max_rec_ln; /* Copy record length */ diff --git a/test/test_kb b/test/test_kb index 1191753..70dc461 100644 --- a/test/test_kb +++ b/test/test_kb @@ -84,6 +84,23 @@ test_10_log_path() { rm -f $custom_log } +#Regression (#61), MD5 (16-byte key) mode: the collate must not drop the record +#of a key whose previous key in the same map slot carries the same data. +test_11_collate_keeps_records_of_adjacent_keys_with_same_data() { + rm -rf /var/lib/ldb/test_kbdup /usr/local/etc/scanoss/ldb/test_kbdup.conf /var/log/scanoss/ldb/test_kbdup.log + tmpdir=$(mktemp -d) + printf '11223344000000000000000000000001,0,3\n11223344000000000000000000000001,0,3\n11223344000000000000000000000002,0,3\n11223344000000000000000000000003,0,2\n11223344000000000000000000000004,0,3\n55667788000000000000000000000001,0,3\n' > "$tmpdir/11.csv" + + echo "bulk insert test_kbdup/t1 from $tmpdir/11.csv with (KEYS=1,KEY_SIZE=16,FIELDS=3,COLLATE=1,MAX_RECORD=2048,VALIDATE_VERSION=0,OVERWRITE=1)" | ../ldb -q + + for k in 11223344000000000000000000000001 11223344000000000000000000000002 11223344000000000000000000000003 11223344000000000000000000000004 55667788000000000000000000000001; do + result=$(echo "select from test_kbdup/t1 key $k csv hex 16" | ../ldb -q | wc -l) + assert_equals "1" "$result" "collate lost (or did not deduplicate) the record of key $k" + done + + rm -rf "$tmpdir" /var/lib/ldb/test_kbdup /usr/local/etc/scanoss/ldb/test_kbdup.conf /var/log/scanoss/ldb/test_kbdup.log +} + setup_suite () { # This suite covers the MD5 (16-byte key) mode. The default generated # configuration targets CRC64 (KEY_SIZE=8), so pin KEY_SIZE=16 in the db @@ -97,4 +114,5 @@ teardown_suite () { rm -rf /var/lib/ldb/test_kb rm -f /usr/local/etc/scanoss/ldb/test_kb.conf rm -f /tmp/null_sector_test /tmp/null_sector_build.log + rm -rf /var/lib/ldb/test_kbdup /usr/local/etc/scanoss/ldb/test_kbdup.conf /var/log/scanoss/ldb/test_kbdup.log } \ No newline at end of file diff --git a/test/test_kb_crc64 b/test/test_kb_crc64 index d3ed2c0..0f4e5e5 100644 --- a/test/test_kb_crc64 +++ b/test/test_kb_crc64 @@ -228,6 +228,46 @@ test_14b_collate_cap_not_applied_before_dedup() { rm -rf "$tmpdir" /var/lib/ldb/test_kbcap /usr/local/etc/scanoss/ldb/test_kbcap.conf /var/log/scanoss/ldb/test_kbcap.log } +#Regression (#61): the collate deduplicated records comparing only their data +#with the previous record, whatever key it belonged to. All the keys sharing the +#4-byte main key are collated together and sorted by key then data, so when the +#last record of a key and the first one of the next key carried the same data, +#the second key lost its record while the import still reported success. +test_15_collate_keeps_records_of_adjacent_keys_with_same_data() { + rm -rf /var/lib/ldb/test_kbdup /usr/local/etc/scanoss/ldb/test_kbdup.conf /var/log/scanoss/ldb/test_kbdup.log + tmpdir=$(mktemp -d) + # 1122334400000001 is duplicated on purpose: real duplicates must still go + printf '1122334400000001,0,3\n1122334400000001,0,3\n1122334400000002,0,3\n1122334400000003,0,2\n1122334400000004,0,3\n5566778800000001,0,3\n' > "$tmpdir/11.csv" + + echo "bulk insert test_kbdup/t1 from $tmpdir/11.csv with (KEYS=1,KEY_SIZE=8,FIELDS=3,COLLATE=1,MAX_RECORD=2048,VALIDATE_VERSION=0,OVERWRITE=1)" | ../ldb -q + + for k in 1122334400000001 1122334400000002 1122334400000003 1122334400000004 5566778800000001; do + result=$(echo "select from test_kbdup/t1 key $k csv hex 8" | ../ldb -q | wc -l) + assert_equals "1" "$result" "collate lost (or did not deduplicate) the record of key $k" + done + result=$(echo "select from test_kbdup/t1 key 1122334400000002 csv hex 8" | ../ldb -q) + assert_equals "1122334400000002,0,3" "$result" + + rm -rf "$tmpdir" /var/lib/ldb/test_kbdup /usr/local/etc/scanoss/ldb/test_kbdup.conf /var/log/scanoss/ldb/test_kbdup.log +} + +#Regression: the collate buffer is reused for every main key of a sector, and a +#record only overwrites the first bytes of its fixed-width slot. The sort compares +#the whole slot, so bytes left by a previous key's longer records could sort two +#identical records apart (around "0,3,m") and they escaped deduplication. +test_16_collate_dedup_ignores_stale_slot_bytes() { + rm -rf /var/lib/ldb/test_kbdup /usr/local/etc/scanoss/ldb/test_kbdup.conf /var/log/scanoss/ldb/test_kbdup.log + tmpdir=$(mktemp -d) + printf '11aaaaaa00000001,0,3,aaaaaa\n11aaaaaa00000001,0,3,zzzzzz\n11bbbbbb00000001,0,3\n11bbbbbb00000001,0,3\n11bbbbbb00000001,0,3,m\n' > "$tmpdir/11.csv" + + echo "bulk insert test_kbdup/t1 from $tmpdir/11.csv with (KEYS=1,KEY_SIZE=8,FIELDS=4,VALIDATE_FIELDS=0,SORT=0,COLLATE=1,MAX_RECORD=2048,VALIDATE_VERSION=0,OVERWRITE=1)" | ../ldb -q + + result=$(echo "select from test_kbdup/t1 key 11bbbbbb00000001 csv hex 8" | ../ldb -q | wc -l) + assert_equals "2" "$result" "collate kept a duplicate separated by stale slot bytes" + + rm -rf "$tmpdir" /var/lib/ldb/test_kbdup /usr/local/etc/scanoss/ldb/test_kbdup.conf /var/log/scanoss/ldb/test_kbdup.log +} + setup_suite () { echo 'bulk insert test_kb from source_crc64/mined with (KEY_SIZE=8)' | ../ldb } @@ -237,4 +277,5 @@ teardown_suite () { rm -f /tmp/null_sector_test /tmp/null_sector_build.log # test_14 leaves an ~86 MB sector behind when it fails before its own cleanup rm -rf /var/lib/ldb/test_kbcap /usr/local/etc/scanoss/ldb/test_kbcap.conf /var/log/scanoss/ldb/test_kbcap.log + rm -rf /var/lib/ldb/test_kbdup /usr/local/etc/scanoss/ldb/test_kbdup.conf /var/log/scanoss/ldb/test_kbdup.log } \ No newline at end of file