Search in sources :

Example 1 with UncleBlock

use of org.starcoin.bean.UncleBlock in project starcoin-search by starcoinorg.

the class BlockService method getUncleBlockByHash.

public UncleBlock getUncleBlockByHash(String network, String hash) {
    SearchRequest searchRequest = new SearchRequest(getIndex(network, Constant.UNCLE_BLOCK_INDEX));
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("header.block_hash", hash);
    searchSourceBuilder.query(termQueryBuilder);
    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse;
    try {
        searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    } catch (IOException e) {
        logger.error("get uncle block by hash error:", e);
        return null;
    }
    Result<UncleBlock> result = ServiceUtils.getSearchResult(searchResponse, UncleBlock.class);
    List<UncleBlock> blocks = result.getContents();
    if (blocks.size() == 1) {
        return blocks.get(0);
    } else {
        logger.warn("get uncle block by hash is null, network: {}, : {}", network, hash);
    }
    return null;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) UncleBlock(org.starcoin.bean.UncleBlock) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with UncleBlock

use of org.starcoin.bean.UncleBlock in project starcoin-search by starcoinorg.

the class BlockService method getUncleBlockByHeight.

public UncleBlock getUncleBlockByHeight(String network, long height) {
    SearchRequest searchRequest = new SearchRequest(getIndex(network, Constant.UNCLE_BLOCK_INDEX));
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("header.number", height);
    searchSourceBuilder.query(termQueryBuilder);
    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse;
    try {
        searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    } catch (IOException e) {
        logger.error("get uncle block error:", e);
        return null;
    }
    Result<UncleBlock> result = ServiceUtils.getSearchResult(searchResponse, UncleBlock.class);
    List<UncleBlock> blocks = result.getContents();
    if (blocks.size() == 1) {
        return blocks.get(0);
    } else {
        logger.warn("get uncle block by height is null, network: {}, : {}", network, height);
    }
    return null;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) UncleBlock(org.starcoin.bean.UncleBlock) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

IOException (java.io.IOException)2 SearchRequest (org.elasticsearch.action.search.SearchRequest)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)2 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)2 UncleBlock (org.starcoin.bean.UncleBlock)2