Java源码示例:io.vertx.ext.mongo.IndexOptions
示例1
@BeforeEach
void setup(Vertx vertx, VertxTestContext testContext) {
JsonObject mongoConfig = new JsonObject()
.put("host", "localhost")
.put("port", 27017)
.put("db_name", "profiles");
mongoClient = MongoClient.createShared(vertx, mongoConfig);
mongoClient
.rxCreateIndexWithOptions("user", new JsonObject().put("username", 1), new IndexOptions().unique(true))
.andThen(mongoClient.rxCreateIndexWithOptions("user", new JsonObject().put("deviceId", 1), new IndexOptions().unique(true)))
.andThen(dropAllUsers())
.flatMapSingle(res -> vertx.rxDeployVerticle(new UserProfileApiVerticle()))
.subscribe(
ok -> testContext.completeNow(),
testContext::failNow);
}
示例2
@Override
public Future<Void> start() {
// initialize indexes
return CompositeFuture.all(
mongoDbCallExecutor.createCollectionIndex(config.getCollectionName(),
new JsonObject().put(
RegistryManagementConstants.FIELD_PAYLOAD_TENANT_ID, 1),
new IndexOptions().unique(true), INDEX_CREATION_MAX_RETRIES),
// add unique index predicate for tenants with field of trusted-ca inside tenant object
// to ensure that no tenants have the same trusted-ca but only if trusted-ca exist
// to to deal with creating/updating tenants containing unique ids.
mongoDbCallExecutor.createCollectionIndex(config.getCollectionName(),
new JsonObject().put(RegistryManagementConstants.FIELD_TENANT + "." +
RegistryManagementConstants.FIELD_PAYLOAD_TRUSTED_CA + "." +
RegistryManagementConstants.FIELD_PAYLOAD_SUBJECT_DN, 1),
new IndexOptions().unique(true)
.partialFilterExpression(new JsonObject().put(
RegistryManagementConstants.FIELD_TENANT + "." +
RegistryManagementConstants.FIELD_PAYLOAD_TRUSTED_CA,
new JsonObject().put("$exists", true))),
INDEX_CREATION_MAX_RETRIES))
.map(ok -> null);
}
示例3
private com.mongodb.client.model.IndexOptions mongoIndexOptions(IndexOptions options) {
return new com.mongodb.client.model.IndexOptions()
.background(options.isBackground())
.unique(options.isUnique())
.name(options.getName())
.sparse(options.isSparse())
.expireAfter(options.getExpireAfter(TimeUnit.SECONDS), TimeUnit.SECONDS)
.version(options.getVersion())
.weights(toBson(options.getWeights()))
.defaultLanguage(options.getDefaultLanguage())
.languageOverride(options.getLanguageOverride())
.textVersion(options.getTextVersion())
.sphereVersion(options.getSphereVersion())
.bits(options.getBits())
.min(options.getMin())
.max(options.getMax())
.bucketSize(options.getBucketSize())
.storageEngine(toBson(options.getStorageEngine()))
.partialFilterExpression(toBson(options.getPartialFilterExpression()));
}
示例4
/**
* Creates an index on a collection.
*
* @param collectionName The name of the collection.
* @param keys The keys to be indexed.
* @param options The options for configuring the index (may be {@code null}).
* @param noOfRetries The number of retries in case the index creation fails.
* @return A future indicating the outcome of the index creation operation.
* @throws NullPointerException if any of the parameters except options are {@code null}.
*/
public Future<Void> createCollectionIndex(
final String collectionName,
final JsonObject keys,
final IndexOptions options,
final int noOfRetries) {
final Promise<Void> indexCreationPromise = Promise.promise();
LOG.debug("creating index [collection: {}]", collectionName);
mongoClient.createIndexWithOptions(collectionName, keys, options, res -> {
if (res.succeeded()) {
LOG.debug("successfully created index [collection: {}]", collectionName);
indexCreationPromise.complete();
} else {
if (noOfRetries > 0) {
LOG.error("failed to create index [collection: {}], scheduling new attempt to create index",
collectionName, res.cause());
vertx.setTimer(INDEX_CREATION_RETRY_INTERVAL_IN_MS,
id -> createCollectionIndex(collectionName, keys, options, noOfRetries - 1));
} else {
LOG.error("failed to create index [collection: {}]", collectionName, res.cause());
indexCreationPromise.fail(res.cause());
}
}
});
return indexCreationPromise.future();
}
示例5
@Override
public Future<Void> start() {
final Promise<Void> startPromise = Promise.promise();
mongoDbCallExecutor.createCollectionIndex(
config.getCollectionName(),
new JsonObject().put(RegistrationConstants.FIELD_PAYLOAD_TENANT_ID, 1)
.put(RegistrationConstants.FIELD_PAYLOAD_DEVICE_ID, 1),
new IndexOptions().unique(true),
INDEX_CREATION_MAX_RETRIES)
.onComplete(startPromise);
return startPromise.future();
}
示例6
private CompositeFuture createIndices() {
final String authIdKey = String.format("%s.%s", MongoDbDeviceRegistryUtils.FIELD_CREDENTIALS,
RegistryManagementConstants.FIELD_AUTH_ID);
final String credentialsTypeKey = String.format("%s.%s", MongoDbDeviceRegistryUtils.FIELD_CREDENTIALS,
RegistryManagementConstants.FIELD_TYPE);
return CompositeFuture.all(
// index based on tenantId & deviceId
mongoDbCallExecutor.createCollectionIndex(
config.getCollectionName(),
new JsonObject()
.put(RegistryManagementConstants.FIELD_PAYLOAD_TENANT_ID, 1)
.put(RegistryManagementConstants.FIELD_PAYLOAD_DEVICE_ID, 1),
new IndexOptions().unique(true),
INDEX_CREATION_MAX_RETRIES),
// index based on tenantId, authId & type
mongoDbCallExecutor.createCollectionIndex(
config.getCollectionName(),
new JsonObject()
.put(RegistryManagementConstants.FIELD_PAYLOAD_TENANT_ID, 1)
.put(authIdKey, 1)
.put(credentialsTypeKey, 1),
new IndexOptions().unique(true)
.partialFilterExpression(new JsonObject()
.put(authIdKey, new JsonObject().put("$exists", true))
.put(credentialsTypeKey, new JsonObject().put("$exists", true))),
INDEX_CREATION_MAX_RETRIES));
}
示例7
@Override
public Future<Void> createIndexWithOptions(String collection, JsonObject key, IndexOptions options) {
requireNonNull(collection, "collection cannot be null");
requireNonNull(key, "fieldName cannot be null");
MongoCollection<JsonObject> coll = getCollection(collection);
com.mongodb.client.model.IndexOptions driverOpts = mongoIndexOptions(options);
Promise<Void> promise = vertx.promise();
coll.createIndex(wrap(key), driverOpts).subscribe(new CompletionSubscriber<>(promise));
return promise.future();
}
示例8
@Override
public MongoClient createIndexWithOptions(String collection, JsonObject key, IndexOptions options, Handler<AsyncResult<Void>> resultHandler) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
示例9
@Override
public Future<Void> createIndexWithOptions(String string, JsonObject jo, IndexOptions io) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
示例10
@Override
public Future<Void> createIndex(String collection, JsonObject key) {
return createIndexWithOptions(collection, key, new IndexOptions());
}
示例11
@Override
public io.vertx.ext.mongo.MongoClient createIndexWithOptions(String collection, JsonObject key, IndexOptions options, Handler<AsyncResult<Void>> resultHandler) {
Future<Void> future = createIndexWithOptions(collection, key, options);
setHandler(future, resultHandler);
return this;
}