PersistentMap/PersistentOrderedMap/TransientOrderedMap.cs

40 lines
1.3 KiB
C#
Raw Normal View History

2026-02-01 20:52:23 +01:00
using System.Collections;
2026-05-07 07:44:55 +02:00
namespace PersistentOrderedMap;
2026-02-01 20:52:23 +01:00
2026-05-07 07:44:55 +02:00
public sealed class TransientOrderedMap<K, V, TStrategy> : BaseOrderedMap<K, V, TStrategy> where TStrategy : IKeyStrategy<K>
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-07 07:44:55 +02:00
public TransientOrderedMap(Node<K> root, TStrategy strategy, int count)
2026-02-01 20:52:23 +01:00
: base(root, strategy, count)
{
_transactionId = OwnerId.Next();
}
public void Set(K key, V value)
{
2026-02-11 12:56:48 +01:00
_root = BTreeFunctions.Set(_root, key, value, _strategy, _transactionId, out bool countChanged);
if (countChanged) Count++;
2026-02-01 20:52:23 +01:00
}
public void Remove(K key)
{
2026-02-11 12:56:48 +01:00
_root = BTreeFunctions.Remove<K,V, TStrategy>(_root, key, _strategy, _transactionId, out bool removed);
if (removed) Count--;
2026-02-01 20:52:23 +01:00
}
2026-05-07 07:44:55 +02:00
public PersistentOrderedMap<K, V, 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
2026-05-07 07:44:55 +02:00
var snapshot = new PersistentOrderedMap<K, V, 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;
}
}