List of usage examples for com.mongodb.async.client MongoCollection listIndexes
ListIndexesIterable<Document> listIndexes();
From source file:org.openo.commontosca.inventory.core.mongo.handler.model.MongoModelInsertRequestHandler.java
License:Apache License
/** * * /*from w w w .ja va 2 s . c o m*/ * @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 ensureDataCollection(Update request) throws Exception { ValueMap model = request.getInventory().model().find().byName(request.getModelName()).execute().asOne(); if (model != null) { MongoCollection<Document> dataCollection = this.getDatabase(request) .getCollection(MongoUtils.getModelCollectionName(request.getModelName())); List<ValueMap> allIndexes = DeferredSingleResult.<List<ValueMap>>execute(deferred -> { dataCollection.listIndexes().map(document -> { return ValueMap.wrap(document); }).into(new ArrayList<>(), deferred); });//from w ww.j a v a2 s . com String primaryKey = "_id"; String displayAttribute = model.requireValue(ModelKey.DISPLAY_ATTRUBITE); this.ensureDataCollectionIndex(dataCollection, allIndexes, DataKey.CREATE_TIME.getKeyName(), true, false); this.ensureDataCollectionIndex(dataCollection, allIndexes, DataKey.LAST_MODIFIED.getKeyName(), true, false); model.requireValue(ModelKey.ATTRIBUTES).stream().map(obj -> { return ValueAccess.wrap(obj).as(ValueMap.class); }).forEach(attribute -> { boolean isUniqueIndex = attribute.optValue(ModelKey.UNIQUE, false); boolean isReferenceTypeIndex = ValueType .parse(attribute.requireValue(ModelKey.TYPE)) == ValueType.REFERENCE; boolean isDisplayAttributeIndex = attribute.requireValue(ModelKey.NAME).equals(displayAttribute); boolean isPrimaryKey = attribute.requireValue(ModelKey.NAME).equals(primaryKey); if (!isPrimaryKey) { boolean hasIndex = isUniqueIndex || isReferenceTypeIndex || isDisplayAttributeIndex; try { this.ensureDataCollectionIndex(dataCollection, allIndexes, attribute.requireValue(ModelKey.NAME), hasIndex, isUniqueIndex || isPrimaryKey); } catch (Exception e) { throw Toolkits.toInventoryException(e); } } }); } else { throw new IllegalArgumentException("No such model: " + request.getModelName()); } }