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

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

Introduction

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

Prototype

public IndexModel(final Bson keys, final IndexOptions options) 

Source Link

Document

Construct an instance with the given keys and options.

Usage

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 .jav a  2  s .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  w w .  j a  v  a 2 s . co  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:org.eclipse.ditto.services.utils.persistence.mongo.indices.Index.java

License:Open Source License

/**
 * Creates a new {@link IndexModel}, which can be used for creating indices using MongoDB Java drivers.
 *
 * @return the created {@link IndexModel}
 */// w  ww .j  a  va2s  . co  m
public IndexModel toIndexModel() {
    final IndexOptions options = new IndexOptions().name(name).unique(unique).sparse(sparse)
            .background(background);

    if (!partialFilterExpression.isEmpty()) {
        options.partialFilterExpression(partialFilterExpression);
    }

    getExpireAfterSeconds().ifPresent(n -> options.expireAfter(n, TimeUnit.SECONDS));

    return new IndexModel(keys, options);
}

From source file:org.wso2.extension.siddhi.store.mongodb.util.MongoTableUtils.java

License:Open Source License

/**
 * Utility method which can be used to check if the given primary key is valid i.e. non empty
 * and is made up of attributes and return an index model when PrimaryKey is valid.
 *
 * @param primaryKey     the PrimaryKey annotation which contains the primary key attributes.
 * @param attributeNames List containing names of the attributes.
 * @return List of String with primary key attributes.
 *///from   w  w  w. ja v  a2  s . co  m
public static IndexModel extractPrimaryKey(Annotation primaryKey, List<String> attributeNames) {
    if (primaryKey == null) {
        return null;
    }
    Document primaryKeyIndex = new Document();
    primaryKey.getElements().forEach(element -> {
        if (!isEmpty(element.getValue()) && attributeNames.contains(element.getValue())) {
            primaryKeyIndex.append(element.getValue(), 1);
        } else {
            throw new SiddhiAppCreationException(
                    "Annotation '" + primaryKey.getName() + "' contains " + "value '" + element.getValue()
                            + "' which is not present in the attributes of the " + "Event Table.");
        }
    });
    return new IndexModel(primaryKeyIndex, new IndexOptions().unique(true));
}