use of org.starcoin.bean.Transfer in project starcoin-search by starcoinorg.
the class SecondaryIndexer method executeInternal.
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) {
logger.info("secondary index handle...");
// read transfer offset
TransferOffset offset = transferHandle.getRemoteOffset();
if (offset != null) {
Result<Transfer> result = transferHandle.getRangeTransfers(offset, bulkSize);
if (result.getTotal() > 0) {
List<Transfer> transferList = result.getContents();
// and update offset
transferHandle.bulk(transferList, offset.getOffset());
logger.info("handle transfer: {}", offset);
} else {
logger.warn("get transfer null: {} {}", offset, result.getContents().size());
}
} else {
// init offset
TransferOffset transferOffset = new TransferOffset();
transferOffset.setOffset(0);
transferOffset.setTimestamp("0");
transferHandle.setRemoteOffset(transferOffset);
logger.info("first init offset: {}", transferOffset);
}
}
use of org.starcoin.bean.Transfer in project starcoin-search by starcoinorg.
the class TransferHandle method getRangeTransfers.
public Result getRangeTransfers(TransferOffset transferOffset, int count) {
SearchRequest searchRequest = new SearchRequest(ServiceUtils.getIndex(network, Constant.TRANSFER_INDEX));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// page size
searchSourceBuilder.size(count);
searchSourceBuilder.from(0);
searchSourceBuilder.sort("timestamp", SortOrder.ASC);
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.rangeQuery("timestamp").gt(transferOffset.getTimestamp()));
searchSourceBuilder.query(boolQueryBuilder);
searchRequest.source(searchSourceBuilder);
searchSourceBuilder.trackTotalHits(true);
SearchResponse searchResponse;
try {
searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
logger.error("get transfer error:", e);
return Result.EmptyResult;
}
// set ids
ResultWithId<Transfer> result = ServiceUtils.getSearchResultWithIds(searchResponse, Transfer.class);
List<Transfer> transferList = result.getContents();
List<String> ids = result.getIds();
if (!transferList.isEmpty()) {
for (int i = 0; i < transferList.size(); i++) {
transferList.get(i).setId(ids.get(i));
}
}
return result;
}
use of org.starcoin.bean.Transfer in project starcoin-search by starcoinorg.
the class TransferHandle method bulk.
public void bulk(List<Transfer> transferList, int offset) {
if (transferList.isEmpty()) {
logger.warn("transfer list is null");
return;
}
int size = transferList.size();
BulkRequest bulkRequest = new BulkRequest();
long timestamp = 0;
int successSize = offset;
for (Transfer transfer : transferList) {
// update token index
BigInteger amount = transfer.getAmountValue();
IndexRequest senderRequest = buildJournalRequest(transfer.getId(), transfer.getTypeTag(), transfer.getSender(), amount.negate(), transfer.getTimestamp());
if (senderRequest != null) {
bulkRequest.add(senderRequest);
} else {
break;
}
IndexRequest receiveRequest = buildJournalRequest(transfer.getId(), transfer.getTypeTag(), transfer.getReceiver(), amount, transfer.getTimestamp());
if (senderRequest != null) {
bulkRequest.add(receiveRequest);
} else {
break;
}
successSize += 1;
timestamp = transfer.getTimestamp();
}
try {
if (!bulkRequest.requests().isEmpty()) {
BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
logger.info("bulk transfer result: {}", response.buildFailureMessage());
// update offset
TransferOffset transferOffset = new TransferOffset();
transferOffset.setOffset(successSize);
transferOffset.setTimestamp(String.valueOf(timestamp));
this.setRemoteOffset(transferOffset);
}
} catch (IOException e) {
logger.error("bulk transfer error:", e);
}
}
Aggregations