Search in sources :

Example 1 with Transfer

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);
    }
}
Also used : TransferOffset(org.starcoin.bean.TransferOffset) Transfer(org.starcoin.bean.Transfer)

Example 2 with Transfer

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;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Transfer(org.starcoin.bean.Transfer) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 3 with Transfer

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);
    }
}
Also used : TransferOffset(org.starcoin.bean.TransferOffset) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) Transfer(org.starcoin.bean.Transfer) BigInteger(java.math.BigInteger) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IOException(java.io.IOException) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Aggregations

Transfer (org.starcoin.bean.Transfer)3 IOException (java.io.IOException)2 TransferOffset (org.starcoin.bean.TransferOffset)2 BigInteger (java.math.BigInteger)1 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)1 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)1 IndexRequest (org.elasticsearch.action.index.IndexRequest)1 SearchRequest (org.elasticsearch.action.search.SearchRequest)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)1