/** * 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 String save(Alert alert) throws ValidationException { checkArgument(alert instanceof AlertImpl, "Supplied argument must be of type " + AlertImpl.class + ", and not " + alert.getClass()); return this.coll.save((AlertImpl)alert).getSavedId(); }
@Override public int delete(String id) { return this.coll.removeById(id).getN(); }
public K save(T entity) { WriteResult<T, K> writeResult = entitiesCollection.save(entity); if (writeResult.getDbObjects().length != 0) { // Had an insert so we can safely reference the ID entity.setId(writeResult.getSavedId()); return writeResult.getSavedId(); } // Return the original ID (no change) return entity.getId(); }
/** * calls {@link DBCollection#remove(com.mongodb.DBObject, com.mongodb.WriteConcern)} with the default WriteConcern * * @param query * the query that documents to be removed must match * @return The write result * @throws MongoException * If an error occurred */ public WriteResult<T, K> remove(DBObject query) throws MongoException { return new WriteResult<T, K>(this, dbCollection.remove(serializeFields(query))); }
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(); }
@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); }
public List<K> createAll(List<T> entities) { return entitiesCollection.insert(entities).getSavedIds(); }
/** * Get the object that was saved. This will contain the updated ID if the ID * was generated. * <p> * Note, this operation is a little expensive because it has to deserialise the object. If you just want the ID, * call getSavedId() instead. * * @return The saved object * @throws MongoException * If no objects were saved */ public T getSavedObject() { if (dbObjects.length == 0) { throw new MongoException("No objects to return"); } return getSavedObjects().get(0); }
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(); }
/** * calls {@link DBCollection#remove(com.mongodb.DBObject, com.mongodb.WriteConcern)} with the default WriteConcern * * @param query * the query * @return The write result * @throws MongoException * If an error occurred */ public WriteResult<T, K> remove(DBQuery.Query query) throws MongoException { return new WriteResult<T, K>(this, dbCollection.remove(serializeQuery(query))); }
public ContentPackInstallation insert(final ContentPackInstallation installation) { final WriteResult<ContentPackInstallation, ObjectId> writeResult = dbCollection.insert(installation); return writeResult.getSavedObject(); }
public int deleteById(ObjectId id) { final WriteResult<ContentPackInstallation, ObjectId> writeResult = dbCollection.removeById(id); return writeResult.getN(); }
@Override public String save(AlarmCallbackConfiguration model) throws ValidationException { return coll.save(implOrFail(model)).getSavedId(); }
/** * Inserts an object into the database. if the objects _id is null, one will * be generated you can get the _id that was generated by calling * getSavedObject() or getSavedId() on the result * * @param object * The object to insert * @return The result * @throws MongoException * If an error occurred */ public WriteResult<T, K> insert(T object) throws MongoException { DBObject dbObject = convertToDbObject(object); return new WriteResult<T, K>(this, dbCollection.insert(dbObject), dbObject); }
public IndexFieldTypesDTO save(IndexFieldTypesDTO dto) { final WriteResult<IndexFieldTypesDTO, ObjectId> save = db.save(dto); return save.getSavedObject(); }
@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); } } }
@Subscribe public void publishClusterEvent(Object event) { if (event instanceof DeadEvent) { LOG.debug("Skipping DeadEvent on cluster event bus"); return; } final String className = AutoValueUtils.getCanonicalName(event.getClass()); final ClusterEvent clusterEvent = ClusterEvent.create(nodeId.toString(), className, event); try { final String id = dbCollection.save(clusterEvent, WriteConcern.JOURNALED).getSavedId(); LOG.debug("Published cluster event with ID <{}> and type <{}>", id, className); } catch (MongoException e) { LOG.error("Couldn't publish cluster event of type <" + className + ">", e); } }
/** * Inserts objects into the database. if the objects _id is null, one will * be generated you can get the _id that were generated by calling * getSavedObjects() or getSavedIds() on the result * * @param objects * The objects to insert * @return The result * @throws MongoException * If an error occurred */ public WriteResult<T, K> insert(T... objects) throws MongoException { DBObject[] dbObjects = convertToDbObjects(objects); return new WriteResult<T, K>(this, dbCollection.insert(dbObjects), dbObjects); }
@Override public Output create(Output request) throws ValidationException { final OutputImpl outputImpl = implOrFail(request); final WriteResult<OutputImpl, String> writeResult = coll.save(outputImpl); return writeResult.getSavedObject(); }