Skip to content
Permalink
Browse files

Open all ColumnFamilies when opening DB

[changelog skip]
  • Loading branch information
earlbread committed Mar 9, 2020
1 parent c4f36de commit e3750fdfb7fb990eb73e45f3f266235633eca58f
Showing with 61 additions and 5 deletions.
  1. +28 −0 Libplanet.RocksDBStore.Tests/RocksDBStoreTest.cs
  2. +33 −5 Libplanet.RocksDBStore/RocksDBStore.cs
@@ -1,4 +1,8 @@
using System;
using System.IO;
using Libplanet.Blockchain;
using Libplanet.Tests.Blockchain;
using Libplanet.Tests.Common.Action;
using Libplanet.Tests.Store;
using Xunit;

@@ -25,5 +29,29 @@ public void Dispose()
{
_fx?.Dispose();
}

[SkippableFact]
public void ReopenStoreAfterDispose()
{
var path = Path.Combine(Path.GetTempPath(), $"rocksdb_test_{Guid.NewGuid()}");

try
{
var store = new RocksDBStore(path);
var blocks = new BlockChain<DumbAction>(
new NullPolicy<DumbAction>(),
store,
Fx.GenesisBlock
);
store.Dispose();

store = new RocksDBStore(path);
store.Dispose();
}
finally
{
Directory.Delete(path, true);
}
}
}
}
@@ -106,10 +106,16 @@ public class RocksDBStore : BaseStore
_txDb = RocksDb.Open(_options, Path.Combine(path, TxDbName));
_stateDb = RocksDb.Open(_options, Path.Combine(path, StateDbName));
_stagedTxDb = RocksDb.Open(_options, Path.Combine(path, StagedTxDbName));
_chainDb = RocksDb.Open(
_options, Path.Combine(path, ChainDbName), new ColumnFamilies());
_stateRefDb = RocksDb.Open(
_options, Path.Combine(path, StateRefDbName), new ColumnFamilies());

// When opening a DB in a read-write mode, you need to specify all Column Families that
// currently exist in a DB. https://github.com/facebook/rocksdb/wiki/Column-Families
var chainDbPath = Path.Combine(path, ChainDbName);
var chainDbColumnFamilies = GetColumnFamilies(_options, chainDbPath);
_chainDb = RocksDb.Open(_options, chainDbPath, chainDbColumnFamilies);

var stateRefDbPath = Path.Combine(path, StateRefDbName);
var stateRefDbColumnFamilies = GetColumnFamilies(_options, stateRefDbPath);
_stateRefDb = RocksDb.Open(_options, stateRefDbPath, stateRefDbColumnFamilies);
}

/// <inheritdoc/>
@@ -971,12 +977,34 @@ private ColumnFamilyHandle GetColumnFamily(RocksDb db, Guid? chainId = null)
}
catch (KeyNotFoundException)
{
cf = db.CreateColumnFamily(new ColumnFamilyOptions(), cfName);
cf = db.CreateColumnFamily(_options, cfName);
}

return cf;
}

private ColumnFamilies GetColumnFamilies(DbOptions options, string path)
{
var columnFamilies = new ColumnFamilies();
List<string> listColumnFamilies;

try
{
listColumnFamilies = RocksDb.ListColumnFamilies(options, path).ToList();
}
catch (RocksDbException)
{
listColumnFamilies = new List<string>();
}

foreach (string name in listColumnFamilies)
{
columnFamilies.Add(name, _options);
}

return columnFamilies;
}

private class StateRef
{
public string StateKey { get; set; }

0 comments on commit e3750fd

Please sign in to comment.
You can’t perform that action at this time.