Repository navigation
Description
Description
When a table is collated (COLLATE=1), ldb can silently drop records that belong to different keys. This happens when two keys share the same first 4 bytes, so they land in the same map slot, and they sit next
to each other in key order with identical record data. The second key loses its record completely. Afterwards, select for that key returns nothing, and so does dump. The import log still reports the full
record count ("collate completed with N records"), so the loss is invisible.
Only the collate step is affected. The same data imported with COLLATE=0 is stored completely.
Cause
In ldb_collate_sector (src/collate.c), the duplicate check compares each record only with the previous record's data:
/* If record is duplicated, skip it */
if (rec_size == last_rec_size) if (!memcmp(data, last_data, rec_size)) continue;
It does not check whether the previous record belongs to the same key. The subkey comparison (new_subkey = ...) happens after this check. So when two adjacent records in a sector belong to different subkeys
but carry the same data, the second one is treated as a duplicate and skipped.
Impact
Tables whose record data takes only a few distinct values are hit hardest, for example a table holding a small score per key. The denser a sector is, the more keys share a map slot, so the loss grows with
table size. In large tables a significant share of keys can disappear. Tables with fixed-size records go through the same loop and are affected as well: a record is lost whenever adjacent keys in a slot carry
the same bytes. The lost records cannot be recovered by collating again. They have to be re-imported with a fixed ldb.
Steps to reproduce
Tested on commit a83a205 (branch feature/custom-log-path-crc64), with KEY_SIZE=8 and with KEY_SIZE=16:
mkdir -p /tmp/collbug/t1
cat > /tmp/collbug/t1/11.csv <<'EOF'
1122334400000001,0,3
1122334400000002,0,3
1122334400000003,0,2
1122334400000004,0,3
5566778800000001,0,3
EOF
echo '{"monthly":"99.01"}' > /tmp/collbug/version.json
printf 'GLOBAL: (KEY_SIZE=8, VALIDATE_VERSION=1, SORT=1, COLLATE=1, MAX_RECORD=2048, TMP_PATH=/tmp)\nt1: (KEYS=1, FIELDS=3)\n'
> /usr/local/etc/scanoss/ldb/collbug.conf
echo "bulk insert collbug from /tmp/collbug with (KEY_SIZE=8,COLLATE=1)" | ldb
for k in 1122334400000001 1122334400000002 1122334400000003 1122334400000004 5566778800000001; do
echo "$k -> $(echo "select from collbug/t1 key $k csv hex 8" | ldb -q)"
done
Expected: all five keys return their record.
Actual:
1122334400000001 -> 1122334400000001,0,3
1122334400000002 ->
1122334400000003 -> 1122334400000003,0,2
1122334400000004 -> 1122334400000004,0,3
5566778800000001 -> 5566778800000001,0,3
1122334400000002 is lost because it follows 1122334400000001 in the same slot with the same data, 0,3. 1122334400000004 survives because the record before it, 0,2, differs. With COLLATE=0, all five records
are stored. With 16-byte keys, padded the same way, the result is identical.
Suggested fix
Only treat a record as a duplicate when it belongs to the same key as the previous record:
- compute new_subkey before the duplicate check
- skip the record only if !new_subkey and the data matches
- reset last_data and last_rec_size whenever a new subkey starts
ldb_eliminate_duplicates, in the same file, compares records by data too. It should be reviewed for the same assumption.
Activity
fix: collate must not drop records of adjacent keys with identical da…