From 2fda40488adf134d9d62525a3a3d294e27ed868c Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Tue, 31 May 2022 17:39:54 +0800 Subject: [PATCH] Fix MemoryStore (#2759) --- src/neo/Persistence/Helper.cs | 22 ---------------------- src/neo/Persistence/MemorySnapshot.cs | 14 +++++++------- src/neo/Persistence/MemoryStore.cs | 14 +++++++------- 3 files changed, 14 insertions(+), 36 deletions(-) delete mode 100644 src/neo/Persistence/Helper.cs diff --git a/src/neo/Persistence/Helper.cs b/src/neo/Persistence/Helper.cs deleted file mode 100644 index b5088af73a..0000000000 --- a/src/neo/Persistence/Helper.cs +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (C) 2015-2021 The Neo Project. -// -// The neo is free software distributed under the MIT software license, -// see the accompanying file LICENSE in the main directory of the -// project or http://www.opensource.org/licenses/mit-license.php -// for more details. -// -// Redistribution and use in source and binary forms with or without -// modifications are permitted. - -using System; - -namespace Neo.Persistence -{ - internal static class Helper - { - public static byte[] EnsureNotNull(this byte[] source) - { - return source ?? Array.Empty(); - } - } -} diff --git a/src/neo/Persistence/MemorySnapshot.cs b/src/neo/Persistence/MemorySnapshot.cs index e86435942b..586ee72e26 100644 --- a/src/neo/Persistence/MemorySnapshot.cs +++ b/src/neo/Persistence/MemorySnapshot.cs @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2021 The Neo Project. +// Copyright (C) 2015-2022 The Neo Project. // // The neo is free software distributed under the MIT software license, // see the accompanying file LICENSE in the main directory of the @@ -40,7 +40,7 @@ public void Commit() public void Delete(byte[] key) { - writeBatch[key.EnsureNotNull()] = null; + writeBatch[key] = null; } public void Dispose() @@ -49,7 +49,7 @@ public void Dispose() public void Put(byte[] key, byte[] value) { - writeBatch[key.EnsureNotNull()] = value; + writeBatch[key[..]] = value[..]; } public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte[] keyOrPrefix, SeekDirection direction = SeekDirection.Forward) @@ -59,18 +59,18 @@ public void Put(byte[] key, byte[] value) if (keyOrPrefix?.Length > 0) records = records.Where(p => comparer.Compare(p.Key, keyOrPrefix) >= 0); records = records.OrderBy(p => p.Key, comparer); - return records.Select(p => (p.Key, p.Value)); + return records.Select(p => (p.Key[..], p.Value[..])); } public byte[] TryGet(byte[] key) { - immutableData.TryGetValue(key.EnsureNotNull(), out byte[] value); - return value; + immutableData.TryGetValue(key, out byte[] value); + return value?[..]; } public bool Contains(byte[] key) { - return innerData.ContainsKey(key.EnsureNotNull()); + return immutableData.ContainsKey(key); } } } diff --git a/src/neo/Persistence/MemoryStore.cs b/src/neo/Persistence/MemoryStore.cs index f73777e992..200c704ad0 100644 --- a/src/neo/Persistence/MemoryStore.cs +++ b/src/neo/Persistence/MemoryStore.cs @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2021 The Neo Project. +// Copyright (C) 2015-2022 The Neo Project. // // The neo is free software distributed under the MIT software license, // see the accompanying file LICENSE in the main directory of the @@ -24,7 +24,7 @@ public class MemoryStore : IStore public void Delete(byte[] key) { - innerData.TryRemove(key.EnsureNotNull(), out _); + innerData.TryRemove(key, out _); } public void Dispose() @@ -38,7 +38,7 @@ public ISnapshot GetSnapshot() public void Put(byte[] key, byte[] value) { - innerData[key.EnsureNotNull()] = value; + innerData[key[..]] = value[..]; } public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte[] keyOrPrefix, SeekDirection direction = SeekDirection.Forward) @@ -51,18 +51,18 @@ public void Put(byte[] key, byte[] value) records = records.Where(p => comparer.Compare(p.Key, keyOrPrefix) >= 0); records = records.OrderBy(p => p.Key, comparer); foreach (var pair in records) - yield return (pair.Key, pair.Value); + yield return (pair.Key[..], pair.Value[..]); } public byte[] TryGet(byte[] key) { - innerData.TryGetValue(key.EnsureNotNull(), out byte[] value); - return value; + innerData.TryGetValue(key, out byte[] value); + return value?[..]; } public bool Contains(byte[] key) { - return innerData.ContainsKey(key.EnsureNotNull()); + return innerData.ContainsKey(key); } } }