From 25774078f4714b232004c303f66ca12a4e8820c6 Mon Sep 17 00:00:00 2001 From: halgari Date: Mon, 10 Jun 2024 17:19:42 -0600 Subject: [PATCH] Fix off-by-one error in in-memory datom storage --- .../DynamicCache.cs | 17 ------------ .../InMemoryBackend/Snapshot.cs | 26 +++++++++++++------ .../ABackendTest.cs | 11 ++++++++ ...sts.CanLoadExistingAttributes.verified.txt | 3 +++ ...sDB.CanLoadExistingAttributes.verified.txt | 3 +++ 5 files changed, 35 insertions(+), 25 deletions(-) delete mode 100644 src/NexusMods.MnemonicDB.Abstractions/DynamicCache.cs create mode 100644 tests/NexusMods.MnemonicDB.Storage.Tests/InMemoryTests.CanLoadExistingAttributes.verified.txt create mode 100644 tests/NexusMods.MnemonicDB.Storage.Tests/RocksDB.CanLoadExistingAttributes.verified.txt diff --git a/src/NexusMods.MnemonicDB.Abstractions/DynamicCache.cs b/src/NexusMods.MnemonicDB.Abstractions/DynamicCache.cs deleted file mode 100644 index 9d96af5d..00000000 --- a/src/NexusMods.MnemonicDB.Abstractions/DynamicCache.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.Linq; -using System.Reactive.Linq; -using System.Reactive.Subjects; -using DynamicData; -using NexusMods.MnemonicDB.Abstractions.Models; - -namespace NexusMods.MnemonicDB.Abstractions; - -public class DynamicCache : IDynamicCache -{ - public IObservable.ReadDatom, EntityId>> Query(Attribute attr, THighLevel value) - { - throw new NotImplementedException(); - } -} diff --git a/src/NexusMods.MnemonicDB.Storage/InMemoryBackend/Snapshot.cs b/src/NexusMods.MnemonicDB.Storage/InMemoryBackend/Snapshot.cs index 65ff8fd2..5797b376 100644 --- a/src/NexusMods.MnemonicDB.Storage/InMemoryBackend/Snapshot.cs +++ b/src/NexusMods.MnemonicDB.Storage/InMemoryBackend/Snapshot.cs @@ -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(); } diff --git a/tests/NexusMods.MnemonicDB.Storage.Tests/ABackendTest.cs b/tests/NexusMods.MnemonicDB.Storage.Tests/ABackendTest.cs index d7ccb7f3..ce03d8f7 100644 --- a/tests/NexusMods.MnemonicDB.Storage.Tests/ABackendTest.cs +++ b/tests/NexusMods.MnemonicDB.Storage.Tests/ABackendTest.cs @@ -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)); + } } diff --git a/tests/NexusMods.MnemonicDB.Storage.Tests/InMemoryTests.CanLoadExistingAttributes.verified.txt b/tests/NexusMods.MnemonicDB.Storage.Tests/InMemoryTests.CanLoadExistingAttributes.verified.txt new file mode 100644 index 00000000..db4922fd --- /dev/null +++ b/tests/NexusMods.MnemonicDB.Storage.Tests/InMemoryTests.CanLoadExistingAttributes.verified.txt @@ -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 diff --git a/tests/NexusMods.MnemonicDB.Storage.Tests/RocksDB.CanLoadExistingAttributes.verified.txt b/tests/NexusMods.MnemonicDB.Storage.Tests/RocksDB.CanLoadExistingAttributes.verified.txt new file mode 100644 index 00000000..db4922fd --- /dev/null +++ b/tests/NexusMods.MnemonicDB.Storage.Tests/RocksDB.CanLoadExistingAttributes.verified.txt @@ -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