Search in sources :

Example 1 with TypeMapper

use of com.jd.blockchain.ledger.core.DatasetHelper.TypeMapper in project jdchain-core by blockchain-jd-com.

the class MerkleComplecatedAccount method initializeDatasets.

private void initializeDatasets(HashDigest headerRoot, HashDigest dataRoot, CryptoSetting cryptoSetting, Bytes keyPrefix, ExPolicyKVStorage exStorage, VersioningKVStorage verStorage, boolean readonly) {
    // // 加载“根数据集”
    // this.rootDataset = new MerkleHashDataset(rootHash, cryptoSetting, keyPrefix, exStorage, verStorage, readonly);
    // 初始化数据修改监听器;
    DataChangedListener<String, TypedValue> dataChangedListener = new DataChangedListener<String, TypedValue>() {

        @Override
        public void onChanged(String key, TypedValue value, long expectedVersion, long newVersion) {
            onUpdated(key, value, expectedVersion, newVersion);
        }
    };
    TypeMapper<byte[], TypedValue> valueMapper = new TypeMapper<byte[], TypedValue>() {

        @Override
        public byte[] encode(TypedValue t2) {
            return BinaryProtocol.encode(t2, BytesValue.class);
        }

        @Override
        public TypedValue decode(byte[] t1) {
            BytesValue v = BinaryProtocol.decode(t1, BytesValue.class);
            return TypedValue.wrap(v);
        }
    };
    // 加载“头数据集”;
    // HashDigest headerRoot = loadHeaderRoot();
    Bytes headerPrefix = keyPrefix.concat(HEADER_PREFIX);
    this.headerDataset = new MerkleHashDataset(headerRoot, cryptoSetting, headerPrefix, exStorage, verStorage, readonly);
    this.typedHeader = DatasetHelper.listen(DatasetHelper.map(headerDataset, valueMapper), dataChangedListener);
    // 加载“主数据集”
    // HashDigest dataRoot = loadDataRoot();
    Bytes dataPrefix = keyPrefix.concat(DATA_PREFIX);
    this.dataDataset = new MerkleHashDataset(dataRoot, cryptoSetting, dataPrefix, exStorage, verStorage, readonly);
    this.typedData = DatasetHelper.listen(DatasetHelper.map(dataDataset, valueMapper), dataChangedListener);
}
Also used : TypeMapper(com.jd.blockchain.ledger.core.DatasetHelper.TypeMapper) Bytes(utils.Bytes) DataChangedListener(com.jd.blockchain.ledger.core.DatasetHelper.DataChangedListener) BytesValue(com.jd.blockchain.ledger.BytesValue) TypedValue(com.jd.blockchain.ledger.TypedValue)

Example 2 with TypeMapper

use of com.jd.blockchain.ledger.core.DatasetHelper.TypeMapper in project jdchain-core by blockchain-jd-com.

the class KvComplecatedAccount method initializeDatasets.

private void initializeDatasets(HashDigest headerRoot, HashDigest dataRoot, CryptoSetting cryptoSetting, Bytes keyPrefix, ExPolicyKVStorage exStorage, VersioningKVStorage verStorage, boolean readonly) {
    // 初始化数据修改监听器;
    DataChangedListener<String, TypedValue> dataChangedListenerHeader = new DataChangedListener<String, TypedValue>() {

        @Override
        public void onChanged(String key, TypedValue value, long expectedVersion, long newVersion) {
            onUpdated(key, value, "header", expectedVersion, newVersion);
        }
    };
    DataChangedListener<String, TypedValue> dataChangedListenerData = new DataChangedListener<String, TypedValue>() {

        @Override
        public void onChanged(String key, TypedValue value, long expectedVersion, long newVersion) {
            onUpdated(key, value, "data", expectedVersion, newVersion);
        }
    };
    TypeMapper<byte[], TypedValue> valueMapper = new TypeMapper<byte[], TypedValue>() {

        @Override
        public byte[] encode(TypedValue t2) {
            return BinaryProtocol.encode(t2, BytesValue.class);
        }

        @Override
        public TypedValue decode(byte[] t1) {
            BytesValue v = BinaryProtocol.decode(t1, BytesValue.class);
            return TypedValue.wrap(v);
        }
    };
    // 加载“头数据集”;
    // HashDigest headerRoot = loadHeaderRoot();
    Bytes headerPrefix = keyPrefix.concat(HEADER_PREFIX);
    this.headerDataset = new KvDataset(preblockHeight, headerRoot, DatasetType.HDKVS, cryptoSetting, headerPrefix, exStorage, verStorage, readonly);
    this.typedHeader = DatasetHelper.listen(DatasetHelper.map(headerDataset, valueMapper), dataChangedListenerHeader);
    // 加载“主数据集”
    // HashDigest dataRoot = loadDataRoot();
    Bytes dataPrefix = keyPrefix.concat(DATA_PREFIX);
    this.dataDataset = new KvDataset(preblockHeight, dataRoot, DatasetType.DTKVS, cryptoSetting, dataPrefix, exStorage, verStorage, readonly);
    this.typedData = DatasetHelper.listen(DatasetHelper.map(dataDataset, valueMapper), dataChangedListenerData);
}
Also used : TypeMapper(com.jd.blockchain.ledger.core.DatasetHelper.TypeMapper) Bytes(utils.Bytes) DataChangedListener(com.jd.blockchain.ledger.core.DatasetHelper.DataChangedListener) BytesValue(com.jd.blockchain.ledger.BytesValue) TypedValue(com.jd.blockchain.ledger.TypedValue)

Aggregations

BytesValue (com.jd.blockchain.ledger.BytesValue)2 TypedValue (com.jd.blockchain.ledger.TypedValue)2 DataChangedListener (com.jd.blockchain.ledger.core.DatasetHelper.DataChangedListener)2 TypeMapper (com.jd.blockchain.ledger.core.DatasetHelper.TypeMapper)2 Bytes (utils.Bytes)2