Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose DbCache (for plugins, etc.) + useful Neo.IO.Helper methods #613

Merged
merged 2 commits into from
Feb 27, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private?

Copy link
Contributor Author

@jsolman jsolman Feb 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also can be needed in code that uses Find from a plugin. I have code that needs it, because to search for a prefix one needs to add a 0 byte after every 16 characters to search for a prefix longer than this length.

Copy link
Contributor Author

@jsolman jsolman Feb 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the Find method inserted the 0 bytes after every 16 characters it wouldn’t be needed. If we change that in a subsequent PR we could change this to be internal perhaps, but for now, we need it public.


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