下面列出了怎么用com.mongodb.client.model.Indexes的API类实例代码及写法,或者点击链接到github查看源代码。
private Bson createIndexField(String property) {
final var split = property.split(":");
//Ensures property exists
schema.getProperty(split[0]);
if (split.length == 1) {
return Indexes.ascending(property);
}
try {
final var i = Integer.parseInt(split[1]);
return new BasicDBObject(property, i);
} catch (NumberFormatException e) {
throw new ElepyConfigException(String.format("%s is not a valid integer", split[1]), e);
}
}
private void initTablesIndex(int chainId) {
//交易关系表
for (int i = 0; i < TX_RELATION_SHARDING_COUNT; i++) {
mongoDBService.createIndex(DBTableConstant.TX_RELATION_TABLE + chainId + "_" + i, Indexes.ascending("address"));
mongoDBService.createIndex(DBTableConstant.TX_RELATION_TABLE + chainId + "_" + i, Indexes.ascending("address", "type"));
mongoDBService.createIndex(DBTableConstant.TX_RELATION_TABLE + chainId + "_" + i, Indexes.ascending("txHash"));
mongoDBService.createIndex(DBTableConstant.TX_RELATION_TABLE + chainId + "_" + i, Indexes.descending("createTime"));
}
//账户信息表
mongoDBService.createIndex(DBTableConstant.ACCOUNT_TABLE + chainId, Indexes.descending("totalBalance"));
mongoDBService.createIndex(DBTableConstant.ACCOUNT_LEDGER_TABLE + chainId, Indexes.descending("address"));
//交易表
mongoDBService.createIndex(DBTableConstant.TX_TABLE + chainId, Indexes.descending("height"));
//block 表
mongoDBService.createIndex(DBTableConstant.BLOCK_HEADER_TABLE + chainId, Indexes.ascending("hash"));
//委托记录表
mongoDBService.createIndex(DBTableConstant.DEPOSIT_TABLE + chainId, Indexes.descending("createTime"));
//智能合约表
mongoDBService.createIndex(DBTableConstant.CONTRACT_TABLE + chainId, Indexes.descending("createTime"));
//账户token表
mongoDBService.createIndex(DBTableConstant.ACCOUNT_TOKEN_TABLE + chainId, Indexes.descending("balance"));
mongoDBService.createIndex(DBTableConstant.ACCOUNT_TOKEN_TABLE + chainId, Indexes.ascending("address"));
mongoDBService.createIndex(DBTableConstant.ACCOUNT_TOKEN_TABLE + chainId, Indexes.ascending("contractAddress"));
//token交易记录表
mongoDBService.createIndex(DBTableConstant.TOKEN_TRANSFER_TABLE + chainId, Indexes.descending("time"));
mongoDBService.createIndex(DBTableConstant.TOKEN_TRANSFER_TABLE + chainId, Indexes.descending("contractAddress","fromAddress"));
mongoDBService.createIndex(DBTableConstant.TOKEN_TRANSFER_TABLE + chainId, Indexes.descending("contractAddress","toAddress"));
}
@Override
public boolean upsertBar(String dbName, String collectionName, BarField bar) {
Document barDocument = barToDocument(bar);
Document filterDocument = new Document();
filterDocument.put("unifiedSymbol", bar.getUnifiedSymbol());
filterDocument.put("actionTimestamp", bar.getActionTimestamp());
todayMarketDataDBClient.getDatabase(dbName).getCollection(collectionName).createIndex(Indexes.ascending("actionTimestamp", "unifiedSymbol"));
return todayMarketDataDBClient.upsert(dbName, collectionName, barDocument, filterDocument);
}
@Override
public boolean upsertBar(String dbName, String collectionName, List<BarField> barList) {
if (barList == null || barList.isEmpty()) {
logger.error("更新插入Bar集合错误,数据集合为空");
return false;
}
List<WriteModel<Document>> writeModelList = new ArrayList<WriteModel<Document>>();
long beginTime = System.currentTimeMillis();
for (BarField bar : barList) {
Document filterDocument = new Document();
filterDocument.put("unifiedSymbol", bar.getUnifiedSymbol());
filterDocument.put("actionTimestamp", bar.getActionTimestamp());
Document barDocument = barToDocument(bar);
ReplaceOptions replaceOptions = new ReplaceOptions();
replaceOptions.upsert(true);
ReplaceOneModel<Document> replaceOneModel = new ReplaceOneModel<Document>(filterDocument, barDocument, replaceOptions);
writeModelList.add(replaceOneModel);
}
logger.info("更新插入Bar集合,数据库{},集合{},数据转换耗时{}ms,共{}条数据", dbName, collectionName, (System.currentTimeMillis() - beginTime), barList.size());
beginTime = System.currentTimeMillis();
todayMarketDataDBClient.getDatabase(dbName).getCollection(collectionName).createIndex(Indexes.ascending("actionTimestamp", "unifiedSymbol"));
todayMarketDataDBClient.getDatabase(dbName).getCollection(collectionName).bulkWrite(writeModelList);
logger.info("更新插入Bar集合,数据库{},集合{},数据库操作耗时{}ms,共{}条操作", dbName, collectionName, (System.currentTimeMillis() - beginTime), writeModelList.size());
return true;
}
@Override
public boolean upsertTick(String dbName, String collectionName, TickField tick) {
Document tickDocument = tickToDocument(tick);
Document filterDocument = new Document();
filterDocument.put("unifiedSymbol", tick.getUnifiedSymbol());
filterDocument.put("actionTimestamp", tick.getActionTimestamp());
todayMarketDataDBClient.getDatabase(dbName).getCollection(collectionName).createIndex(Indexes.ascending("actionTimestamp"));
return todayMarketDataDBClient.upsert(dbName, collectionName, tickDocument, filterDocument);
}
@Override
public boolean upsertTick(String dbName, String collectionName, List<TickField> tickList) {
if (tickList == null || tickList.isEmpty()) {
logger.error("更新插入Tick集合错误,数据集合为空");
return false;
}
List<WriteModel<Document>> writeModelList = new ArrayList<WriteModel<Document>>();
long beginTime = System.currentTimeMillis();
for (TickField tick : tickList) {
Document filterDocument = new Document();
filterDocument.put("unifiedSymbol", tick.getUnifiedSymbol());
filterDocument.put("actionTimestamp", tick.getActionTimestamp());
Document tickDocument = tickToDocument(tick);
ReplaceOptions replaceOptions = new ReplaceOptions();
replaceOptions.upsert(true);
ReplaceOneModel<Document> replaceOneModel = new ReplaceOneModel<Document>(filterDocument, tickDocument, replaceOptions);
writeModelList.add(replaceOneModel);
}
logger.info("更新插入Tick集合,数据库{},集合{},数据转换耗时{}ms,共{}条数据", dbName, collectionName, (System.currentTimeMillis() - beginTime), tickList.size());
beginTime = System.currentTimeMillis();
todayMarketDataDBClient.getDatabase(dbName).getCollection(collectionName).createIndex(Indexes.ascending("actionTimestamp"));
todayMarketDataDBClient.getDatabase(dbName).getCollection(collectionName).bulkWrite(writeModelList);
logger.info("更新插入Tick集合,数据库{},集合{},数据库操作耗时{}ms,共{}条操作", dbName, collectionName, (System.currentTimeMillis() - beginTime), writeModelList.size());
return true;
}
private void createIndex(MongoIndex annotation) {
if (annotation.properties().length == 0) {
throw new ElepyConfigException("No properties specified in MongoIndex");
}
final var indexOptions = new IndexOptions();
if (!isDefault(annotation.text())) {
indexOptions.textVersion(annotation.text());
}
if (!isDefault(annotation.expireAfterSeconds())) {
indexOptions.expireAfter(annotation.expireAfterSeconds(), TimeUnit.SECONDS);
}
if (!isDefault(annotation.name())) {
indexOptions.name(annotation.name());
}
if (!isDefault(annotation.unique())) {
indexOptions.unique(annotation.unique());
}
final var compoundIndex = Indexes.compoundIndex(Arrays
.stream(annotation.properties())
.map(this::createIndexField)
.collect(Collectors.toList()));
mongoCollection.createIndex(compoundIndex, indexOptions);
}
@Autowired
public ConfigService(
MongoClient mongoClient,
@Value("${mongo.database}") String databaseName
)
{
MongoDatabase database = mongoClient.getDatabase(databaseName);
MongoCollection<Document> collection = database.getCollection("config");
this.mongoCollection = collection;
// Create unique index on _userId
IndexOptions indexOptions = new IndexOptions().unique(true);
collection.createIndex(Indexes.ascending("_userId"), indexOptions);
}
@Inject
public DifferBotMappingStore(MongoDatabase database, IDocumentBuilder documentBuilder) {
checkNotNull(database, "database");
this.collectionDocument = database.getCollection(COLLECTION_DIFFER_BOT_MAPPINGS, Document.class);
this.collectionObject = database.getCollection(COLLECTION_DIFFER_BOT_MAPPINGS, DifferBotMapping.class);
this.documentBuilder = documentBuilder;
this.differBotMappingResourceStore = new DifferBotMappingResourceStore();
collectionObject.createIndex(Indexes.ascending(BOT_USER_ID_FIELD));
collectionObject.createIndex(Indexes.ascending(BOT_INTENT_FIELD));
}
@Inject
public DifferConversationStore(MongoDatabase database) {
checkNotNull(database, "database");
this.collection = database.getCollection(COLLECTION_DIFFER_CONVERSATIONS, DifferConversationInfo.class);
this.userConversationStore = new DifferConversationResourceStore();
collection.createIndex(Indexes.ascending(CONVERSATION_ID_FIELD), new IndexOptions().unique(true));
}
@Inject
public ChannelDefinitionStore(MongoDatabase database) {
checkNotNull(database, "database");
this.collection = database.getCollection(COLLECTION_CHANNELS, ChannelDefinition.class);
this.channelDefinitionResourceStore = new ChannelDefinitionResourceStore();
collection.createIndex(Indexes.ascending(NAME_FIELD));
}
@Inject
public UserConversationStore(MongoDatabase database,
IJsonSerialization jsonSerialization,
IDocumentBuilder documentBuilder) {
this.jsonSerialization = jsonSerialization;
RuntimeUtilities.checkNotNull(database, "database");
this.collection = database.getCollection(COLLECTION_USER_CONVERSATIONS);
this.documentBuilder = documentBuilder;
this.userConversationStore = new UserConversationResourceStore();
collection.createIndex(
Indexes.compoundIndex(
Indexes.ascending(INTENT_FIELD),
Indexes.ascending(USER_ID_FIELD)),
new IndexOptions().unique(true));
}
@Inject
public BotTriggerStore(MongoDatabase database,
IJsonSerialization jsonSerialization,
IDocumentBuilder documentBuilder) {
this.jsonSerialization = jsonSerialization;
RuntimeUtilities.checkNotNull(database, "database");
this.collection = database.getCollection(COLLECTION_BOT_TRIGGERS);
this.documentBuilder = documentBuilder;
this.botTriggerStore = new BotTriggerResourceStore();
collection.createIndex(Indexes.ascending(INTENT_FIELD), new IndexOptions().unique(true));
}
@Inject
public PropertiesStore(MongoDatabase database) {
RuntimeUtilities.checkNotNull(database, "database");
this.collection = database.getCollection(COLLECTION_PROPERTIES);
this.propertiesStore = new PropertiesResourceStore();
collection.createIndex(Indexes.ascending(USER_ID), new IndexOptions().unique(true));
}
public DescriptorStore(MongoDatabase database, IPermissionStore permissionStore, IUserStore userStore,
IGroupStore groupStore, IDocumentBuilder documentBuilder, Class<T> documentType) {
RuntimeUtilities.checkNotNull(database, "database");
RuntimeUtilities.checkNotNull(permissionStore, "permissionStore");
MongoCollection<Document> descriptorCollection = database.getCollection(COLLECTION_DESCRIPTORS);
MongoResourceStorage<T> resourceStorage =
new MongoResourceStorage<>(database, collectionName, documentBuilder, documentType);
this.descriptorResourceStore = new ModifiableHistorizedResourceStore<>(resourceStorage);
this.resourceFilter = new ResourceFilter<>(descriptorCollection, descriptorResourceStore,
permissionStore, userStore, groupStore, documentBuilder, documentType);
descriptorCollection.createIndex(Indexes.ascending(FIELD_RESOURCE), new IndexOptions().unique(true));
}
@Inject
public ConversationMemoryStore(MongoDatabase database) {
this.conversationCollectionDocument = database.getCollection(CONVERSATION_COLLECTION, Document.class);
this.conversationCollectionObject = database.getCollection(CONVERSATION_COLLECTION, ConversationMemorySnapshot.class);
conversationCollectionDocument.createIndex(Indexes.ascending(CONVERSATION_STATE_FIELD));
conversationCollectionDocument.createIndex(Indexes.ascending(CONVERSATION_BOT_ID_FIELD));
conversationCollectionDocument.createIndex(Indexes.ascending(CONVERSATION_BOT_VERSION_FIELD));
}
public static void createMongoDocIdIndex(String collectionName) {
try {
MongoDatabase mongoDb = getMongoDb();
if (mongoDb != null) {
IndexOptions indexOptions = new IndexOptions().unique(true).background(true).name("document_id_1");
MongoCollection<org.bson.Document> collection = mongoDb.getCollection(collectionName);
String indexName = collection.createIndex(Indexes.ascending("document_id"), indexOptions);
LoggerUtil.getStandardLogger().mdwDebug("Created Index : " + indexName + " on collection : " + collectionName);
collectionDocIdIndexed.putIfAbsent(collectionName, true);
}
}
catch (Exception e) {
LoggerUtil.getStandardLogger().info("Failed to create index for 'document_id' on " + collectionName + " collection", e);
}
}
@Override
protected void initialize() {
MongoConfig mongo = config(MongoConfig.class);
mongo.uri("mongodb://localhost:27017/test");
mongo.collection(TestMongoEntity.class);
mongo.view(TestMongoView.class);
bean(Mongo.class).createIndex("entity", Indexes.ascending("string_field"));
mongo = config(MongoConfig.class, "other");
mongo.uri("mongodb://localhost:27018/test");
mongo.collection(TestMongoEntity.class);
}
@Override
protected final void ensureIndexes() {
IndexOptions options = new IndexOptions().background(true);
collection().createIndex(Indexes.compoundIndex(Indexes.ascending(JobStructure.JOB_TYPE.key()), Indexes.descending(JobStructure.STARTED.key())), options);
collection().createIndex(Indexes.ascending(JobStructure.STARTED.key()), options);
collection().createIndex(Indexes.ascending(JobStructure.LAST_UPDATED.key(), JobStructure.STOPPED.key()), options);
}
@Before
public void setUp() throws Exception {
MongoDatabase db = mongo.getDatabase(DATABASE_NAME);
db.createCollection(TEST_WRITE_COLLECTION);
db.createCollection(UNIQUE_KEY_EXCEPTION_COLLECTION);
testWriteCollection = db.getCollection(TEST_WRITE_COLLECTION);
testWriteCollection.createIndex(Indexes.text("name"), new IndexOptions().unique(true));
}
@Override
public void collectionProcessComplete() throws AnalysisEngineProcessException {
// Create an index over the 'cui' field for faster searching
this.coll.createIndex(Indexes.text(CUI_FIELD));
}
InstanceSynchronizationConfig(final MongoDatabase configDb) {
this.namespacesColl = configDb
.getCollection("namespaces", NamespaceSynchronizationConfig.class);
this.docsColl = configDb
.getCollection("documents", CoreDocumentSynchronizationConfig.class);
this.namespacesColl.createIndex(
Indexes.ascending(
NamespaceSynchronizationConfig.ConfigCodec.Fields.NAMESPACE_FIELD),
new IndexOptions().unique(true));
this.docsColl.createIndex(
Indexes.ascending(
CoreDocumentSynchronizationConfig.ConfigCodec.Fields.NAMESPACE_FIELD,
CoreDocumentSynchronizationConfig.ConfigCodec.Fields.DOCUMENT_ID_FIELD),
new IndexOptions().unique(true));
// used to scan for stale documents when the namespace is marked as not stale,
this.docsColl.createIndex(
Indexes.ascending(
CoreDocumentSynchronizationConfig.ConfigCodec.Fields.NAMESPACE_FIELD,
CoreDocumentSynchronizationConfig.ConfigCodec.Fields.IS_STALE,
CoreDocumentSynchronizationConfig.ConfigCodec.Fields.DOCUMENT_ID_FIELD));
// used to scan for unpaused documents when the whole namespace is marked as stale
this.docsColl.createIndex(
Indexes.ascending(
CoreDocumentSynchronizationConfig.ConfigCodec.Fields.NAMESPACE_FIELD,
CoreDocumentSynchronizationConfig.ConfigCodec.Fields.IS_PAUSED,
CoreDocumentSynchronizationConfig.ConfigCodec.Fields.DOCUMENT_ID_FIELD)
);
this.instanceLock = new ReentrantReadWriteLock();
this.namespaces = new HashMap<>();
// Fill from db
namespacesColl.find().forEach(new Block<NamespaceSynchronizationConfig>() {
@Override
public void apply(
@Nonnull final NamespaceSynchronizationConfig nsConfig
) {
namespaces.put(nsConfig.getNamespace(), new NamespaceSynchronizationConfig(
namespacesColl,
docsColl,
nsConfig));
}
});
}
public MongoIndex ascending(String... keys) {
this.bson = Indexes.ascending(keys);
return this;
}
public MongoIndex descending(String... keys) {
this.bson = Indexes.descending(keys);
return this;
}
public MongoIndex geo2dsphere(String... keys) {
this.bson = Indexes.geo2dsphere(keys);
return this;
}
public MongoIndex geo2d(String key) {
this.bson = Indexes.geo2d(key);
return this;
}
public MongoIndex geoHaystack(String key, Bson additional) {
this.bson = Indexes.geoHaystack(key, additional);
return this;
}
public MongoIndex text(String key) {
this.bson = Indexes.text(key);
return this;
}
public MongoIndex hashed(String key) {
this.bson = Indexes.hashed(key);
return this;
}
public MongoIndex add(MongoIndex mongoIndex) {
indexModels.add(new IndexModel(Indexes.compoundIndex(mongoIndex.getBson()), mongoIndex));
return this;
}