private <T> void createIndex(@Nullable final ClientSession clientSession, final MongoCollection<T> collection, final Document index, final IndexOptions indexOptions) { if (clientSession != null) { collection.createIndex(clientSession, index, indexOptions); } else { collection.createIndex(index, indexOptions); } }
@Override public Void call() { collection().createIndex(convertToIndex(fields), options); return null; } });
@Inject public MongoDBSessionServiceImpl(MongoConnection mongoConnection) { super(mongoConnection); final MongoDatabase database = mongoConnection.getMongoDatabase(); final MongoCollection<Document> sessions = database.getCollection(MongoDbSession.COLLECTION_NAME); sessions.createIndex(Indexes.ascending(MongoDbSession.FIELD_SESSION_ID)); }
private void createTableMetadata(SchemaTableName schemaTableName, List<MongoColumnHandle> columns) throws TableNotFoundException { String schemaName = schemaTableName.getSchemaName(); String tableName = schemaTableName.getTableName(); MongoDatabase db = client.getDatabase(schemaName); Document metadata = new Document(TABLE_NAME_KEY, tableName); ArrayList<Document> fields = new ArrayList<>(); if (!columns.stream().anyMatch(c -> c.getName().equals("_id"))) { fields.add(new MongoColumnHandle("_id", OBJECT_ID, true).getDocument()); } fields.addAll(columns.stream() .map(MongoColumnHandle::getDocument) .collect(toList())); metadata.append(FIELDS_KEY, fields); MongoCollection<Document> schema = db.getCollection(schemaCollection); schema.createIndex(new Document(TABLE_NAME_KEY, 1), new IndexOptions().unique(true)); schema.insertOne(metadata); }
@Signature public void createIndex(BasicDBObject keys, IndexOptions options) { getWrappedObject().createIndex(keys, options); }
@Signature public void createIndex(BasicDBObject keys) { getWrappedObject().createIndex(keys); }
private static void createIndex(MongoConnection mongoConnection) { final IndexOptions indexOptions = new IndexOptions() .name(INDEX_NAME) .unique(true); mongoConnection.getMongoDatabase() .getCollection(COLLECTION_NAME) .createIndex(Indexes.ascending("name"), indexOptions); }
private void syncTtl() { if (!ttlSync.get()) { ttlSync.set(true); if (timeout <= 0) { return; } log.debug("creating session timeout index"); if (existsIdx(SESSION_IDX)) { Document command = new Document("collMod", collection) .append("index", new Document("keyPattern", new Document("_accessedAt", 1)) .append("expireAfterSeconds", timeout)); log.debug("{}", command); Document result = db.runCommand(command); log.debug("{}", result); } else { sessions.createIndex( new Document("_accessedAt", 1), new IndexOptions() .name(SESSION_IDX) .expireAfter(timeout, TimeUnit.SECONDS)); } } }
private Document getTableMetadata(SchemaTableName schemaTableName) throws TableNotFoundException { String schemaName = schemaTableName.getSchemaName(); String tableName = schemaTableName.getTableName(); MongoDatabase db = client.getDatabase(schemaName); MongoCollection<Document> schema = db.getCollection(schemaCollection); Document doc = schema .find(new Document(TABLE_NAME_KEY, tableName)).first(); if (doc == null) { if (!collectionExists(db, tableName)) { throw new TableNotFoundException(schemaTableName); } else { Document metadata = new Document(TABLE_NAME_KEY, tableName); metadata.append(FIELDS_KEY, guessTableFields(schemaTableName)); schema.createIndex(new Document(TABLE_NAME_KEY, 1), new IndexOptions().unique(true)); schema.insertOne(metadata); return metadata; } } return doc; }
.name(MongoDbGrokPatternService.INDEX_NAME) .unique(true); collection.createIndex(Indexes.ascending("name"), indexOptions);
void createIndex(final MongoCollection collection, final MappedClass mc, final Index index, final boolean background) { Index normalized = IndexBuilder.normalize(index); BsonDocument keys = calculateKeys(mc, normalized); com.mongodb.client.model.IndexOptions indexOptions = convert(normalized.options(), background); calculateWeights(normalized, indexOptions); collection.createIndex(keys, indexOptions); } }
private void createTransactionsGlobalTxKeyIndexIfNecessary() { String databaseName = CommonUtils.getApplication(this.endpoint).replaceAll("\\W", "_"); MongoDatabase database = this.mongoClient.getDatabase(databaseName); MongoCollection<Document> transactions = database.getCollection(CONSTANTS_TB_TRANSACTIONS); ListIndexesIterable<Document> transactionIndexList = transactions.listIndexes(); boolean transactionIndexExists = false; MongoCursor<Document> transactionCursor = null; try { transactionCursor = transactionIndexList.iterator(); while (transactionIndexExists == false && transactionCursor.hasNext()) { Document document = transactionCursor.next(); Boolean unique = document.getBoolean("unique"); Document key = (Document) document.get("key"); boolean globalExists = key.containsKey(CONSTANTS_FD_GLOBAL); boolean lengthEquals = key.size() == 1; transactionIndexExists = lengthEquals && globalExists; if (transactionIndexExists && (unique == null || unique == false)) { throw new IllegalStateException(); } } } finally { IOUtils.closeQuietly(transactionCursor); } if (transactionIndexExists == false) { Document index = new Document(CONSTANTS_FD_GLOBAL, 1); transactions.createIndex(index, new IndexOptions().unique(true)); } }
private void createLocksIndexIfNecessary() { String databaseName = CommonUtils.getApplication(this.endpoint).replaceAll("\\W", "_"); MongoDatabase database = this.mongoClient.getDatabase(databaseName); MongoCollection<Document> locks = database.getCollection(CONSTANTS_TB_LOCKS); ListIndexesIterable<Document> lockIndexList = locks.listIndexes(); boolean transactionIndexExists = false; MongoCursor<Document> lockCursor = null; try { lockCursor = lockIndexList.iterator(); while (transactionIndexExists == false && lockCursor.hasNext()) { Document document = lockCursor.next(); Boolean unique = document.getBoolean("unique"); Document key = (Document) document.get("key"); boolean globalExists = key.containsKey(CONSTANTS_FD_GLOBAL); boolean lengthEquals = key.size() == 1; transactionIndexExists = lengthEquals && globalExists; if (transactionIndexExists && (unique == null || unique == false)) { throw new IllegalStateException(); } } } finally { IOUtils.closeQuietly(lockCursor); } if (transactionIndexExists == false) { Document index = new Document(CONSTANTS_FD_GLOBAL, 1); locks.createIndex(index, new IndexOptions().unique(true)); } }
public String ensureIndex(final IndexDefinition indexDefinition) { return execute(collection -> { Document indexOptions = indexDefinition.getIndexOptions(); IndexOptions ops = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition); if (indexOptions.containsKey(PARTIAL_FILTER_EXPRESSION_KEY)) { Assert.isInstanceOf(Document.class, indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY)); ops.partialFilterExpression(mapper.getMappedObject((Document) indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY), lookupPersistentEntity(type, collectionName))); } return collection.createIndex(indexDefinition.getIndexKeys(), ops); }); }
/** * Create TTL index * * @param idleTime idle time in seconds * @see https://docs.mongodb.com/manual/core/index-ttl/ */ private void createIndex(long idleTime) { try { this.sessions.createIndex( new Document(SESSION_TTL, 1), new IndexOptions() .expireAfter(idleTime, TimeUnit.SECONDS) .name(SESSION_INDEX_NAME)); } catch (MongoException ex) {//update idle time this.sessions.dropIndex(SESSION_INDEX_NAME); this.sessions.createIndex( new Document(SESSION_TTL, 1), new IndexOptions() .expireAfter(idleTime, TimeUnit.SECONDS) .name(SESSION_INDEX_NAME)); } }
public String ensureIndex(final IndexDefinition indexDefinition) { return execute(collection -> { Document indexOptions = indexDefinition.getIndexOptions(); IndexOptions ops = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition); if (indexOptions.containsKey(PARTIAL_FILTER_EXPRESSION_KEY)) { Assert.isInstanceOf(Document.class, indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY)); ops.partialFilterExpression(mapper.getMappedObject((Document) indexOptions.get(PARTIAL_FILTER_EXPRESSION_KEY), lookupPersistentEntity(type, collectionName))); } return collection.createIndex(indexDefinition.getIndexKeys(), ops); }); }
private <T> void createIndex(@Nullable final ClientSession clientSession, final MongoCollection<T> collection, final Document index, final IndexOptions indexOptions) { if (clientSession != null) { collection.createIndex(clientSession, index, indexOptions); } else { collection.createIndex(index, indexOptions); } }
private void createCollectionAndUniqueIndexIfNotExists(MongoDatabase db) { Document indexKeys = new Document(KEY_PROP_NAME, INDEX_SORT_ASC); IndexOptions indexOptions = new IndexOptions().unique(true).name("mongobeelock_key_idx"); db.getCollection(lockCollectionName).createIndex(indexKeys, indexOptions); }
@NotNull @Override public String createIndex(@NotNull Bson keys, @NotNull IndexOptions indexOptions) { return collection.createIndex(keys, indexOptions); }
/** * Ensure that expected collection indexes exist. * * @throws SiteWhereException */ protected void ensureIndexes() throws SiteWhereException { getMongoClient().getSchedulesCollection().createIndex(new Document(MongoSchedule.PROP_TOKEN, 1), new IndexOptions().unique(true)); getMongoClient().getScheduledJobsCollection().createIndex(new Document(MongoScheduledJob.PROP_TOKEN, 1), new IndexOptions().unique(true)); }