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

[LevelDbStore] Updated features & Usability of Interop #3274

Closed
Closed
Show file tree
Hide file tree
Changes from 6 commits
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
44 changes: 44 additions & 0 deletions src/Plugins/LevelDBStore/Cache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// Cache.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository 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.

namespace LevelDB
{
/// <summary>
/// A Cache is an interface that maps keys to values. It has internal
/// synchronization and may be safely accessed concurrently from
/// multiple threads. It may automatically evict entries to make room
/// for new entries. Values have a specified charge against the cache
/// capacity. For example, a cache where the values are variable
/// length strings, may use the length of the string as the charge for
/// the string.
///
/// A builtin cache implementation with a least-recently-used eviction
/// policy is provided. Clients may use their own implementations if
/// they want something more sophisticated (like scan-resistance, a
/// custom eviction policy, variable cache sizing, etc.)
/// </summary>
public class Cache : LevelDBHandle
{
/// <summary>
/// Create a new cache with a fixed size capacity. This implementation
/// of Cache uses a LRU eviction policy.
/// </summary>
public Cache(int capacity)
{
Handle = LevelDBInterop.leveldb_cache_create_lru(capacity);
}

protected override void FreeUnManagedObjects()
{
LevelDBInterop.leveldb_cache_destroy(Handle);
}
}
}
138 changes: 138 additions & 0 deletions src/Plugins/LevelDBStore/Comparator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// Comparator.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository 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 LevelDB.NativePointer;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace LevelDB
{
public class Comparator : LevelDBHandle
{
private sealed class Inner : IDisposable
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void Destructor(IntPtr gCHandleThis);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate int Compare(IntPtr gCHandleThisg,
IntPtr data1, IntPtr size1,
IntPtr data2, IntPtr size2);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate IntPtr Name(IntPtr gCHandleThis);


private static readonly Destructor s_destructor = (gCHandleThis) =>
{
var h = GCHandle.FromIntPtr(gCHandleThis);
var @this = h.Target as Inner;

@this.Dispose();
h.Free();
};

private static readonly Compare s_compare = (gCHandleThis, data1, size1, data2, size2) =>
{
var @this = GCHandle.FromIntPtr(gCHandleThis).Target as Inner;
return @this._cmp(new NativeArray { _baseAddr = data1, _byteLength = size1 },
new NativeArray { _baseAddr = data2, _byteLength = size2 });
};

private static readonly Name s_nameAccessor = (gCHandleThis) =>
{
var @this = GCHandle.FromIntPtr(gCHandleThis).Target as Inner;
return @this.NameValue;
};

private Func<NativeArray, NativeArray, int> _cmp;
private GCHandle _namePinned;

public IntPtr Init(string name, Func<NativeArray, NativeArray, int> cmp)
{
// TODO: Complete member initialization
_cmp = cmp;

_namePinned = GCHandle.Alloc(
Encoding.ASCII.GetBytes(name),
GCHandleType.Pinned);

var thisHandle = GCHandle.Alloc(this);

var chandle = LevelDBInterop.leveldb_comparator_create(
GCHandle.ToIntPtr(thisHandle),
Marshal.GetFunctionPointerForDelegate(s_destructor),
Marshal.GetFunctionPointerForDelegate(s_compare),
Marshal.GetFunctionPointerForDelegate(s_nameAccessor)
);

if (chandle == default)
thisHandle.Free();
return chandle;
}

private unsafe IntPtr NameValue
{
get
{
// TODO: this is probably not the most effective way to get a pinned string
var s = ((byte[])_namePinned.Target);
fixed (byte* p = s)
{
// Note: pinning the GCHandle ensures this value should remain stable
// Note: outside of the 'fixed' block.
return (IntPtr)p;
}
}
}

public void Dispose()
{
if (_namePinned.IsAllocated)
_namePinned.Free();
}
}

private Comparator(string name, Func<NativeArray, NativeArray, int> cmp)
{
var inner = new Inner();
try
{
Handle = inner.Init(name, cmp);
}
finally
{
if (Handle == default)
inner.Dispose();
}
}

public static Comparator Create(string name, Func<NativeArray, NativeArray, int> cmp)
{
return new Comparator(name, cmp);
}
public static Comparator Create(string name, IComparer<NativeArray> cmp)
{
return new Comparator(name, (a, b) => cmp.Compare(a, b));
}

protected override void FreeUnManagedObjects()
{
if (Handle != default)
{
// indirectly invoked CleanupInner
LevelDBInterop.leveldb_comparator_destroy(Handle);
}
}
}
}
25 changes: 25 additions & 0 deletions src/Plugins/LevelDBStore/CompressionLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// CompressionLevel.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository 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.

namespace LevelDB
{
/// <summary>
/// DB contents are stored in a set of blocks, each of which holds a
/// sequence of key,value pairs. Each block may be compressed before
/// being stored in a file. The following enum describes which
/// compression method (if any) is used to compress a block.
/// </summary>
public enum CompressionLevel
{
NoCompression = 0,
SnappyCompression = 1
}
}
Loading
Loading