use of com.jd.blockchain.storage.service.VersioningKVStorage in project jdchain-core by blockchain-jd-com.
the class RocksDBStorageTest method test.
@Test
public void test() {
String dbUri = initEmptyDB("rocksdb_storage_test");
long expectedVersion;
try (DbConnectionFactory dbConnFactory = new RocksDBConnectionFactory()) {
DbConnection conn = dbConnFactory.connect(dbUri);
VersioningKVStorage verStorage = conn.getStorageService().getVersioningKVStorage();
ExPolicyKVStorage exStorage = conn.getStorageService().getExPolicyKVStorage();
expectedVersion = test(verStorage);
test(exStorage);
}
try (DbConnectionFactory dbConnFactory = new RocksDBConnectionFactory()) {
DbConnection conn = dbConnFactory.connect(dbUri);
VersioningKVStorage verStorage = conn.getStorageService().getVersioningKVStorage();
ExPolicyKVStorage exStorage = conn.getStorageService().getExPolicyKVStorage();
testAfterReload(verStorage, expectedVersion);
testAfterReload(exStorage);
}
}
use of com.jd.blockchain.storage.service.VersioningKVStorage in project jdchain-core by blockchain-jd-com.
the class Test method main.
public static void main(String[] args) {
Bytes key = Bytes.fromString("test111");
try (Jedis jedis = new Jedis("192.168.151.33", 6379)) {
jedis.connect();
// BytesUtils.toBytes(key, "UTF-8");
byte[] kbytes = key.toBytes();
byte[] valueBytes = jedis.get(kbytes);
String value = BytesUtils.toString(valueBytes, "UTF-8");
System.out.println(String.format("%s=%s", key, value));
}
System.out.println("=================================");
System.out.println("================= test expolicy storage =================");
RedisConnectionFactory connFactory = new RedisConnectionFactory();
try (DbConnection conn = connFactory.connect("redis://192.168.151.33:6379")) {
ExPolicyKVStorage exKVStorage = conn.getStorageService().getExPolicyKVStorage();
byte[] valueBytes = exKVStorage.get(key);
String value = BytesUtils.toString(valueBytes, "UTF-8");
System.out.println(String.format("%s=%s", key, value));
System.out.println(String.format("%s=%s", key, value));
boolean success = exKVStorage.set(key, BytesUtils.toBytes("New Value by ExPolicyStorage interface..."), ExPolicy.NOT_EXISTING);
System.out.println("update key when NX " + (success ? "success" : "fail"));
success = exKVStorage.set(key, BytesUtils.toBytes("New Value by ExPolicyStorage interface..."), ExPolicy.EXISTING);
System.out.println("update key when XX " + (success ? "success" : "fail"));
valueBytes = exKVStorage.get(key);
value = BytesUtils.toString(valueBytes, "UTF-8");
System.out.println(String.format("Retrieve... %s=%s", key, value));
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("=================================");
try (DbConnection conn = connFactory.connect("redis://192.168.151.33:6379/0")) {
ExPolicyKVStorage exKVStorage = conn.getStorageService().getExPolicyKVStorage();
byte[] valueBytes = exKVStorage.get(key);
String value = BytesUtils.toString(valueBytes, "UTF-8");
System.out.println(String.format("%s=%s", key, value));
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("=================================");
System.out.println("================= test versioning storage =================");
Bytes addr = Bytes.fromString("User001");
try (DbConnection conn = connFactory.connect("redis://192.168.151.33:6379/0")) {
VersioningKVStorage verStorage = conn.getStorageService().getVersioningKVStorage();
long version = verStorage.getVersion(addr);
System.out.println(String.format("Version of key[%s]=%s", addr, version));
byte[] v1 = BytesUtils.toBytes("value-" + version);
version = verStorage.set(addr, v1, version);
System.out.println(String.format("Update[%s] to V1, version=%s", addr, version));
byte[] v2 = BytesUtils.toBytes("value-" + version);
version = verStorage.set(addr, v2, version);
System.out.println(String.format("Update[%s] to V2, version=%s", addr, version));
byte[] v3 = BytesUtils.toBytes("value-" + version);
version = verStorage.set(addr, v3, version);
System.out.println(String.format("Update[%s] to V3, version=%s", addr, version));
version = verStorage.getVersion(addr);
System.out.println(String.format("Now the latest version of key[%s]=%s", addr, version));
for (int i = 0; i <= version; i++) {
String value = BytesUtils.toString(verStorage.get(addr, i));
System.out.println(String.format("The version[%s] value of key[%s] is :%s", i, addr, value));
}
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("=================================");
}
use of com.jd.blockchain.storage.service.VersioningKVStorage in project jdchain-core by blockchain-jd-com.
the class LedgerManager method register.
@Override
public LedgerRepository register(HashDigest ledgerHash, KVStorageService storageService, LedgerDataStructure dataStructure) {
if (ledgers.containsKey(ledgerHash)) {
LedgerRepositoryContext ledgerCtx = ledgers.get(ledgerHash);
return ledgerCtx.ledgerRepo;
}
// 加载账本数据库;
VersioningKVStorage ledgerVersioningStorage = storageService.getVersioningKVStorage();
ExPolicyKVStorage ledgerExPolicyStorage = storageService.getExPolicyKVStorage();
// 数据锚定merkle tree的方式
LedgerRepository ledgerRepo = new LedgerRepositoryImpl(ledgerHash, LEDGER_PREFIX, ledgerExPolicyStorage, ledgerVersioningStorage, dataStructure);
// 校验 crypto service provider ;
CryptoSetting cryptoSetting = ledgerRepo.getAdminInfo().getSettings().getCryptoSetting();
checkCryptoSetting(cryptoSetting, ledgerHash);
// 创建账本上下文;
LedgerRepositoryContext ledgerCtx = new LedgerRepositoryContext(ledgerRepo, storageService);
ledgers.put(ledgerHash, ledgerCtx);
return ledgerRepo;
}
Aggregations