Skip to content

Commit ee1393d

Browse files
committed
distinct by one primary indexAttribute
1 parent 140ab2d commit ee1393d

11 files changed

Lines changed: 131 additions & 12 deletions

File tree

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

100755100644
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ public boolean has(byte[] key) {
6868
@Override
6969
public void put(byte[] key, AccountCapsule item) {
7070
if (indexHelper != null) {
71-
indexHelper.add(item.getInstance());
71+
indexHelper.update(item.getInstance());
7272
}
7373
super.put(key, item);
7474
}
7575

76-
/** get all accounts. */
76+
/**
77+
* get all accounts.
78+
*/
7779
public List<AccountCapsule> getAllAccounts() {
7880
return dbSource
7981
.allValues()
@@ -82,21 +84,27 @@ public List<AccountCapsule> getAllAccounts() {
8284
.collect(Collectors.toList());
8385
}
8486

85-
/** Max TRX account. */
87+
/**
88+
* Max TRX account.
89+
*/
8690
public AccountCapsule getSun() {
8791
byte[] data = dbSource.getData(assertsAddress.get("Sun"));
8892
AccountCapsule accountCapsule = new AccountCapsule(data);
8993
return accountCapsule;
9094
}
9195

92-
/** Min TRX account. */
96+
/**
97+
* Min TRX account.
98+
*/
9399
public AccountCapsule getBlackhole() {
94100
byte[] data = dbSource.getData(assertsAddress.get("Blackhole"));
95101
AccountCapsule accountCapsule = new AccountCapsule(data);
96102
return accountCapsule;
97103
}
98104

99-
/** Get foundation account info. */
105+
/**
106+
* Get foundation account info.
107+
*/
100108
public AccountCapsule getZion() {
101109
byte[] data = dbSource.getData(assertsAddress.get("Zion"));
102110
AccountCapsule accountCapsule = new AccountCapsule(data);

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

100755100644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public boolean has(byte[] key) {
6363
@Override
6464
public void put(byte[] key, AssetIssueCapsule item) {
6565
if (indexHelper != null) {
66-
indexHelper.add(item.getInstance());
66+
indexHelper.update(item.getInstance());
6767
}
6868
super.put(key, item);
6969
}

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

100755100644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static void destroy() {
4949
@Override
5050
public void put(byte[] key, BlockCapsule item) {
5151
if (indexHelper != null) {
52-
indexHelper.add(item.getInstance());
52+
indexHelper.update(item.getInstance());
5353
}
5454
super.put(key, item);
5555
}

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

100755100644
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public boolean has(byte[] key) {
3535
@Override
3636
public void put(byte[] key, TransactionCapsule item) {
3737
if (indexHelper != null) {
38-
indexHelper.add(item.getInstance());
38+
indexHelper.update(item.getInstance());
3939
}
4040
super.put(key, item);
4141
}
@@ -82,5 +82,5 @@ public byte[] findTransactionByHash(byte[] trxHash) {
8282
public Iterator<TransactionCapsule> iterator() {
8383
return new TransactionIterator(dbSource.iterator());
8484
}
85-
85+
8686
}

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

100755100644
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static void destory() {
5353
@Override
5454
public void put(byte[] key, WitnessCapsule item) {
5555
if (indexHelper != null) {
56-
indexHelper.add(item.getInstance());
56+
indexHelper.update(item.getInstance());
5757
}
5858
super.put(key, item);
5959
}
@@ -74,7 +74,9 @@ public static WitnessStore create(String dbName) {
7474
return instance;
7575
}
7676

77-
/** get all witnesses. */
77+
/**
78+
* get all witnesses.
79+
*/
7880
public List<WitnessCapsule> getAllWitnesses() {
7981
return dbSource
8082
.allValues()

‎src/main/java/org/tron/core/db/api/IndexHelper.java‎

100755100644
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.tron.core.db.BlockStore;
1212
import org.tron.core.db.TransactionStore;
1313
import org.tron.core.db.WitnessStore;
14+
import org.tron.core.db.api.index.AbstractIndex;
1415
import org.tron.protos.Contract.AssetIssueContract;
1516
import org.tron.protos.Protocol.Account;
1617
import org.tron.protos.Protocol.Block;
@@ -79,6 +80,30 @@ public void add(AssetIssueContract a) {
7980
add(assetIssueIndex, a);
8081
}
8182

83+
public <T> void update(IndexedCollection<T> index, T t) {
84+
((AbstractIndex<T>) index).update(t);
85+
}
86+
87+
public void update(Transaction t) {
88+
update(transactionIndex, t);
89+
}
90+
91+
public void update(Block b) {
92+
update(blockIndex, b);
93+
}
94+
95+
public void update(Witness w) {
96+
update(witnessIndex, w);
97+
}
98+
99+
public void update(Account a) {
100+
update(accountIndex, a);
101+
}
102+
103+
public void update(AssetIssueContract a) {
104+
update(assetIssueIndex, a);
105+
}
106+
82107
private <T> void remove(IndexedCollection<T> index, T t) {
83108
index.remove(t);
84109
}

‎src/main/java/org/tron/core/db/api/index/AbstractIndex.java‎

100755100644
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public AbstractIndex(Persistence<T, ? extends Comparable> persistence) {
1414
super(persistence);
1515
}
1616

17+
public boolean update(T t) {
18+
return add(t);
19+
}
20+
1721
@Override
1822
public String getName() {
1923
return this.getClass().getSimpleName();

‎src/main/java/org/tron/core/db/api/index/AccountIndex.java‎

100755100644
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.tron.core.db.api.index;
22

33
import static com.googlecode.cqengine.query.QueryFactory.attribute;
4+
import static com.googlecode.cqengine.query.QueryFactory.equal;
45

6+
import com.google.common.collect.ImmutableList;
57
import com.googlecode.cqengine.attribute.Attribute;
68
import com.googlecode.cqengine.index.navigable.NavigableIndex;
79
import com.googlecode.cqengine.index.suffix.SuffixTreeIndex;
@@ -30,10 +32,17 @@ public AccountIndex(Persistence<Account, ? extends Comparable> persistence) {
3032
super(persistence);
3133
}
3234

35+
@Override
36+
public boolean update(Account account) {
37+
return update(
38+
retrieve(equal(Account_ADDRESS, ByteArray.toHexString(account.getAddress().toByteArray()))),
39+
ImmutableList.of(account)
40+
);
41+
}
42+
3343
@PostConstruct
3444
public void init() {
3545
addIndex(SuffixTreeIndex.onAttribute(Account_ADDRESS));
3646
addIndex(NavigableIndex.onAttribute(Account_BALANCE));
3747
}
38-
3948
}

‎src/main/java/org/tron/core/db/api/index/AssetIssueIndex.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.tron.core.db.api.index;
22

33
import static com.googlecode.cqengine.query.QueryFactory.attribute;
4+
import static com.googlecode.cqengine.query.QueryFactory.equal;
45

6+
import com.google.common.collect.ImmutableList;
57
import com.googlecode.cqengine.attribute.Attribute;
68
import com.googlecode.cqengine.index.navigable.NavigableIndex;
79
import com.googlecode.cqengine.index.suffix.SuffixTreeIndex;
@@ -38,6 +40,14 @@ public AssetIssueIndex(Persistence<AssetIssueContract, ? extends Comparable> per
3840
super(persistence);
3941
}
4042

43+
@Override
44+
public boolean update(AssetIssueContract assetIssue) {
45+
return update(
46+
retrieve(equal(AssetIssue_NAME, assetIssue.getName().toStringUtf8())),
47+
ImmutableList.of(assetIssue)
48+
);
49+
}
50+
4151
@PostConstruct
4252
public void init() {
4353
addIndex(SuffixTreeIndex.onAttribute(AssetIssue_OWNER_RADDRESS));

‎src/main/java/org/tron/core/db/api/index/WitnessIndex.java‎

100755100644
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.tron.core.db.api.index;
22

33
import static com.googlecode.cqengine.query.QueryFactory.attribute;
4+
import static com.googlecode.cqengine.query.QueryFactory.equal;
45

6+
import com.google.common.collect.ImmutableList;
57
import com.googlecode.cqengine.attribute.Attribute;
68
import com.googlecode.cqengine.index.suffix.SuffixTreeIndex;
79
import com.googlecode.cqengine.persistence.Persistence;
@@ -32,6 +34,14 @@ public WitnessIndex(Persistence<Witness, ? extends Comparable> persistence) {
3234
super(persistence);
3335
}
3436

37+
@Override
38+
public boolean update(Witness witness) {
39+
return update(
40+
retrieve(equal(Witness_ADDRESS, ByteArray.toHexString(witness.getAddress().toByteArray()))),
41+
ImmutableList.of(witness)
42+
);
43+
}
44+
3545
@PostConstruct
3646
public void init() {
3747
addIndex(SuffixTreeIndex.onAttribute(Witness_ADDRESS));

0 commit comments

Comments
 (0)