Skip to content

Commit

Permalink
Fix a bug with retractions when used with Hashed blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari committed Jun 12, 2024
1 parent 64fa1a5 commit 2da3106
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/NexusMods.MnemonicDB.Abstractions/Datom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,19 @@ public int Compare(Datom other, IndexType indexType)
throw new ArgumentOutOfRangeException(nameof(indexType), indexType, "Unknown index type");
}
}

/// <summary>
/// Clone this datom and return it as a retraction datom
/// </summary>
/// <returns></returns>
public Datom Retract()
{
var data = GC.AllocateUninitializedArray<byte>(RawSpan.Length);
var dataSpan = data.AsSpan();
RawSpan.CopyTo(dataSpan);
var prefix = MemoryMarshal.Read<KeyPrefix>(dataSpan);
var newPrefix = new KeyPrefix().Set(prefix.E, prefix.A, TxId.Tmp, true);
MemoryMarshal.Write(dataSpan, newPrefix);
return new Datom(data, registry);
}
}
6 changes: 6 additions & 0 deletions src/NexusMods.MnemonicDB.Abstractions/ITransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using NexusMods.MnemonicDB.Abstractions.Attributes;
using NexusMods.MnemonicDB.Abstractions.DatomIterators;
using NexusMods.MnemonicDB.Abstractions.Models;
using NexusMods.MnemonicDB.Abstractions.TxFunctions;

Expand Down Expand Up @@ -59,6 +60,11 @@ public interface ITransaction : IDisposable
void Retract<TVal, TLowLevel>(EntityId entityId, Attribute<TVal, TLowLevel> attribute, TVal val)
=> Add(entityId, attribute, val, isRetract: true);

/// <summary>
/// Retract a specific datom
/// </summary>
void Add(Datom datom);

/// <summary>
/// Commits the transaction
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,9 @@ private static bool ShouldRecursiveDelete(IDb db, IReadDatom referenceDatom)

private static void DeleteThisOnly(ITransaction tx, IDb db, EntityId eid)
{
var segment = db.Get(eid);
foreach (var datom in segment)
foreach (var datom in db.Get(eid))
{
var resolved = datom.Resolved;
resolved.Retract(tx);
tx.Add(datom.Retract());
}
}
}
6 changes: 6 additions & 0 deletions src/NexusMods.MnemonicDB.Storage/InternalTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.MnemonicDB.Abstractions.Attributes;
using NexusMods.MnemonicDB.Abstractions.DatomIterators;
using NexusMods.MnemonicDB.Abstractions.IndexSegments;
using NexusMods.MnemonicDB.Abstractions.Models;
using NexusMods.MnemonicDB.Abstractions.TxFunctions;
Expand Down Expand Up @@ -57,6 +58,11 @@ public void Attach(ITemporaryEntity entity)
_temporaryEntities.Add(entity);
}

public void Add(Datom datom)
{
datoms.Add(datom.RawSpan);
}

/// <inheritdoc />
public Task<ICommitResult> Commit()
{
Expand Down
7 changes: 7 additions & 0 deletions src/NexusMods.MnemonicDB/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.MnemonicDB.Abstractions.Attributes;
using NexusMods.MnemonicDB.Abstractions.DatomIterators;
using NexusMods.MnemonicDB.Abstractions.IndexSegments;
using NexusMods.MnemonicDB.Abstractions.Internals;
using NexusMods.MnemonicDB.Abstractions.Models;
Expand Down Expand Up @@ -52,6 +53,12 @@ public void Add(EntityId entityId, ReferencesAttribute attribute, IEnumerable<En
}
}

/// <inheritdoc />
public void Add(Datom datom)
{
_datoms.Add(datom.RawSpan);
}

public void Add(ITxFunction fn)
{
if (_committed)
Expand Down
1 change: 1 addition & 0 deletions tests/NexusMods.MnemonicDB.Storage.Tests/ABackendTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public async Task CanStoreDataInBlobs(IndexType type)
await Verify(datoms.ToTable(Registry))
.UseDirectory("BackendTestVerifyData")
.UseParameters(type);

}


Expand Down

0 comments on commit 2da3106

Please sign in to comment.