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

Add BloomFilter to levelDB #253

Merged
merged 12 commits into from
Jul 16, 2020
3 changes: 3 additions & 0 deletions src/LevelDBStore/IO/Data/LevelDB/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ public static class Native

[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void leveldb_options_set_filter_policy(IntPtr /*Options*/ options, IntPtr /*FilterPolicy*/ policy);

[DllImport("libleveldb", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr leveldb_filterpolicy_create_bloom(int bits_per_key);
#endregion

#region ReadOptions
Expand Down
5 changes: 3 additions & 2 deletions src/LevelDBStore/LevelDBStore/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
{
"PluginConfiguration": {
"Path": "Data_LevelDB_{0}"
"Path": "Data_LevelDB_{0}",
"BloomFilterBitsPerKey": 20
Qiao-Jin marked this conversation as resolved.
Show resolved Hide resolved
}
}
8 changes: 6 additions & 2 deletions src/LevelDBStore/Plugins/Storage/LevelDBStore.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
using Microsoft.Extensions.Configuration;
using Neo.Persistence;

namespace Neo.Plugins.Storage
{
public class LevelDBStore : Plugin, IStoragePlugin
{
private string path;
private int bloomFilterBitsPerKey;

public override string Description => "Uses LevelDB to store the blockchain data";

protected override void Configure()
{
path = string.Format(GetConfiguration().GetSection("Path").Value ?? "Data_LevelDB_{0}", ProtocolSettings.Default.Magic.ToString("X8"));
IConfigurationSection config = GetConfiguration();
path = string.Format(config.GetSection("Path").Value ?? "Data_LevelDB_{0}", ProtocolSettings.Default.Magic.ToString("X8"));
bloomFilterBitsPerKey = config.GetSection("BloomFilterBitsPerKey")?.Get<int>() ?? 0;
}

public IStore GetStore()
{
return new Store(path);
return new Store(path, bloomFilterBitsPerKey);
}
}
}
7 changes: 5 additions & 2 deletions src/LevelDBStore/Plugins/Storage/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ internal class Store : IStore
private const byte SYS_Version = 0xf0;
private readonly DB db;

public Store(string path)
public Store(string path, int bloomFilterBitsPerKey)
{
this.db = DB.Open(path, new Options { CreateIfMissing = true });
if (bloomFilterBitsPerKey > 0)
this.db = DB.Open(path, new Options { CreateIfMissing = true, FilterPolicy = Native.leveldb_filterpolicy_create_bloom(bloomFilterBitsPerKey) });
else
this.db = DB.Open(path, new Options { CreateIfMissing = true });
byte[] value = db.Get(ReadOptions.Default, LHelper.CreateKey(SYS_Version));
if (value != null && Version.TryParse(Encoding.ASCII.GetString(value), out Version version) && version >= Version.Parse("3.0.0"))
return;
Expand Down