diff --git a/src/events.cpp b/src/events.cpp index 7a8f6a6..f874f18 100644 --- a/src/events.cpp +++ b/src/events.cpp @@ -305,7 +305,13 @@ void writeEvents(lmdb::txn &txn, std::vector &evs, uint64_t logLev if (parsedKey.s == searchStr && parsedKey.n == flat->kind()) { auto otherEv = lookupEventByLevId(txn, lmdb::from_sv(v)); - if (otherEv.flat_nested()->created_at() < flat->created_at()) { + auto thisTimestamp = flat->created_at(); + auto otherTimestamp = otherEv.flat_nested()->created_at(); + LI << thisTimestamp << " / " << otherTimestamp; + LI << to_hex(sv(flat->id())) << " / " << to_hex(sv(otherEv.flat_nested()->id())); + + if (otherTimestamp < thisTimestamp || + (otherTimestamp == thisTimestamp && sv(flat->id()) < sv(otherEv.flat_nested()->id()))) { if (logLevel >= 1) LI << "Deleting event (d-tag). id=" << to_hex(sv(otherEv.flat_nested()->id())); levIdsToDelete.push_back(otherEv.primaryKeyId); } else { diff --git a/test/writeTest.pl b/test/writeTest.pl index ba61338..9d7e3be 100644 --- a/test/writeTest.pl +++ b/test/writeTest.pl @@ -8,6 +8,8 @@ $SIG{ __DIE__ } = \&Carp::confess; use Data::Dumper; use JSON::XS; +$Data::Dumper::Sortkeys = 1; + my $ids = [ { @@ -22,6 +24,7 @@ my $ids = [ + doTest({ desc => "Basic insert", events => [ @@ -83,6 +86,25 @@ doTest({ verify => [ 1, ], }); +doTest({ + desc => "Equal timestamps: replacement does not happen because new id > old id", + events => [ + qq{--sec $ids->[0]->{sec} --content "c1" --kind 10000 --created-at 5000 }, + qq{--sec $ids->[0]->{sec} --content "c2" --kind 10000 --created-at 5000 }, + ], + assertIds => [qw/ 7c ae /], + verify => [ 0, ], +}); + +doTest({ + desc => "Equal timestamps: replacement does happen because new id < old id", + events => [ + qq{--sec $ids->[0]->{sec} --content "c1" --kind 10000 --created-at 5000 }, + qq{--sec $ids->[0]->{sec} --content "c4" --kind 10000 --created-at 5000 }, + ], + assertIds => [qw/ 7c 63 /], + verify => [ 1, ], +}); @@ -221,6 +243,10 @@ sub doTest { push @$eventIds, addEvent($ev); } + for (my $i = 0; $i < @{ $spec->{assertIds} || [] }; $i++) { + die "assertId incorrect" unless rindex($eventIds->[$i], $spec->{assertIds}->[$i], 0) == 0; + } + my $finalEventIds = []; { @@ -255,6 +281,7 @@ sub addEvent { system(qq{ rm test-eventXYZ.json }); my $event = decode_json($eventJson); + print Dumper($event) if $ENV{DUMP_EVENTS}; return $event->{id}; }