/** * Returns the results of a view. * * @param viewQuery view query that represents a view in couch-db. * @return result of view. */ public ViewResult queryStore(ViewQuery viewQuery) { return dbConnector.queryView(viewQuery); }
/** * Update or insert a document identified by docId in the database. * * @param docId document id. * @param document document. */ public void upsertDocument(String docId, @Nonnull Object document) { if (docId != null && dbConnector.contains(docId)) { dbConnector.update(document); } else { //create a document & if docId is null then couch db will generate a random id. dbConnector.create(document); } }
@Override public void flush() { db.executeBulk(bulkDocs); bulkDocs.clear(); db.flushBulkBuffer(); }
/** * * @param id * @param rev * @return * @throws DocumentNotFoundException if the document was not found. * @deprecated use get(String id, Options options) */ public T get(String id, String rev) { return db.get(type, id, rev); }
public void storeJob(CouchDbJobDetail newJob, boolean replaceExisting) throws JobPersistenceException { CouchDbJobDetail existingJob = retrieveJob(newJob.getKey()); if (existingJob == null) { db.create(newJob); return; } if (replaceExisting) { newJob.setId(existingJob.getId()); newJob.setRevision(existingJob.getRevision()); db.update(newJob); } else { throw new ObjectAlreadyExistsException("job already exists"); } }
/** * Can be overidden in order to customize document creation. * @param n * @param id */ protected void createDocument(JsonNode n, String id) { db.create(id, n); } }
@Override protected void update(final Map<String, Object> attributes) { final String id = (String) attributes.get(COUCH_ID); try { final InputStream oldDocStream = couchDbConnector.getAsStream(id); final Map<String, Object> res = objectMapper.readValue(oldDocStream, typeRef); res.putAll(attributes); couchDbConnector.update(res); logger.debug("Updating id: {} with attributes: {}", id, attributes); } catch (DocumentNotFoundException e) { logger.debug("Insert doc (not found by update(): {}", attributes); couchDbConnector.create(attributes); } catch (IOException e) { logger.error("Unexpected IO CouchDB Exception", e); } }
public void bulkAddOrUpdate(List<T> records) { if (CollectionUtils.isNotEmpty(records)) { db.executeBulk(records); } }
public static ViewQuery createAndFetchViewQuery() { if (!connector.contains(DESIGN_DOC_ID)) { //The design document doesn't exist in the database so we create it. JsonNode rootNode = mapper.createObjectNode(); ((ObjectNode)rootNode).put("language", "javascript"); ((ObjectNode)rootNode).putObject("views").putObject(TEST_VIEW).put("map", "function(doc) {\n emit(doc._id, doc);\n}"); connector.create(DESIGN_DOC_ID, rootNode); } return new ViewQuery().designDocId(DESIGN_DOC_ID).viewName(TEST_VIEW); }
/** * @throws UpdateConflictException if there was an update conflict. */ public void update(T entity) { assertEntityNotNull(entity); db.update(entity); }
public boolean removeJob(JobKey jobKey) throws JobPersistenceException { CouchDbJobDetail job = retrieveJob(jobKey); if (job == null) { return false; } db.delete(job); return true; }
private void initDesignDocInternal(int invocations) { DesignDocument designDoc; if (db.contains(stdDesignDocumentId)) { designDoc = getDesignDocumentFactory().getFromDatabase(db, stdDesignDocumentId); } else { log.debug("DesignDocument changed or new. Updating database"); try { db.update(designDoc); } catch (UpdateConflictException e) { log.warn("Update conflict occurred when trying to update design document: {}", designDoc.getId());
@Override protected void deleteById(final String id) { logger.debug("Delete id: {}", id); try { final InputStream oldDocStream = couchDbConnector.getAsStream(id); final JsonNode oldDoc = objectMapper.readTree(oldDocStream); final String rev = oldDoc.get("_rev").asText(); couchDbConnector.delete(id, rev); } catch (DocumentNotFoundException e) { logger.debug("id {} is not in the database", id); } catch (IOException e) { logger.error("Unexpected IO CouchDB Exception", e); } }
/** * Returns if a document identified by the document id is present in the database or not. * * @param docId document id. * @return true if the document is in the database; false otherwise. */ public boolean containsDocument(String docId) { return dbConnector.contains(docId); }
/** * * @param id * @param options * @return * @throws DocumentNotFoundException if the document was not found. */ public T get(String id, Options options) { return db.get(type, id, options); } /**
private void createOrUpdateContent(StreamContent streamContent, StreamContent streamContentFromDB, boolean resourceDoesNotExist) { if (resourceDoesNotExist) { db.create(streamContent); createAttachment(streamContent.getId(), streamContent.getRevision(), streamContent); } else { streamContentFromDB.setChecksum(streamContent.getChecksum()); streamContentFromDB.setContentType(streamContent.getContentType()); db.update(streamContentFromDB); createAttachment(streamContentFromDB.getId(), streamContentFromDB.getRevision(), streamContent); } }
private void insertDocuments(List<Map<String, Object>> documentsIterator) { for (Map<String, Object> map : documentsIterator) { this.connector.create(map); } }
protected void deleteAll() { if (toDelete.size() > 0) { dbConnector.executeBulk(toDelete); } toDelete.clear(); }