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
|
|
|
|
2026-05-21 13:13:22 +02: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;
|
|
|
|
|
|
2026-05-21 13:13:22 +02:00
|
|
|
public TransientOrderedMap(Node<TK> root, TStrategy strategy, int count)
|
2026-02-01 20:52:23 +01:00
|
|
|
: base(root, strategy, count)
|
|
|
|
|
{
|
|
|
|
|
_transactionId = OwnerId.Next();
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-21 13:13:22 +02:00
|
|
|
public void Set(TK key, TV value)
|
2026-02-01 20:52:23 +01:00
|
|
|
{
|
2026-05-21 13:13:22 +02: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
|
|
|
}
|
|
|
|
|
|
2026-05-21 13:13:22 +02:00
|
|
|
public void Remove(TK key)
|
2026-02-01 20:52:23 +01:00
|
|
|
{
|
2026-05-21 13:13:22 +02: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
|
|
|
}
|
|
|
|
|
|
2026-05-21 13:13:22 +02:00
|
|
|
public PersistentOrderedMap<TK, TV, TStrategy> ToPersistent()
|
2026-02-01 20:52:23 +01:00
|
|
|
{
|
2026-04-16 11:51:38 +02:00
|
|
|
// 1. Create the snapshot by copying all relevant information
|
2026-02-01 20:52:23 +01:00
|
|
|
|
2026-05-21 13:13:22 +02: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
|
2026-04-16 11:51:38 +02:00
|
|
|
// so that future edits will be done by CoW
|
2026-02-01 20:52:23 +01:00
|
|
|
_transactionId = OwnerId.Next();
|
|
|
|
|
|
|
|
|
|
return snapshot;
|
|
|
|
|
}
|
|
|
|
|
}
|