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);
}
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);
}
}
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;
}
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);
}
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);
}
}
});
}
Aggregations