Skip to content

Commit

Permalink
Make atomic value updates/reads if required. This is needed for struc…
Browse files Browse the repository at this point in the history
…ts bigger than processor word size.
  • Loading branch information
koculu committed Jul 6, 2022
1 parent 6ffd7ae commit daa0131
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
47 changes: 40 additions & 7 deletions src/ZoneTree/Collections/SkipList.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Tenray.Collections;
using System.Runtime.CompilerServices;
using Tenray.Collections;

namespace ZoneTree.Collections;

Expand Down Expand Up @@ -300,9 +301,46 @@ public bool Remove(in TKey key)

public class SkipListNode
{
/// <summary>
/// A conforming CLI shall guarantee that read and write access
/// to properly aligned memory locations no larger than the native word size
/// (the size of type native int) is atomic.
/// </summary>
private static bool IsValueAssignmentAtomic =
Unsafe.SizeOf<TValue>() <= IntPtr.Size;

public SkipListNode[] Next;

public TKey Key;
public TValue Value;

private TValue _value;

public TValue Value {
get
{
if (IsValueAssignmentAtomic)
return _value;
else
{
lock(this)
{
return _value;
}
}
}
set
{
if(IsValueAssignmentAtomic)
_value = value;
else
{
lock(this)
{
_value = value;
}
}
}
}
public readonly int Level;
public volatile bool isInserted;
public SkipListNode NextNode => Next[0];
Expand Down Expand Up @@ -344,10 +382,5 @@ public void MarkInserted()
{
isInserted = true;
}

public ref TValue GetValueRef()
{
return ref Value;
}
}
}
4 changes: 2 additions & 2 deletions src/ZoneTree/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<Authors>Ahmed Yasin Koculu</Authors>
<PackageId>Topaz</PackageId>
<Title>Topaz</Title>
<ProductVersion>1.0.0.0</ProductVersion>
<Version>1.0.0.0</Version>
<ProductVersion>1.0.1.0</ProductVersion>
<Version>1.0.1.0</Version>
<Authors>Ahmed Yasin Koculu</Authors>
<AssemblyTitle>ZoneTree</AssemblyTitle>
<Description>ZoneTree is a persistent, high-performance key-value database for .NET. It can operate in memory or on disk. (Optimized for SSDs)</Description>
Expand Down
6 changes: 4 additions & 2 deletions src/ZoneTree/Segments/MutableSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,17 @@ public AddOrUpdateResult Delete(TKey key)
var status = SkipList.AddOrUpdate(key,
(x) =>
{
ref var value = ref x.GetValueRef();
var value = x.Value;
MarkValueDeleted(ref value);
x.Value = value;
WriteAheadLog.Append(in key, in value);
return AddOrUpdateResult.ADDED;
},
(x) =>
{
ref var value = ref x.GetValueRef();
var value = x.Value;
MarkValueDeleted(ref value);
x.Value = value;
WriteAheadLog.Append(in key, in value);
return AddOrUpdateResult.UPDATED;
});
Expand Down

0 comments on commit daa0131

Please sign in to comment.