forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoreAPI.java
More file actions
146 lines (132 loc) · 5.77 KB
/
StoreAPI.java
File metadata and controls
146 lines (132 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package org.tron.core.db.api;
import static com.googlecode.cqengine.query.QueryFactory.ascending;
import static com.googlecode.cqengine.query.QueryFactory.equal;
import static com.googlecode.cqengine.query.QueryFactory.orderBy;
import static com.googlecode.cqengine.query.QueryFactory.queryOptions;
import static org.tron.core.config.Parameter.DatabaseConstants.TRANSACTIONS_COUNT_LIMIT_MAX;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Streams;
import com.googlecode.cqengine.resultset.ResultSet;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.tron.core.db.api.index.Index;
import org.tron.core.db.api.index.TransactionIndex;
import org.tron.core.exception.NonUniqueObjectException;
import org.tron.protos.Protocol.Transaction;
@Component
@Slf4j
public class StoreAPI {
@Autowired(required = false)
private IndexHelper indexHelper;
/********************************************************************************
* account api *
********************************************************************************
*/
// public Account getAccountByAddress(String address) throws NonUniqueObjectException {
// if (StringUtils.isEmpty(address)) {
// logger.info("address is empty");
// return null;
// }
// Index.Iface<Account> index = indexHelper.getAccountIndex();
// try (ResultSet<Account> resultSet = index
// .retrieve(equal(AccountIndex.Account_ADDRESS, address))) {
// if (resultSet.isEmpty()) {
// return null;
// }
//
// return resultSet.uniqueResult();
// } catch (com.googlecode.cqengine.resultset.common.NonUniqueObjectException e) {
// throw new NonUniqueObjectException(e);
// }
// }
/********************************************************************************
* block api *
********************************************************************************
*/
// public Block getBlockByNumber(long number) throws NonUniqueObjectException {
// Index.Iface<Block> index = indexHelper.getBlockIndex();
// try (ResultSet<Block> resultSet = index.retrieve(equal(BlockIndex.Block_NUMBER, number))) {
// if (resultSet.isEmpty()) {
// return null;
// }
//
// return resultSet.uniqueResult();
// } catch (com.googlecode.cqengine.resultset.common.NonUniqueObjectException e) {
// throw new NonUniqueObjectException(e);
// }
// }
/*******************************************************************************
* transaction api *
*******************************************************************************
*/
public Transaction getTransactionById(String id) throws NonUniqueObjectException {
if (StringUtils.isEmpty(id)) {
logger.info("id is empty");
return null;
}
Index.Iface<Transaction> index = indexHelper.getTransactionIndex();
try (ResultSet<Transaction> resultSet = index
.retrieve(equal(TransactionIndex.Transaction_ID, id))) {
if (resultSet.isEmpty()) {
return null;
}
return resultSet.uniqueResult();
} catch (com.googlecode.cqengine.resultset.common.NonUniqueObjectException e) {
throw new NonUniqueObjectException(e);
}
}
public List<Transaction> getTransactionsFromThis(String address, long offset, long limit) {
if (StringUtils.isEmpty(address)) {
logger.info("address is empty");
return Lists.newArrayList();
}
Index.Iface<Transaction> index = indexHelper.getTransactionIndex();
try (ResultSet<Transaction> resultSet =
index.retrieve(
equal(TransactionIndex.OWNERS, address),
queryOptions(
orderBy(ascending(TransactionIndex.TIMESTAMP))))) {
if (limit > TRANSACTIONS_COUNT_LIMIT_MAX) {
limit = TRANSACTIONS_COUNT_LIMIT_MAX;
}
return ImmutableList.copyOf(Streams.stream(resultSet).skip(offset).limit(limit).iterator());
}
}
public List<Transaction> getTransactionsToThis(String address, long offset, long limit) {
if (StringUtils.isEmpty(address)) {
logger.info("address is empty");
return Lists.newArrayList();
}
Index.Iface<Transaction> index = indexHelper.getTransactionIndex();
try (ResultSet<Transaction> resultSet =
index.retrieve(
equal(TransactionIndex.TOS, address),
queryOptions(
orderBy(ascending(TransactionIndex.TIMESTAMP))))) {
if (limit > TRANSACTIONS_COUNT_LIMIT_MAX) {
limit = TRANSACTIONS_COUNT_LIMIT_MAX;
}
return ImmutableList.copyOf(Streams.stream(resultSet).skip(offset).limit(limit).iterator());
}
}
/*******************************************************************************
* witness api *
*******************************************************************************
*/
// public List<Witness> getWitnessAll() {
// Index.Iface<Witness> index = indexHelper.getWitnessIndex();
// return ImmutableList.copyOf(index);
// }
/********************************************************************************
* AssetIssue api *
********************************************************************************
*/
// public List<AssetIssueContract> getAssetIssueAll() {
// Index.Iface<AssetIssueContract> index = indexHelper.getAssetIssueIndex();
// return ImmutableList.copyOf(index);
// }
}