/** * Synchronize all design documents on desk to the database. * @see #synchronizeWithDb(DesignDocument) * @see CouchDbClient#syncDesignDocsWithDb() */ public void synchronizeAllWithDb() { List<DesignDocument> documents = getAllFromDesk(); for (DesignDocument dd : documents) { synchronizeWithDb(dd); } }
/** * Gets all design documents from desk. * @see #getFromDesk(String) * @return a list of all design documents */ public List<DesignDocument> getAllFromDesk() { final List<DesignDocument> designDocsList = new ArrayList<DesignDocument>(); for (String docName : listResources(format("%s/", DESIGN_DOCS_DIR))) { designDocsList.add(getFromDesk(docName)); } return designDocsList; }
/** * Synchronize all design documents with the database. */ public void syncDesignDocsWithDb() { design().synchronizeAllWithDb(); }
private Message addUserView() { try { DesignDocument userDocument = prepareDocument("_design/users"); Map<String, MapReduce> views = new HashMap<>(); MapReduce passwordFunction = new MapReduce(); passwordFunction.setMap("function(doc) { if(doc.email&& doc.password) { emit(doc.email, doc.password); } }"); MapReduce usernameFunction = new MapReduce(); usernameFunction.setMap("function(doc) { if(doc.email) { emit(doc.email, doc); } }"); views.put("password", passwordFunction); views.put("username", usernameFunction); userDocument.setViews(views); Response resp = Utils.getCouchDbUserClient().design().synchronizeWithDb(userDocument); if (resp.getError() != null) return Notifications.error("Preparing database 'users'..."); else return Notifications.success("Preparing database 'users'..."); } catch (Exception e) { return Notifications.error("Preparing database 'users'..."); } }
dd.setLanguage(JAVASCRIPT); dd.setViews(views); dd.setFilters(populateMap(rootPath, elements, FILTERS)); dd.setShows(populateMap(rootPath, elements, SHOWS)); dd.setLists(populateMap(rootPath, elements, LISTS)); dd.setUpdates(populateMap(rootPath, elements, UPDATES)); dd.setValidateDocUpdate(readContent(elements, rootPath, VALIDATE_DOC)); dd.setRewrites(dbc.getGson().fromJson(readContent(elements, rootPath, REWRITES), JsonArray.class)); dd.setFulltext(dbc.getGson().fromJson(readContent(elements, rootPath, FULLTEXT), JsonObject.class)); dd.setIndexes(dbc.getGson().fromJson(readContent(elements, rootPath, INDEXES), JsonObject.class)); return dd;
/** * Synchronizes a design document to the Database. * <p>This method will first try to find a document in the database with the same id * as the given document, if it is not found then the given document will be saved to the database. * <p>If the document was found in the database, it will be compared with the given document using * {@code equals()}. If both documents are not equal, then the given document will be saved to the * database and updates the existing document. * @param document The design document to synchronize * @return {@link Response} as a result of a document save or update, or returns {@code null} if no * action was taken and the document in the database is up-to-date with the given document. */ public Response synchronizeWithDb(DesignDocument document) { assertNotEmpty(document, "Document"); DesignDocument documentFromDb = null; try { documentFromDb = getFromDb(document.getId()); } catch (NoDocumentException e) { return dbc.save(document); } if(!document.equals(documentFromDb)) { document.setRevision(documentFromDb.getRevision()); return dbc.update(document); } return null; }
CouchDbClientBase(CouchDbConfig config) { final CouchDbProperties props = config.getProperties(); this.httpClient = createHttpClient(props); this.gson = initGson(new GsonBuilder()); this.host = new HttpHost(props.getHost(), props.getPort(), props.getProtocol()); final String path = props.getPath() != null ? props.getPath() : ""; this.baseURI = buildUri().scheme(props.getProtocol()).host(props.getHost()).port(props.getPort()).path("/").path(path).build(); this.dbURI = buildUri(baseURI).path(props.getDbName()).path("/").build(); this.context = new CouchDbContext(this, props); this.design = new CouchDbDesign(this); }
private Message addConnectionView() { try { DesignDocument connectionDocument = prepareDocument("_design/connection"); Map<String, MapReduce> views = new HashMap<>(); MapReduce frequentFunction = new MapReduce(); frequentFunction.setMap("function(doc) { if(doc.from && doc.to) { emit([doc.from, doc.to] , 1 ); } }"); frequentFunction.setReduce("function (key, values) { return sum(values); }"); views.put("frequent", frequentFunction); connectionDocument.setViews(views); Response resp = Utils.getCouchDbConnectionClient().design().synchronizeWithDb(connectionDocument); if (resp.getError() != null) return Notifications.error("Preparing database 'connection'..."); else return Notifications.success("Preparing database 'connection'..."); } catch (Exception e) { return Notifications.error("Preparing database 'connection'..."); } }
private Message addUserView() { try { DesignDocument userDocument = prepareDocument("_design/users"); Map<String, MapReduce> views = new HashMap<>(); MapReduce passwordFunction = new MapReduce(); passwordFunction.setMap("function(doc) { if(doc.email&& doc.password) { emit(doc.email, doc.password); } }"); MapReduce usernameFunction = new MapReduce(); usernameFunction.setMap("function(doc) { if(doc.email) { emit(doc.email, doc); } }"); views.put("password", passwordFunction); views.put("username", usernameFunction); userDocument.setViews(views); Response resp = Utils.getCouchDbUserClient().design().synchronizeWithDb(userDocument); if (resp.getError() != null) return Notifications.error("Preparing database 'users'..."); else return Notifications.success("Preparing database 'users'..."); } catch (Exception e) { return Notifications.error("Preparing database 'users'..."); } }
private Message addConnectionView() { try { DesignDocument connectionDocument = prepareDocument("_design/connection"); Map<String, MapReduce> views = new HashMap<>(); MapReduce frequentFunction = new MapReduce(); frequentFunction.setMap("function(doc) { if(doc.from && doc.to) { emit([doc.from, doc.to] , 1 ); } }"); frequentFunction.setReduce("function (key, values) { return sum(values); }"); views.put("frequent", frequentFunction); connectionDocument.setViews(views); Response resp = Utils.getCouchDbConnectionClient().design().synchronizeWithDb(connectionDocument); if (resp.getError() != null) return Notifications.error("Preparing database 'connection'..."); else return Notifications.success("Preparing database 'connection'..."); } catch (Exception e) { return Notifications.error("Preparing database 'connection'..."); } }