Search in sources :

Example 1 with BatchOperation

use of io.nuls.db.service.BatchOperation in project nuls by nuls-io.

the class ContractUtxoStorageServiceImpl method batchSaveAndDeleteUTXO.

@Override
public Result<List<Entry<byte[], byte[]>>> batchSaveAndDeleteUTXO(List<Entry<byte[], byte[]>> utxosToSave, List<byte[]> utxosToDelete) {
    BatchOperation batch = dbService.createWriteBatch(ContractStorageConstant.DB_NAME_CONTRACT_LEDGER_UTXO);
    List<Entry<byte[], byte[]>> deleteUtxoEntryList = new ArrayList<>();
    byte[] deleteUtxo;
    if (utxosToDelete != null) {
        for (byte[] key : utxosToDelete) {
            batch.delete(key);
        }
    }
    if (utxosToSave != null) {
        for (Entry<byte[], byte[]> entry : utxosToSave) {
            batch.put(entry.getKey(), entry.getValue());
        }
    }
    Result batchResult = batch.executeBatch();
    if (batchResult.isFailed()) {
        return batchResult;
    }
    return Result.getSuccess().setData(deleteUtxoEntryList);
}
Also used : Entry(io.nuls.db.model.Entry) ArrayList(java.util.ArrayList) BatchOperation(io.nuls.db.service.BatchOperation) Result(io.nuls.kernel.model.Result)

Example 2 with BatchOperation

use of io.nuls.db.service.BatchOperation in project nuls by nuls-io.

the class UtxoLedgerServiceImpl method rollbackCoinData.

private Result rollbackCoinData(Transaction tx) throws IOException, NulsException {
    byte[] txHashBytes = tx.getHash().serialize();
    BatchOperation batch = utxoLedgerUtxoStorageService.createWriteBatch();
    CoinData coinData = tx.getCoinData();
    if (coinData != null) {
        // 保存utxo已花费 - from
        List<Coin> froms = coinData.getFrom();
        Coin recovery;
        for (Coin from : froms) {
            try {
                NulsByteBuffer byteBuffer = new NulsByteBuffer(from.getOwner());
                NulsDigestData fromTxHash = byteBuffer.readHash();
                int fromIndex = (int) byteBuffer.readVarInt();
                Transaction fromTx = utxoLedgerTransactionStorageService.getTx(fromTxHash);
                recovery = fromTx.getCoinData().getTo().get(fromIndex);
                recovery.setFrom(from.getFrom());
                batch.put(from.getOwner(), recovery.serialize());
            } catch (IOException e) {
                Log.error(e);
                return Result.getFailed(KernelErrorCode.IO_ERROR);
            }
        }
        // 删除utxo - to
        List<Coin> tos = coinData.getTo();
        for (int i = 0, length = tos.size(); i < length; i++) {
            byte[] owner = Arrays.concatenate(txHashBytes, new VarInt(i).encode());
            // Log.info("批量删除:" + Hex.encode(owner));
            batch.delete(owner);
        }
        // 执行批量
        Result batchResult = batch.executeBatch();
        if (batchResult.isFailed()) {
            return batchResult;
        }
    }
    return Result.getSuccess();
}
Also used : VarInt(io.nuls.kernel.utils.VarInt) BatchOperation(io.nuls.db.service.BatchOperation) IOException(java.io.IOException) NulsByteBuffer(io.nuls.kernel.utils.NulsByteBuffer) ValidateResult(io.nuls.kernel.validate.ValidateResult)

Example 3 with BatchOperation

use of io.nuls.db.service.BatchOperation in project nuls by nuls-io.

the class UtxoLedgerServiceImpl method saveCoinData.

private Result saveCoinData(Transaction tx) throws IOException {
    CoinData coinData = tx.getCoinData();
    // TestLog-
    if (coinData != null) {
        BatchOperation batch = utxoLedgerUtxoStorageService.createWriteBatch();
        // 删除utxo已花费 - from
        List<Coin> froms = coinData.getFrom();
        for (Coin from : froms) {
            // TestLog+
            // Coin preFrom = utxoLedgerUtxoStorageService.getUtxo(from.());
            // if (preFrom != null) {
            // Log.info("花费:height: +" + tx.getBlockHeight() + ", “+txHash-" + tx.getHash() + ", " + Hex.encode(from.()));
            // }
            // Log.info("delete utxo:" + Hex.encode(from.()));
            // TestLog-
            batch.delete(from.getOwner());
        }
        // 保存utxo - to
        byte[] txHashBytes = tx.getHash().serialize();
        List<Coin> tos = coinData.getTo();
        for (int i = 0, length = tos.size(); i < length; i++) {
            try {
                byte[] owner = Arrays.concatenate(txHashBytes, new VarInt(i).encode());
                // Log.info("129 save utxo:::" + Hex.encode(owner));
                batch.put(owner, tos.get(i).serialize());
            } catch (IOException e) {
                Log.error(e);
                return Result.getFailed(KernelErrorCode.IO_ERROR);
            }
        }
        // 执行批量
        Result batchResult = batch.executeBatch();
        if (batchResult.isFailed()) {
            return batchResult;
        }
    }
    return Result.getSuccess();
}
Also used : VarInt(io.nuls.kernel.utils.VarInt) BatchOperation(io.nuls.db.service.BatchOperation) IOException(java.io.IOException) ValidateResult(io.nuls.kernel.validate.ValidateResult)

Example 4 with BatchOperation

use of io.nuls.db.service.BatchOperation in project nuls by nuls-io.

the class LocalUtxoStorageServiceImpl method batchSaveUTXO.

@Override
public Result<Integer> batchSaveUTXO(Map<byte[], byte[]> utxos) {
    BatchOperation batch = dbService.createWriteBatch(AccountLedgerStorageConstant.DB_NAME_ACCOUNT_LEDGER_COINDATA);
    Set<Map.Entry<byte[], byte[]>> utxosToSaveEntries = utxos.entrySet();
    for (Map.Entry<byte[], byte[]> entry : utxosToSaveEntries) {
        batch.put(entry.getKey(), entry.getValue());
    }
    Result batchResult = batch.executeBatch();
    if (batchResult.isFailed()) {
        return batchResult;
    }
    Result result = Result.getSuccess().setData(utxos.size());
    if (result.isSuccess() && cacheMap != null) {
        for (Map.Entry<byte[], byte[]> entry : utxosToSaveEntries) {
            cacheMap.put(new String(entry.getKey()), new Entry(entry.getKey(), entry.getValue()));
        }
    }
    return result;
}
Also used : Entry(io.nuls.db.model.Entry) BatchOperation(io.nuls.db.service.BatchOperation) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Result(io.nuls.kernel.model.Result)

Example 5 with BatchOperation

use of io.nuls.db.service.BatchOperation in project nuls by nuls-io.

the class LocalUtxoStorageServiceImpl method batchDeleteUTXO.

@Override
public Result batchDeleteUTXO(Set<byte[]> utxos) {
    BatchOperation batch = dbService.createWriteBatch(AccountLedgerStorageConstant.DB_NAME_ACCOUNT_LEDGER_COINDATA);
    for (byte[] key : utxos) {
        batch.delete(key);
    }
    Result batchResult = batch.executeBatch();
    if (batchResult.isFailed()) {
        return batchResult;
    }
    Result result = Result.getSuccess().setData(new Integer(utxos.size()));
    if (result.isSuccess() && cacheMap != null) {
        for (byte[] key : utxos) {
            cacheMap.remove(new String(key));
        }
    }
    return result;
}
Also used : BatchOperation(io.nuls.db.service.BatchOperation) Result(io.nuls.kernel.model.Result)

Aggregations

BatchOperation (io.nuls.db.service.BatchOperation)7 Result (io.nuls.kernel.model.Result)4 Entry (io.nuls.db.model.Entry)2 VarInt (io.nuls.kernel.utils.VarInt)2 ValidateResult (io.nuls.kernel.validate.ValidateResult)2 IOException (java.io.IOException)2 NulsByteBuffer (io.nuls.kernel.utils.NulsByteBuffer)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1