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 ReadCache option to LevelDB and RocksDB #255

Closed
Closed
Show file tree
Hide file tree
Changes from 5 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
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}",
"ReadCache": true
}
}
8 changes: 6 additions & 2 deletions src/LevelDBStore/Plugins/Storage/LevelDBStore.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
using Microsoft.Extensions.Configuration;
using Neo.Persistence;

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

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"));
readCache = config.GetSection("ReadCache")?.Get<bool>() ?? false;
}

public IStore GetStore()
{
return new Store(path);
return new Store(path, readCache);
}
}
}
4 changes: 2 additions & 2 deletions src/LevelDBStore/Plugins/Storage/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ internal class Snapshot : ISnapshot
private readonly ReadOptions options;
private readonly WriteBatch batch;

public Snapshot(DB db)
public Snapshot(DB db, bool readCache)
{
this.db = db;
this.snapshot = db.GetSnapshot();
this.options = new ReadOptions { FillCache = false, Snapshot = snapshot };
this.options = new ReadOptions { FillCache = readCache, Snapshot = snapshot };
this.batch = new WriteBatch();
}

Expand Down
6 changes: 4 additions & 2 deletions src/LevelDBStore/Plugins/Storage/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ internal class Store : IStore
{
private const byte SYS_Version = 0xf0;
private readonly DB db;
private bool readCache;

public Store(string path)
public Store(string path, bool readCache)
{
this.db = DB.Open(path, new Options { CreateIfMissing = true });
byte[] value = db.Get(ReadOptions.Default, Helper.CreateKey(SYS_Version));
if (value != null && Version.TryParse(Encoding.ASCII.GetString(value), out Version version) && version >= Version.Parse("3.0.0"))
return;

WriteBatch batch = new WriteBatch();
this.readCache = readCache;

if (value != null)
{
Expand Down Expand Up @@ -56,7 +58,7 @@ public void Dispose()

public ISnapshot GetSnapshot()
{
return new Snapshot(db);
return new Snapshot(db, readCache);
}

public void Put(byte table, byte[] key, byte[] value)
Expand Down
8 changes: 7 additions & 1 deletion src/RocksDBStore/Plugins/Storage/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Neo.Plugins.Storage
public static class Options
{
public static readonly DbOptions Default = CreateDbOptions();
public static readonly ReadOptions ReadDefault = new ReadOptions();
public static ReadOptions ReadDefault { get; private set; }
public static readonly WriteOptions WriteDefault = new WriteOptions();
public static readonly WriteOptions WriteDefaultSync = new WriteOptions().SetSync(true);

Expand All @@ -21,5 +21,11 @@ public static DbOptions CreateDbOptions()
options.SetBlockBasedTableFactory(new BlockBasedTableOptions().SetBlockSize(4096));
return options;
}

public static void SetDefaultReadOptions(bool readCache)
{
ReadDefault = new ReadOptions();
ReadDefault.SetFillCache(readCache);
}
}
}
2 changes: 1 addition & 1 deletion src/RocksDBStore/Plugins/Storage/RocksDBStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ protected override void Configure()
/// Get store
/// </summary>
/// <returns>RocksDbStore</returns>
public IStore GetStore() => new Store(Settings.Default.Path);
public IStore GetStore() => new Store(Settings.Default.Path, Settings.Default.ReadCache);
}
}
3 changes: 3 additions & 0 deletions src/RocksDBStore/Plugins/Storage/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ internal class Settings
{
public string Path { get; }

public bool ReadCache { get; }

public static Settings Default { get; private set; }

private Settings(IConfigurationSection section)
{
this.Path = string.Format(section.GetSection("Path").Value ?? "Data_RocksDB_{0}", ProtocolSettings.Default.Magic.ToString("X8"));
this.ReadCache = section.GetSection("ReadCache")?.Get<bool>() ?? false;
}

public static void Load(IConfigurationSection section)
Expand Down
3 changes: 2 additions & 1 deletion src/RocksDBStore/Plugins/Storage/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ internal class Store : IStore
private readonly RocksDb db;
private readonly Dictionary<byte, ColumnFamilyHandle> _families = new Dictionary<byte, ColumnFamilyHandle>();

public Store(string path)
public Store(string path, bool readCache)
{
var families = new ColumnFamilies();
for (int x = 0; x <= byte.MaxValue; x++)
families.Add(new ColumnFamilies.Descriptor(x.ToString(), new ColumnFamilyOptions()));
db = RocksDb.Open(Options.Default, Path.GetFullPath(path), families);
Options.SetDefaultReadOptions(readCache);

ColumnFamilyHandle defaultFamily = db.GetDefaultColumnFamily();
byte[] value = db.Get(SYS_Version, defaultFamily, Options.ReadDefault);
Expand Down
3 changes: 2 additions & 1 deletion src/RocksDBStore/RocksDBStore/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"PluginConfiguration": {
"Path": "Data_RocksDB_{0}"
"Path": "Data_RocksDB_{0}",
"ReadCache": true
}
}