From 02af69d6da3d12e03063a3c4e08261f0b51a3bc2 Mon Sep 17 00:00:00 2001 From: Ahmed Yasin Koculu Date: Wed, 20 Jul 2022 23:08:18 +0200 Subject: [PATCH] Non-transactional upsert method cannot return upsert status. --- src/ZoneTree/Core/ZoneTree.cs | 15 ++++++++------- src/ZoneTree/ITransactionalZoneTree.cs | 6 +++--- src/ZoneTree/IZoneTree.cs | 8 ++------ .../Transactional/OptimisticZoneTree.cs | 17 ++++++----------- 4 files changed, 19 insertions(+), 27 deletions(-) diff --git a/src/ZoneTree/Core/ZoneTree.cs b/src/ZoneTree/Core/ZoneTree.cs index f322185..b7f6b11 100644 --- a/src/ZoneTree/Core/ZoneTree.cs +++ b/src/ZoneTree/Core/ZoneTree.cs @@ -182,7 +182,8 @@ public sealed class ZoneTree : IZoneTree, IZoneTreeM { if (ContainsKey(key)) return false; - return Upsert(key, value); + Upsert(key, value); + return true; } } @@ -194,7 +195,8 @@ public sealed class ZoneTree : IZoneTree, IZoneTreeM { if (!ContainsKey(key)) return false; - return Upsert(key, value); + Upsert(key, value); + return true; } } @@ -241,15 +243,15 @@ public sealed class ZoneTree : IZoneTree, IZoneTreeM } } - public bool AtomicUpsert(in TKey key, in TValue value) + public void AtomicUpsert(in TKey key, in TValue value) { lock (AtomicUpdateLock) { - return Upsert(in key, in value); + Upsert(in key, in value); } } - public bool Upsert(in TKey key, in TValue value) + public void Upsert(in TKey key, in TValue value) { if (key == null) throw new ArgumentNullException(nameof(key)); @@ -265,7 +267,7 @@ public sealed class ZoneTree : IZoneTree, IZoneTreeM MoveSegmentZeroForward(segmentZero); continue; default: - return status == AddOrUpdateResult.ADDED; + return; } } } @@ -351,7 +353,6 @@ public sealed class ZoneTree : IZoneTree, IZoneTreeM DiskSegment.SegmentId, ReadOnlySegmentQueue.Select(x => x.SegmentId).Reverse().ToArray()); } - } public async ValueTask StartMergeOperation() diff --git a/src/ZoneTree/ITransactionalZoneTree.cs b/src/ZoneTree/ITransactionalZoneTree.cs index a022020..9037741 100644 --- a/src/ZoneTree/ITransactionalZoneTree.cs +++ b/src/ZoneTree/ITransactionalZoneTree.cs @@ -29,7 +29,8 @@ public interface ITransactionalZoneTree : IDisposable /// Transaction Id /// Key /// Value - /// + /// /// true if the key/value pair was inserted; + /// false if the key/value pair was updated. /// bool Upsert(long transactionId, in TKey key, in TValue value); @@ -112,8 +113,7 @@ public interface ITransactionalZoneTree : IDisposable /// /// Key /// Value - /// - bool UpsertAutoCommit(in TKey key, in TValue value); + void UpsertAutoCommit(in TKey key, in TValue value); /// /// Starts a transaction, diff --git a/src/ZoneTree/IZoneTree.cs b/src/ZoneTree/IZoneTree.cs index d6d96e9..56adce1 100644 --- a/src/ZoneTree/IZoneTree.cs +++ b/src/ZoneTree/IZoneTree.cs @@ -55,10 +55,8 @@ public interface IZoneTree : IDisposable /// /// The key of the element to upsert. /// The value of the element to upsert. It can be null. - /// true if the key/value pair was inserted; - /// false if the key/value pair was updated. /// key is null. - bool AtomicUpsert(in TKey key, in TValue value); + void AtomicUpsert(in TKey key, in TValue value); /// /// Adds or updates the specified key/value pair. @@ -89,10 +87,8 @@ public interface IZoneTree : IDisposable /// /// The key of the element to upsert. /// The value of the element to upsert. It can be null. - /// true if the key/value pair was inserted; - /// false if the key/value pair was updated. /// key is null. - bool Upsert(in TKey key, in TValue value); + void Upsert(in TKey key, in TValue value); /// /// Attempts to delete the specified key. diff --git a/src/ZoneTree/Transactional/OptimisticZoneTree.cs b/src/ZoneTree/Transactional/OptimisticZoneTree.cs index b4babde..d32f82f 100644 --- a/src/ZoneTree/Transactional/OptimisticZoneTree.cs +++ b/src/ZoneTree/Transactional/OptimisticZoneTree.cs @@ -366,21 +366,18 @@ public sealed class OptimisticZoneTree : } } - public bool UpsertAutoCommit(in TKey key, in TValue value) + public void UpsertAutoCommit(in TKey key, in TValue value) { lock (this) { var transactionId = BeginTransaction(); var transaction = GetOrCreateTransaction(transactionId); - var readWriteStamp = new ReadWriteStamp - { - WriteStamp = transactionId - }; + TransactionLog.TryGetReadWriteStamp(key, out var readWriteStamp); + readWriteStamp.WriteStamp = transactionId; TransactionLog.AddOrUpdateReadWriteStamp(key, in readWriteStamp); - var result = ZoneTree.Upsert(in key, in value); + ZoneTree.Upsert(in key, in value); transaction.IsReadyToCommit = true; DoCommit(transaction); - return result; } } @@ -390,10 +387,8 @@ public sealed class OptimisticZoneTree : { var transactionId = BeginTransaction(); var transaction = GetOrCreateTransaction(transactionId); - var readWriteStamp = new ReadWriteStamp - { - WriteStamp = transactionId - }; + TransactionLog.TryGetReadWriteStamp(key, out var readWriteStamp); + readWriteStamp.WriteStamp = transactionId; TransactionLog.AddOrUpdateReadWriteStamp(key, in readWriteStamp); ZoneTree.ForceDelete(in key); transaction.IsReadyToCommit = true;