Skip to content

Commit

Permalink
Fix off-by-one error in in-memory datom storage
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari committed Jun 10, 2024
1 parent a0fae78 commit 2577407
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 25 deletions.
17 changes: 0 additions & 17 deletions src/NexusMods.MnemonicDB.Abstractions/DynamicCache.cs

This file was deleted.

26 changes: 18 additions & 8 deletions src/NexusMods.MnemonicDB.Storage/InMemoryBackend/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,42 +30,52 @@ public IndexSegment Datoms(SliceDescriptor descriptor)

var idxLower = thisIndex.IndexOf(descriptor.From.RawSpan.ToArray());
var idxUpper = thisIndex.IndexOf(descriptor.To.RawSpan.ToArray());
bool upperExact = true;
bool lowerExact = true;

if (idxLower < 0)
{
idxLower = ~idxLower;
lowerExact = false;
}

if (idxUpper < 0)
{
idxUpper = ~idxUpper;
upperExact = false;
}

var lower = idxLower;
var upper = idxUpper;
var reverse = false;

if (idxLower > idxUpper)
if (descriptor.IsReverse)
{
lower = idxUpper;
upper = idxLower;
reverse = true;
(lowerExact, upperExact) = (upperExact, lowerExact);
}

using var segmentBuilder = new IndexSegmentBuilder(_registry);

if (!reverse)
if (descriptor.IsReverse)
{
for (var i = lower; i <= upper; i++)
if (!lowerExact)
lower++;
for (var i = upper; i >= lower; i--)
{
if (i >= thisIndex.Count)
break;
segmentBuilder.Add(thisIndex.ElementAt(i));
}
}
else
{
for (var i = upper; i >= lower; i--)
if (!upperExact)
upper--;
for (var i = lower; i <= upper; i++)
{
segmentBuilder.Add(thisIndex.ElementAt(i));
}
}

return segmentBuilder.Build();
}

Expand Down
11 changes: 11 additions & 0 deletions tests/NexusMods.MnemonicDB.Storage.Tests/ABackendTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,15 @@ await Verify(datoms.ToTable(Registry))
.UseDirectory("BackendTestVerifyData")
.UseParameters(type);
}


[Fact]
public async Task CanLoadExistingAttributes()
{
var attrs = DatomStore.GetSnapshot().Datoms(SliceDescriptor.Create(BuiltInAttributes.UniqueId, Registry))
.Select(d => d.Resolved)
.ToArray();

await Verify(attrs.ToTable(Registry));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
+ | 0000000000000001 | (0001) UniqueId | NexusMods.MnemonicDB.DatomStore/UniqueId | 0100000000000001
+ | 0000000000000002 | (0001) UniqueId | NexusMods.MnemonicDB.DatomStore/ValueType | 0100000000000001
+ | 0000000000000003 | (0001) UniqueId | NexusMods.MnemonicDB.Transaction/Timestamp | 0100000000000001
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
+ | 0000000000000001 | (0001) UniqueId | NexusMods.MnemonicDB.DatomStore/UniqueId | 0100000000000001
+ | 0000000000000002 | (0001) UniqueId | NexusMods.MnemonicDB.DatomStore/ValueType | 0100000000000001
+ | 0000000000000003 | (0001) UniqueId | NexusMods.MnemonicDB.Transaction/Timestamp | 0100000000000001

0 comments on commit 2577407

Please sign in to comment.