下面列出了com.mongodb.client.model.IndexOptions#background ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public ProfilingWriter(BlockingQueue<ProfilingEntry> jobQueue) {
this.jobQueue = jobQueue;
serverDto = ConfigReader.getCollectorServer();
runningSince = new Date();
final MongoDbAccessor mongo = getMongoDbAccessor();
try {
final MongoCollection<Document> profileCollection = getProfileCollection(mongo);
IndexOptions indexOptions = new IndexOptions();
indexOptions.background(true);
LOG.info("Create index {ts:-1, lbl:1} in the background if it does not yet exists");
profileCollection.createIndex(new BasicDBObject("ts",-1).append("lbl", 1), indexOptions);
LOG.info("Create index {adr:1, db:1, ts:-1} in the background if it does not yet exists");
profileCollection.createIndex(new BasicDBObject("adr",1).append("db",1).append("ts", -1), indexOptions);
LOG.info("ProfilingWriter is ready at {}", serverDto.getHosts());
} catch (MongoException e) {
LOG.error("Exception while connecting to: {}", serverDto.getHosts(), e);
}
}
private void init(MongoDbAccessor mongo) {
LOG.info(">>> init");
try {
final MongoCollection<Document> profileCollection = getProfileCollection(mongo);
IndexOptions indexOptions = new IndexOptions();
indexOptions.background(true);
LOG.info("Create index {ts:-1, lbl:1} in the background if it does not yet exists");
profileCollection.createIndex(new BasicDBObject("ts",-1).append("lbl", 1), indexOptions);
LOG.info("Create index {adr:1, db:1, ts:-1} in the background if it does not yet exists");
profileCollection.createIndex(new BasicDBObject("adr",1).append("db",1).append("ts", -1), indexOptions);
ApplicationStatusDto.addWebLog("ProfilingWriter is successfully connected to its collector database.");
} catch (MongoException e) {
LOG.error("Exception while connecting to: {}", serverDto.getHosts(), e);
ApplicationStatusDto.addWebLog("ProfilingWriter could not connect to its collector database.");
}
LOG.info("<<< init");
}
@Override
public IndexOptions convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
if (arg.isNull()) return null;
ArrayMemory arr = arg.toValue(ArrayMemory.class);
IndexOptions options = new IndexOptions();
if (arr.containsKey("background")) { options.background(arg.valueOfIndex("background").toBoolean()); }
if (arr.containsKey("defaultLanguage")) { options.defaultLanguage(arg.valueOfIndex("defaultLanguage").toString()); }
if (arr.containsKey("bits")) { options.bits(arg.valueOfIndex("bits").toInteger()); }
if (arr.containsKey("name")) { options.name(arg.valueOfIndex("name").toString()); }
if (arr.containsKey("max")) { options.max(arg.valueOfIndex("max").toDouble()); }
if (arr.containsKey("min")) { options.min(arg.valueOfIndex("min").toDouble()); }
if (arr.containsKey("languageOverride")) { options.languageOverride(arg.valueOfIndex("languageOverride").toString()); }
if (arr.containsKey("sparse")) { options.sparse(arg.valueOfIndex("sparse").toBoolean()); }
if (arr.containsKey("unique")) { options.unique(arg.valueOfIndex("unique").toBoolean()); }
if (arr.containsKey("version")) { options.version(arg.valueOfIndex("version").toInteger()); }
if (arr.containsKey("textVersion")) { options.textVersion(arg.valueOfIndex("textVersion").toInteger()); }
if (arr.containsKey("sphereVersion")) { options.sphereVersion(arg.valueOfIndex("sphereVersion").toInteger()); }
return options;
}
public AbstractOperation(MongoDbAccessor mongoDbAccessor, String db, String collection, String queriedField){
this.mongoDbAccessor = mongoDbAccessor;
mongoCollection = mongoDbAccessor.getMongoDatabase(db).getCollection(collection);
this.queriedField = queriedField;
final IndexOptions options = new IndexOptions();
options.background(false);
mongoCollection.createIndex(new BasicDBObject(queriedField, 1), options);
minId = getMinMax(mongoDbAccessor, queriedField, true);
maxId = getMinMax(mongoDbAccessor, queriedField, false);
}
private ExampleSlowOpsCache(){
cache = new HashSet<>();
serverDto = ConfigReader.getCollectorServer();
readLock = globalLock.readLock();
writeLock = globalLock.writeLock();
final MongoDbAccessor mongo = new MongoDbAccessor(serverDto.getAdminUser(), serverDto.getAdminPw(), serverDto.getSsl(), serverDto.getHosts());
try {
exampleCollection = getExampleCollection(mongo);
final IndexOptions indexOptions = new IndexOptions();
indexOptions.background(true);
//index for retrieval by fingerprint (don't make it unique in case of collisions by the hashing algorithm)
LOG.info("Create index {fp:1} in the background if it does not yet exists");
exampleCollection.createIndex(new BasicDBObject("fp", 1), indexOptions);
//index for removing old entries
// e.g. when the slow ops collection is a capped collection
// then entries older than the oldest slow op can be removed from the example collection
// (the entry may be added automatically anew if the corresponding query is collected again)
LOG.info("Create index {ts:1} in the background if it does not yet exists");
exampleCollection.createIndex(new BasicDBObject("ts", 1), indexOptions);
loadCache(exampleCollection);
LOG.info("ExampleSlowOpsCache is ready at {}", serverDto.getHosts());
} catch (MongoException e) {
LOG.error("Exception while connecting to: {}", serverDto.getHosts(), e);
}
}