@Inject public ActionService(MongoConnection mongoConnection, MongoJackObjectMapperProvider mapper){ dbCollection = JacksonDBCollection.wrap( mongoConnection.getDatabase().getCollection(COLLECTION_NAME), CollectorActions.class, ObjectId.class, mapper.get()); }
@Override public Set<Output> loadAll() { try (org.mongojack.DBCursor<OutputImpl> outputs = coll.find()) { return ImmutableSet.copyOf((Iterable<OutputImpl>) outputs); } }
public ContentPackInstallation insert(final ContentPackInstallation installation) { final WriteResult<ContentPackInstallation, ObjectId> writeResult = dbCollection.insert(installation); return writeResult.getSavedObject(); }
public Optional<DataAdapterDto> get(String idOrName) { try { return Optional.ofNullable(db.findOneById(new ObjectId(idOrName))); } catch (IllegalArgumentException e) { // not an ObjectId, try again with name return Optional.ofNullable(db.findOne(DBQuery.is("name", idOrName))); } }
public CollectorActions findActionBySidecar(String sidecarId, boolean remove) { if (remove) { return dbCollection.findAndRemove(DBQuery.is("sidecar_id", sidecarId)); } else { return dbCollection.findOne(DBQuery.is("sidecar_id", sidecarId)); } } }
public void delete(String idOrIndexName) { try { db.removeById(new ObjectId(idOrIndexName)); } catch (IllegalArgumentException e) { // Not an ObjectId, try again with index_name db.remove(DBQuery.is(IndexFieldTypesDTO.FIELD_INDEX_NAME, idOrIndexName)); } }
@Override public <T> T create(T t) throws CompatibilityDataException { MongoClient client = null; try { client = getService().createClient(); JacksonDBCollection coll = JacksonDBCollection.wrap(client.getDB(database).getCollection(collection), t.getClass()); return (T)coll.insert(t).getSavedObject(); } catch (Exception ex) { throw new CompatibilityDataException(String.format("Failed to insert object %s", t), ex); } finally { if(client != null) { client.close(); } } }
@Inject public DBLookupTableService(MongoConnection mongoConnection, MongoJackObjectMapperProvider mapper, ClusterEventBus clusterEventBus) { this.db = JacksonDBCollection.wrap(mongoConnection.getDatabase().getCollection("lut_tables"), LookupTableDto.class, ObjectId.class, mapper.get()); this.clusterEventBus = clusterEventBus; db.createIndex(new BasicDBObject("name", 1), new BasicDBObject("unique", true)); }
/** * Stores the given {@link DTO} in the database. * * @param dto the {@link DTO} to save * @return the newly saved {@link DTO} */ public DTO save(DTO dto) { final WriteResult<DTO, ObjectId> save = db.save(dto); return save.getSavedObject(); }
@Override public Decorator save(Decorator decorator) { checkArgument(decorator instanceof DecoratorImpl, "Argument must be an instance of DecoratorImpl, not %s", decorator.getClass()); if (!Strings.isNullOrEmpty(decorator.id())) { this.coll.updateById(decorator.id(), (DecoratorImpl)decorator); return this.coll.findOneById(decorator.id()); } return this.coll.save((DecoratorImpl) decorator).getSavedObject(); }
@Override public ConfigurationVariable save(ConfigurationVariable configurationVariable) { return db.findAndModify(DBQuery.is("_id", configurationVariable.id()), new BasicDBObject(), new BasicDBObject(), false, configurationVariable, true, true); } }
@Override public Decorator findById(String decoratorId) throws NotFoundException { final Decorator result = coll.findOneById(decoratorId); if (result == null) { throw new NotFoundException("Decorator with id " + decoratorId + " not found."); } return result; }
@Override public <T> void write(T payload) { if (payload == null) { LOG.debug("Payload was null. Skipping."); return; } String canonicalClassName = AutoValueUtils.getCanonicalName(payload.getClass()); ClusterConfig clusterConfig = ClusterConfig.create(canonicalClassName, payload, nodeId.toString()); dbCollection.update(DBQuery.is("type", canonicalClassName), clusterConfig, true, false, WriteConcern.JOURNALED); ClusterConfigChangedEvent event = ClusterConfigChangedEvent.create( DateTime.now(DateTimeZone.UTC), nodeId.toString(), canonicalClassName); clusterEventBus.post(event); }
@Override public void doRun() { try { LOG.debug("Removing stale events from MongoDB collection \"{}\"", COLLECTION_NAME); final long timestamp = DateTime.now(DateTimeZone.UTC).getMillis() - maxEventAge; final DBQuery.Query query = DBQuery.lessThan("timestamp", timestamp); final WriteResult<ClusterEvent, String> writeResult = dbCollection.remove(query, WriteConcern.JOURNALED); LOG.debug("Removed {} stale events from \"{}\"", writeResult.getN(), COLLECTION_NAME); } catch (Exception e) { LOG.warn("Error while removing stale cluster events from MongoDB", e); } } }
/** * {@inheritDoc} */ @Override public Optional<IndexSetConfig> findOne(DBQuery.Query query) { return Optional.ofNullable(collection.findOne(query)); }
/** * calls {@link DBCollection#update(com.mongodb.DBObject, com.mongodb.DBObject, boolean, boolean)} with upsert=false * and multi=false * * @param id * the id of the object to update * @param object * object with which to update <tt>query</tt> * @return The result * @throws MongoException * If an error occurred */ public WriteResult<T, K> updateById(K id, T object) throws MongoException { return update(createIdQuery(id), convertToDbObject(object), false, false); }
@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)); }
com.mongodb.MapReduceCommand build(JacksonDBCollection<?, ?> collection) { DBObject query = null; if (this.query != null) { query = collection.serializeQuery(this.query); } com.mongodb.MapReduceCommand command = new com.mongodb.MapReduceCommand( collection.getDbCollection(), map, reduce, this.collection, outputType.getDriverType(), query); if (finalize != null) { command.setFinalize(finalize); } if (readPreference != null) { command.setReadPreference(readPreference); } if (outputDB != null) { command.setOutputDB(outputDB); } if (sort != null) { command.setSort(sort); } command.setLimit(limit); if (scope != null) { command.setScope(scope); } command.setVerbose(verbose); return command; }
public Optional<LookupTableDto> get(String idOrName) { try { return Optional.ofNullable(db.findOneById(new ObjectId(idOrName))); } catch (IllegalArgumentException e) { // not an ObjectId, try again with name return Optional.ofNullable(db.findOne(DBQuery.is("name", idOrName))); } }