private DesignDocument prepareDocument(String id) { DesignDocument doc = new DesignDocument(); doc.setLanguage("javascript"); doc.setId(id); return doc; } }
final DesignDocument dd = new DesignDocument(); final String rootPath = format("%s/%s/", DESIGN_DOCS_DIR, id); final List<String> elements = listResources(rootPath); dd.setId(DESIGN_PREFIX + id); 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; }
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 DesignDocument prepareDocument(String id) { DesignDocument doc = new DesignDocument(); doc.setLanguage("javascript"); doc.setId(id); return doc; } }
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'..."); } }