Skip to content

Commit

Permalink
Use ConcurrentDictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang committed Nov 26, 2019
1 parent 434810e commit fec14ab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
20 changes: 7 additions & 13 deletions src/neo/Persistence/Memory/Snapshot.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
using Neo.IO;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;

namespace Neo.Persistence.Memory
{
internal class Snapshot : ISnapshot
{
private readonly ReaderWriterLockSlim readerWriterLock;
private readonly Dictionary<byte[], byte[]>[] innerData;
private readonly ConcurrentDictionary<byte[], byte[]>[] innerData;
private readonly ImmutableDictionary<byte[], byte[]>[] immutableData;
private readonly Dictionary<byte[], byte[]>[] writeBatch;
private readonly ConcurrentDictionary<byte[], byte[]>[] writeBatch;

public Snapshot(Dictionary<byte[], byte[]>[] innerData, ReaderWriterLockSlim readerWriterLock)
public Snapshot(ConcurrentDictionary<byte[], byte[]>[] innerData)
{
this.readerWriterLock = readerWriterLock;
this.innerData = innerData;
readerWriterLock.EnterReadLock();
this.immutableData = innerData.Select(p => p.ToImmutableDictionary(ByteArrayEqualityComparer.Default)).ToArray();
readerWriterLock.ExitReadLock();
this.writeBatch = new Dictionary<byte[], byte[]>[innerData.Length];
this.writeBatch = new ConcurrentDictionary<byte[], byte[]>[innerData.Length];
for (int i = 0; i < writeBatch.Length; i++)
writeBatch[i] = new Dictionary<byte[], byte[]>(ByteArrayEqualityComparer.Default);
writeBatch[i] = new ConcurrentDictionary<byte[], byte[]>(ByteArrayEqualityComparer.Default);
}

public void Commit()
{
readerWriterLock.EnterWriteLock();
for (int i = 0; i < writeBatch.Length; i++)
foreach (var pair in writeBatch[i])
if (pair.Value is null)
innerData[i].Remove(pair.Key);
innerData[i].TryRemove(pair.Key, out _);
else
innerData[i][pair.Key] = pair.Value;
readerWriterLock.ExitWriteLock();
}

public void Delete(byte table, byte[] key)
Expand Down
22 changes: 6 additions & 16 deletions src/neo/Persistence/Memory/Store.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
using Neo.IO;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace Neo.Persistence.Memory
{
public class Store : IStore
{
private readonly ReaderWriterLockSlim readerWriterLock = new ReaderWriterLockSlim();
private readonly Dictionary<byte[], byte[]>[] innerData;
private readonly ConcurrentDictionary<byte[], byte[]>[] innerData;

public Store()
{
innerData = new Dictionary<byte[], byte[]>[256];
innerData = new ConcurrentDictionary<byte[], byte[]>[256];
for (int i = 0; i < innerData.Length; i++)
innerData[i] = new Dictionary<byte[], byte[]>(ByteArrayEqualityComparer.Default);
innerData[i] = new ConcurrentDictionary<byte[], byte[]>(ByteArrayEqualityComparer.Default);
}

public void Delete(byte table, byte[] key)
{
readerWriterLock.EnterWriteLock();
innerData[table].Remove(key.EnsureNotNull());
readerWriterLock.ExitWriteLock();
innerData[table].TryRemove(key.EnsureNotNull(), out _);
}

public void Dispose()
{
readerWriterLock.Dispose();
}

public IEnumerable<(byte[] Key, byte[] Value)> Find(byte table, byte[] prefix)
Expand All @@ -36,15 +32,13 @@ public IEnumerable<(byte[] Key, byte[] Value)> Find(byte table, byte[] prefix)
if (prefix?.Length > 0)
records = records.Where(p => p.Key.Length >= prefix.Length && p.Key.Take(prefix.Length).SequenceEqual(prefix));
records = records.OrderBy(p => p.Key, ByteArrayComparer.Default);
readerWriterLock.EnterReadLock();
foreach (var pair in records)
yield return (pair.Key, pair.Value);
readerWriterLock.ExitReadLock();
}

public ISnapshot GetSnapshot()
{
return new Snapshot(innerData, readerWriterLock);
return new Snapshot(innerData);
}

public void Put(byte table, byte[] key, byte[] value)
Expand All @@ -54,16 +48,12 @@ public void Put(byte table, byte[] key, byte[] value)

public void PutSync(byte table, byte[] key, byte[] value)
{
readerWriterLock.EnterWriteLock();
innerData[table][key.EnsureNotNull()] = value;
readerWriterLock.ExitWriteLock();
}

public byte[] TryGet(byte table, byte[] key)
{
readerWriterLock.EnterReadLock();
innerData[table].TryGetValue(key.EnsureNotNull(), out byte[] value);
readerWriterLock.ExitReadLock();
return value;
}
}
Expand Down

0 comments on commit fec14ab

Please sign in to comment.