Search in sources :

Example 1 with ExPolicyKVStorage

use of com.jd.blockchain.storage.service.ExPolicyKVStorage 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);
    }
}
Also used : ExPolicyKVStorage(com.jd.blockchain.storage.service.ExPolicyKVStorage) VersioningKVStorage(com.jd.blockchain.storage.service.VersioningKVStorage) DbConnectionFactory(com.jd.blockchain.storage.service.DbConnectionFactory) RocksDBConnectionFactory(com.jd.blockchain.storage.service.impl.rocksdb.RocksDBConnectionFactory) DbConnection(com.jd.blockchain.storage.service.DbConnection) Test(org.junit.Test)

Example 2 with ExPolicyKVStorage

use of com.jd.blockchain.storage.service.ExPolicyKVStorage 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("=================================");
}
Also used : Bytes(utils.Bytes) Jedis(redis.clients.jedis.Jedis) ExPolicyKVStorage(com.jd.blockchain.storage.service.ExPolicyKVStorage) VersioningKVStorage(com.jd.blockchain.storage.service.VersioningKVStorage) RedisConnectionFactory(com.jd.blockchain.storage.service.impl.redis.RedisConnectionFactory) DbConnection(com.jd.blockchain.storage.service.DbConnection)

Example 3 with ExPolicyKVStorage

use of com.jd.blockchain.storage.service.ExPolicyKVStorage 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;
}
Also used : ExPolicyKVStorage(com.jd.blockchain.storage.service.ExPolicyKVStorage) VersioningKVStorage(com.jd.blockchain.storage.service.VersioningKVStorage) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting)

Aggregations

ExPolicyKVStorage (com.jd.blockchain.storage.service.ExPolicyKVStorage)3 VersioningKVStorage (com.jd.blockchain.storage.service.VersioningKVStorage)3 DbConnection (com.jd.blockchain.storage.service.DbConnection)2 CryptoSetting (com.jd.blockchain.ledger.CryptoSetting)1 DbConnectionFactory (com.jd.blockchain.storage.service.DbConnectionFactory)1 RedisConnectionFactory (com.jd.blockchain.storage.service.impl.redis.RedisConnectionFactory)1 RocksDBConnectionFactory (com.jd.blockchain.storage.service.impl.rocksdb.RocksDBConnectionFactory)1 Test (org.junit.Test)1 Jedis (redis.clients.jedis.Jedis)1 Bytes (utils.Bytes)1