if two replaceable events have equal timestamps, retain the one with the lowest id

This commit is contained in:
Doug Hoyte
2023-05-19 00:45:11 -04:00
parent dcb2920578
commit fbbfc3a974
2 changed files with 34 additions and 1 deletions

View File

@ -305,7 +305,13 @@ void writeEvents(lmdb::txn &txn, std::vector<EventToWrite> &evs, uint64_t logLev
if (parsedKey.s == searchStr && parsedKey.n == flat->kind()) {
auto otherEv = lookupEventByLevId(txn, lmdb::from_sv<uint64_t>(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 {

View File

@ -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};
}