Example usage for com.mongodb.client.model IndexOptions IndexOptions

List of usage examples for com.mongodb.client.model IndexOptions IndexOptions

Introduction

In this page you can find the example usage for com.mongodb.client.model IndexOptions IndexOptions.

Prototype

IndexOptions

Source Link

Usage

From source file:it.av.wikipedia.client.util.MongoDBStorage.java

@Override
public void index(boolean unique, String... keys) {
    IndexOptions opt = new IndexOptions();
    opt.unique(unique);//www  .  j  a v  a  2  s.com
    Document indexBson = new Document();
    for (String key : keys) {
        indexBson.append(key, 1);
    }
    System.out.println(this.mongoCol.createIndex(indexBson, opt));
}

From source file:mongofx.js.api.Collection.java

License:Open Source License

public String createIndex(Bindings index, Bindings options) {
    return getCollection().createIndex(dbObjectFromMap(index), buildOptions(new IndexOptions(), options));
}

From source file:net.netzgut.integral.mongo.internal.services.MongoServiceImplementation.java

License:Apache License

private void setupIndexes(Class<? extends Serializable> entityClass, MongoCollection<Document> collection) {
    if (entityClass == null) {
        log.error("Entity Class can't be null");
        throw new IllegalArgumentException("Entity Class can't be null");
    }/*from   w ww  .java  2  s . c om*/

    if (collection == null) {
        log.error("Collection can't be null");
        throw new IllegalArgumentException("Collection can't be null");
    }

    findAllAnnotations(Index.class, entityClass).stream().filter(index -> Objects.nonNull(index.value())) //
            .filter(index -> index.value().isEmpty() == false) //
            .forEach(index -> {
                Document indexDocument = new Document();
                indexDocument.put(index.value(), index.direction());

                IndexOptions options = new IndexOptions();
                options.unique(index.unique());
                options.background(index.background());
                collection.createIndex(indexDocument, options);
            });

}

From source file:org.apache.drill.exec.store.mongo.MongoTestSuit.java

License:Apache License

private static void createDbAndCollections(String dbName, String collectionName, String indexFieldName) {
    MongoDatabase db = mongoClient.getDatabase(dbName);
    MongoCollection<Document> mongoCollection = db.getCollection(collectionName);
    if (mongoCollection == null) {
        db.createCollection(collectionName);
        mongoCollection = db.getCollection(collectionName);
    }/* w ww  .j  a va 2  s . c  o m*/
    IndexOptions indexOptions = new IndexOptions().unique(true).background(false).name(indexFieldName);
    Bson keys = Indexes.ascending(indexFieldName);
    mongoCollection.createIndex(keys, indexOptions);
}

From source file:org.apache.eagle.alert.engine.publisher.dedup.MongoDedupEventsStore.java

License:Apache License

private void init() {
    db = client.getDatabase(DB_NAME);// w  w  w.j av a2  s.c om
    stateCollection = db.getCollection(ALERT_STATE_COLLECTION);
    // dedup id index
    IndexOptions io = new IndexOptions().background(true).unique(true).name(DEDUP_ID + "_index");
    BsonDocument doc = new BsonDocument();
    doc.append(DEDUP_ID, new BsonInt32(1));
    stateCollection.createIndex(doc, io);
}

From source file:org.apache.eagle.alert.metadata.impl.MongoMetadataDaoImpl.java

License:Apache License

private void init() {
    db = client.getDatabase(DB_NAME);/*from   w ww  .jav  a2s  .  co m*/
    IndexOptions io = new IndexOptions().background(true).name("nameIndex");
    BsonDocument doc = new BsonDocument();
    doc.append("name", new BsonInt32(1));
    cluster = db.getCollection("clusters");
    cluster.createIndex(doc, io);
    {
        BsonDocument doc2 = new BsonDocument();
        doc2.append("streamId", new BsonInt32(1));
        schema = db.getCollection("schemas");
        schema.createIndex(doc2, io);
    }
    datasource = db.getCollection("datasources");
    datasource.createIndex(doc, io);
    policy = db.getCollection("policies");
    policy.createIndex(doc, io);
    publishment = db.getCollection("publishments");
    publishment.createIndex(doc, io);
    topologies = db.getCollection("topologies");
    topologies.createIndex(doc, io);

    publishmentType = db.getCollection("publishmentTypes");
    {
        IndexOptions io1 = new IndexOptions().background(true).unique(true).name("pubTypeIndex");
        BsonDocument doc1 = new BsonDocument();
        doc1.append("type", new BsonInt32(1));
        publishmentType.createIndex(doc1, io1);
    }

    alerts = db.getCollection("alerts");
    {
        IndexOptions io1 = new IndexOptions().background(true).unique(true).name("alertIndex");
        BsonDocument doc1 = new BsonDocument();
        doc1.append("alertId", new BsonInt32(1));
        alerts.createIndex(doc1, io1);
    }

    // below is for schedule_specs and its splitted collections
    BsonDocument doc1 = new BsonDocument();
    IndexOptions io1 = new IndexOptions().background(true).name("versionIndex");
    doc1.append("version", new BsonInt32(1));
    scheduleStates = getCollection("schedule_specs");
    scheduleStates.createIndex(doc1, io1);

    spoutSpecs = getCollection("spoutSpecs");
    {
        IndexOptions ioInternal = new IndexOptions().background(true).name("topologyIdIndex");
        BsonDocument docInternal = new BsonDocument();
        docInternal.append("topologyId", new BsonInt32(1));
        spoutSpecs.createIndex(docInternal, ioInternal);
    }

    alertSpecs = getCollection("alertSpecs");
    {
        IndexOptions ioInternal = new IndexOptions().background(true).name("topologyNameIndex");
        BsonDocument docInternal = new BsonDocument();
        docInternal.append("topologyName", new BsonInt32(1));
        alertSpecs.createIndex(docInternal, ioInternal);
    }

    groupSpecs = getCollection("groupSpecs");
    groupSpecs.createIndex(doc1, io1);

    publishSpecs = getCollection("publishSpecs");
    publishSpecs.createIndex(doc1, io1);

    policySnapshots = getCollection("policySnapshots");
    policySnapshots.createIndex(doc1, io);

    streamSnapshots = getCollection("streamSnapshots");
    streamSnapshots.createIndex(doc1, io);

    monitoredStreams = getCollection("monitoredStreams");
    monitoredStreams.createIndex(doc1, io);

    assignments = getCollection("assignments");
    assignments.createIndex(doc1, io1);
}

From source file:org.apache.sling.nosql.mongodb.resourceprovider.impl.MongoDBNoSqlAdapter.java

License:Apache License

@Override
public void createIndexDefinitions() {
    // create index on parent path field (if it does not exist yet)
    try {//  ww  w . j  ava 2s  .  c  om
        Document parenPathtIndex = new Document(PN_PARENT_PATH, 1);
        this.collection.createIndex(parenPathtIndex);
    } catch (DuplicateKeyException ex) {
        // index already exists, ignore
    } catch (Throwable ex) {
        log.error("Unable to create index on " + PN_PARENT_PATH + ": " + ex.getMessage(), ex);
    }

    // create unique index on path field (if it does not exist yet)
    try {
        Document pathIndex = new Document(PN_PATH, 1);
        IndexOptions idxOptions = new IndexOptions();
        idxOptions.unique(true);
        this.collection.createIndex(pathIndex, idxOptions);
    } catch (DuplicateKeyException ex) {
        // index already exists, ignore
    } catch (Throwable ex) {
        log.error("Unable to create unique index on " + PN_PATH + ": " + ex.getMessage(), ex);
    }
}

From source file:org.axonframework.mongo.eventsourcing.eventstore.AbstractMongoEventStorageStrategy.java

License:Apache License

@Override
public void ensureIndexes(MongoCollection<Document> eventsCollection,
        MongoCollection<Document> snapshotsCollection) {
    eventsCollection.createIndex(// ww w  .ja v a2 s.c om
            new BasicDBObject(eventConfiguration.aggregateIdentifierProperty(), ORDER_ASC)
                    .append(eventConfiguration.sequenceNumberProperty(), ORDER_ASC),
            new IndexOptions().unique(true).name("uniqueAggregateIndex"));

    eventsCollection.createIndex(
            new BasicDBObject(eventConfiguration.timestampProperty(), ORDER_ASC)
                    .append(eventConfiguration.sequenceNumberProperty(), ORDER_ASC),
            new IndexOptions().unique(false).name("orderedEventStreamIndex"));
    snapshotsCollection.createIndex(
            new BasicDBObject(eventConfiguration.aggregateIdentifierProperty(), ORDER_ASC)
                    .append(eventConfiguration.sequenceNumberProperty(), ORDER_ASC),
            new IndexOptions().unique(true).name("uniqueAggregateIndex"));
}

From source file:org.axonframework.mongo.eventsourcing.eventstore.documentpercommit.DocumentPerCommitStorageStrategy.java

License:Apache License

@Override
public void ensureIndexes(MongoCollection<Document> eventsCollection,
        MongoCollection<Document> snapshotsCollection) {
    super.ensureIndexes(eventsCollection, snapshotsCollection);
    //prevents duplicate commits
    eventsCollection.createIndex(//from  www  .  j ava2 s.c  o  m
            new BasicDBObject(eventConfiguration().aggregateIdentifierProperty(), ORDER_ASC)
                    .append(commitEntryConfiguration.firstSequenceNumberProperty(), ORDER_ASC),
            new IndexOptions().unique(true).name("uniqueAggregateStartIndex"));
}

From source file:org.axonframework.mongo.eventsourcing.tokenstore.MongoTokenStore.java

License:Apache License

/**
 * Creates the indexes required to work with the TokenStore.
 *//* w w w.ja  v  a 2  s .  c o m*/
@PostConstruct
public void ensureIndexes() {
    mongoTemplate.trackingTokensCollection().createIndex(Indexes.ascending("processorName", "segment"),
            new IndexOptions().unique(true));
}