/** * Queries a view as an {@link InputStream} * * <p>The stream should be properly closed after usage, as to avoid connection leaks. * * @return The result as an {@link InputStream}. */ public InputStream queryForStream() { URI uri = uriBuilder.build(); if (allDocsKeys != null) { // bulk docs return getStream(dbc.post(uri, allDocsKeys)); } if (mapRedtempViewM != null) { // temp view return getStream(dbc.post(uri, gson.toJson(mapRedtempViewM))); } return dbc.get(uri); }
View(CouchDbClientBase dbc, String viewId) { assertNotEmpty(viewId, "View id"); this.dbc = dbc; this.gson = dbc.getGson(); String view = viewId; if (viewId.contains("/")) { String[] v = viewId.split("/"); view = String.format("_design/%s/_view/%s", v[0], v[1]); } this.uriBuilder = URIBuilder.buildUri(dbc.getDBUri()).path(view); }
/** * 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; }
/** * Gets a design document from desk. * * @param id The document id to get. * @return {@link DesignDocument} */ public DesignDocument getFromDesk(String id) { assertNotEmpty(id, "id"); final DesignDocument dd = new DesignDocument(); final String rootPath = format("%s/%s/", DESIGN_DOCS_DIR, id); final List<String> elements = listResources(rootPath); if (elements == null) { throw new IllegalArgumentException("Design docs directory cannot be empty."); } // Views Map<String, MapReduce> views = null; if (elements.contains(VIEWS)) { views = new HashMap<String, MapReduce>(); final String viewsPath = format("%s%s/", rootPath, VIEWS); for (String viewDirName : listResources(viewsPath)) { // views sub-dirs final MapReduce mr = new MapReduce(); final String viewPath = format("%s%s/", viewsPath, viewDirName); final List<String> dirList = listResources(viewPath); for (String fileName : dirList) { // view files final String def = readFile(format("/%s%s", viewPath, fileName)); if (MAP_JS.equals(fileName)) mr.setMap(def); else if (REDUCE_JS.equals(fileName)) mr.setReduce(def); } // /foreach view files views.put(viewDirName, mr); } // /foreach views sub-dirs } // /views 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; }
/** * Gets a design document from the database. * * @param id The document id * @param rev The document revision * @return {@link DesignDocument} */ public DesignDocument getFromDb(String id, String rev) { assertNotEmpty(id, "id"); assertNotEmpty(id, "rev"); final URI uri = buildUri(dbc.getDBUri()).path(id).query("rev", rev).build(); return dbc.get(uri, DesignDocument.class); }