Search in sources :

Example 6 with IndexOptions

use of com.mongodb.client.model.IndexOptions in project drill by apache.

the class MongoTestSuit method createDbAndCollections.

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);
    }
    IndexOptions indexOptions = new IndexOptions().unique(true).background(false).name(indexFieldName);
    Bson keys = Indexes.ascending(indexFieldName);
    mongoCollection.createIndex(keys, indexOptions);
}
Also used : IndexOptions(com.mongodb.client.model.IndexOptions) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase) Bson(org.bson.conversions.Bson)

Example 7 with IndexOptions

use of com.mongodb.client.model.IndexOptions in project sling by apache.

the class MongoDBNoSqlAdapter method createIndexDefinitions.

@Override
public void createIndexDefinitions() {
    // create index on parent path field (if it does not exist yet)
    try {
        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);
    }
}
Also used : IndexOptions(com.mongodb.client.model.IndexOptions) Document(org.bson.Document) DuplicateKeyException(com.mongodb.DuplicateKeyException)

Example 8 with IndexOptions

use of com.mongodb.client.model.IndexOptions in project presto by prestodb.

the class MongoSession method getTableMetadata.

// Internal Schema management
private Document getTableMetadata(SchemaTableName schemaTableName) throws TableNotFoundException {
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();
    MongoDatabase db = client.getDatabase(schemaName);
    MongoCollection<Document> schema = db.getCollection(schemaCollection);
    Document doc = schema.find(new Document(TABLE_NAME_KEY, tableName)).first();
    if (doc == null) {
        if (!collectionExists(db, tableName)) {
            throw new TableNotFoundException(schemaTableName);
        } else {
            Document metadata = new Document(TABLE_NAME_KEY, tableName);
            metadata.append(FIELDS_KEY, guessTableFields(schemaTableName));
            schema.createIndex(new Document(TABLE_NAME_KEY, 1), new IndexOptions().unique(true));
            schema.insertOne(metadata);
            return metadata;
        }
    }
    return doc;
}
Also used : TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) IndexOptions(com.mongodb.client.model.IndexOptions) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 9 with IndexOptions

use of com.mongodb.client.model.IndexOptions in project presto by prestodb.

the class MongoSession method createTableMetadata.

private void createTableMetadata(SchemaTableName schemaTableName, List<MongoColumnHandle> columns) throws TableNotFoundException {
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();
    MongoDatabase db = client.getDatabase(schemaName);
    Document metadata = new Document(TABLE_NAME_KEY, tableName);
    ArrayList<Document> fields = new ArrayList<>();
    if (!columns.stream().anyMatch(c -> c.getName().equals("_id"))) {
        fields.add(new MongoColumnHandle("_id", OBJECT_ID, true).getDocument());
    }
    fields.addAll(columns.stream().map(MongoColumnHandle::getDocument).collect(toList()));
    metadata.append(FIELDS_KEY, fields);
    MongoCollection<Document> schema = db.getCollection(schemaCollection);
    schema.createIndex(new Document(TABLE_NAME_KEY, 1), new IndexOptions().unique(true));
    schema.insertOne(metadata);
}
Also used : Document(org.bson.Document) Arrays(java.util.Arrays) LoadingCache(com.google.common.cache.LoadingCache) TypeManager(com.facebook.presto.spi.type.TypeManager) Date(java.util.Date) MongoDatabase(com.mongodb.client.MongoDatabase) BIGINT(com.facebook.presto.spi.type.BigintType.BIGINT) SchemaTableName(com.facebook.presto.spi.SchemaTableName) BOOLEAN(com.facebook.presto.spi.type.BooleanType.BOOLEAN) SchemaNotFoundException(com.facebook.presto.spi.SchemaNotFoundException) Map(java.util.Map) TypeSignatureParameter(com.facebook.presto.spi.type.TypeSignatureParameter) StandardTypes(com.facebook.presto.spi.type.StandardTypes) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) CacheLoader(com.google.common.cache.CacheLoader) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) Domain(com.facebook.presto.spi.predicate.Domain) List(java.util.List) FindIterable(com.mongodb.client.FindIterable) Optional(java.util.Optional) CacheBuilder(com.google.common.cache.CacheBuilder) TypeSignature(com.facebook.presto.spi.type.TypeSignature) IntStream(java.util.stream.IntStream) DOUBLE(com.facebook.presto.spi.type.DoubleType.DOUBLE) MongoCollection(com.mongodb.client.MongoCollection) Slice(io.airlift.slice.Slice) Logger(io.airlift.log.Logger) OBJECT_ID(com.facebook.presto.mongodb.ObjectIdType.OBJECT_ID) MINUTES(java.util.concurrent.TimeUnit.MINUTES) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) MongoCursor(com.mongodb.client.MongoCursor) Verify.verify(com.google.common.base.Verify.verify) Type(com.facebook.presto.spi.type.Type) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) Objects.requireNonNull(java.util.Objects.requireNonNull) TIMESTAMP(com.facebook.presto.spi.type.TimestampType.TIMESTAMP) Throwables(com.google.common.base.Throwables) Range(com.facebook.presto.spi.predicate.Range) IndexOptions(com.mongodb.client.model.IndexOptions) ExecutionException(java.util.concurrent.ExecutionException) VarcharType.createUnboundedVarcharType(com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType) Collectors.toList(java.util.stream.Collectors.toList) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) ColumnHandle(com.facebook.presto.spi.ColumnHandle) MongoClient(com.mongodb.MongoClient) DeleteResult(com.mongodb.client.result.DeleteResult) ObjectId(org.bson.types.ObjectId) HOURS(java.util.concurrent.TimeUnit.HOURS) VisibleForTesting(com.google.common.annotations.VisibleForTesting) IndexOptions(com.mongodb.client.model.IndexOptions) ArrayList(java.util.ArrayList) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 10 with IndexOptions

use of com.mongodb.client.model.IndexOptions in project mongo-java-driver by mongodb.

the class GridFSIndexCheckImpl method checkChunksIndex.

private void checkChunksIndex(final SingleResultCallback<Void> callback) {
    final Document chunksIndex = new Document("files_id", 1).append("n", 1);
    hasIndex(chunksCollection.withReadPreference(primary()), chunksIndex, new SingleResultCallback<Boolean>() {

        @Override
        public void onResult(final Boolean result, final Throwable t) {
            if (t != null) {
                callback.onResult(null, t);
            } else if (!result) {
                chunksCollection.createIndex(chunksIndex, new IndexOptions().unique(true), new SingleResultCallback<String>() {

                    @Override
                    public void onResult(final String result, final Throwable t) {
                        if (t != null) {
                            callback.onResult(null, t);
                        } else {
                            callback.onResult(null, null);
                        }
                    }
                });
            } else {
                callback.onResult(null, null);
            }
        }
    });
}
Also used : IndexOptions(com.mongodb.client.model.IndexOptions) Document(org.bson.Document)

Aggregations

IndexOptions (com.mongodb.client.model.IndexOptions)11 Document (org.bson.Document)11 Test (org.junit.Test)4 MongoDatabase (com.mongodb.client.MongoDatabase)3 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)2 ArrayList (java.util.ArrayList)2 OBJECT_ID (com.facebook.presto.mongodb.ObjectIdType.OBJECT_ID)1 ColumnHandle (com.facebook.presto.spi.ColumnHandle)1 SchemaNotFoundException (com.facebook.presto.spi.SchemaNotFoundException)1 SchemaTableName (com.facebook.presto.spi.SchemaTableName)1 Domain (com.facebook.presto.spi.predicate.Domain)1 Range (com.facebook.presto.spi.predicate.Range)1 TupleDomain (com.facebook.presto.spi.predicate.TupleDomain)1 BIGINT (com.facebook.presto.spi.type.BigintType.BIGINT)1 BOOLEAN (com.facebook.presto.spi.type.BooleanType.BOOLEAN)1 DOUBLE (com.facebook.presto.spi.type.DoubleType.DOUBLE)1 NamedTypeSignature (com.facebook.presto.spi.type.NamedTypeSignature)1 StandardTypes (com.facebook.presto.spi.type.StandardTypes)1 TIMESTAMP (com.facebook.presto.spi.type.TimestampType.TIMESTAMP)1 Type (com.facebook.presto.spi.type.Type)1