com.mongodb.client.model.Indexes Java Examples

The following examples show how to use com.mongodb.client.model.Indexes. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MongoDao.java    From elepy with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #2
Source File: MarketDataServiceBasicImpl.java    From redtorch with MIT License 5 votes vote down vote up
@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);
}
 
Example #3
Source File: MongoDBTargetIT.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #4
Source File: MongoJobRepository.java    From edison-microservice with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #5
Source File: TestModule.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #6
Source File: MongoDocumentDb.java    From mdw with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #7
Source File: ConversationMemoryStore.java    From EDDI with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #8
Source File: DescriptorStore.java    From EDDI with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example #9
Source File: PropertiesStore.java    From EDDI with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #10
Source File: UserConversationStore.java    From EDDI with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #11
Source File: ChannelDefinitionStore.java    From EDDI with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #12
Source File: DifferConversationStore.java    From EDDI with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #13
Source File: DifferBotMappingStore.java    From EDDI with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #14
Source File: ConfigService.java    From runelite with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@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);
}
 
Example #15
Source File: MongoDao.java    From elepy with Apache License 2.0 5 votes vote down vote up
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);
    }
 
Example #16
Source File: MarketDataServiceBasicImpl.java    From redtorch with MIT License 5 votes vote down vote up
@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;
}
 
Example #17
Source File: MarketDataServiceBasicImpl.java    From redtorch with MIT License 5 votes vote down vote up
@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);
}
 
Example #18
Source File: MarketDataServiceBasicImpl.java    From redtorch with MIT License 5 votes vote down vote up
@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;
}
 
Example #19
Source File: MongoDBTableServiceImpl.java    From nuls-v2 with MIT License 5 votes vote down vote up
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"));
}
 
Example #20
Source File: MongoIndex.java    From MongoDB-Plugin with Apache License 2.0 4 votes vote down vote up
public MongoIndex text(String key) {
    this.bson = Indexes.text(key);
    return this;
}
 
Example #21
Source File: MongoIndex.java    From MongoDB-Plugin with Apache License 2.0 4 votes vote down vote up
public MongoIndex hashed(String key) {
    this.bson = Indexes.hashed(key);
    return this;
}
 
Example #22
Source File: MongoIndex.java    From MongoDB-Plugin with Apache License 2.0 4 votes vote down vote up
public MongoIndex add(MongoIndex mongoIndex) {
    indexModels.add(new IndexModel(Indexes.compoundIndex(mongoIndex.getBson()), mongoIndex));
    return this;
}
 
Example #23
Source File: MongoIndex.java    From MongoDB-Plugin with Apache License 2.0 4 votes vote down vote up
public MongoIndex geoHaystack(String key, Bson additional) {
    this.bson = Indexes.geoHaystack(key, additional);
    return this;
}
 
Example #24
Source File: MongoIndex.java    From MongoDB-Plugin with Apache License 2.0 4 votes vote down vote up
public MongoIndex geo2d(String key) {
    this.bson = Indexes.geo2d(key);
    return this;
}
 
Example #25
Source File: MongoIndex.java    From MongoDB-Plugin with Apache License 2.0 4 votes vote down vote up
public MongoIndex geo2dsphere(String... keys) {
    this.bson = Indexes.geo2dsphere(keys);
    return this;
}
 
Example #26
Source File: MongoIndex.java    From MongoDB-Plugin with Apache License 2.0 4 votes vote down vote up
public MongoIndex descending(String... keys) {
    this.bson = Indexes.descending(keys);
    return this;
}
 
Example #27
Source File: MongoIndex.java    From MongoDB-Plugin with Apache License 2.0 4 votes vote down vote up
public MongoIndex ascending(String... keys) {
    this.bson = Indexes.ascending(keys);
    return this;
}
 
Example #28
Source File: InstanceSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 4 votes vote down vote up
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));
    }
  });
}
 
Example #29
Source File: MongoDBWriter.java    From ctakes-docker with Apache License 2.0 4 votes vote down vote up
@Override
public void collectionProcessComplete() throws AnalysisEngineProcessException {
    // Create an index over the 'cui' field for faster searching
    this.coll.createIndex(Indexes.text(CUI_FIELD));
}
 
Example #30
Source File: BotTriggerStore.java    From EDDI with Apache License 2.0 3 votes vote down vote up
@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));
}