|
1 | 1 | package org.tron.core.db.api; |
2 | 2 |
|
3 | | -import com.googlecode.cqengine.IndexedCollection; |
4 | | -import javax.annotation.PostConstruct; |
5 | | -import javax.annotation.Resource; |
6 | 3 | import lombok.Getter; |
7 | 4 | import lombok.extern.slf4j.Slf4j; |
8 | | -import org.springframework.beans.factory.annotation.Autowired; |
9 | | -import org.tron.core.db.AccountStore; |
10 | | -import org.tron.core.db.AssetIssueStore; |
11 | | -import org.tron.core.db.BlockStore; |
12 | | -import org.tron.core.db.TransactionStore; |
13 | | -import org.tron.core.db.WitnessStore; |
14 | | -import org.tron.core.db.api.index.AbstractIndex; |
| 5 | +import org.tron.core.capsule.AccountCapsule; |
| 6 | +import org.tron.core.capsule.AssetIssueCapsule; |
| 7 | +import org.tron.core.capsule.BlockCapsule; |
| 8 | +import org.tron.core.capsule.TransactionCapsule; |
| 9 | +import org.tron.core.capsule.WitnessCapsule; |
| 10 | +import org.tron.core.db.api.index.Index; |
15 | 11 | import org.tron.protos.Contract.AssetIssueContract; |
16 | 12 | import org.tron.protos.Protocol.Account; |
17 | 13 | import org.tron.protos.Protocol.Block; |
18 | 14 | import org.tron.protos.Protocol.Transaction; |
19 | 15 | import org.tron.protos.Protocol.Witness; |
20 | 16 |
|
| 17 | +import javax.annotation.Resource; |
| 18 | + |
21 | 19 | @Slf4j |
22 | 20 | public class IndexHelper { |
23 | 21 |
|
24 | 22 | @Getter |
25 | 23 | @Resource |
26 | | - private IndexedCollection<Transaction> transactionIndex; |
| 24 | + private Index.Iface<Transaction> transactionIndex; |
27 | 25 | @Getter |
28 | 26 | @Resource |
29 | | - private IndexedCollection<Block> blockIndex; |
| 27 | + private Index.Iface<Block> blockIndex; |
30 | 28 | @Getter |
31 | 29 | @Resource |
32 | | - private IndexedCollection<Witness> witnessIndex; |
| 30 | + private Index.Iface<Witness> witnessIndex; |
33 | 31 | @Getter |
34 | 32 | @Resource |
35 | | - private IndexedCollection<Account> accountIndex; |
| 33 | + private Index.Iface<Account> accountIndex; |
36 | 34 | @Getter |
37 | 35 | @Resource |
38 | | - private IndexedCollection<AssetIssueContract> assetIssueIndex; |
39 | | - |
40 | | - private BlockStore blockStore; |
41 | | - private WitnessStore witnessStore; |
42 | | - private AccountStore accountStore; |
43 | | - private TransactionStore transactionStore; |
44 | | - private AssetIssueStore assetIssueStore; |
| 36 | + private Index.Iface<AssetIssueContract> assetIssueIndex; |
45 | 37 |
|
46 | | - /** |
47 | | - * init index |
48 | | - */ |
49 | | - @PostConstruct |
50 | | - public void init() { |
51 | | - long start = System.currentTimeMillis(); |
52 | | - logger.info("begin to initialize indexHelper"); |
53 | | - blockStore.forEach(b -> blockIndex.add(b.getValue().getInstance())); |
54 | | - witnessStore.forEach(w -> witnessIndex.add(w.getValue().getInstance())); |
55 | | - transactionStore.forEach(t -> transactionIndex.add(t.getValue().getInstance())); |
56 | | - accountStore.forEach(a -> accountIndex.add(a.getValue().getInstance())); |
57 | | - assetIssueStore.forEach(a -> assetIssueIndex.add(a.getValue().getInstance())); |
58 | | - logger.info("end to initialize indexHelper. cost:{}s", System.currentTimeMillis() - start); |
59 | | - } |
60 | | - |
61 | | - private <T> void add(IndexedCollection<T> index, T t) { |
62 | | - index.add(t); |
| 38 | + private <T> void add(Index.Iface<T> index, byte[] bytes) { |
| 39 | + index.add(bytes); |
63 | 40 | } |
64 | 41 |
|
65 | 42 | public void add(Transaction t) { |
66 | | - add(transactionIndex, t); |
| 43 | + add(transactionIndex, getKey(t)); |
67 | 44 | } |
68 | 45 |
|
69 | 46 | public void add(Block b) { |
70 | | - add(blockIndex, b); |
| 47 | + add(blockIndex, getKey(b)); |
71 | 48 | } |
72 | 49 |
|
73 | 50 | public void add(Witness w) { |
74 | | - add(witnessIndex, w); |
| 51 | + add(witnessIndex, getKey(w)); |
75 | 52 | } |
76 | 53 |
|
77 | 54 | public void add(Account a) { |
78 | | - add(accountIndex, a); |
| 55 | + add(accountIndex, getKey(a)); |
79 | 56 | } |
80 | 57 |
|
81 | 58 | public void add(AssetIssueContract a) { |
82 | | - add(assetIssueIndex, a); |
| 59 | + add(assetIssueIndex, getKey(a)); |
83 | 60 | } |
84 | 61 |
|
85 | | - public <T> void update(IndexedCollection<T> index, T t) { |
86 | | - ((AbstractIndex<T>) index).update(t); |
| 62 | + private <T> void update(Index.Iface<T> index, byte[] bytes) { |
| 63 | + index.update(bytes); |
87 | 64 | } |
88 | 65 |
|
89 | 66 | public void update(Transaction t) { |
90 | | - update(transactionIndex, t); |
| 67 | + update(transactionIndex, getKey(t)); |
91 | 68 | } |
92 | 69 |
|
93 | 70 | public void update(Block b) { |
94 | | - update(blockIndex, b); |
| 71 | + update(blockIndex, getKey(b)); |
95 | 72 | } |
96 | 73 |
|
97 | 74 | public void update(Witness w) { |
98 | | - update(witnessIndex, w); |
| 75 | + update(witnessIndex, getKey(w)); |
99 | 76 | } |
100 | 77 |
|
101 | 78 | public void update(Account a) { |
102 | | - update(accountIndex, a); |
| 79 | + update(accountIndex, getKey(a)); |
103 | 80 | } |
104 | 81 |
|
105 | 82 | public void update(AssetIssueContract a) { |
106 | | - update(assetIssueIndex, a); |
| 83 | + update(assetIssueIndex, getKey(a)); |
107 | 84 | } |
108 | 85 |
|
109 | | - private <T> void remove(IndexedCollection<T> index, T t) { |
110 | | - index.remove(t); |
| 86 | + private <T> void remove(Index.Iface<T> index, byte[] bytes) { |
| 87 | + index.remove(bytes); |
111 | 88 | } |
112 | 89 |
|
113 | 90 | public void remove(Transaction t) { |
114 | | - remove(transactionIndex, t); |
| 91 | + remove(transactionIndex, getKey(t)); |
115 | 92 | } |
116 | 93 |
|
117 | 94 | public void remove(Block b) { |
118 | | - remove(blockIndex, b); |
| 95 | + remove(blockIndex, getKey(b)); |
119 | 96 | } |
120 | 97 |
|
121 | 98 | public void remove(Witness w) { |
122 | | - remove(witnessIndex, w); |
| 99 | + remove(witnessIndex, getKey(w)); |
123 | 100 | } |
124 | 101 |
|
125 | 102 | public void remove(Account a) { |
126 | | - remove(accountIndex, a); |
| 103 | + remove(accountIndex, getKey(a)); |
127 | 104 | } |
128 | 105 |
|
129 | 106 | public void remove(AssetIssueContract a) { |
130 | | - remove(assetIssueIndex, a); |
| 107 | + remove(assetIssueIndex, getKey(a)); |
131 | 108 | } |
132 | 109 |
|
133 | | - @Autowired |
134 | | - public void setBlockStore(BlockStore blockStore) { |
135 | | - this.blockStore = blockStore; |
| 110 | + private byte[] getKey(Transaction t) { |
| 111 | + return new TransactionCapsule(t).getTransactionId().getBytes(); |
136 | 112 | } |
137 | 113 |
|
138 | | - @Autowired |
139 | | - public void setWitnessStore(WitnessStore witnessStore) { |
140 | | - this.witnessStore = witnessStore; |
| 114 | + private byte[] getKey(Block b) { |
| 115 | + return new BlockCapsule(b).getBlockId().getBytes(); |
141 | 116 | } |
142 | 117 |
|
143 | | - @Autowired |
144 | | - public void setAccountStore(AccountStore accountStore) { |
145 | | - this.accountStore = accountStore; |
| 118 | + private byte[] getKey(Witness w) { |
| 119 | + return new WitnessCapsule(w).createDbKey(); |
146 | 120 | } |
147 | 121 |
|
148 | | - @Autowired |
149 | | - public void setTransactionStore(TransactionStore transactionStore) { |
150 | | - this.transactionStore = transactionStore; |
| 122 | + private byte[] getKey(Account a) { |
| 123 | + return new AccountCapsule(a).createDbKey(); |
151 | 124 | } |
152 | 125 |
|
153 | | - @Autowired |
154 | | - public void setAssetIssueStore(AssetIssueStore assetIssueStore) { |
155 | | - this.assetIssueStore = assetIssueStore; |
| 126 | + private byte[] getKey(AssetIssueContract a) { |
| 127 | + return new AssetIssueCapsule(a).getName().toByteArray(); |
156 | 128 | } |
| 129 | + |
157 | 130 | } |
0 commit comments