List of usage examples for com.mongodb.async.client MongoCollection createIndex
void createIndex(ClientSession clientSession, Bson key, SingleResultCallback<String> callback);
From source file:io.vertx.ext.mongo.impl.MongoClientImpl.java
License:Open Source License
@Override public io.vertx.ext.mongo.MongoClient createIndexWithOptions(String collection, JsonObject key, IndexOptions options, Handler<AsyncResult<Void>> resultHandler) { requireNonNull(collection, "collection cannot be null"); requireNonNull(key, "fieldName cannot be null"); requireNonNull(resultHandler, "resultHandler cannot be null"); MongoCollection<JsonObject> coll = getCollection(collection); com.mongodb.client.model.IndexOptions driverOpts = new com.mongodb.client.model.IndexOptions() .background(options.isBackground()).unique(options.isUnique()).name(options.getName()) .sparse(options.isSparse()).expireAfter(options.getExpireAfter(TimeUnit.SECONDS), TimeUnit.SECONDS) .version(options.getVersion()).weights(toBson(options.getWeights())) .defaultLanguage(options.getDefaultLanguage()).languageOverride(options.getLanguageOverride()) .textVersion(options.getTextVersion()).sphereVersion(options.getSphereVersion()) .bits(options.getBits()).min(options.getMin()).max(options.getMax()) .bucketSize(options.getBucketSize()).storageEngine(toBson(options.getStorageEngine())) .partialFilterExpression(toBson(options.getPartialFilterExpression())); coll.createIndex(wrap(key), driverOpts, wrapCallback(toVoidAsyncResult(resultHandler))); return this; }
From source file:org.openo.commontosca.inventory.core.mongo.handler.model.MongoModelInsertRequestHandler.java
License:Apache License
/** * * //from w ww . j a va2 s . com * @param request * @throws Exception */ private void ensureModelCollection(Insert request) throws Exception { MongoCollection<Document> modelCollection = this.getDatabase(request) .getCollection(Model.MODEL_DEFAULT_COLLECTION_NAME); boolean noneMatch = DeferredSingleResult.<List<Document>>execute(deferred -> { modelCollection.listIndexes().into(new ArrayList<>(), deferred); }).stream().map(d -> { return ValueMap.wrap(d); }).noneMatch(v -> { return v.requireMap("key").get(ModelKey.NAME) != null; }); if (noneMatch) { DeferredSingleResult.<String>execute(deferred -> { modelCollection.createIndex(Indexes.ascending(ModelKey.NAME.getKeyName()), new IndexOptions().unique(true), deferred); }); } }
From source file:org.openo.commontosca.inventory.core.mongo.handler.model.MongoModelUpdateRequestHandler.java
License:Apache License
private void ensureDataCollectionIndex(MongoCollection<Document> dataCollection, List<ValueMap> allIndexes, String attributeName, boolean hasIndex, boolean uniqueIndex) throws Exception { Optional<ValueMap> findIndex = allIndexes.stream().filter(map -> { return map.requireMap("key").get(attributeName) != null; }).findAny();//from w ww . ja va2s.c om if (findIndex.isPresent()) { ValueMap theIndex = findIndex.get(); String indexName = theIndex.requireString("name"); if (hasIndex) { boolean oldIsUnique = theIndex.optBoolean("unique", false); if (uniqueIndex != oldIsUnique) { DeferredSingleResult.<Void>execute(deferred -> { dataCollection.dropIndex(indexName, deferred); }); allIndexes.removeIf(map -> { return map.requireMap("key").get(attributeName) != null; }); this.ensureDataCollectionIndex(dataCollection, allIndexes, attributeName, hasIndex, uniqueIndex); } } else { DeferredSingleResult.<Void>execute(deferred -> { dataCollection.dropIndex(indexName, deferred); }); } } else if (hasIndex) { DeferredSingleResult.<String>execute(deferred -> { dataCollection.createIndex(Indexes.ascending(attributeName), new IndexOptions().unique(uniqueIndex), deferred); }); } }