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

RocksDB #1063

Closed
wants to merge 53 commits into from
Closed

RocksDB #1063

Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
8ae0260
RocksDB
shargon Aug 27, 2019
456b299
Format
shargon Aug 27, 2019
1e412eb
Using
shargon Aug 27, 2019
d1f528a
Add AggressiveInlining
shargon Aug 27, 2019
f9e7008
Indent
shargon Aug 27, 2019
2a3ee99
Add comment
shargon Aug 27, 2019
220f13f
Add block size
shargon Aug 27, 2019
b065e93
Column families as prefixes
shargon Aug 27, 2019
2f9631d
Restoring LevelDB for allow switch
shargon Aug 27, 2019
f163634
Format
shargon Aug 27, 2019
c675418
Restore ut
shargon Aug 27, 2019
6306289
Real restore
shargon Aug 27, 2019
5048773
Fix
shargon Aug 27, 2019
fb9c667
Allow neo-cli to use Store instead of LevelDBStore
shargon Aug 27, 2019
f7dc852
Merge branch 'master' into rocks-db
shargon Aug 27, 2019
df64336
Add comments
shargon Aug 27, 2019
b93db17
Merge remote-tracking branch 'shargon/rocks-db' into rocks-db
shargon Aug 27, 2019
2823922
Remove Key from MetaDataCache
shargon Aug 27, 2019
b77dde2
Remove Slide from return
shargon Aug 27, 2019
0adbe39
Remove Slide from return from LevelDB
shargon Aug 27, 2019
ea5b706
Follow VisualStudio recomendations
shargon Aug 28, 2019
0090916
Optimize RocsDB reducing Slide conversions
shargon Aug 28, 2019
75455b6
Fix
shargon Aug 28, 2019
3de653e
Move optional parameters at the end
shargon Aug 28, 2019
07ac2b6
DefaultSync in LevelDB
shargon Aug 28, 2019
e02fb7f
Rename DB to XDBCore
shargon Aug 28, 2019
318a87a
Remove Slide from RocksDB
shargon Aug 28, 2019
9598680
Merge branch 'master' into rocks-db
shargon Aug 28, 2019
fe6b4e3
Merge branch 'master' into rocks-db
shargon Aug 28, 2019
3fe6170
Remove prefix from rocksDB
shargon Aug 28, 2019
53fc3e3
Prefix to LevelDB again
shargon Aug 28, 2019
b5f5eaa
Remove null
shargon Aug 28, 2019
2fb7af6
Update neo/Persistence/RocksDB/RocksDBStore.cs
shargon Aug 29, 2019
c799545
Update neo/Persistence/RocksDB/DbCache.cs
shargon Aug 29, 2019
d3b412a
Remove BOM
shargon Aug 29, 2019
5fb477c
Merge branch 'master' into rocks-db
shargon Aug 29, 2019
78e2fbc
Some fixes
shargon Aug 29, 2019
703037f
Allow to clear a column family
shargon Aug 29, 2019
f385417
Merge branch 'master' into rocks-db
shargon Aug 29, 2019
19b18f9
Refactor column family creation
shargon Aug 29, 2019
6051e67
Merge remote-tracking branch 'shargon/rocks-db' into rocks-db
shargon Aug 29, 2019
6880333
Move to options
shargon Aug 29, 2019
7cf5b7a
Merge branch 'master' into rocks-db
vncoelho Aug 30, 2019
ace3685
Revert LevelDB Slice changes
shargon Aug 30, 2019
7786d00
Rename DefaultSync to SyncWrite
shargon Aug 30, 2019
f2241e1
dotnet format
shargon Aug 30, 2019
a404e90
Remove using
erikzhang Aug 31, 2019
3ff4cde
Update RocksDb nuget
shargon Sep 1, 2019
452d452
Merge branch 'master' into rocks-db
shargon Sep 1, 2019
ab7d7b5
Merge branch 'master' into rocks-db
shargon Sep 2, 2019
9d33256
Merge branch 'master' into rocks-db
lock9 Sep 30, 2019
9ee542c
Merge branch 'master' into rocks-db
shargon Nov 17, 2019
46c552d
Merge branch 'master' into rocks-db
lock9 Nov 17, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Runtime.InteropServices;
using System.Text;

namespace Neo.UnitTests.IO.Data.LevelDb
namespace Neo.UnitTests.IO.Data.LevelDB
{
public class Test { }

Expand Down
18 changes: 8 additions & 10 deletions neo/IO/Data/LevelDB/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public static void Delete(this WriteBatch batch, byte prefix, ISerializable key)
batch.Delete(SliceBuilder.Begin(prefix).Add(key));
}

public static IEnumerable<T> Find<T>(this DB db, ReadOptions options, byte prefix) where T : class, ISerializable, new()
public static IEnumerable<T> Find<T>(this LevelDBCore db, ReadOptions options, byte prefix) where T : class, ISerializable, new()
{
return Find(db, options, SliceBuilder.Begin(prefix), (k, v) => v.ToArray().AsSerializable<T>());
}

public static IEnumerable<T> Find<T>(this DB db, ReadOptions options, Slice prefix, Func<Slice, Slice, T> resultSelector)
public static IEnumerable<T> Find<T>(this LevelDBCore db, ReadOptions options, Slice prefix, Func<Slice, Slice, T> resultSelector)
{
using (Iterator it = db.NewIterator(options))
{
Expand All @@ -32,12 +32,12 @@ public static IEnumerable<T> Find<T>(this DB db, ReadOptions options, Slice pref
}
}

public static T Get<T>(this DB db, ReadOptions options, byte prefix, ISerializable key) where T : class, ISerializable, new()
public static T Get<T>(this LevelDBCore db, ReadOptions options, byte prefix, ISerializable key) where T : class, ISerializable, new()
{
return db.Get(options, SliceBuilder.Begin(prefix).Add(key)).ToArray().AsSerializable<T>();
}

public static T Get<T>(this DB db, ReadOptions options, byte prefix, ISerializable key, Func<Slice, T> resultSelector)
public static T Get<T>(this LevelDBCore db, ReadOptions options, byte prefix, ISerializable key, Func<Slice, T> resultSelector)
{
return resultSelector(db.Get(options, SliceBuilder.Begin(prefix).Add(key)));
}
Expand All @@ -47,18 +47,16 @@ public static void Put(this WriteBatch batch, byte prefix, ISerializable key, IS
batch.Put(SliceBuilder.Begin(prefix).Add(key), value.ToArray());
}

public static T TryGet<T>(this DB db, ReadOptions options, byte prefix, ISerializable key) where T : class, ISerializable, new()
public static T TryGet<T>(this LevelDBCore db, ReadOptions options, byte prefix, ISerializable key) where T : class, ISerializable, new()
{
Slice slice;
if (!db.TryGet(options, SliceBuilder.Begin(prefix).Add(key), out slice))
if (!db.TryGet(options, SliceBuilder.Begin(prefix).Add(key), out var slice))
return null;
return slice.ToArray().AsSerializable<T>();
}

public static T TryGet<T>(this DB db, ReadOptions options, byte prefix, ISerializable key, Func<Slice, T> resultSelector) where T : class
public static T TryGet<T>(this LevelDBCore db, ReadOptions options, byte prefix, ISerializable key, Func<Slice, T> resultSelector) where T : class
{
Slice slice;
if (!db.TryGet(options, SliceBuilder.Begin(prefix).Add(key), out slice))
if (!db.TryGet(options, SliceBuilder.Begin(prefix).Add(key), out var slice))
return null;
return resultSelector(slice);
}
Expand Down
9 changes: 3 additions & 6 deletions neo/IO/Data/LevelDB/Iterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ internal Iterator(IntPtr handle)

private void CheckError()
{
IntPtr error;
Native.leveldb_iter_get_error(handle, out error);
Native.leveldb_iter_get_error(handle, out var error);
NativeHelper.CheckError(error);
}

Expand All @@ -29,8 +28,7 @@ public void Dispose()

public Slice Key()
{
UIntPtr length;
IntPtr key = Native.leveldb_iter_key(handle, out length);
IntPtr key = Native.leveldb_iter_key(handle, out var length);
CheckError();
return new Slice(key, length);
}
Expand Down Expand Up @@ -69,8 +67,7 @@ public bool Valid()

public Slice Value()
{
UIntPtr length;
IntPtr value = Native.leveldb_iter_value(handle, out length);
IntPtr value = Native.leveldb_iter_value(handle, out var length);
CheckError();
return new Slice(value, length);
}
Expand Down
30 changes: 11 additions & 19 deletions neo/IO/Data/LevelDB/DB.cs → neo/IO/Data/LevelDB/LevelDBCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Neo.IO.Data.LevelDB
{
public class DB : IDisposable
public class LevelDBCore : IDisposable
{
private IntPtr handle;

Expand All @@ -11,7 +11,7 @@ public class DB : IDisposable
/// </summary>
public bool IsDisposed => handle == IntPtr.Zero;

private DB(IntPtr handle)
private LevelDBCore(IntPtr handle)
{
this.handle = handle;
}
Expand All @@ -27,16 +27,13 @@ public void Dispose()

public void Delete(WriteOptions options, Slice key)
{
IntPtr error;
Native.leveldb_delete(handle, options.handle, key.buffer, (UIntPtr)key.buffer.Length, out error);
Native.leveldb_delete(handle, options.handle, key.buffer, (UIntPtr)key.buffer.Length, out var error);
NativeHelper.CheckError(error);
}

public Slice Get(ReadOptions options, Slice key)
{
UIntPtr length;
IntPtr error;
IntPtr value = Native.leveldb_get(handle, options.handle, key.buffer, (UIntPtr)key.buffer.Length, out length, out error);
IntPtr value = Native.leveldb_get(handle, options.handle, key.buffer, (UIntPtr)key.buffer.Length, out var length, out var error);
try
{
NativeHelper.CheckError(error);
Expand All @@ -60,31 +57,27 @@ public Iterator NewIterator(ReadOptions options)
return new Iterator(Native.leveldb_create_iterator(handle, options.handle));
}

public static DB Open(string name)
public static LevelDBCore Open(string name)
{
return Open(name, Options.Default);
}

public static DB Open(string name, Options options)
public static LevelDBCore Open(string name, Options options)
{
IntPtr error;
IntPtr handle = Native.leveldb_open(options.handle, name, out error);
IntPtr handle = Native.leveldb_open(options.handle, name, out var error);
NativeHelper.CheckError(error);
return new DB(handle);
return new LevelDBCore(handle);
}

public void Put(WriteOptions options, Slice key, Slice value)
{
IntPtr error;
Native.leveldb_put(handle, options.handle, key.buffer, (UIntPtr)key.buffer.Length, value.buffer, (UIntPtr)value.buffer.Length, out error);
Native.leveldb_put(handle, options.handle, key.buffer, (UIntPtr)key.buffer.Length, value.buffer, (UIntPtr)value.buffer.Length, out var error);
NativeHelper.CheckError(error);
}

public bool TryGet(ReadOptions options, Slice key, out Slice value)
{
UIntPtr length;
IntPtr error;
IntPtr v = Native.leveldb_get(handle, options.handle, key.buffer, (UIntPtr)key.buffer.Length, out length, out error);
IntPtr v = Native.leveldb_get(handle, options.handle, key.buffer, (UIntPtr)key.buffer.Length, out var length, out var error);
if (error != IntPtr.Zero)
{
Native.leveldb_free(error);
Expand Down Expand Up @@ -112,8 +105,7 @@ public void Write(WriteOptions options, WriteBatch write_batch)
{
try
{
IntPtr error;
Native.leveldb_write(handle, options.handle, write_batch.handle, out error);
Native.leveldb_write(handle, options.handle, write_batch.handle, out var error);
NativeHelper.CheckError(error);
break;
}
Expand Down
73 changes: 57 additions & 16 deletions neo/IO/Data/LevelDB/Slice.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
using Neo.Cryptography;
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

namespace Neo.IO.Data.LevelDB
{
public struct Slice : IComparable<Slice>, IEquatable<Slice>
{
internal byte[] buffer;
internal readonly byte[] buffer;

/// <summary>
/// Constructor
/// </summary>
/// <param name="data">Data</param>
/// <param name="length">Length</param>
internal Slice(IntPtr data, UIntPtr length)
{
buffer = new byte[(int)length];
Marshal.Copy(data, buffer, 0, (int)length);
}

/// <summary>
/// Constructor
/// </summary>
/// <param name="input">Input</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal Slice(byte[] input)
{
buffer = input;
}

public int CompareTo(Slice other)
{
for (int i = 0; i < buffer.Length && i < other.buffer.Length; i++)
Expand All @@ -34,16 +50,16 @@ public bool Equals(Slice other)

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (!(obj is Slice)) return false;
return Equals((Slice)obj);
if (!(obj is Slice slide)) return false;
return Equals(slide);
}

public override int GetHashCode()
{
return (int)buffer.Murmur32(0);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte[] ToArray()
{
return buffer ?? new byte[0];
Expand Down Expand Up @@ -116,6 +132,7 @@ unsafe public float ToSingle()
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override string ToString()
{
return Encoding.UTF8.GetString(buffer);
Expand Down Expand Up @@ -151,91 +168,115 @@ unsafe public ulong ToUInt64()
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator byte[](Slice value)
{
return value.buffer;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Slice(byte[] data)
{
return new Slice { buffer = data };
return new Slice(data);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Slice(bool data)
{
return new Slice { buffer = BitConverter.GetBytes(data) };
return new Slice(BitConverter.GetBytes(data));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Slice(byte data)
{
return new Slice { buffer = new[] { data } };
return new Slice(new[] { data });
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Slice(double data)
{
return new Slice { buffer = BitConverter.GetBytes(data) };
return new Slice(BitConverter.GetBytes(data));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Slice(short data)
{
return new Slice { buffer = BitConverter.GetBytes(data) };
return new Slice(BitConverter.GetBytes(data));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Slice(int data)
{
return new Slice { buffer = BitConverter.GetBytes(data) };
return new Slice(BitConverter.GetBytes(data));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Slice(long data)
{
return new Slice { buffer = BitConverter.GetBytes(data) };
return new Slice(BitConverter.GetBytes(data));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Slice(float data)
{
return new Slice { buffer = BitConverter.GetBytes(data) };
return new Slice(BitConverter.GetBytes(data));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Slice(string data)
{
return new Slice { buffer = Encoding.UTF8.GetBytes(data) };
return new Slice(Encoding.UTF8.GetBytes(data));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Slice(ushort data)
{
return new Slice { buffer = BitConverter.GetBytes(data) };
return new Slice(BitConverter.GetBytes(data));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Slice(uint data)
{
return new Slice { buffer = BitConverter.GetBytes(data) };
return new Slice(BitConverter.GetBytes(data));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Slice(ulong data)
{
return new Slice { buffer = BitConverter.GetBytes(data) };
return new Slice(BitConverter.GetBytes(data));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator <(Slice x, Slice y)
{
return x.CompareTo(y) < 0;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator <=(Slice x, Slice y)
{
return x.CompareTo(y) <= 0;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator >(Slice x, Slice y)
{
return x.CompareTo(y) > 0;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator >=(Slice x, Slice y)
{
return x.CompareTo(y) >= 0;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Slice x, Slice y)
{
return x.Equals(y);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Slice x, Slice y)
{
return !x.Equals(y);
Expand Down
Loading