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:com.github.achatain.catalog.dao.impl.CollectionDaoImpl.java

License:Open Source License

@Override
public void createIndex(final String userId, final String collectionId, final String fieldName) {
    getDatabase(userId).getCollection(collectionId).createIndex(
            Indexes.ascending(ATTRIBUTES_PREFIX + fieldName),
            new IndexOptions().name(fieldName).background(true));
}

From source file:com.helger.as2lib.partner.mongodb.MongoDBPartnershipFactory.java

License:Apache License

public MongoDBPartnershipFactory(final MongoCollection<Document> partnerships, final Logger logger) {
    this.logger = logger;
    partnerships.createIndex(new Document(NAME_KEY, Integer.valueOf(1)), new IndexOptions().unique(true));
    this.partnerships = partnerships;
}

From source file:com.helion3.prism.storage.mongodb.MongoStorageAdapter.java

License:MIT License

/**
 * Establish connections to the database
 *
 * @return Whether we could connect properly
 *//*  ww w  .jav  a 2 s  .co  m*/
@Override
public boolean connect() throws Exception {
    String host = Prism.getConfig().getNode("db", "mongo", "host").getString();
    int port = Prism.getConfig().getNode("db", "mongo", "port").getInt();

    mongoClient = new MongoClient(host, port);

    // @todo support auth: boolean auth = db.authenticate(myUserName, myPassword);

    // Connect to the database
    database = mongoClient.getDatabase(databaseName);

    // Create indexes
    try {
        getCollection(collectionEventRecordsName).createIndex(new Document("Location.X", 1)
                .append("Location.Z", 1).append("Location.Y", 1).append("Created", -1));
        getCollection(collectionEventRecordsName)
                .createIndex(new Document("Created", -1).append("EventName", 1));

        // TTL
        IndexOptions options = new IndexOptions().expireAfter(0L, TimeUnit.SECONDS);
        getCollection(collectionEventRecordsName).createIndex(new Document("Expires", 1), options);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

    return false;
}

From source file:com.px100systems.data.plugin.storage.mongo.MongoDatabaseStorage.java

License:Open Source License

public void createEntity(String unitName, Collection<String> indexedFields,
        List<CompoundIndexDescriptor> compoundIndexes) {
    MongoDatabase db = mongoClient.getDatabase(databaseName);
    db.createCollection(unitName);//from   w ww .j  a  va 2s  .  co m

    List<IndexModel> indexes = new ArrayList<>();

    for (CompoundIndexDescriptor ci : compoundIndexes) {
        Map<String, Object> fields = new HashMap<>();
        for (CompoundIndexDescriptor.Field field : ci.getFields())
            fields.put(field.getName(), field.isDescending() ? "-1" : "1");
        indexes.add(new IndexModel(new Document(fields),
                new IndexOptions().name(indexName(ci.getName())).background(true)));
    }

    for (String idx : indexedFields)
        indexes.add(
                new IndexModel(new Document(idx, 1), new IndexOptions().name(indexName(idx)).background(true)));

    db.getCollection(unitName).createIndexes(indexes);
}

From source file:com.px100systems.data.plugin.storage.mongo.MongoDatabaseStorage.java

License:Open Source License

public void addIndexes(String unitName, List<String> newIndexes,
        List<CompoundIndexDescriptor> newCompoundIndexes) {
    MongoDatabase db = mongoClient.getDatabase(databaseName);

    List<IndexModel> indexes = new ArrayList<>();

    for (CompoundIndexDescriptor ci : newCompoundIndexes) {
        Map<String, Object> fields = new HashMap<>();
        for (CompoundIndexDescriptor.Field field : ci.getFields())
            fields.put(field.getName(), field.isDescending() ? "-1" : "1");
        indexes.add(new IndexModel(new Document(fields),
                new IndexOptions().name(indexName(ci.getName())).background(true)));
    }// w  ww. j a  v  a2 s .c o  m

    for (String idx : newIndexes)
        indexes.add(
                new IndexModel(new Document(idx, 1), new IndexOptions().name(indexName(idx)).background(true)));

    db.getCollection(unitName).createIndexes(indexes);
}

From source file:com.redhat.jielicious.storage.mongo.handler.MongoStorageHandler.java

License:Open Source License

private void putAll(String body, SecurityContext context, String systemId, String agentId, String jvmId,
        final String namespace, AsyncResponse asyncResponse, final String collectionSuffix) {
    if (!namespaceSet.contains(namespace)) {
        namespaceSet.add(namespace);/*from   w w  w  .ja v a 2 s  .c o m*/
    }

    if (!indexSet.contains(namespace + collectionSuffix)) {
        ThermostatMongoStorage.getDatabase().getCollection(namespace + collectionSuffix)
                .createIndex(Indexes.descending("systemId"), new IndexOptions().background(true));
        ThermostatMongoStorage.getDatabase().getCollection(namespace + collectionSuffix)
                .createIndex(Indexes.descending("agentId"), new IndexOptions().background(true));
        ThermostatMongoStorage.getDatabase().getCollection(namespace + collectionSuffix)
                .createIndex(Indexes.descending("jvmId"), new IndexOptions().background(true));
        indexSet.add(namespace + collectionSuffix);
    }

    try {
        BasicDBList inputList = (BasicDBList) JSON.parse(body);

        final List<Document> items = new ArrayList<>();
        for (Object item : inputList) {
            items.add(Document.parse(new DocumentBuilder(item.toString())
                    .addTags(context.getUserPrincipal().getName()).addId("systemId", systemId)
                    .addId("agentId", agentId).addId("jvmId", jvmId).build()));
        }

        TimedRequest<Boolean> timedRequest = new TimedRequest<>();

        Boolean response = timedRequest.run(new TimedRequest.TimedRunnable<Boolean>() {
            @Override
            public Boolean run() {
                try {
                    ThermostatMongoStorage.getDatabase().getCollection(namespace + collectionSuffix)
                            .insertMany(items);
                } catch (Exception e) {
                    return Boolean.FALSE;
                }
                return Boolean.TRUE;
            }
        });

        asyncResponse.resume(Response.status(Response.Status.OK).entity("PUT: " + response.toString()).build());
    } catch (Exception e) {
        asyncResponse.resume(Response.status(Response.Status.BAD_REQUEST).build());
    }
}

From source file:com.sitewhere.asset.persistence.mongodb.MongoAssetManagement.java

License:Open Source License

/**
 * Ensure that expected collection indexes exist.
 * //from   w w  w .  java 2s .c  om
 * @throws SiteWhereException
 */
protected void ensureIndexes() throws SiteWhereException {
    getMongoClient().getAssetTypesCollection().createIndex(Indexes.ascending(MongoAssetType.PROP_TOKEN),
            new IndexOptions().unique(true));
    getMongoClient().getAssetsCollection().createIndex(Indexes.ascending(MongoAsset.PROP_TOKEN),
            new IndexOptions().unique(true));
}

From source file:com.sitewhere.batch.persistence.mongodb.MongoBatchManagement.java

License:Open Source License

/**
 * Ensure that expected collection indexes exist.
 * //from   www.ja va 2s .c o m
 * @throws SiteWhereException
 */
protected void ensureIndexes() throws SiteWhereException {
    // Batch operation indexes.
    getMongoClient().getBatchOperationsCollection().createIndex(new Document(MongoBatchOperation.PROP_TOKEN, 1),
            new IndexOptions().unique(true));
    getMongoClient().getBatchOperationElementsCollection()
            .createIndex(new Document(MongoBatchElement.PROP_BATCH_OPERATION_ID, 1)
                    .append(MongoBatchElement.PROP_DEVICE_ID, 1), new IndexOptions().unique(true));
}

From source file:com.sitewhere.device.persistence.mongodb.MongoDeviceManagement.java

License:Open Source License

/**
 * Ensure that expected collection indexes exist.
 * /*  w w w .j av  a 2  s .  c o m*/
 * @throws SiteWhereException
 */
protected void ensureIndexes() throws SiteWhereException {
    // Area indexes.
    getMongoClient().getAreasCollection().createIndex(new Document(MongoArea.PROP_TOKEN, 1),
            new IndexOptions().unique(true));

    // Device-type-related indexes.
    getMongoClient().getDeviceTypesCollection().createIndex(new Document(MongoDeviceType.PROP_TOKEN, 1),
            new IndexOptions().unique(true));
    getMongoClient().getDeviceStatusesCollection().createIndex(
            new Document(MongoDeviceStatus.PROP_DEVICE_TYPE_ID, 1).append(MongoDeviceStatus.PROP_CODE, 1),
            new IndexOptions().unique(true));

    // Devices.
    getMongoClient().getDevicesCollection().createIndex(new Document(MongoDevice.PROP_TOKEN, 1),
            new IndexOptions().unique(true));

    // Device assignments.
    getMongoClient().getDeviceAssignmentsCollection()
            .createIndex(new Document(MongoDeviceAssignment.PROP_TOKEN, 1), new IndexOptions().unique(true));
    getMongoClient().getDeviceAssignmentsCollection()
            .createIndex(new Document(MongoDeviceAssignment.PROP_AREA_ID, 1)
                    .append(MongoDeviceAssignment.PROP_ASSET_ID, 1)
                    .append(MongoDeviceAssignment.PROP_STATUS, 1));

    // Device group indexes.
    getMongoClient().getDeviceGroupsCollection().createIndex(new Document(MongoDeviceGroup.PROP_TOKEN, 1),
            new IndexOptions().unique(true));
    getMongoClient().getDeviceGroupsCollection().createIndex(new Document(MongoDeviceGroup.PROP_ROLES, 1));

    // Device group element indexes.
    getMongoClient().getGroupElementsCollection()
            .createIndex(new Document(MongoDeviceGroupElement.PROP_GROUP_ID, 1)
                    .append(MongoDeviceGroupElement.PROP_DEVICE_ID, 1), new IndexOptions().unique(true));
    getMongoClient().getGroupElementsCollection()
            .createIndex(new Document(MongoDeviceGroupElement.PROP_GROUP_ID, 1)
                    .append(MongoDeviceGroupElement.PROP_ROLES, 1));
}

From source file:com.sitewhere.event.persistence.mongodb.MongoDeviceEventManagement.java

License:Open Source License

/**
 * Ensure that expected collection indexes exist.
 * //  w  w  w .  ja va  2s  . co  m
 * @throws SiteWhereException
 */
protected void ensureIndexes() throws SiteWhereException {
    getMongoClient().getEventsCollection().createIndex(new BasicDBObject(MongoDeviceEvent.PROP_ALTERNATE_ID, 1),
            new IndexOptions().unique(true).sparse(true));
    getMongoClient().getEventsCollection()
            .createIndex(new BasicDBObject(MongoDeviceEvent.PROP_DEVICE_ASSIGNMENT_ID, 1)
                    .append(MongoDeviceEvent.PROP_EVENT_DATE, -1).append(MongoDeviceEvent.PROP_EVENT_TYPE, 1));
    getMongoClient().getEventsCollection().createIndex(new BasicDBObject(MongoDeviceEvent.PROP_AREA_ID, 1)
            .append(MongoDeviceEvent.PROP_EVENT_DATE, -1).append(MongoDeviceEvent.PROP_EVENT_TYPE, 1));
}