|
| 1 | +/* |
| 2 | + * Copyright 2014-2020 [fisco-dev] |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + * |
| 14 | + */ |
| 15 | +package org.fisco.bcos.sdk.contract.precompiled.crud; |
| 16 | + |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import org.fisco.bcos.sdk.client.Client; |
| 21 | +import org.fisco.bcos.sdk.codec.datatypes.generated.tuples.generated.Tuple2; |
| 22 | +import org.fisco.bcos.sdk.contract.precompiled.callback.PrecompiledCallback; |
| 23 | +import org.fisco.bcos.sdk.contract.precompiled.crud.common.Entry; |
| 24 | +import org.fisco.bcos.sdk.contract.precompiled.model.PrecompiledAddress; |
| 25 | +import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair; |
| 26 | +import org.fisco.bcos.sdk.model.PrecompiledConstant; |
| 27 | +import org.fisco.bcos.sdk.model.PrecompiledRetCode; |
| 28 | +import org.fisco.bcos.sdk.model.RetCode; |
| 29 | +import org.fisco.bcos.sdk.model.TransactionReceipt; |
| 30 | +import org.fisco.bcos.sdk.model.callback.TransactionCallback; |
| 31 | +import org.fisco.bcos.sdk.transaction.codec.decode.ReceiptParser; |
| 32 | +import org.fisco.bcos.sdk.transaction.model.exception.ContractException; |
| 33 | +import org.fisco.bcos.sdk.utils.StringUtils; |
| 34 | + |
| 35 | +/** This class not support in FISCO BCOS 3.0.0 rc1 Do not use it. */ |
| 36 | +public class KVTableService { |
| 37 | + private final KVTablePrecompiled kvTablePrecompiled; |
| 38 | + private static final String VALUE_FIELDS_DELIMITER = ","; |
| 39 | + |
| 40 | + public KVTableService(Client client, CryptoKeyPair credential) { |
| 41 | + this.kvTablePrecompiled = |
| 42 | + KVTablePrecompiled.load( |
| 43 | + client.isWASM() |
| 44 | + ? PrecompiledAddress.KV_TABLE_PRECOMPILED_NAME |
| 45 | + : PrecompiledAddress.KV_TABLE_PRECOMPILED_ADDRESS, |
| 46 | + client, |
| 47 | + credential); |
| 48 | + } |
| 49 | + |
| 50 | + public static String convertValueFieldsToString(List<String> valueFields) { |
| 51 | + return StringUtils.join(valueFields, VALUE_FIELDS_DELIMITER); |
| 52 | + } |
| 53 | + |
| 54 | + public void checkKey(String key) throws ContractException { |
| 55 | + if (key.length() > PrecompiledConstant.TABLE_KEY_MAX_LENGTH) { |
| 56 | + throw new ContractException(PrecompiledRetCode.OVER_TABLE_KEY_LENGTH_LIMIT); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public RetCode createTable(String tableName, String keyFieldName, List<String> valueFields) |
| 61 | + throws ContractException { |
| 62 | + checkKey(keyFieldName); |
| 63 | + String valueFieldsString = convertValueFieldsToString(valueFields); |
| 64 | + return ReceiptParser.parseTransactionReceipt( |
| 65 | + kvTablePrecompiled.createTable(tableName, keyFieldName, valueFieldsString)); |
| 66 | + } |
| 67 | + |
| 68 | + public RetCode set(String tableName, String key, Entry fieldNameToValue) |
| 69 | + throws ContractException { |
| 70 | + return ReceiptParser.parseTransactionReceipt( |
| 71 | + kvTablePrecompiled.set(tableName, key, fieldNameToValue.getKVPrecompiledEntry())); |
| 72 | + } |
| 73 | + |
| 74 | + public Map<String, String> get(String tableName, String key) throws ContractException { |
| 75 | + try { |
| 76 | + Tuple2<Boolean, KVTablePrecompiled.Entry> getResult = |
| 77 | + kvTablePrecompiled.get(tableName, key); |
| 78 | + if (!getResult.getValue1()) { |
| 79 | + throw new ContractException("get from " + tableName + " failed, return false."); |
| 80 | + } |
| 81 | + return parseGetResult(getResult.getValue2()); |
| 82 | + } catch (ContractException e) { |
| 83 | + throw ReceiptParser.parseExceptionCall(e); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public static Map<String, String> parseGetResult(KVTablePrecompiled.Entry getResult) { |
| 88 | + Map<String, String> result = new HashMap<>(); |
| 89 | + for (KVTablePrecompiled.KVField kvField : getResult.fields.getValue()) { |
| 90 | + result.put(kvField.key, kvField.value); |
| 91 | + } |
| 92 | + return result; |
| 93 | + } |
| 94 | + |
| 95 | + public Map<String, String> desc(String tableName) throws ContractException { |
| 96 | + Tuple2<String, String> descOutput = |
| 97 | + kvTablePrecompiled.getDescOutput(kvTablePrecompiled.desc(tableName)); |
| 98 | + Map<String, String> tableDesc = new HashMap<>(); |
| 99 | + tableDesc.put(PrecompiledConstant.KEY_FIELD_NAME, descOutput.getValue1()); |
| 100 | + tableDesc.put(PrecompiledConstant.VALUE_FIELD_NAME, descOutput.getValue2()); |
| 101 | + return tableDesc; |
| 102 | + } |
| 103 | + |
| 104 | + public void asyncSet(String tableName, String key, Entry entry, PrecompiledCallback callback) { |
| 105 | + this.kvTablePrecompiled.set( |
| 106 | + tableName, |
| 107 | + key, |
| 108 | + entry.getKVPrecompiledEntry(), |
| 109 | + new TransactionCallback() { |
| 110 | + @Override |
| 111 | + public void onResponse(TransactionReceipt receipt) { |
| 112 | + RetCode retCode = new RetCode(); |
| 113 | + try { |
| 114 | + retCode = ReceiptParser.parseTransactionReceipt(receipt); |
| 115 | + } catch (ContractException e) { |
| 116 | + retCode.setCode(e.getErrorCode()); |
| 117 | + retCode.setMessage(e.getMessage()); |
| 118 | + retCode.setTransactionReceipt(receipt); |
| 119 | + } |
| 120 | + callback.onResponse(retCode); |
| 121 | + } |
| 122 | + }); |
| 123 | + } |
| 124 | +} |
0 commit comments