/** * Forces creation of an index on a set of fields, if one does not already exist. * * @param keys a document that contains pairs with the name of the field or fields to index and order of the index * @param name an identifier for the index. If null or empty, the default name will be used. * @throws MongoException if the operation failed * @mongodb.driver.manual /administration/indexes-creation/ Index Creation Tutorials */ public void createIndex(final DBObject keys, final String name) { createIndex(keys, name, false); }
/** * Creates an index on the field specified, if that index does not already exist. * * @param keys a document that contains pairs with the name of the field or fields to index and order of the index * @mongodb.driver.manual /administration/indexes-creation/ Index Creation Tutorials */ public void createIndex(final DBObject keys) { createIndex(keys, new BasicDBObject()); }
/** * Forces creation of an ascending index on a field with the default options. * * @param name name of field to index on * @throws MongoException if the operation failed * @mongodb.driver.manual /administration/indexes-creation/ Index Creation Tutorials */ public void createIndex(final String name) { createIndex(new BasicDBObject(name, 1)); }
@Inject public NotificationServiceImpl(NodeId nodeId, MongoConnection mongoConnection, AuditEventSender auditEventSender) { super(mongoConnection); this.nodeId = checkNotNull(nodeId); this.auditEventSender = auditEventSender; collection(NotificationImpl.class).createIndex(NotificationImpl.FIELD_TYPE); }
@Inject public StreamRuleServiceImpl(MongoConnection mongoConnection, ClusterEventBus clusterEventBus) { super(mongoConnection); collection(StreamRuleImpl.class).createIndex(StreamRuleImpl.FIELD_STREAM_ID); this.clusterEventBus = clusterEventBus; }
@Inject public UserServiceImpl(final MongoConnection mongoConnection, final Configuration configuration, final RoleService roleService, final AccessTokenService accessTokenService, final UserImpl.Factory userFactory, final InMemoryRolePermissionResolver inMemoryRolePermissionResolver, final EventBus serverEventBus) { super(mongoConnection); this.configuration = configuration; this.roleService = roleService; this.accesstokenService = accessTokenService; this.userFactory = userFactory; this.inMemoryRolePermissionResolver = inMemoryRolePermissionResolver; this.serverEventBus = serverEventBus; // ensure that the users' roles array is indexed collection(UserImpl.class).createIndex(UserImpl.ROLES); }
@Override public String save(AccessToken accessToken) throws ValidationException { // make sure we cannot overwrite an existing access token collection(AccessTokenImpl.class).createIndex(new BasicDBObject(AccessTokenImpl.TOKEN, 1), new BasicDBObject("unique", true)); return super.save(accessToken); }
@VisibleForTesting protected MongoIndexSetService(JacksonDBCollection<IndexSetConfig, ObjectId> collection, StreamService streamService, ClusterConfigService clusterConfigService, ClusterEventBus clusterEventBus) { this.collection = requireNonNull(collection); this.streamService = streamService; this.clusterConfigService = clusterConfigService; this.clusterEventBus = requireNonNull(clusterEventBus); this.collection.getDbCollection().createIndex(DBSort.asc(IndexSetConfig.FIELD_INDEX_PREFIX), null, true); this.collection.getDbCollection().createIndex(DBSort.desc(IndexSetConfig.FIELD_CREATION_DATE)); }
@Inject public SystemMessageServiceImpl(MongoConnection mongoConnection) { super(mongoConnection); final DBCollection collection = this.collection(SystemMessageImpl.class); collection.createIndex(DBSort.desc("timestamp")); }
@Inject public AlertServiceImpl(MongoConnection mongoConnection, MongoJackObjectMapperProvider mapperProvider, AlertConditionFactory alertConditionFactory, AlarmCallbackHistoryService alarmCallbackHistoryService) { this.alertConditionFactory = alertConditionFactory; this.alarmCallbackHistoryService = alarmCallbackHistoryService; final String collectionName = AlertImpl.class.getAnnotation(CollectionName.class).value(); final DBCollection dbCollection = mongoConnection.getDatabase().getCollection(collectionName); dbCollection.createIndex(new BasicDBObject(ImmutableMap.of( AlertImpl.FIELD_TRIGGERED_AT, -1, AlertImpl.FIELD_STREAM_ID, 1 ))); this.coll = JacksonDBCollection.wrap(dbCollection, AlertImpl.class, String.class, mapperProvider.get()); }
@VisibleForTesting static DBCollection prepareCollection(final MongoConnection mongoConnection) { DBCollection coll = mongoConnection.getDatabase().getCollection(COLLECTION_NAME); coll.createIndex(DBSort.asc("type"), "unique_type", true); coll.setWriteConcern(WriteConcern.JOURNALED); return coll; }
@Inject public AlarmCallbackConfigurationServiceImpl(MongoConnection mongoConnection, MongoJackObjectMapperProvider mapperProvider) { final String collectionName = AlarmCallbackConfigurationImpl.class.getAnnotation(CollectionName.class).value(); final DBCollection dbCollection = mongoConnection.getDatabase().getCollection(collectionName); this.coll = JacksonDBCollection.wrap(dbCollection, AlarmCallbackConfigurationImpl.class, String.class, mapperProvider.get()); dbCollection.createIndex(AlarmCallbackConfigurationImpl.FIELD_STREAM_ID); }
@Inject public AlarmCallbackHistoryServiceImpl(MongoConnection mongoConnection, MongoJackObjectMapperProvider mapperProvider) { final String collectionName = AlarmCallbackHistoryImpl.class.getAnnotation(CollectionName.class).value(); final DBCollection dbCollection = mongoConnection.getDatabase().getCollection(collectionName); this.coll = JacksonDBCollection.wrap(dbCollection, AlarmCallbackHistoryImpl.class, String.class, mapperProvider.get()); dbCollection.createIndex(AlarmCallbackHistoryImpl.FIELD_ALERTID); }
@Inject public IndexFailureServiceImpl(MongoConnection mongoConnection) { super(mongoConnection); // Make sure that the index failures collection is always created capped. final String collectionName = IndexFailureImpl.class.getAnnotation(CollectionName.class).value(); if (!mongoConnection.getDatabase().collectionExists(collectionName)) { final DBObject options = BasicDBObjectBuilder.start() .add("capped", true) .add("size", 52428800) // 50MB max size. .get(); final DBCollection collection = mongoConnection.getDatabase().createCollection(collectionName, options); collection.createIndex(new BasicDBObject("timestamp", 1)); collection.createIndex(new BasicDBObject("letter_id", 1)); } }
/** * Forces creation of an index on a set of fields, if one does not already exist. * * @param keys a document that contains pairs with the name of the field or fields to index and order of the index * @param name an identifier for the index. If null or empty, the default name will be used. * @param unique if the index should be unique * @throws MongoException if the operation failed * @mongodb.driver.manual /administration/indexes-creation/ Index Creation Tutorials */ public void createIndex(final DBObject keys, @Nullable final String name, final boolean unique) { DBObject options = new BasicDBObject(); if (name != null && name.length() > 0) { options.put("name", name); } if (unique) { options.put("unique", Boolean.TRUE); } createIndex(keys, options); }
@VisibleForTesting static DBCollection prepareCollection(final MongoConnection mongoConnection) { final DB db = mongoConnection.getDatabase(); DBCollection coll = db.getCollection(COLLECTION_NAME); coll.createIndex(DBSort .asc("timestamp") .asc("producer") .asc("consumers")); coll.setWriteConcern(WriteConcern.JOURNALED); return coll; }
/** * Creates a GridFS instance for the specified bucket in the given database. Set the preferred WriteConcern on the give DB with * DB.setWriteConcern * * @param db database to work with * @param bucket bucket to use in the given database * @throws com.mongodb.MongoException if there's a failure * @see com.mongodb.WriteConcern */ public GridFS(final DB db, final String bucket) { this.database = db; this.bucketName = bucket; this.filesCollection = database.getCollection(bucketName + ".files"); this.chunksCollection = database.getCollection(bucketName + ".chunks"); // ensure standard indexes as long as collections are small try { if (filesCollection.count() < 1000) { filesCollection.createIndex(new BasicDBObject("filename", 1).append("uploadDate", 1)); } if (chunksCollection.count() < 1000) { chunksCollection.createIndex(new BasicDBObject("files_id", 1).append("n", 1), new BasicDBObject("unique", true)); } } catch (MongoException e) { //TODO: Logging } filesCollection.setObjectClass(GridFSDBFile.class); }
db.getCollection(this.shardCollection).createIndex(this.shardKey);
/** * Ensures (creating if necessary) the index including the field(s) + directions; eg fields = "field1, -field2" ({field1:1, field2:-1}) */ public void ensureIndex(String collName, String name, String fields, boolean unique, boolean dropDupsOnCreate) { BasicDBObject dbFields = parseFieldsString(fields); final BasicDBObjectBuilder keyOpts = new BasicDBObjectBuilder(); if (name != null && name.length() != 0) { keyOpts.add("name", name); } if (unique) { keyOpts.add("unique", true); if (dropDupsOnCreate) { keyOpts.add("dropDups", true); } } final DBCollection dbColl = getCollection(getCollName(collName)); final BasicDBObject opts = (BasicDBObject) keyOpts.get(); if (opts.isEmpty()) { LOGGER.debug("Ensuring index for " + dbColl.getName() + " with keys:" + dbFields); dbColl.createIndex(dbFields); } else { LOGGER.debug("Ensuring index for " + dbColl.getName() + " with keys:" + fields + " and opts:" + opts); dbColl.createIndex(dbFields, opts); } }
/** * Ensures (creating if necessary) the index including the field(s) + directions; eg fields = "field1, -field2" ({field1:1, field2:-1}) */ public void ensureIndex(String collName, String name, String fields, boolean unique, boolean dropDupsOnCreate) { BasicDBObject dbFields = parseFieldsString(fields); final BasicDBObjectBuilder keyOpts = new BasicDBObjectBuilder(); if (name != null && name.length() != 0) { keyOpts.add("name", name); } if (unique) { keyOpts.add("unique", true); if (dropDupsOnCreate) { keyOpts.add("dropDups", true); } } final DBCollection dbColl = getCollection(getCollName(collName)); final BasicDBObject opts = (BasicDBObject) keyOpts.get(); if (opts.isEmpty()) { LOGGER.debug("Ensuring index for " + dbColl.getName() + " with keys:" + dbFields); dbColl.createIndex(dbFields); } else { LOGGER.debug("Ensuring index for " + dbColl.getName() + " with keys:" + fields + " and opts:" + opts); dbColl.createIndex(dbFields, opts); } }