Skip to content

Commit

Permalink
Expose DbCache (for plugins, etc.) + useful Neo.IO.Helper methods (#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsolman committed Feb 27, 2019
1 parent b5bd56d commit e86a8b6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions neo/IO/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Neo.IO
{
public static class Helper
{
public const int GroupingSizeInBytes = 16;

public static T AsSerializable<T>(this byte[] value, int start = 0) where T : ISerializable, new()
{
using (MemoryStream ms = new MemoryStream(value, start, value.Length - start, false))
Expand Down Expand Up @@ -40,7 +42,7 @@ public static T[] AsSerializableArray<T>(this byte[] value, int max = 0x1000000)
}
}

internal static int GetVarSize(int value)
public static int GetVarSize(int value)
{
if (value < 0xFD)
return sizeof(byte);
Expand All @@ -50,7 +52,7 @@ internal static int GetVarSize(int value)
return sizeof(byte) + sizeof(uint);
}

internal static int GetVarSize<T>(this T[] value)
public static int GetVarSize<T>(this T[] value)
{
int value_size;
Type t = typeof(T);
Expand Down Expand Up @@ -79,25 +81,24 @@ internal static int GetVarSize<T>(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;
}

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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion neo/Persistence/LevelDB/DbCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Neo.Persistence.LevelDB
{
internal class DbCache<TKey, TValue> : DataCache<TKey, TValue>
public class DbCache<TKey, TValue> : DataCache<TKey, TValue>
where TKey : IEquatable<TKey>, ISerializable, new()
where TValue : class, ICloneable<TValue>, ISerializable, new()
{
Expand Down

0 comments on commit e86a8b6

Please sign in to comment.