Skip to content

Commit 9236174

Browse files
authored
add async interface for crudService(insert/update/remove) (FISCO-BCOS#55)
1 parent 3c05fac commit 9236174

File tree

5 files changed

+206
-5
lines changed

5 files changed

+206
-5
lines changed

.ci/ci_check.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ check_sm_node()
8484
clean_node
8585
}
8686

87-
LOG_INFO "------ check_basic---------"
88-
check_basic
8987
LOG_INFO "------ download_build_chain---------"
9088
download_build_chain
9189
LOG_INFO "------ check_standard_node---------"
9290
check_standard_node
9391
LOG_INFO "------ check_sm_node---------"
94-
check_sm_node
92+
check_sm_node
93+
LOG_INFO "------ check_basic---------"
94+
check_basic

.codecov.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
codecov:
22
branch: *
33
coverage:
4+
ignore:
5+
- "src/integration-test/**/*"
46
status:
57
project:
68
default:

src/integration-test/java/org/fisco/bcos/sdk/precompiled/PrecompiledTest.java

+134-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import org.fisco.bcos.sdk.demo.contract.HelloWorld;
3737
import org.fisco.bcos.sdk.model.RetCode;
3838
import org.fisco.bcos.sdk.model.TransactionReceipt;
39+
import org.fisco.bcos.sdk.test.service.GroupServiceTest;
40+
import org.fisco.bcos.sdk.transaction.model.callback.TransactionSucCallback;
3941
import org.junit.Assert;
4042
import org.junit.FixMethodOrder;
4143
import org.junit.Test;
@@ -46,6 +48,9 @@
4648
import java.util.HashMap;
4749
import java.util.List;
4850
import java.util.Map;
51+
import java.util.concurrent.ExecutorService;
52+
import java.util.concurrent.Executors;
53+
import java.util.concurrent.atomic.AtomicLong;
4954

5055
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
5156
public class PrecompiledTest
@@ -203,7 +208,7 @@ private void testSystemConfigService(Client client, SystemConfigService systemCo
203208
Assert.assertTrue(queriedValue.equals(updatedValue));
204209
Assert.assertTrue(queriedValue.equals(value.add(BigInteger.valueOf(1000))));
205210
}
206-
211+
// Note: Please make sure that the ut is before the permission-related ut
207212
@Test
208213
public void test5CRUDService() throws ConfigException, ContractException {
209214
try {
@@ -251,6 +256,134 @@ public void test5CRUDService() throws ConfigException, ContractException {
251256
}
252257
}
253258

259+
// Note: Please make sure that the ut is before the permission-related ut
260+
@Test
261+
public void test51SyncCRUDService() throws ConfigException {
262+
try {
263+
BcosSDK sdk = new BcosSDK(configFile);
264+
Client client = sdk.getClient(Integer.valueOf(1));
265+
CryptoInterface cryptoInterface = client.getCryptoInterface();
266+
TableCRUDService crudService = new TableCRUDService(client, cryptoInterface);
267+
String tableName = "test_sync";
268+
List<String> valueFiled = new ArrayList<>();
269+
valueFiled.add("field");
270+
RetCode retCode = crudService.createTable(tableName, "key", valueFiled);
271+
System.out.println("createResult: " + retCode.getCode() + ", message: " + retCode.getMessage());
272+
// create a thread pool to parallel insert and select
273+
ExecutorService threadPool = Executors.newFixedThreadPool(50);
274+
275+
BigInteger orgTxCount = new BigInteger(client.getTotalTransactionCount().getTotalTransactionCount().getTxSum().substring(2), 16);
276+
for(int i = 0; i < 100; i++)
277+
{
278+
final Integer index = i;
279+
threadPool.execute(new Runnable() {
280+
@Override
281+
public void run() {
282+
try {
283+
Map<String, String> value = new HashMap<>();
284+
value.put("field", "field" + index);
285+
String valueOfKey = "key_value" + index;
286+
// insert
287+
crudService.insert(tableName, valueOfKey , new Entry(value), null);
288+
// select
289+
crudService.select(tableName, valueOfKey, null);
290+
// update
291+
value.clear();
292+
value.put("field", "field" + index + 100);
293+
crudService.update(tableName, valueOfKey, new Entry(value), null);
294+
// remove
295+
crudService.remove(tableName, valueOfKey, null);
296+
}catch(ContractException e)
297+
{
298+
System.out.println("call crudService failed, error information: " + e.getMessage());
299+
}
300+
}
301+
});
302+
}
303+
GroupServiceTest.awaitAfterShutdown(threadPool);
304+
BigInteger currentTxCount = new BigInteger(client.getTotalTransactionCount().getTotalTransactionCount().getTxSum().substring(2), 16);
305+
System.out.println("orgTxCount: " + orgTxCount + ", currentTxCount:" + currentTxCount);
306+
Assert.assertTrue(currentTxCount.equals(orgTxCount.add(BigInteger.valueOf(300))));
307+
}catch(ContractException e)
308+
{
309+
System.out.println("test9SyncCRUDService failed, error info: " + e.getMessage());
310+
}
311+
}
312+
313+
class FakeTransactionCallback extends TransactionSucCallback {
314+
public TransactionReceipt receipt;
315+
public AtomicLong receiptCount = new AtomicLong();
316+
@Override
317+
public void onTimeout() {
318+
super.onTimeout();
319+
}
320+
321+
// wait until get the transactionReceipt
322+
@Override
323+
public void onResponse(TransactionReceipt receipt) {
324+
cancelTimeout();
325+
this.receipt = receipt;
326+
receiptCount.addAndGet(1);
327+
}
328+
}
329+
330+
@Test
331+
public void test52AsyncCRUDService()
332+
{
333+
try {
334+
BcosSDK sdk = new BcosSDK(configFile);
335+
Client client = sdk.getClient(Integer.valueOf(1));
336+
CryptoInterface cryptoInterface = client.getCryptoInterface();
337+
TableCRUDService crudService = new TableCRUDService(client, cryptoInterface);
338+
// create table
339+
String tableName = "send_async";
340+
List<String> valueFiled = new ArrayList<>();
341+
valueFiled.add("field");
342+
String key = "key";
343+
crudService.createTable(tableName, key, valueFiled);
344+
// create a thread pool to parallel insert and select
345+
ExecutorService threadPool = Executors.newFixedThreadPool(50);
346+
FakeTransactionCallback callback = new FakeTransactionCallback();
347+
BigInteger orgTxCount = new BigInteger(client.getTotalTransactionCount().getTotalTransactionCount().getTxSum().substring(2), 16);
348+
for(int i = 0; i < 100; i++)
349+
{
350+
final Integer index = i;
351+
threadPool.execute(new Runnable() {
352+
@Override
353+
public void run() {
354+
try {
355+
Map<String, String> value = new HashMap<>();
356+
value.put("field", "field" + index);
357+
String valueOfKey = "key_value" + index;
358+
// insert
359+
crudService.asyncInsert(tableName, valueOfKey , new Entry(value), null, callback);
360+
// update
361+
value.clear();
362+
value.put("field", "field" + index + 100);
363+
crudService.asyncUpdate(tableName, valueOfKey, new Entry(value), null, callback);
364+
// remove
365+
crudService.asyncRemove(tableName, valueOfKey, null, callback);
366+
}catch(ContractException e)
367+
{
368+
System.out.println("call crudService failed, error information: " + e.getMessage());
369+
}
370+
}
371+
});
372+
}
373+
while(callback.receiptCount.get() != 300)
374+
{
375+
Thread.sleep(1000);
376+
}
377+
GroupServiceTest.awaitAfterShutdown(threadPool);
378+
BigInteger currentTxCount = new BigInteger(client.getTotalTransactionCount().getTotalTransactionCount().getTxSum().substring(2), 16);
379+
System.out.println("orgTxCount: " + orgTxCount + ", currentTxCount:" + currentTxCount);
380+
Assert.assertTrue(currentTxCount.equals(orgTxCount.add(BigInteger.valueOf(300))));
381+
}catch(ContractException | ConfigException | InterruptedException e)
382+
{
383+
System.out.println("test10AsyncCRUDService failed, error info: " + e.getMessage());
384+
}
385+
}
386+
254387
@Test
255388
public void test6PermissionService() throws ConfigException, ContractException {
256389
try {

src/main/java/org/fisco/bcos/sdk/contract/precompiled/crud/TableCRUDService.java

+66
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.fisco.bcos.sdk.model.NodeVersion;
3636
import org.fisco.bcos.sdk.model.ReceiptParser;
3737
import org.fisco.bcos.sdk.model.RetCode;
38+
import org.fisco.bcos.sdk.transaction.model.callback.TransactionSucCallback;
3839
import org.fisco.bcos.sdk.utils.ObjectMapperFactory;
3940
import org.fisco.bcos.sdk.utils.StringUtils;
4041

@@ -211,4 +212,69 @@ public List<Map<String, String>> desc(String tableName) throws ContractException
211212
throw ReceiptParser.parseExceptionCall(e);
212213
}
213214
}
215+
216+
public void asyncInsert(
217+
String tableName,
218+
String key,
219+
Entry fieldNameToValue,
220+
Condition condition,
221+
TransactionSucCallback callback)
222+
throws ContractException {
223+
checkKey(key);
224+
try {
225+
String fieldNameToValueStr =
226+
ObjectMapperFactory.getObjectMapper()
227+
.writeValueAsString(fieldNameToValue.getFieldNameToValue());
228+
String conditionStr = encodeCondition(condition);
229+
this.crudService.insert(tableName, key, fieldNameToValueStr, conditionStr, callback);
230+
} catch (JsonProcessingException e) {
231+
throw new ContractException(
232+
"asyncInsert "
233+
+ fieldNameToValue.toString()
234+
+ " to "
235+
+ tableName
236+
+ " failed, error info:"
237+
+ e.getMessage(),
238+
e);
239+
}
240+
}
241+
242+
public void asyncUpdate(
243+
String tableName,
244+
String key,
245+
Entry fieldNameToValue,
246+
Condition condition,
247+
TransactionSucCallback callback)
248+
throws ContractException {
249+
checkKey(key);
250+
try {
251+
String fieldNameToValueStr =
252+
ObjectMapperFactory.getObjectMapper()
253+
.writeValueAsString(fieldNameToValue.getFieldNameToValue());
254+
String conditionStr = encodeCondition(condition);
255+
this.crudService.update(
256+
tableName, key, fieldNameToValueStr, conditionStr, "", callback);
257+
} catch (JsonProcessingException e) {
258+
throw new ContractException(
259+
"asyncUpdate "
260+
+ fieldNameToValue.toString()
261+
+ " to "
262+
+ tableName
263+
+ " failed, error info:"
264+
+ e.getMessage(),
265+
e);
266+
}
267+
}
268+
269+
public void asyncRemove(
270+
String tableName, String key, Condition condition, TransactionSucCallback callback)
271+
throws ContractException {
272+
checkKey(key);
273+
try {
274+
this.crudService.remove(tableName, key, encodeCondition(condition), "", callback);
275+
} catch (JsonProcessingException e) {
276+
throw new ContractException(
277+
"asyncRemove " + key + " with condition from " + tableName + " failed");
278+
}
279+
}
214280
}

src/test/java/org/fisco/bcos/sdk/test/service/GroupServiceTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void run() {
124124
awaitAfterShutdown(threadPool2);
125125
}
126126

127-
public void awaitAfterShutdown(ExecutorService threadPool) {
127+
public static void awaitAfterShutdown(ExecutorService threadPool) {
128128
threadPool.shutdown();
129129
try {
130130
while (!threadPool.isTerminated()) {

0 commit comments

Comments
 (0)