|
2 | 2 |
|
3 | 3 | import java.util.concurrent.TimeUnit; |
4 | 4 |
|
| 5 | +import org.bson.Document; |
5 | 6 | import org.slf4j.Logger; |
6 | 7 | import org.slf4j.LoggerFactory; |
| 8 | +import org.springframework.beans.factory.InitializingBean; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
7 | 10 | import org.springframework.beans.factory.annotation.Value; |
8 | 11 | import org.springframework.context.annotation.Bean; |
9 | 12 | import org.springframework.context.annotation.Configuration; |
| 13 | +import org.springframework.data.domain.Sort.Direction; |
10 | 14 | import org.springframework.data.mongodb.core.MongoOperations; |
11 | 15 | import org.springframework.data.mongodb.core.ReactiveMongoTemplate; |
| 16 | +import org.springframework.data.mongodb.core.index.CompoundIndexDefinition; |
| 17 | +import org.springframework.data.mongodb.core.index.Index; |
12 | 18 |
|
13 | 19 | import com.bitcoin.indexer.core.Coin; |
14 | 20 | import com.bitcoin.indexer.facade.InsightsFacade; |
|
19 | 25 | import com.bitcoin.indexer.repository.TransactionRepositoryImpl; |
20 | 26 | import com.bitcoin.indexer.repository.UtxoRepository; |
21 | 27 | import com.bitcoin.indexer.repository.UtxoRepositoryImpl; |
| 28 | +import com.bitcoin.indexer.repository.db.AllOutputsDbObject; |
| 29 | +import com.bitcoin.indexer.repository.db.BlockDbObject; |
| 30 | +import com.bitcoin.indexer.repository.db.TransactionDbObject; |
22 | 31 | import com.mongodb.MongoClientOptions; |
23 | 32 |
|
24 | 33 | import okhttp3.OkHttpClient; |
25 | 34 |
|
26 | 35 | @Configuration |
27 | | -public class Config { |
| 36 | +public class Config implements InitializingBean { |
28 | 37 |
|
29 | 38 | private static final Logger logger = LoggerFactory.getLogger(Config.class); |
30 | 39 |
|
| 40 | + @Autowired |
| 41 | + public ReactiveMongoTemplate reactiveMongoTemplate; |
| 42 | + |
31 | 43 | @Bean |
32 | 44 | public Coin coin() { |
33 | 45 | return Coin.BCH; |
@@ -61,4 +73,103 @@ public UtxoRepository utxoRepository(ReactiveMongoTemplate reactiveMongoTemplate |
61 | 73 | public SlpDetailsRepository detailsRepository(ReactiveMongoTemplate reactiveMongoTemplate) { |
62 | 74 | return new SlpDetailsRepositoryImpl(reactiveMongoTemplate); |
63 | 75 | } |
| 76 | + |
| 77 | + @Value("${spring.data.mongodb.database}") |
| 78 | + String mongoDb; |
| 79 | + |
| 80 | + @Override |
| 81 | + public void afterPropertiesSet() throws Exception { |
| 82 | + logger.info("Running for database={}", mongoDb); |
| 83 | + ReactiveMongoTemplate mongoTemplate = reactiveMongoTemplate; |
| 84 | + mongoTemplate.indexOps(AllOutputsDbObject.class).ensureIndex(new Index("address", Direction.ASC).named("address_output")).block(); |
| 85 | + mongoTemplate.indexOps(AllOutputsDbObject.class).ensureIndex(new Index("txId", Direction.ASC).named("txId_output")).block(); |
| 86 | + |
| 87 | + Document document = new Document(); |
| 88 | + document.put("address", 1); |
| 89 | + document.put("isSpent", 1); |
| 90 | + mongoTemplate.indexOps(AllOutputsDbObject.class).ensureIndex(new CompoundIndexDefinition( |
| 91 | + document |
| 92 | + ).named("address_spent_output")).block(); |
| 93 | + |
| 94 | + document = new Document(); |
| 95 | + document.put("address", 1); |
| 96 | + document.put("isSpent", 1); |
| 97 | + document.put("slpUtxoType.parentTransactionValid.valid", 1); |
| 98 | + mongoTemplate.indexOps(AllOutputsDbObject.class).ensureIndex(new CompoundIndexDefinition( |
| 99 | + document |
| 100 | + ).named("address_spent_valid_output")).block(); |
| 101 | + |
| 102 | + document = new Document(); |
| 103 | + document.put("slpUtxoType.slpTokenId", 1); |
| 104 | + document.put("slpUtxoType.hasBaton", 1); |
| 105 | + mongoTemplate.indexOps(AllOutputsDbObject.class).ensureIndex(new CompoundIndexDefinition( |
| 106 | + document |
| 107 | + ).named("tokenId_baton_output")).block(); |
| 108 | + |
| 109 | + document = new Document(); |
| 110 | + document.put("slpUtxoType.slpTokenId", 1); |
| 111 | + document.put("slpUtxoType.amount", 1); |
| 112 | + document.put("isSpent", 1); |
| 113 | + mongoTemplate.indexOps(AllOutputsDbObject.class).ensureIndex(new CompoundIndexDefinition( |
| 114 | + document |
| 115 | + ).named("tokenId_amount_spent_output")).block(); |
| 116 | + |
| 117 | + document = new Document(); |
| 118 | + document.put("slpUtxoType.slpTokenId", 1); |
| 119 | + document.put("isSpent", 1); |
| 120 | + document.put("slpUtxoType.parentTransactionValid.valid", 1); |
| 121 | + mongoTemplate.indexOps(AllOutputsDbObject.class).ensureIndex(new CompoundIndexDefinition( |
| 122 | + document |
| 123 | + ).named("tokenId_spent_valid_output")).block(); |
| 124 | + |
| 125 | + document = new Document(); |
| 126 | + document.put("slpUtxoType.slpTokenId", 1); |
| 127 | + document.put("isSpent", 1); |
| 128 | + mongoTemplate.indexOps(AllOutputsDbObject.class).ensureIndex(new CompoundIndexDefinition( |
| 129 | + document |
| 130 | + ).named("tokenId_spent_output")).block(); |
| 131 | + |
| 132 | + document = new Document(); |
| 133 | + document.put("_id", 1); |
| 134 | + document.put("slpValid.valid", 1); |
| 135 | + mongoTemplate.indexOps(TransactionDbObject.class).ensureIndex(new CompoundIndexDefinition( |
| 136 | + document |
| 137 | + ).named("txId_valid_tx")).block(); |
| 138 | + |
| 139 | + document = new Document(); |
| 140 | + document.put("_id", 1); |
| 141 | + document.put("slpValid.valid", 1); |
| 142 | + document.put("outputs.slpUtxoType.slpTokenId", 1); |
| 143 | + mongoTemplate.indexOps(TransactionDbObject.class).ensureIndex(new CompoundIndexDefinition( |
| 144 | + document |
| 145 | + ).named("txId_valid_tokenId_tx")).block(); |
| 146 | + |
| 147 | + document = new Document(); |
| 148 | + document.put("slpValid.valid", 1); |
| 149 | + document.put("outputs.slpUtxoType.slpTokenId", 1); |
| 150 | + mongoTemplate.indexOps(TransactionDbObject.class).ensureIndex(new CompoundIndexDefinition( |
| 151 | + document |
| 152 | + ).named("valid_tokenId_tx")).block(); |
| 153 | + |
| 154 | + mongoTemplate.indexOps(TransactionDbObject.class) |
| 155 | + .ensureIndex(new Index("outputs.slpUtxoType.slpTokenId", Direction.ASC).named("tokenId_tx")).block(); |
| 156 | + |
| 157 | + mongoTemplate.indexOps(TransactionDbObject.class) |
| 158 | + .ensureIndex(new Index("time", Direction.DESC).named("time_tx")).block(); |
| 159 | + |
| 160 | + mongoTemplate.indexOps(TransactionDbObject.class).ensureIndex(new Index( |
| 161 | + "inputs.address", Direction.ASC) |
| 162 | + .named("input_addr").background()).block(); |
| 163 | + |
| 164 | + mongoTemplate.indexOps(TransactionDbObject.class).ensureIndex(new Index( |
| 165 | + "outputs.address", Direction.ASC) |
| 166 | + .named("output_addr").background()).block(); |
| 167 | + |
| 168 | + mongoTemplate.indexOps(AllOutputsDbObject.class) |
| 169 | + .ensureIndex(new Index("slpUtxoType.slpTokenId", Direction.ASC).named("tokenId_output")).block(); |
| 170 | + |
| 171 | + mongoTemplate.indexOps(BlockDbObject.class) |
| 172 | + .ensureIndex(new Index("height", Direction.ASC).named("block_height")).block(); |
| 173 | + |
| 174 | + } |
64 | 175 | } |
0 commit comments