PersistentMap/PersistentOrderedMap/TransientOrderedMap.cs

39 lines
1.3 KiB
C#
Raw Permalink Normal View History

2026-02-01 20:52:23 +01:00
2026-05-07 07:44:55 +02:00
namespace PersistentOrderedMap;
2026-02-01 20:52:23 +01:00
public sealed class TransientOrderedMap<TK, TV, TStrategy> : BaseOrderedMap<TK, TV, TStrategy> where TStrategy : IKeyStrategy<TK>
2026-02-01 20:52:23 +01:00
{
// This is mutable, but we treat it as readonly for the ID generation logic usually.
private OwnerId _transactionId;
public TransientOrderedMap(Node<TK> root, TStrategy strategy, int count)
2026-02-01 20:52:23 +01:00
: base(root, strategy, count)
{
_transactionId = OwnerId.Next();
}
public void Set(TK key, TV value)
2026-02-01 20:52:23 +01:00
{
Root = BTreeFunctions.Set(Root, key, value, Strategy, _transactionId, out bool countChanged);
2026-02-11 12:56:48 +01:00
if (countChanged) Count++;
2026-02-01 20:52:23 +01:00
}
public void Remove(TK key)
2026-02-01 20:52:23 +01:00
{
Root = BTreeFunctions.Remove<TK,TV, TStrategy>(Root, key, Strategy, _transactionId, out bool removed);
2026-02-11 12:56:48 +01:00
if (removed) Count--;
2026-02-01 20:52:23 +01:00
}
public PersistentOrderedMap<TK, TV, TStrategy> ToPersistent()
2026-02-01 20:52:23 +01:00
{
// 1. Create the snapshot by copying all relevant information
2026-02-01 20:52:23 +01:00
var snapshot = new PersistentOrderedMap<TK, TV, TStrategy>(Root, Strategy, Count);
2026-02-01 20:52:23 +01:00
2026-05-07 07:44:55 +02:00
// 2. Protect the snapshot from THIS TransientOrderedMap by getting a new ownerId
// so that future edits will be done by CoW
2026-02-01 20:52:23 +01:00
_transactionId = OwnerId.Next();
return snapshot;
}
}