diff --git a/src/Plugins/LevelDBStore/IO/Data/LevelDB/DB.cs b/src/Plugins/LevelDBStore/IO/Data/LevelDB/DB.cs
index 60e0e24e5a..531cc542a8 100644
--- a/src/Plugins/LevelDBStore/IO/Data/LevelDB/DB.cs
+++ b/src/Plugins/LevelDBStore/IO/Data/LevelDB/DB.cs
@@ -10,57 +10,156 @@
// modifications are permitted.
using System;
-using System.IO;
+using System.Collections;
+using System.Collections.Generic;
+using System.Runtime.InteropServices;
-namespace Neo.IO.Data.LevelDB
+namespace Neo.IO.Storage.LevelDB
{
- public class DB : IDisposable
+ ///
+ /// A DB is a persistent ordered map from keys to values.
+ /// A DB is safe for concurrent access from multiple threads without any external synchronization.
+ ///
+ public class DB : LevelDBHandle, IEnumerable>
{
- private IntPtr handle;
+ static void Throw(IntPtr error)
+ {
+ Throw(error, msg => new LevelDBException(msg));
+ }
+
+ static void Throw(IntPtr error, Func exception)
+ {
+ if (error != IntPtr.Zero)
+ {
+ try
+ {
+ var msg = Marshal.PtrToStringAnsi(error);
+ throw exception(msg);
+ }
+ finally
+ {
+ Native.leveldb_free(error);
+ }
+ }
+ }
///
- /// Return true if haven't got valid handle
+ /// Open the database with the specified "name".
///
- public bool IsDisposed => handle == IntPtr.Zero;
+ public DB(string name, Options options)
+ {
+ Handle = Native.leveldb_open(options.Handle, name, out var error);
+ Throw(error, msg => new UnauthorizedAccessException(msg));
+ }
- private DB(IntPtr handle)
+ public void Close()
{
- this.handle = handle;
+ (this as IDisposable).Dispose();
}
- public void Dispose()
+ ///
+ /// Set the database entry for "key" to "value".
+ ///
+ public void Put(byte[] key, byte[] value)
{
- if (handle != IntPtr.Zero)
- {
- Native.leveldb_close(handle);
- handle = IntPtr.Zero;
- }
+ Put(key, value, new WriteOptions());
+ }
+
+ ///
+ /// Set the database entry for "key" to "value".
+ /// Note: consider setting new WriteOptions{ Sync = true }.
+ ///
+ public void Put(byte[] key, byte[] value, WriteOptions options)
+ {
+ Native.leveldb_put(Handle, options.Handle, key, checked((IntPtr)key.LongLength), value, checked((IntPtr)value.LongLength), out var error);
+ Throw(error);
}
- public void Delete(WriteOptions options, byte[] key)
+ ///
+ /// Remove the database entry (if any) for "key".
+ /// It is not an error if "key" did not exist in the database.
+ ///
+ public void Delete(byte[] key)
{
- Native.leveldb_delete(handle, options.handle, key, (UIntPtr)key.Length, out IntPtr error);
- NativeHelper.CheckError(error);
+ Delete(key, new WriteOptions());
}
- public byte[] Get(ReadOptions options, byte[] key)
+ ///
+ /// Remove the database entry (if any) for "key".
+ /// It is not an error if "key" did not exist in the database.
+ /// Note: consider setting new WriteOptions{ Sync = true }.
+ ///
+ public void Delete(byte[] key, WriteOptions options)
{
- IntPtr value = Native.leveldb_get(handle, options.handle, key, (UIntPtr)key.Length, out UIntPtr length, out IntPtr error);
- try
- {
- NativeHelper.CheckError(error);
- return value.ToByteArray(length);
- }
- finally
+ Native.leveldb_delete(Handle, options.Handle, key, checked((nint)key.LongLength), out var error);
+ Throw(error);
+ }
+
+ public void Write(WriteBatch batch)
+ {
+ Write(batch, new WriteOptions());
+ }
+
+ public void Write(WriteBatch batch, WriteOptions options)
+ {
+ Native.leveldb_write(Handle, options.Handle, batch.Handle, out var error);
+ Throw(error);
+ }
+
+ ///
+ /// If the database contains an entry for "key" return the value,
+ /// otherwise return null.
+ ///
+ public byte[] Get(byte[] key)
+ {
+ return Get(key, ReadOptions.Default);
+ }
+
+ ///
+ /// If the database contains an entry for "key" return the value,
+ /// otherwise return null.
+ ///
+ public byte[] Get(byte[] key, ReadOptions options)
+ {
+ var v = Native.leveldb_get(Handle, options.Handle, key, checked((IntPtr)key.LongLength), out var length, out var error);
+ Throw(error);
+
+ if (v != IntPtr.Zero)
{
- if (value != IntPtr.Zero) Native.leveldb_free(value);
+ try
+ {
+
+ var len = (long)length;
+ int c = 0, si = 0;
+
+ var bytes = new byte[len];
+
+ while (si < len)
+ {
+ if (len - si > int.MaxValue)
+ c += int.MaxValue;
+ else
+ c += checked((int)(len - si));
+
+ // Method has a ~2GB limit.
+ Marshal.Copy(v, bytes, si, c);
+
+ si += c;
+ }
+ return bytes;
+ }
+ finally
+ {
+ Native.leveldb_free(v);
+ }
}
+ return null;
}
- public bool Contains(ReadOptions options, byte[] key)
+ public bool Contains(byte[] key, ReadOptions options)
{
- IntPtr value = Native.leveldb_get(handle, options.handle, key, (UIntPtr)key.Length, out _, out IntPtr error);
- NativeHelper.CheckError(error);
+ var value = Native.leveldb_get(Handle, options.Handle, key, key.Length, out _, out var error);
+ Throw(error);
if (value != IntPtr.Zero)
{
@@ -71,44 +170,109 @@ public bool Contains(ReadOptions options, byte[] key)
return false;
}
- public Snapshot GetSnapshot()
+ ///
+ /// Return an iterator over the contents of the database.
+ /// The result of CreateIterator is initially invalid (caller must
+ /// call one of the Seek methods on the iterator before using it).
+ ///
+ public Iterator CreateIterator()
+ {
+ return CreateIterator(ReadOptions.Default);
+ }
+
+ ///
+ /// Return an iterator over the contents of the database.
+ /// The result of CreateIterator is initially invalid (caller must
+ /// call one of the Seek methods on the iterator before using it).
+ ///
+ public Iterator CreateIterator(ReadOptions options)
{
- return new Snapshot(handle);
+ return new Iterator(Native.leveldb_create_iterator(Handle, options.Handle));
}
- public Iterator NewIterator(ReadOptions options)
+ ///
+ /// Return a handle to the current DB state.
+ /// Iterators and Gets created with this handle will all observe a stable snapshot of the current DB state.
+ ///
+ public SnapShot CreateSnapshot()
{
- return new Iterator(Native.leveldb_create_iterator(handle, options.handle));
+ return new SnapShot(Native.leveldb_create_snapshot(Handle), this);
}
- public static DB Open(string name)
+ ///
+ /// DB implementations can export properties about their state
+ /// via this method. If "property" is a valid property understood by this
+ /// DB implementation, fills "*value" with its current value and returns
+ /// true. Otherwise returns false.
+ ///
+ /// Valid property names include:
+ ///
+ /// "leveldb.num-files-at-level" - return the number of files at level ,
+ /// where is an ASCII representation of a level number (e.g. "0").
+ /// "leveldb.stats" - returns a multi-line string that describes statistics
+ /// about the internal operation of the DB.
+ ///
+ public string PropertyValue(string name)
{
- return Open(name, Options.Default);
+ string result = null;
+ var ptr = Native.leveldb_property_value(Handle, name);
+ if (ptr != IntPtr.Zero)
+ {
+ try
+ {
+ return Marshal.PtrToStringAnsi(ptr);
+ }
+ finally
+ {
+ Native.leveldb_free(ptr);
+ }
+ }
+ return result;
}
- public static DB Open(string name, Options options)
+ ///
+ /// If a DB cannot be opened, you may attempt to call this method to
+ /// resurrect as much of the contents of the database as possible.
+ /// Some data may be lost, so be careful when calling this function
+ /// on a database that contains important information.
+ ///
+ public static void Repair(Options options, string name)
+ {
+ Native.leveldb_repair_db(options.Handle, name, out var error);
+ Throw(error);
+ }
+
+ ///
+ /// Destroy the contents of the specified database.
+ /// Be very careful using this method.
+ ///
+ public static void Destroy(Options options, string name)
{
- IntPtr handle = Native.leveldb_open(options.handle, Path.GetFullPath(name), out IntPtr error);
- NativeHelper.CheckError(error);
- return new DB(handle);
+ Native.leveldb_destroy_db(options.Handle, name, out var error);
+ Throw(error);
}
- public void Put(WriteOptions options, byte[] key, byte[] value)
+ protected override void FreeUnManagedObjects()
{
- Native.leveldb_put(handle, options.handle, key, (UIntPtr)key.Length, value, (UIntPtr)value.Length, out IntPtr error);
- NativeHelper.CheckError(error);
+ if (Handle != default)
+ Native.leveldb_close(Handle);
}
- public static void Repair(string name, Options options)
+ public IEnumerator> GetEnumerator()
{
- Native.leveldb_repair_db(options.handle, Path.GetFullPath(name), out IntPtr error);
- NativeHelper.CheckError(error);
+ using var sn = CreateSnapshot();
+ using var iterator = CreateIterator(new ReadOptions { Snapshot = sn });
+ iterator.SeekToFirst();
+ while (iterator.IsValid())
+ {
+ yield return new KeyValuePair(iterator.Key(), iterator.Value());
+ iterator.Next();
+ }
}
- public void Write(WriteOptions options, WriteBatch write_batch)
+ IEnumerator IEnumerable.GetEnumerator()
{
- Native.leveldb_write(handle, options.handle, write_batch.handle, out IntPtr error);
- NativeHelper.CheckError(error);
+ return GetEnumerator();
}
}
}
diff --git a/src/Plugins/LevelDBStore/IO/Data/LevelDB/Helper.cs b/src/Plugins/LevelDBStore/IO/Data/LevelDB/Helper.cs
index 2ac3a0005f..52e7b43407 100644
--- a/src/Plugins/LevelDBStore/IO/Data/LevelDB/Helper.cs
+++ b/src/Plugins/LevelDBStore/IO/Data/LevelDB/Helper.cs
@@ -9,55 +9,34 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.
-using Neo.Persistence;
using System;
using System.Collections.Generic;
-using System.Runtime.InteropServices;
-namespace Neo.IO.Data.LevelDB
+namespace Neo.IO.Storage.LevelDB
{
public static class Helper
{
- public static IEnumerable Seek(this DB db, ReadOptions options, byte[] prefix, SeekDirection direction, Func resultSelector)
+ public static IEnumerable<(byte[], byte[])> Seek(this DB db, byte[] prefix, ReadOptions options)
{
- using Iterator it = db.NewIterator(options);
- if (direction == SeekDirection.Forward)
- {
- for (it.Seek(prefix); it.Valid(); it.Next())
- yield return resultSelector(it.Key(), it.Value());
- }
- else
- {
- // SeekForPrev
+ using var it = db.CreateIterator(options);
- it.Seek(prefix);
- if (!it.Valid())
- it.SeekToLast();
- else if (it.Key().AsSpan().SequenceCompareTo(prefix) > 0)
- it.Prev();
-
- for (; it.Valid(); it.Prev())
- yield return resultSelector(it.Key(), it.Value());
- }
+ for (it.Seek(prefix); it.IsValid(); it.Next())
+ yield return new(it.Key(), it.Value());
}
- public static IEnumerable FindRange(this DB db, ReadOptions options, byte[] startKey, byte[] endKey, Func resultSelector)
+ public static IEnumerable<(byte[], byte[])> SeekPrev(this DB db, byte[] prefix, ReadOptions options)
{
- using Iterator it = db.NewIterator(options);
- for (it.Seek(startKey); it.Valid(); it.Next())
- {
- byte[] key = it.Key();
- if (key.AsSpan().SequenceCompareTo(endKey) > 0) break;
- yield return resultSelector(key, it.Value());
- }
- }
+ using var it = db.CreateIterator(options);
- internal static byte[] ToByteArray(this IntPtr data, UIntPtr length)
- {
- if (data == IntPtr.Zero) return null;
- byte[] buffer = new byte[(int)length];
- Marshal.Copy(data, buffer, 0, (int)length);
- return buffer;
+ it.Seek(prefix);
+
+ if (!it.IsValid())
+ it.SeekToLast();
+ else if (it.Key().AsSpan().SequenceCompareTo(prefix) > 0)
+ it.Prev();
+
+ for (; it.IsValid(); it.Prev())
+ yield return new(it.Key(), it.Value());
}
}
}
diff --git a/src/Plugins/LevelDBStore/IO/Data/LevelDB/Iterator.cs b/src/Plugins/LevelDBStore/IO/Data/LevelDB/Iterator.cs
index 6c025380fb..5c307964dd 100644
--- a/src/Plugins/LevelDBStore/IO/Data/LevelDB/Iterator.cs
+++ b/src/Plugins/LevelDBStore/IO/Data/LevelDB/Iterator.cs
@@ -10,77 +10,131 @@
// modifications are permitted.
using System;
+using System.Runtime.InteropServices;
-namespace Neo.IO.Data.LevelDB
+namespace Neo.IO.Storage.LevelDB
{
- public class Iterator : IDisposable
+ ///
+ /// An iterator yields a sequence of key/value pairs from a database.
+ ///
+ public class Iterator : LevelDBHandle
{
- private IntPtr handle;
+ internal Iterator(nint handle)
+ {
+ Handle = handle;
+ }
- internal Iterator(IntPtr handle)
+ ///
+ /// An iterator is either positioned at a key/value pair, or
+ /// not valid.
+ ///
+ /// This method returns true iff the iterator is valid.
+ public bool IsValid()
{
- this.handle = handle;
+ return Native.leveldb_iter_valid(Handle);
}
- private void CheckError()
+ ///
+ /// Position at the first key in the source.
+ /// The iterator is Valid() after this call iff the source is not empty.
+ ///
+ public void SeekToFirst()
{
- Native.leveldb_iter_get_error(handle, out IntPtr error);
- NativeHelper.CheckError(error);
+ Native.leveldb_iter_seek_to_first(Handle);
+ Throw();
}
- public void Dispose()
+ ///
+ /// Position at the last key in the source.
+ /// The iterator is Valid() after this call iff the source is not empty.
+ ///
+ public void SeekToLast()
{
- if (handle != IntPtr.Zero)
- {
- Native.leveldb_iter_destroy(handle);
- handle = IntPtr.Zero;
- }
+ Native.leveldb_iter_seek_to_last(Handle);
+ Throw();
}
- public byte[] Key()
+ ///
+ /// Position at the first key in the source that at or past target
+ /// The iterator is Valid() after this call iff the source contains
+ /// an entry that comes at or past target.
+ ///
+ public void Seek(byte[] key)
{
- IntPtr key = Native.leveldb_iter_key(handle, out UIntPtr length);
- CheckError();
- return key.ToByteArray(length);
+ Native.leveldb_iter_seek(Handle, key, key.Length);
+ Throw();
}
+ ///
+ /// Moves to the next entry in the source.
+ /// After this call, Valid() is true iff the iterator was not positioned at the last entry in the source.
+ /// REQUIRES: Valid()
+ ///
public void Next()
{
- Native.leveldb_iter_next(handle);
- CheckError();
+ Native.leveldb_iter_next(Handle);
+ Throw();
}
+ ///
+ /// Moves to the previous entry in the source.
+ /// After this call, Valid() is true iff the iterator was not positioned at the first entry in source.
+ /// REQUIRES: Valid()
+ ///
public void Prev()
{
- Native.leveldb_iter_prev(handle);
- CheckError();
+ Native.leveldb_iter_prev(Handle);
+ Throw();
}
- public void Seek(byte[] target)
+ ///
+ /// Return the key for the current entry.
+ /// REQUIRES: Valid()
+ ///
+ public byte[] Key()
{
- Native.leveldb_iter_seek(handle, target, (UIntPtr)target.Length);
+ var key = Native.leveldb_iter_key(Handle, out var length);
+ Throw();
+
+ var bytes = new byte[length];
+ Marshal.Copy(key, bytes, 0, length);
+ return bytes;
}
- public void SeekToFirst()
+ ///
+ /// Return the value for the current entry.
+ /// REQUIRES: Valid()
+ ///
+ public byte[] Value()
{
- Native.leveldb_iter_seek_to_first(handle);
+ var value = Native.leveldb_iter_value(Handle, out var length);
+ Throw();
+
+ var bytes = new byte[length];
+ Marshal.Copy(value, bytes, 0, length);
+ return bytes;
}
- public void SeekToLast()
+ ///
+ /// If an error has occurred, throw it.
+ ///
+ void Throw()
{
- Native.leveldb_iter_seek_to_last(handle);
+ Throw(msg => new Exception(msg));
}
- public bool Valid()
+ ///
+ /// If an error has occurred, throw it.
+ ///
+ void Throw(Func exception)
{
- return Native.leveldb_iter_valid(handle);
+ Native.leveldb_iter_get_error(Handle, out var error);
+ if (error != IntPtr.Zero) throw exception(Marshal.PtrToStringAnsi(error));
}
- public byte[] Value()
+ protected override void FreeUnManagedObjects()
{
- IntPtr value = Native.leveldb_iter_value(handle, out UIntPtr length);
- CheckError();
- return value.ToByteArray(length);
+ Native.leveldb_iter_destroy(Handle);
}
}
}
diff --git a/src/Plugins/LevelDBStore/IO/Data/LevelDB/LevelDBException.cs b/src/Plugins/LevelDBStore/IO/Data/LevelDB/LevelDBException.cs
index c9cca42070..ba900a655e 100644
--- a/src/Plugins/LevelDBStore/IO/Data/LevelDB/LevelDBException.cs
+++ b/src/Plugins/LevelDBStore/IO/Data/LevelDB/LevelDBException.cs
@@ -11,7 +11,7 @@
using System.Data.Common;
-namespace Neo.IO.Data.LevelDB
+namespace Neo.IO.Storage.LevelDB
{
public class LevelDBException : DbException
{
diff --git a/src/Plugins/LevelDBStore/IO/Data/LevelDB/LevelDBHandle.cs b/src/Plugins/LevelDBStore/IO/Data/LevelDB/LevelDBHandle.cs
new file mode 100644
index 0000000000..e2b8b338c4
--- /dev/null
+++ b/src/Plugins/LevelDBStore/IO/Data/LevelDB/LevelDBHandle.cs
@@ -0,0 +1,60 @@
+// Copyright (C) 2015-2024 The Neo Project.
+//
+// LevelDBHandle.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 System;
+
+namespace Neo.IO.Storage.LevelDB
+{
+ ///
+ /// Base class for all LevelDB objects
+ ///
+ public abstract class LevelDBHandle : IDisposable
+ {
+ public nint Handle { protected set; get; }
+
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ protected virtual void FreeManagedObjects()
+ {
+ }
+
+ protected virtual void FreeUnManagedObjects()
+ {
+ }
+
+ bool _disposed = false;
+ void Dispose(bool disposing)
+ {
+ if (!_disposed)
+ {
+ if (disposing)
+ {
+ FreeManagedObjects();
+ }
+ if (Handle != nint.Zero)
+ {
+ FreeUnManagedObjects();
+ Handle = nint.Zero;
+ }
+ _disposed = true;
+ }
+ }
+
+ ~LevelDBHandle()
+ {
+ Dispose(false);
+ }
+ }
+}
diff --git a/src/Plugins/LevelDBStore/IO/Data/LevelDB/Native.cs b/src/Plugins/LevelDBStore/IO/Data/LevelDB/Native.cs
index acf8fa82b9..4605f0bdcc 100644
--- a/src/Plugins/LevelDBStore/IO/Data/LevelDB/Native.cs
+++ b/src/Plugins/LevelDBStore/IO/Data/LevelDB/Native.cs
@@ -10,71 +10,79 @@
// modifications are permitted.
using System;
-using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
-namespace Neo.IO.Data.LevelDB
+namespace Neo.IO.Storage.LevelDB
{
- public enum CompressionType : byte
+ public enum CompressionType : int
{
- kNoCompression = 0x0,
- kSnappyCompression = 0x1
+ NoCompression = 0,
+ SnappyCompression = 1,
}
public static class Native
{
#region Logger
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern IntPtr leveldb_logger_create(IntPtr /* Action */ logger);
+ public static extern nint leveldb_logger_create(nint /* Action */ logger);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_logger_destroy(IntPtr /* logger*/ option);
+ public static extern void leveldb_logger_destroy(nint /* logger*/ option);
#endregion
#region DB
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern IntPtr leveldb_open(IntPtr /* Options*/ options, string name, out IntPtr error);
+ public static extern nint leveldb_open(nint /* Options*/ options, string name, out nint error);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_close(IntPtr /*DB */ db);
+ public static extern void leveldb_close(nint /*DB */ db);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_put(IntPtr /* DB */ db, IntPtr /* WriteOptions*/ options, byte[] key, UIntPtr keylen, byte[] val, UIntPtr vallen, out IntPtr errptr);
+ public static extern void leveldb_put(nint /* DB */ db, nint /* WriteOptions*/ options, byte[] key, nint keylen, byte[] val, nint vallen, out nint errptr);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_delete(IntPtr /* DB */ db, IntPtr /* WriteOptions*/ options, byte[] key, UIntPtr keylen, out IntPtr errptr);
+ public static extern void leveldb_put(nint /* DB */ db, nint /* WriteOptions*/ options, ref int key, nint keylen, int[] val, nint vallen, out nint errptr);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_write(IntPtr /* DB */ db, IntPtr /* WriteOptions*/ options, IntPtr /* WriteBatch */ batch, out IntPtr errptr);
+ public static extern void leveldb_delete(nint /* DB */ db, nint /* WriteOptions*/ options, byte[] key, nint keylen, out nint errptr);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern IntPtr leveldb_get(IntPtr /* DB */ db, IntPtr /* ReadOptions*/ options, byte[] key, UIntPtr keylen, out UIntPtr vallen, out IntPtr errptr);
+ public static extern void leveldb_write(nint /* DB */ db, nint /* WriteOptions*/ options, nint /* WriteBatch */ batch, out nint errptr);
+
+ [DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
+ public static extern nint leveldb_get(nint /* DB */ db, nint /* ReadOptions*/ options, byte[] key, nint keylen, out nint vallen, out nint errptr);
+
+ [DllImport("libleveldb", CallingConvention = CallingConvention.Cdecl)]
+ public static extern nint leveldb_get(nint /* DB */ db, nint /* ReadOptions*/ options, ref int key, nint keylen, out nint vallen, out nint errptr);
+
+ [DllImport("libleveldb", CallingConvention = CallingConvention.Cdecl)]
+ public static extern nint leveldb_get(nint /* DB */ db, nint /* ReadOptions*/ options, nint key, nint keylen, out nint vallen, out nint errptr);
//[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- //static extern void leveldb_approximate_sizes(IntPtr /* DB */ db, int num_ranges, byte[] range_start_key, long range_start_key_len, byte[] range_limit_key, long range_limit_key_len, out long sizes);
+ //static extern void leveldb_approximate_sizes(nint /* DB */ db, int num_ranges, byte[] range_start_key, long range_start_key_len, byte[] range_limit_key, long range_limit_key_len, out long sizes);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern IntPtr leveldb_create_iterator(IntPtr /* DB */ db, IntPtr /* ReadOption */ options);
+ public static extern nint leveldb_create_iterator(nint /* DB */ db, nint /* ReadOption */ options);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern IntPtr leveldb_create_snapshot(IntPtr /* DB */ db);
+ public static extern nint leveldb_create_snapshot(nint /* DB */ db);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_release_snapshot(IntPtr /* DB */ db, IntPtr /* SnapShot*/ snapshot);
+ public static extern void leveldb_release_snapshot(nint /* DB */ db, nint /* SnapShot*/ snapshot);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern IntPtr leveldb_property_value(IntPtr /* DB */ db, string propname);
+ public static extern nint leveldb_property_value(nint /* DB */ db, string propname);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_repair_db(IntPtr /* Options*/ options, string name, out IntPtr error);
+ public static extern void leveldb_repair_db(nint /* Options*/ options, string name, out nint error);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_destroy_db(IntPtr /* Options*/ options, string name, out IntPtr error);
+ public static extern void leveldb_destroy_db(nint /* Options*/ options, string name, out nint error);
#region extensions
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_free(IntPtr /* void */ ptr);
+ public static extern void leveldb_free(nint /* void */ ptr);
#endregion
@@ -83,182 +91,169 @@ public static class Native
#region Env
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern IntPtr leveldb_create_default_env();
+ public static extern nint leveldb_create_default_env();
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_env_destroy(IntPtr /*Env*/ cache);
+ public static extern void leveldb_env_destroy(nint /*Env*/ cache);
#endregion
#region Iterator
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_iter_destroy(IntPtr /*Iterator*/ iterator);
+ public static extern void leveldb_iter_destroy(nint /*Iterator*/ iterator);
+
+ [DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
+ public static extern bool leveldb_iter_valid(nint /*Iterator*/ iterator);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- [return: MarshalAs(UnmanagedType.U1)]
- public static extern bool leveldb_iter_valid(IntPtr /*Iterator*/ iterator);
+ public static extern void leveldb_iter_seek_to_first(nint /*Iterator*/ iterator);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_iter_seek_to_first(IntPtr /*Iterator*/ iterator);
+ public static extern void leveldb_iter_seek_to_last(nint /*Iterator*/ iterator);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_iter_seek_to_last(IntPtr /*Iterator*/ iterator);
+ public static extern void leveldb_iter_seek(nint /*Iterator*/ iterator, byte[] key, int length);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_iter_seek(IntPtr /*Iterator*/ iterator, byte[] key, UIntPtr length);
+ public static extern void leveldb_iter_seek(nint /*Iterator*/ iterator, ref int key, int length);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_iter_next(IntPtr /*Iterator*/ iterator);
+ public static extern void leveldb_iter_next(nint /*Iterator*/ iterator);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_iter_prev(IntPtr /*Iterator*/ iterator);
+ public static extern void leveldb_iter_prev(nint /*Iterator*/ iterator);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern IntPtr leveldb_iter_key(IntPtr /*Iterator*/ iterator, out UIntPtr length);
+ public static extern nint leveldb_iter_key(nint /*Iterator*/ iterator, out int length);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern IntPtr leveldb_iter_value(IntPtr /*Iterator*/ iterator, out UIntPtr length);
+ public static extern nint leveldb_iter_value(nint /*Iterator*/ iterator, out int length);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_iter_get_error(IntPtr /*Iterator*/ iterator, out IntPtr error);
+ public static extern void leveldb_iter_get_error(nint /*Iterator*/ iterator, out nint error);
#endregion
#region Options
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern IntPtr leveldb_options_create();
+ public static extern nint leveldb_options_create();
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_options_destroy(IntPtr /*Options*/ options);
+ public static extern void leveldb_options_destroy(nint /*Options*/ options);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_options_set_create_if_missing(IntPtr /*Options*/ options, [MarshalAs(UnmanagedType.U1)] bool o);
+ public static extern void leveldb_options_set_create_if_missing(nint /*Options*/ options, bool o);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_options_set_error_if_exists(IntPtr /*Options*/ options, [MarshalAs(UnmanagedType.U1)] bool o);
+ public static extern void leveldb_options_set_error_if_exists(nint /*Options*/ options, bool o);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_options_set_info_log(IntPtr /*Options*/ options, IntPtr /* Logger */ logger);
+ public static extern void leveldb_options_set_info_log(nint /*Options*/ options, nint /* Logger */ logger);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_options_set_paranoid_checks(IntPtr /*Options*/ options, [MarshalAs(UnmanagedType.U1)] bool o);
+ public static extern void leveldb_options_set_paranoid_checks(nint /*Options*/ options, bool o);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_options_set_env(IntPtr /*Options*/ options, IntPtr /*Env*/ env);
+ public static extern void leveldb_options_set_env(nint /*Options*/ options, nint /*Env*/ env);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_options_set_write_buffer_size(IntPtr /*Options*/ options, UIntPtr size);
+ public static extern void leveldb_options_set_write_buffer_size(nint /*Options*/ options, long size);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_options_set_max_open_files(IntPtr /*Options*/ options, int max);
+ public static extern void leveldb_options_set_max_open_files(nint /*Options*/ options, int max);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_options_set_cache(IntPtr /*Options*/ options, IntPtr /*Cache*/ cache);
+ public static extern void leveldb_options_set_cache(nint /*Options*/ options, nint /*Cache*/ cache);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_options_set_block_size(IntPtr /*Options*/ options, UIntPtr size);
+ public static extern void leveldb_options_set_block_size(nint /*Options*/ options, long size);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_options_set_block_restart_interval(IntPtr /*Options*/ options, int interval);
+ public static extern void leveldb_options_set_block_restart_interval(nint /*Options*/ options, int interval);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_options_set_compression(IntPtr /*Options*/ options, CompressionType level);
+ public static extern void leveldb_options_set_compression(nint /*Options*/ options, CompressionType level);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_options_set_comparator(IntPtr /*Options*/ options, IntPtr /*Comparator*/ comparer);
+ public static extern void leveldb_options_set_filter_policy(nint /*Options*/ options, nint /*FilterPolicy*/ policy);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_options_set_filter_policy(IntPtr /*Options*/ options, IntPtr /*FilterPolicy*/ policy);
-
+ public static extern void leveldb_options_set_comparator(nint /*Options*/ options, nint /*Comparator*/ comparer);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern IntPtr leveldb_filterpolicy_create_bloom(int bits_per_key);
+ public static extern nint leveldb_filterpolicy_create_bloom(int bits_per_key);
#endregion
#region ReadOptions
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern IntPtr leveldb_readoptions_create();
+ public static extern nint leveldb_readoptions_create();
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_readoptions_destroy(IntPtr /*ReadOptions*/ options);
+ public static extern void leveldb_readoptions_destroy(nint /*ReadOptions*/ options);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_readoptions_set_verify_checksums(IntPtr /*ReadOptions*/ options, [MarshalAs(UnmanagedType.U1)] bool o);
+ public static extern void leveldb_readoptions_set_verify_checksums(nint /*ReadOptions*/ options, bool o);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_readoptions_set_fill_cache(IntPtr /*ReadOptions*/ options, [MarshalAs(UnmanagedType.U1)] bool o);
+ public static extern void leveldb_readoptions_set_fill_cache(nint /*ReadOptions*/ options, bool o);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_readoptions_set_snapshot(IntPtr /*ReadOptions*/ options, IntPtr /*SnapShot*/ snapshot);
+ public static extern void leveldb_readoptions_set_snapshot(nint /*ReadOptions*/ options, nint /*SnapShot*/ snapshot);
#endregion
#region WriteBatch
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern IntPtr leveldb_writebatch_create();
+ public static extern nint leveldb_writebatch_create();
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_writebatch_destroy(IntPtr /* WriteBatch */ batch);
+ public static extern void leveldb_writebatch_destroy(nint /* WriteBatch */ batch);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_writebatch_clear(IntPtr /* WriteBatch */ batch);
+ public static extern void leveldb_writebatch_clear(nint /* WriteBatch */ batch);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_writebatch_put(IntPtr /* WriteBatch */ batch, byte[] key, UIntPtr keylen, byte[] val, UIntPtr vallen);
+ public static extern void leveldb_writebatch_put(nint /* WriteBatch */ batch, byte[] key, int keylen, byte[] val, int vallen);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_writebatch_delete(IntPtr /* WriteBatch */ batch, byte[] key, UIntPtr keylen);
+ public static extern void leveldb_writebatch_delete(nint /* WriteBatch */ batch, byte[] key, int keylen);
[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern void leveldb_writebatch_iterate(IntPtr /* WriteBatch */ batch, object state, Action