From e86a8b63fdaf1402491b0a99dda9c46d5b45bc94 Mon Sep 17 00:00:00 2001 From: jsolman Date: Wed, 27 Feb 2019 09:06:07 -0800 Subject: [PATCH] Expose DbCache (for plugins, etc.) + useful Neo.IO.Helper methods (#613) --- neo/IO/Helper.cs | 26 +++++++++++++------------- neo/Persistence/LevelDB/DbCache.cs | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/neo/IO/Helper.cs b/neo/IO/Helper.cs index 02169969f8..0e1d28779b 100644 --- a/neo/IO/Helper.cs +++ b/neo/IO/Helper.cs @@ -9,6 +9,8 @@ namespace Neo.IO { public static class Helper { + public const int GroupingSizeInBytes = 16; + public static T AsSerializable(this byte[] value, int start = 0) where T : ISerializable, new() { using (MemoryStream ms = new MemoryStream(value, start, value.Length - start, false)) @@ -40,7 +42,7 @@ public static T[] AsSerializableArray(this byte[] value, int max = 0x1000000) } } - internal static int GetVarSize(int value) + public static int GetVarSize(int value) { if (value < 0xFD) return sizeof(byte); @@ -50,7 +52,7 @@ internal static int GetVarSize(int value) return sizeof(byte) + sizeof(uint); } - internal static int GetVarSize(this T[] value) + public static int GetVarSize(this T[] value) { int value_size; Type t = typeof(T); @@ -79,7 +81,7 @@ internal static int GetVarSize(this T[] value) return GetVarSize(value.Length) + value_size; } - internal static int GetVarSize(this string value) + public static int GetVarSize(this string value) { int size = Encoding.UTF8.GetByteCount(value); return GetVarSize(size) + size; @@ -87,17 +89,16 @@ internal static int GetVarSize(this string value) public static byte[] ReadBytesWithGrouping(this BinaryReader reader) { - const int GROUP_SIZE = 16; using (MemoryStream ms = new MemoryStream()) { int padding = 0; do { - byte[] group = reader.ReadBytes(GROUP_SIZE); + byte[] group = reader.ReadBytes(GroupingSizeInBytes); padding = reader.ReadByte(); - if (padding > GROUP_SIZE) + if (padding > GroupingSizeInBytes) throw new FormatException(); - int count = GROUP_SIZE - padding; + int count = GroupingSizeInBytes - padding; if (count > 0) ms.Write(group, 0, count); } while (padding == 0); @@ -193,19 +194,18 @@ public static void Write(this BinaryWriter writer, ISerializable value) public static void WriteBytesWithGrouping(this BinaryWriter writer, byte[] value) { - const int GROUP_SIZE = 16; int index = 0; int remain = value.Length; - while (remain >= GROUP_SIZE) + while (remain >= GroupingSizeInBytes) { - writer.Write(value, index, GROUP_SIZE); + writer.Write(value, index, GroupingSizeInBytes); writer.Write((byte)0); - index += GROUP_SIZE; - remain -= GROUP_SIZE; + index += GroupingSizeInBytes; + remain -= GroupingSizeInBytes; } if (remain > 0) writer.Write(value, index, remain); - int padding = GROUP_SIZE - remain; + int padding = GroupingSizeInBytes - remain; for (int i = 0; i < padding; i++) writer.Write((byte)0); writer.Write((byte)padding); diff --git a/neo/Persistence/LevelDB/DbCache.cs b/neo/Persistence/LevelDB/DbCache.cs index b3d207d4de..86ff7a39ea 100644 --- a/neo/Persistence/LevelDB/DbCache.cs +++ b/neo/Persistence/LevelDB/DbCache.cs @@ -6,7 +6,7 @@ namespace Neo.Persistence.LevelDB { - internal class DbCache : DataCache + public class DbCache : DataCache where TKey : IEquatable, ISerializable, new() where TValue : class, ICloneable, ISerializable, new() {