Skip to content

Commit 5f60fb4

Browse files
Merge pull request #594 from tronprotocol/index_key_instead_of_object
Index key instead of object
2 parents 3f636c2 + 3fee99e commit 5f60fb4

32 files changed

+791
-495
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ src/main/resources/META-INF/
4949

5050
/output_manager/
5151
/output_witness/
52-
52+
output*
5353
nodeId.properties

src/main/java/org/tron/core/db/AccountStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ public boolean has(byte[] key) {
6868

6969
@Override
7070
public void put(byte[] key, AccountCapsule item) {
71+
super.put(key, item);
7172
if (indexHelper != null) {
7273
indexHelper.update(item.getInstance());
7374
}
74-
super.put(key, item);
7575
}
7676

7777
/**

src/main/java/org/tron/core/db/AssetIssueStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ public boolean has(byte[] key) {
6363

6464
@Override
6565
public void put(byte[] key, AssetIssueCapsule item) {
66+
super.put(key, item);
6667
if (indexHelper != null) {
6768
indexHelper.update(item.getInstance());
6869
}
69-
super.put(key, item);
7070
}
7171

7272
/**

src/main/java/org/tron/core/db/BlockStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ public static void destroy() {
5454

5555
@Override
5656
public void put(byte[] key, BlockCapsule item) {
57+
super.put(key, item);
5758
if (indexHelper != null) {
5859
indexHelper.update(item.getInstance());
5960
}
60-
super.put(key, item);
6161
}
6262

6363
/**

src/main/java/org/tron/core/db/TransactionStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ public boolean has(byte[] key) {
3434

3535
@Override
3636
public void put(byte[] key, TransactionCapsule item) {
37+
super.put(key, item);
3738
if (indexHelper != null) {
3839
indexHelper.update(item.getInstance());
3940
}
40-
super.put(key, item);
4141
}
4242

4343
/**

src/main/java/org/tron/core/db/WitnessStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ public static void destory() {
5353

5454
@Override
5555
public void put(byte[] key, WitnessCapsule item) {
56+
super.put(key, item);
5657
if (indexHelper != null) {
5758
indexHelper.update(item.getInstance());
5859
}
59-
super.put(key, item);
6060
}
6161

6262
/**
Lines changed: 45 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,157 +1,130 @@
11
package org.tron.core.db.api;
22

3-
import com.googlecode.cqengine.IndexedCollection;
4-
import javax.annotation.PostConstruct;
5-
import javax.annotation.Resource;
63
import lombok.Getter;
74
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;
1511
import org.tron.protos.Contract.AssetIssueContract;
1612
import org.tron.protos.Protocol.Account;
1713
import org.tron.protos.Protocol.Block;
1814
import org.tron.protos.Protocol.Transaction;
1915
import org.tron.protos.Protocol.Witness;
2016

17+
import javax.annotation.Resource;
18+
2119
@Slf4j
2220
public class IndexHelper {
2321

2422
@Getter
2523
@Resource
26-
private IndexedCollection<Transaction> transactionIndex;
24+
private Index.Iface<Transaction> transactionIndex;
2725
@Getter
2826
@Resource
29-
private IndexedCollection<Block> blockIndex;
27+
private Index.Iface<Block> blockIndex;
3028
@Getter
3129
@Resource
32-
private IndexedCollection<Witness> witnessIndex;
30+
private Index.Iface<Witness> witnessIndex;
3331
@Getter
3432
@Resource
35-
private IndexedCollection<Account> accountIndex;
33+
private Index.Iface<Account> accountIndex;
3634
@Getter
3735
@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;
4537

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);
6340
}
6441

6542
public void add(Transaction t) {
66-
add(transactionIndex, t);
43+
add(transactionIndex, getKey(t));
6744
}
6845

6946
public void add(Block b) {
70-
add(blockIndex, b);
47+
add(blockIndex, getKey(b));
7148
}
7249

7350
public void add(Witness w) {
74-
add(witnessIndex, w);
51+
add(witnessIndex, getKey(w));
7552
}
7653

7754
public void add(Account a) {
78-
add(accountIndex, a);
55+
add(accountIndex, getKey(a));
7956
}
8057

8158
public void add(AssetIssueContract a) {
82-
add(assetIssueIndex, a);
59+
add(assetIssueIndex, getKey(a));
8360
}
8461

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);
8764
}
8865

8966
public void update(Transaction t) {
90-
update(transactionIndex, t);
67+
update(transactionIndex, getKey(t));
9168
}
9269

9370
public void update(Block b) {
94-
update(blockIndex, b);
71+
update(blockIndex, getKey(b));
9572
}
9673

9774
public void update(Witness w) {
98-
update(witnessIndex, w);
75+
update(witnessIndex, getKey(w));
9976
}
10077

10178
public void update(Account a) {
102-
update(accountIndex, a);
79+
update(accountIndex, getKey(a));
10380
}
10481

10582
public void update(AssetIssueContract a) {
106-
update(assetIssueIndex, a);
83+
update(assetIssueIndex, getKey(a));
10784
}
10885

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);
11188
}
11289

11390
public void remove(Transaction t) {
114-
remove(transactionIndex, t);
91+
remove(transactionIndex, getKey(t));
11592
}
11693

11794
public void remove(Block b) {
118-
remove(blockIndex, b);
95+
remove(blockIndex, getKey(b));
11996
}
12097

12198
public void remove(Witness w) {
122-
remove(witnessIndex, w);
99+
remove(witnessIndex, getKey(w));
123100
}
124101

125102
public void remove(Account a) {
126-
remove(accountIndex, a);
103+
remove(accountIndex, getKey(a));
127104
}
128105

129106
public void remove(AssetIssueContract a) {
130-
remove(assetIssueIndex, a);
107+
remove(assetIssueIndex, getKey(a));
131108
}
132109

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();
136112
}
137113

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();
141116
}
142117

143-
@Autowired
144-
public void setAccountStore(AccountStore accountStore) {
145-
this.accountStore = accountStore;
118+
private byte[] getKey(Witness w) {
119+
return new WitnessCapsule(w).createDbKey();
146120
}
147121

148-
@Autowired
149-
public void setTransactionStore(TransactionStore transactionStore) {
150-
this.transactionStore = transactionStore;
122+
private byte[] getKey(Account a) {
123+
return new AccountCapsule(a).createDbKey();
151124
}
152125

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();
156128
}
129+
157130
}

0 commit comments

Comments
 (0)