@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); }
public Optional<IndexFieldTypesDTO> upsert(IndexFieldTypesDTO dto) { final WriteResult<IndexFieldTypesDTO, ObjectId> update = db.update( DBQuery.and( DBQuery.is(IndexFieldTypesDTO.FIELD_INDEX_NAME, dto.indexName()), DBQuery.is(IndexFieldTypesDTO.FIELD_INDEX_SET_ID, dto.indexSetId()) ), dto, true, false ); final Object upsertedId = update.getUpsertedId(); if (upsertedId instanceof ObjectId) { return get(((ObjectId) upsertedId).toHexString()); } else if (upsertedId instanceof String) { return get((String) upsertedId); } return Optional.empty(); }
public void updateTraffic(DateTime observationTime, NodeId nodeId, long inLastMinute, long outLastMinute, long decodedLastMinute) { // we bucket our database by days final DateTime dayBucket = getDayBucket(observationTime); if (LOG.isDebugEnabled()) { LOG.debug("Updating traffic for node {} at {}: in/decoded/out {}/{}/{} bytes", nodeId.toString(), dayBucket, inLastMinute, decodedLastMinute, outLastMinute); } final WriteResult<TrafficDto, ObjectId> update = db.update(DBQuery.is("bucket", dayBucket), // sigh DBUpdate.inc only takes integers, but we have a long. new DBUpdate.Builder() .addOperation("$inc", "input." + nodeId.toString(), new SingleUpdateOperationValue(false, false, inLastMinute)) .addOperation("$inc", "output." + nodeId.toString(), new SingleUpdateOperationValue(false, false, outLastMinute)) .addOperation("$inc", "decoded." + nodeId.toString(), new SingleUpdateOperationValue(false, false, decodedLastMinute)), true, false); if (update.getN() == 0) { LOG.warn("Unable to update traffic of node {}: {}", nodeId, update); } }
@Override public GrokPattern update(GrokPattern pattern) throws ValidationException { try { if (!validate(pattern)) { throw new ValidationException("Invalid pattern " + pattern); } } catch (GrokException | PatternSyntaxException e) { throw new ValidationException("Invalid pattern " + pattern + "\n" + e.getMessage()); } if (pattern.id() == null) { throw new ValidationException("Invalid pattern " + pattern); } WriteResult<GrokPattern, ObjectId> result = dbCollection.update(DBQuery.is("_id", new ObjectId(pattern.id())), pattern); if (result.isUpdateOfExisting()) { clusterBus.post(GrokPatternsUpdatedEvent.create(ImmutableSet.of(pattern.name()))); return pattern; } throw new ValidationException("Invalid pattern " + pattern); }
/** * calls {@link DBCollection#update(com.mongodb.DBObject, com.mongodb.DBObject, boolean, boolean)} with upsert=false * and multi=false * * @param query * search query for old 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> update(DBObject query, DBObject object) throws MongoException { return update(query, object, false, false); }
/** * calls {@link DBCollection#update(com.mongodb.DBObject, com.mongodb.DBObject, boolean, boolean)} with upsert=false * and multi=false * * @param query * search query for old 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> update(DBQuery.Query query, T object) throws MongoException { return update(query, object, false, false); }
/** * Performs an update operation. * * @param query * search query for old object to update * @param update * update with which to update <tt>query</tt> * @return The write result * @throws MongoException * If an error occurred */ public WriteResult<T, K> update(DBQuery.Query query, DBUpdate.Builder update) throws MongoException { return this.update(query, update, false, false); }
/** * calls {@link DBCollection#update(com.mongodb.DBObject, com.mongodb.DBObject, boolean, boolean)} with upsert=false * and multi=true * * @param query * search query for old 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> updateMulti(DBObject query, DBObject object) throws MongoException { return update(query, object, false, true); }
/** * calls {@link DBCollection#update(com.mongodb.DBObject, com.mongodb.DBObject, boolean, boolean)} with upsert=false * and multi=true * * @param query * search query for old object to update * @param update * update with which to update <tt>query</tt> * @return The write result * @throws MongoException * If an error occurred */ public WriteResult<T, K> updateMulti(DBQuery.Query query, DBUpdate.Builder update) throws MongoException { return update(query, update, false, true); }
/** * calls {@link DBCollection#update(com.mongodb.DBObject, com.mongodb.DBObject, boolean, boolean)} with upsert=false * and multi=true * * @param query * search query for old 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> updateMulti(DBQuery.Query query, T object) throws MongoException { return update(query, object, false, true); }
/** * calls * {@link DBCollection#update(com.mongodb.DBObject, com.mongodb.DBObject, boolean, boolean, com.mongodb.WriteConcern)} * with default WriteConcern. * * @param query * search query for old object to update * @param object * object with which to update <tt>q</tt> * @param upsert * if the database should create the element if it does not exist * @param multi * if the update should be applied to all objects matching (db * version 1.1.3 and above) See * http://www.mongodb.org/display/DOCS/Atomic+Operations * @return The result * @throws MongoException * If an error occurred */ public WriteResult<T, K> update(DBObject query, DBObject object, boolean upsert, boolean multi) throws MongoException { return update(query, object, upsert, multi, getWriteConcern()); }
/** * calls * {@link DBCollection#update(com.mongodb.DBObject, com.mongodb.DBObject, boolean, boolean, com.mongodb.WriteConcern)} * with default WriteConcern. * * @param query * search query for old object to update * @param object * object with which to update <tt>q</tt> * @param upsert * if the database should create the element if it does not exist * @param multi * if the update should be applied to all objects matching (db * version 1.1.3 and above) See * http://www.mongodb.org/display/DOCS/Atomic+Operations * @return The result * @throws MongoException * If an error occurred */ public WriteResult<T, K> update(DBQuery.Query query, T object, boolean upsert, boolean multi) throws MongoException { return update(query, object, upsert, multi, getWriteConcern()); }
/** * Performs an update operation. * * @param query * search query for old object to update * @param update * update with which to update <tt>query</tt> * @param upsert * if the database should create the element if it does not exist * @param multi * if the update should be applied to all objects matching (db * version 1.1.3 and above). An object will not be inserted if it * does not exist in the collection and upsert=true and * multi=true. See <a * href="http://www.mongodb.org/display/DOCS/Atomic+Operations" * >http://www.mongodb.org/display/DOCS/Atomic+Operations</a> * @return The write result * @throws MongoException * If an error occurred */ public WriteResult<T, K> update(DBQuery.Query query, DBUpdate.Builder update, boolean upsert, boolean multi) throws MongoException { return this.update(query, update, upsert, multi, getWriteConcern()); }
/** * 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); }
/** * Performs an update operation. * * @param id * The id of the document to update * @param update * update with which to update <tt>query</tt> * @return The write result * @throws MongoException * If an error occurred */ public WriteResult<T, K> updateById(K id, DBUpdate.Builder update) throws MongoException { return this.update(createIdQuery(id), update.serialiseAndGet(objectMapper, type)); }
@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); }
public K upsert(T entity) { WriteResult<T, K> writeResult = entitiesCollection.update(findById(entity), entity, true, false); if (writeResult.getDbObjects().length != 0) { // Had an insert so we can safely reference the ID return writeResult.getSavedId(); } // Return the original ID (no change) return entity.getId(); }
public void updateTraffic(DateTime observationTime, NodeId nodeId, long inLastMinute, long outLastMinute, long decodedLastMinute) { // we bucket our database by days final DateTime dayBucket = getDayBucket(observationTime); if (LOG.isDebugEnabled()) { LOG.debug("Updating traffic for node {} at {}: in/decoded/out {}/{}/{} bytes", nodeId.toString(), dayBucket, inLastMinute, decodedLastMinute, outLastMinute); } final WriteResult<TrafficDto, ObjectId> update = db.update(DBQuery.is("bucket", dayBucket), // sigh DBUpdate.inc only takes integers, but we have a long. new DBUpdate.Builder() .addOperation("$inc", "input." + nodeId.toString(), new SingleUpdateOperationValue(false, false, inLastMinute)) .addOperation("$inc", "output." + nodeId.toString(), new SingleUpdateOperationValue(false, false, outLastMinute)) .addOperation("$inc", "decoded." + nodeId.toString(), new SingleUpdateOperationValue(false, false, decodedLastMinute)), true, false); if (update.getN() == 0) { LOG.warn("Unable to update traffic of node {}: {}", nodeId, update); } }