diff --git a/src/Plugins/LevelDBStore/IO/Data/LevelDB/DB.cs b/src/Plugins/LevelDBStore/IO/Data/LevelDB/DB.cs index ecb79344cc..583385d34a 100644 --- a/src/Plugins/LevelDBStore/IO/Data/LevelDB/DB.cs +++ b/src/Plugins/LevelDBStore/IO/Data/LevelDB/DB.cs @@ -9,10 +9,13 @@ // Redistribution and use in source and binary forms with or without // modifications are permitted. +#nullable enable + using System; +using System.Collections.Generic; using System.IO; -namespace Neo.IO.Data.LevelDB +namespace Neo.IO.Storage.LevelDB { /// /// A DB is a persistent ordered map from keys to values. @@ -90,9 +93,9 @@ public static DB Open(string name) public static DB Open(string name, Options options) { - var Handle = Native.leveldb_open(options.Handle, Path.GetFullPath(name), out var error); + var handle = Native.leveldb_open(options.Handle, Path.GetFullPath(name), out var error); NativeHelper.CheckError(error); - return new DB(Handle); + return new DB(handle); } /// @@ -122,5 +125,19 @@ public void Write(WriteOptions options, WriteBatch write_batch) Native.leveldb_write(Handle, options.Handle, write_batch.Handle, out var error); NativeHelper.CheckError(error); } + + public IEnumerable> GetAll(Snapshot? snapshot = null) + { + using var options = new ReadOptions(); + if (snapshot != null) options.Snapshot = snapshot; + + using var iterator = NewIterator(options); + iterator.SeekToFirst(); + while (iterator.Valid()) + { + yield return new KeyValuePair(iterator.Key(), iterator.Value()); + iterator.Next(); + } + } } } diff --git a/src/Plugins/LevelDBStore/IO/Data/LevelDB/Helper.cs b/src/Plugins/LevelDBStore/IO/Data/LevelDB/Helper.cs index 2ac3a0005f..907452353a 100644 --- a/src/Plugins/LevelDBStore/IO/Data/LevelDB/Helper.cs +++ b/src/Plugins/LevelDBStore/IO/Data/LevelDB/Helper.cs @@ -14,17 +14,17 @@ 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, ReadOptions options, byte[] prefix, SeekDirection direction) { 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()); + yield return new(it.Key(), it.Value()); } else { @@ -37,18 +37,7 @@ public static IEnumerable Seek(this DB db, ReadOptions options, byte[] pre it.Prev(); for (; it.Valid(); it.Prev()) - yield return resultSelector(it.Key(), it.Value()); - } - } - - public static IEnumerable FindRange(this DB db, ReadOptions options, byte[] startKey, byte[] endKey, Func resultSelector) - { - 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()); + 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 d3e36a3a9c..2fa007af7c 100644 --- a/src/Plugins/LevelDBStore/IO/Data/LevelDB/Iterator.cs +++ b/src/Plugins/LevelDBStore/IO/Data/LevelDB/Iterator.cs @@ -11,7 +11,7 @@ using System; -namespace Neo.IO.Data.LevelDB +namespace Neo.IO.Storage.LevelDB { /// /// An iterator yields a sequence of key/value pairs from a database. 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 index 2f8aa9e4db..6868eae884 100644 --- a/src/Plugins/LevelDBStore/IO/Data/LevelDB/LevelDBHandle.cs +++ b/src/Plugins/LevelDBStore/IO/Data/LevelDB/LevelDBHandle.cs @@ -11,7 +11,7 @@ using System; -namespace Neo.IO.Data.LevelDB +namespace Neo.IO.Storage.LevelDB { /// /// Base class for all LevelDB objects diff --git a/src/Plugins/LevelDBStore/IO/Data/LevelDB/Native.cs b/src/Plugins/LevelDBStore/IO/Data/LevelDB/Native.cs index acf8fa82b9..bf91524fae 100644 --- a/src/Plugins/LevelDBStore/IO/Data/LevelDB/Native.cs +++ b/src/Plugins/LevelDBStore/IO/Data/LevelDB/Native.cs @@ -13,7 +13,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -namespace Neo.IO.Data.LevelDB +namespace Neo.IO.Storage.LevelDB { public enum CompressionType : byte { @@ -25,56 +25,56 @@ 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, UIntPtr keylen, byte[] val, UIntPtr 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_delete(nint /* DB */ db, nint /* WriteOptions*/ options, byte[] key, UIntPtr keylen, 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_write(nint /* DB */ db, nint /* WriteOptions*/ options, nint /* WriteBatch */ batch, 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 nint leveldb_get(nint /* DB */ db, nint /* ReadOptions*/ options, byte[] key, UIntPtr keylen, out UIntPtr 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 + #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,167 +83,167 @@ 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)] [return: MarshalAs(UnmanagedType.U1)] - public static extern bool leveldb_iter_valid(IntPtr /*Iterator*/ iterator); + public static extern bool leveldb_iter_valid(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_first(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_to_last(nint /*Iterator*/ iterator); [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, byte[] key, UIntPtr 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 UIntPtr 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 UIntPtr 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, [MarshalAs(UnmanagedType.U1)] 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, [MarshalAs(UnmanagedType.U1)] 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, [MarshalAs(UnmanagedType.U1)] 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, UIntPtr 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, UIntPtr 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_comparator(nint /*Options*/ options, nint /*Comparator*/ comparer); [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_filter_policy(nint /*Options*/ options, nint /*FilterPolicy*/ policy); [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, [MarshalAs(UnmanagedType.U1)] 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, [MarshalAs(UnmanagedType.U1)] 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, UIntPtr keylen, byte[] val, UIntPtr 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, UIntPtr keylen); [DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] - public static extern void leveldb_writebatch_iterate(IntPtr /* WriteBatch */ batch, object state, Action put, Action deleted); + public static extern void leveldb_writebatch_iterate(nint /* WriteBatch */ batch, object state, Action put, Action deleted); #endregion #region WriteOptions [DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] - public static extern IntPtr leveldb_writeoptions_create(); + public static extern nint leveldb_writeoptions_create(); [DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] - public static extern void leveldb_writeoptions_destroy(IntPtr /*WriteOptions*/ options); + public static extern void leveldb_writeoptions_destroy(nint /*WriteOptions*/ options); [DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] - public static extern void leveldb_writeoptions_set_sync(IntPtr /*WriteOptions*/ options, [MarshalAs(UnmanagedType.U1)] bool o); + public static extern void leveldb_writeoptions_set_sync(nint /*WriteOptions*/ options, [MarshalAs(UnmanagedType.U1)] bool o); #endregion - #region Cache + #region Cache [DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] - public static extern IntPtr leveldb_cache_create_lru(int capacity); + public static extern nint leveldb_cache_create_lru(int capacity); [DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] - public static extern void leveldb_cache_destroy(IntPtr /*Cache*/ cache); + public static extern void leveldb_cache_destroy(nint /*Cache*/ cache); #endregion #region Comparator [DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] - public static extern IntPtr /* leveldb_comparator_t* */ + public static extern nint /* leveldb_comparator_t* */ leveldb_comparator_create( - IntPtr /* void* */ state, - IntPtr /* void (*)(void*) */ destructor, - IntPtr + nint /* void* */ state, + nint /* void (*)(void*) */ destructor, + nint /* int (*compare)(void*, const char* a, size_t alen, const char* b, size_t blen) */ compare, - IntPtr /* const char* (*)(void*) */ name); + nint /* const char* (*)(void*) */ name); [DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] - public static extern void leveldb_comparator_destroy(IntPtr /* leveldb_comparator_t* */ cmp); + public static extern void leveldb_comparator_destroy(nint /* leveldb_comparator_t* */ cmp); #endregion } @@ -251,9 +251,9 @@ public static extern IntPtr /* leveldb_comparator_t* */ internal static class NativeHelper { [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void CheckError(IntPtr error) + public static void CheckError(nint error) { - if (error != IntPtr.Zero) + if (error != nint.Zero) { string message = Marshal.PtrToStringAnsi(error); Native.leveldb_free(error); diff --git a/src/Plugins/LevelDBStore/IO/Data/LevelDB/Options.cs b/src/Plugins/LevelDBStore/IO/Data/LevelDB/Options.cs index 99c7fb2739..2c91d2f9b8 100644 --- a/src/Plugins/LevelDBStore/IO/Data/LevelDB/Options.cs +++ b/src/Plugins/LevelDBStore/IO/Data/LevelDB/Options.cs @@ -11,7 +11,7 @@ using System; -namespace Neo.IO.Data.LevelDB +namespace Neo.IO.Storage.LevelDB { /// /// Options to control the behavior of a database (passed to Open) diff --git a/src/Plugins/LevelDBStore/IO/Data/LevelDB/ReadOptions.cs b/src/Plugins/LevelDBStore/IO/Data/LevelDB/ReadOptions.cs index 9a1d48f76e..2a219fa1df 100644 --- a/src/Plugins/LevelDBStore/IO/Data/LevelDB/ReadOptions.cs +++ b/src/Plugins/LevelDBStore/IO/Data/LevelDB/ReadOptions.cs @@ -9,7 +9,7 @@ // Redistribution and use in source and binary forms with or without // modifications are permitted. -namespace Neo.IO.Data.LevelDB +namespace Neo.IO.Storage.LevelDB { /// /// Options that control read operations. diff --git a/src/Plugins/LevelDBStore/IO/Data/LevelDB/Snapshot.cs b/src/Plugins/LevelDBStore/IO/Data/LevelDB/Snapshot.cs index e9ca8d1ff6..668f06718b 100644 --- a/src/Plugins/LevelDBStore/IO/Data/LevelDB/Snapshot.cs +++ b/src/Plugins/LevelDBStore/IO/Data/LevelDB/Snapshot.cs @@ -11,7 +11,7 @@ using System; -namespace Neo.IO.Data.LevelDB +namespace Neo.IO.Storage.LevelDB { /// /// A Snapshot is an immutable object and can therefore be safely diff --git a/src/Plugins/LevelDBStore/IO/Data/LevelDB/WriteBatch.cs b/src/Plugins/LevelDBStore/IO/Data/LevelDB/WriteBatch.cs index 7e9c04305b..300476a5f1 100644 --- a/src/Plugins/LevelDBStore/IO/Data/LevelDB/WriteBatch.cs +++ b/src/Plugins/LevelDBStore/IO/Data/LevelDB/WriteBatch.cs @@ -11,7 +11,7 @@ using System; -namespace Neo.IO.Data.LevelDB +namespace Neo.IO.Storage.LevelDB { /// /// WriteBatch holds a collection of updates to apply atomically to a DB. diff --git a/src/Plugins/LevelDBStore/IO/Data/LevelDB/WriteOptions.cs b/src/Plugins/LevelDBStore/IO/Data/LevelDB/WriteOptions.cs index 994a97d8a2..a4a304ad33 100644 --- a/src/Plugins/LevelDBStore/IO/Data/LevelDB/WriteOptions.cs +++ b/src/Plugins/LevelDBStore/IO/Data/LevelDB/WriteOptions.cs @@ -9,7 +9,7 @@ // Redistribution and use in source and binary forms with or without // modifications are permitted. -namespace Neo.IO.Data.LevelDB +namespace Neo.IO.Storage.LevelDB { /// /// Options that control write operations. diff --git a/src/Plugins/LevelDBStore/Plugins/Storage/LevelDBStore.cs b/src/Plugins/LevelDBStore/Plugins/Storage/LevelDBStore.cs index 9c676e8a7f..6f39abd6af 100644 --- a/src/Plugins/LevelDBStore/Plugins/Storage/LevelDBStore.cs +++ b/src/Plugins/LevelDBStore/Plugins/Storage/LevelDBStore.cs @@ -9,7 +9,7 @@ // Redistribution and use in source and binary forms with or without // modifications are permitted. -using Neo.IO.Data.LevelDB; +using Neo.IO.Storage.LevelDB; using Neo.Persistence; using System; using System.Linq; diff --git a/src/Plugins/LevelDBStore/Plugins/Storage/Snapshot.cs b/src/Plugins/LevelDBStore/Plugins/Storage/Snapshot.cs index 4f7c73d820..fd7febd451 100644 --- a/src/Plugins/LevelDBStore/Plugins/Storage/Snapshot.cs +++ b/src/Plugins/LevelDBStore/Plugins/Storage/Snapshot.cs @@ -9,10 +9,10 @@ // Redistribution and use in source and binary forms with or without // modifications are permitted. -using Neo.IO.Data.LevelDB; +using Neo.IO.Storage.LevelDB; using Neo.Persistence; using System.Collections.Generic; -using LSnapshot = Neo.IO.Data.LevelDB.Snapshot; +using LSnapshot = Neo.IO.Storage.LevelDB.Snapshot; namespace Neo.Plugins.Storage { @@ -44,11 +44,12 @@ public void Delete(byte[] key) public void Dispose() { _snapshot.Dispose(); + _options.Dispose(); } public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte[] prefix, SeekDirection direction = SeekDirection.Forward) { - return _db.Seek(_options, prefix, direction, (k, v) => (k, v)); + return _db.Seek(_options, prefix, direction); } public void Put(byte[] key, byte[] value) diff --git a/src/Plugins/LevelDBStore/Plugins/Storage/Store.cs b/src/Plugins/LevelDBStore/Plugins/Storage/Store.cs index 337a598a61..c77a6beeb1 100644 --- a/src/Plugins/LevelDBStore/Plugins/Storage/Store.cs +++ b/src/Plugins/LevelDBStore/Plugins/Storage/Store.cs @@ -9,7 +9,7 @@ // Redistribution and use in source and binary forms with or without // modifications are permitted. -using Neo.IO.Data.LevelDB; +using Neo.IO.Storage.LevelDB; using Neo.Persistence; using System.Collections.Generic; @@ -18,10 +18,12 @@ namespace Neo.Plugins.Storage internal class Store : IStore { private readonly DB _db; + private readonly Options _options; public Store(string path) { - _db = DB.Open(path, new Options { CreateIfMissing = true, FilterPolicy = Native.leveldb_filterpolicy_create_bloom(15) }); + _options = new Options { CreateIfMissing = true, FilterPolicy = Native.leveldb_filterpolicy_create_bloom(15) }; + _db = DB.Open(path, _options); } public void Delete(byte[] key) @@ -29,7 +31,11 @@ public void Delete(byte[] key) _db.Delete(WriteOptions.Default, key); } - public void Dispose() => _db.Dispose(); + public void Dispose() + { + _db.Dispose(); + _options.Dispose(); + } public ISnapshot GetSnapshot() => new Snapshot(_db); @@ -53,6 +59,6 @@ public bool TryGet(byte[] key, out byte[] value) } public IEnumerable<(byte[], byte[])> Seek(byte[] prefix, SeekDirection direction = SeekDirection.Forward) => - _db.Seek(ReadOptions.Default, prefix, direction, (k, v) => (k, v)); + _db.Seek(ReadOptions.Default, prefix, direction); } }