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

Fix storage name #876

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
52 changes: 42 additions & 10 deletions src/LevelDBStore/Plugins/Storage/LevelDBStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,57 @@

using Neo.IO.Data.LevelDB;
using Neo.Persistence;
using System;
using System.Linq;
using System.Collections.Generic;

namespace Neo.Plugins.Storage
{
public class LevelDBStore : Plugin, IStoreProvider
internal class LevelDBStore : IStore
{
public override string Description => "Uses LevelDB to store the blockchain data";
private readonly DB db;

public LevelDBStore()
public LevelDBStore(string path)
{
StoreFactory.RegisterProvider(this);
this.db = DB.Open(path, new Options { CreateIfMissing = true, FilterPolicy = Native.leveldb_filterpolicy_create_bloom(15) });
}

public IStore GetStore(string path)
public void Delete(byte[] key)
{
if (Environment.CommandLine.Split(' ').Any(p => p == "/repair" || p == "--repair"))
DB.Repair(path, Options.Default);
return new Store(path);
db.Delete(WriteOptions.Default, key);
}

public void Dispose()
{
db.Dispose();
}

public IEnumerable<(byte[], byte[])> Seek(byte[] prefix, SeekDirection direction = SeekDirection.Forward)
{
return db.Seek(ReadOptions.Default, prefix, direction, (k, v) => (k, v));
}

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

public void Put(byte[] key, byte[] value)
{
db.Put(WriteOptions.Default, key, value);
}

public void PutSync(byte[] key, byte[] value)
{
db.Put(WriteOptions.SyncWrite, key, value);
}

public bool Contains(byte[] key)
{
return db.Contains(ReadOptions.Default, key);
}

public byte[] TryGet(byte[] key)
{
return db.Get(ReadOptions.Default, key);
}
}
}
35 changes: 35 additions & 0 deletions src/LevelDBStore/Plugins/Storage/LevelDBStorePlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// LevelDBStorePlugin.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 Neo.IO.Data.LevelDB;
using Neo.Persistence;
using System;
using System.Linq;

namespace Neo.Plugins.Storage
{
public class LevelDBStorePlugin : Plugin, IStoreProvider
{
public override string Description => "Uses LevelDB to store the blockchain data";

public LevelDBStorePlugin()
{
StoreFactory.RegisterProvider(this);
}

public IStore GetStore(string path)
{
if (Environment.CommandLine.Split(' ').Any(p => p == "/repair" || p == "--repair"))
DB.Repair(path, Options.Default);
return new LevelDBStore(path);
}
}
}
67 changes: 0 additions & 67 deletions src/LevelDBStore/Plugins/Storage/Store.cs

This file was deleted.

63 changes: 53 additions & 10 deletions src/RocksDBStore/Plugins/Storage/RocksDBStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,68 @@
// modifications are permitted.

using Neo.Persistence;
using RocksDbSharp;
using System;
using System.Collections.Generic;
using System.IO;

namespace Neo.Plugins.Storage
{
public class RocksDBStore : Plugin, IStoreProvider
internal class RocksDBStore : IStore
{
public override string Description => "Uses RocksDBStore to store the blockchain data";
private readonly RocksDb db;

public RocksDBStore()
public RocksDBStore(string path)
{
StoreFactory.RegisterProvider(this);
db = RocksDb.Open(Options.Default, Path.GetFullPath(path));
}

/// <summary>
/// Get store
/// </summary>
/// <returns>RocksDbStore</returns>
public IStore GetStore(string path)
public void Dispose()
{
return new Store(path);
db.Dispose();
}

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

public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte[] keyOrPrefix, SeekDirection direction = SeekDirection.Forward)
{
if (keyOrPrefix == null) keyOrPrefix = Array.Empty<byte>();

using var it = db.NewIterator();
if (direction == SeekDirection.Forward)
for (it.Seek(keyOrPrefix); it.Valid(); it.Next())
yield return (it.Key(), it.Value());
else
for (it.SeekForPrev(keyOrPrefix); it.Valid(); it.Prev())
yield return (it.Key(), it.Value());
}

public bool Contains(byte[] key)
{
return db.Get(key, Array.Empty<byte>(), 0, 0) >= 0;
}

public byte[] TryGet(byte[] key)
{
return db.Get(key);
}

public void Delete(byte[] key)
{
db.Remove(key);
}

public void Put(byte[] key, byte[] value)
{
db.Put(key, value);
}

public void PutSync(byte[] key, byte[] value)
{
db.Put(key, value, writeOptions: Options.WriteDefaultSync);
}
}
}
34 changes: 34 additions & 0 deletions src/RocksDBStore/Plugins/Storage/RocksDBStorePlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// RocksDBStorePlugin.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 Neo.Persistence;

namespace Neo.Plugins.Storage
{
public class RocksDBStorePlugin : Plugin, IStoreProvider
{
public override string Description => "Uses RocksDBStore to store the blockchain data";

public RocksDBStorePlugin()
{
StoreFactory.RegisterProvider(this);
}

/// <summary>
/// Get store
/// </summary>
/// <returns>RocksDbStore</returns>
public IStore GetStore(string path)
{
return new RocksDBStore(path);
}
}
}
77 changes: 0 additions & 77 deletions src/RocksDBStore/Plugins/Storage/Store.cs

This file was deleted.

4 changes: 2 additions & 2 deletions tests/Neo.Plugins.Storage.Tests/StoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void TestMemory()
[TestMethod]
public void TestLevelDb()
{
using var plugin = new LevelDBStore();
using var plugin = new LevelDBStorePlugin();
TestPersistenceDelete(plugin.GetStore(path_leveldb));
// Test all with the same store

Expand All @@ -66,7 +66,7 @@ public void TestLevelDb()
[TestMethod]
public void TestRocksDb()
{
using var plugin = new RocksDBStore();
using var plugin = new RocksDBStorePlugin();
TestPersistenceDelete(plugin.GetStore(path_rocksdb));
// Test all with the same store

Expand Down
Loading