Skip to content

Commit bfab4b9

Browse files
committed
index key instead of object
1 parent c135ad6 commit bfab4b9

17 files changed

Lines changed: 497 additions & 279 deletions

‎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: 48 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package org.tron.core.db.api;
22

3-
import com.googlecode.cqengine.IndexedCollection;
4-
import javax.annotation.PostConstruct;
53
import javax.annotation.Resource;
64
import lombok.Getter;
75
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;
6+
import org.tron.core.capsule.AccountCapsule;
7+
import org.tron.core.capsule.AssetIssueCapsule;
8+
import org.tron.core.capsule.BlockCapsule;
9+
import org.tron.core.capsule.ProtoCapsule;
10+
import org.tron.core.capsule.TransactionCapsule;
11+
import org.tron.core.capsule.WitnessCapsule;
1412
import org.tron.core.db.api.index.AbstractIndex;
13+
import org.tron.core.db.api.index.AccountIndex;
14+
import org.tron.core.db.api.index.AssetIssueIndex;
15+
import org.tron.core.db.api.index.BlockIndex;
16+
import org.tron.core.db.api.index.TransactionIndex;
17+
import org.tron.core.db.api.index.WitnessIndex;
1518
import org.tron.protos.Contract.AssetIssueContract;
1619
import org.tron.protos.Protocol.Account;
1720
import org.tron.protos.Protocol.Block;
@@ -23,135 +26,110 @@ public class IndexHelper {
2326

2427
@Getter
2528
@Resource
26-
private IndexedCollection<Transaction> transactionIndex;
29+
private TransactionIndex transactionIndex;
2730
@Getter
2831
@Resource
29-
private IndexedCollection<Block> blockIndex;
32+
private BlockIndex blockIndex;
3033
@Getter
3134
@Resource
32-
private IndexedCollection<Witness> witnessIndex;
35+
private WitnessIndex witnessIndex;
3336
@Getter
3437
@Resource
35-
private IndexedCollection<Account> accountIndex;
38+
private AccountIndex accountIndex;
3639
@Getter
3740
@Resource
38-
private IndexedCollection<AssetIssueContract> assetIssueIndex;
41+
private AssetIssueIndex assetIssueIndex;
3942

40-
private BlockStore blockStore;
41-
private WitnessStore witnessStore;
42-
private AccountStore accountStore;
43-
private TransactionStore transactionStore;
44-
private AssetIssueStore assetIssueStore;
45-
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);
43+
private <T> void add(AbstractIndex<? extends ProtoCapsule, T> index, byte[] bytes) {
44+
index.add(bytes);
6345
}
6446

6547
public void add(Transaction t) {
66-
add(transactionIndex, t);
48+
add(transactionIndex, getKey(t));
6749
}
6850

6951
public void add(Block b) {
70-
add(blockIndex, b);
52+
add(blockIndex, getKey(b));
7153
}
7254

7355
public void add(Witness w) {
74-
add(witnessIndex, w);
56+
add(witnessIndex, getKey(w));
7557
}
7658

7759
public void add(Account a) {
78-
add(accountIndex, a);
60+
add(accountIndex, getKey(a));
7961
}
8062

8163
public void add(AssetIssueContract a) {
82-
add(assetIssueIndex, a);
64+
add(assetIssueIndex, getKey(a));
8365
}
8466

85-
public <T> void update(IndexedCollection<T> index, T t) {
86-
((AbstractIndex<T>) index).update(t);
67+
private <T> void update(AbstractIndex<? extends ProtoCapsule, T> index, byte[] bytes) {
68+
index.update(bytes);
8769
}
8870

8971
public void update(Transaction t) {
90-
update(transactionIndex, t);
72+
update(transactionIndex, getKey(t));
9173
}
9274

9375
public void update(Block b) {
94-
update(blockIndex, b);
76+
update(blockIndex, getKey(b));
9577
}
9678

9779
public void update(Witness w) {
98-
update(witnessIndex, w);
80+
update(witnessIndex, getKey(w));
9981
}
10082

10183
public void update(Account a) {
102-
update(accountIndex, a);
84+
update(accountIndex, getKey(a));
10385
}
10486

10587
public void update(AssetIssueContract a) {
106-
update(assetIssueIndex, a);
88+
update(assetIssueIndex, getKey(a));
10789
}
10890

109-
private <T> void remove(IndexedCollection<T> index, T t) {
110-
index.remove(t);
91+
private <T> void remove(AbstractIndex<? extends ProtoCapsule, T> index, byte[] bytes) {
92+
index.remove(bytes);
11193
}
11294

11395
public void remove(Transaction t) {
114-
remove(transactionIndex, t);
96+
remove(transactionIndex, getKey(t));
11597
}
11698

11799
public void remove(Block b) {
118-
remove(blockIndex, b);
100+
remove(blockIndex, getKey(b));
119101
}
120102

121103
public void remove(Witness w) {
122-
remove(witnessIndex, w);
104+
remove(witnessIndex, getKey(w));
123105
}
124106

125107
public void remove(Account a) {
126-
remove(accountIndex, a);
108+
remove(accountIndex, getKey(a));
127109
}
128110

129111
public void remove(AssetIssueContract a) {
130-
remove(assetIssueIndex, a);
112+
remove(assetIssueIndex, getKey(a));
131113
}
132114

133-
@Autowired
134-
public void setBlockStore(BlockStore blockStore) {
135-
this.blockStore = blockStore;
115+
private byte[] getKey(Transaction t) {
116+
return new TransactionCapsule(t).getTransactionId().getBytes();
136117
}
137118

138-
@Autowired
139-
public void setWitnessStore(WitnessStore witnessStore) {
140-
this.witnessStore = witnessStore;
119+
private byte[] getKey(Block b) {
120+
return new BlockCapsule(b).getBlockId().getBytes();
141121
}
142122

143-
@Autowired
144-
public void setAccountStore(AccountStore accountStore) {
145-
this.accountStore = accountStore;
123+
private byte[] getKey(Witness w) {
124+
return new WitnessCapsule(w).createDbKey();
146125
}
147126

148-
@Autowired
149-
public void setTransactionStore(TransactionStore transactionStore) {
150-
this.transactionStore = transactionStore;
127+
private byte[] getKey(Account a) {
128+
return new AccountCapsule(a).getAddress().toByteArray();
151129
}
152130

153-
@Autowired
154-
public void setAssetIssueStore(AssetIssueStore assetIssueStore) {
155-
this.assetIssueStore = assetIssueStore;
131+
private byte[] getKey(AssetIssueContract a) {
132+
return new AssetIssueCapsule(a).getName().toByteArray();
156133
}
134+
157135
}

0 commit comments

Comments
 (0)