/** * @return {@link CouchDbInfo} Containing the DB server info. */ public CouchDbInfo info() { return dbc.get(buildUri(dbc.getDBUri()).build(), CouchDbInfo.class); }
/** * Requests Change notifications of feed type continuous. * <p>Feed notifications are accessed in an <i>iterator</i> style. * @return {@link Changes} */ public Changes continuousChanges() { final URI uri = uriBuilder.query("feed", "continuous").build(); httpGet = new HttpGet(uri); final InputStream in = dbc.get(httpGet); final InputStreamReader is = new InputStreamReader(in, Charsets.UTF_8); setReader(new BufferedReader(is)); return this; }
public Replicator replicatorDB(String replicatorDB) { this.replicatorDB = replicatorDB; dbURI = buildUri(dbc.getBaseUri()).path(replicatorDB).path("/").build(); return this; }
/** * Requests Change notifications of feed type normal. * @return {@link ChangesResult} */ public ChangesResult getChanges() { final URI uri = uriBuilder.query("feed", "normal").build(); return dbc.get(uri, ChangesResult.class); }
/** * Requests CouchDB deletes a database. * @param dbName The database name * @param confirm A confirmation string with the value: <tt>delete database</tt> */ public void deleteDB(String dbName, String confirm) { assertNotEmpty(dbName, "dbName"); if(!"delete database".equals(confirm)) throw new IllegalArgumentException("Invalid confirm!"); dbc.delete(buildUri(dbc.getBaseUri()).path(dbName).build()); }
/** * 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)); } return dbc.get(uri); }
/** * @return DB Server version. */ public String serverVersion() { InputStream instream = null; try { instream = dbc.get(buildUri(dbc.getBaseUri()).build()); Reader reader = new InputStreamReader(instream, Charsets.UTF_8); return getAsString(new JsonParser().parse(reader).getAsJsonObject(), "version"); } finally { close(instream); } }
/** * Requests the database commits any recent changes to disk. */ public void ensureFullCommit() { HttpResponse response = null; try { response = dbc.post(buildUri(dbc.getDBUri()).path("_ensure_full_commit").build(), ""); } finally { close(response); } }
/** * Gets a design document from the database. * @param id The document id * @return {@link DesignDocument} */ public DesignDocument getFromDb(String id) { assertNotEmpty(id, "id"); final URI uri = buildUri(dbc.getDBUri()).path(id).build(); return dbc.get(uri, DesignDocument.class); }
public Replicator(CouchDbClientBase dbc) { this.dbc = dbc; replicatorDoc = new ReplicatorDocument(); replicatorDB = "_replicator"; // default replicator db userCtxRoles = new String[0]; // default roles dbURI = buildUri(dbc.getBaseUri()).path(replicatorDB).path("/").build(); }
/** * Finds a document and return the result as {@link InputStream}. * <p><b>Note</b>: The stream must be closed after use to release the connection. * @param id The document _id field. * @return The result as {@link InputStream} * @throws NoDocumentException If the document is not found in the database. * @see #find(String, String) */ public InputStream find(String id) { assertNotEmpty(id, "id"); return get(buildUri(getDBUri()).path(id).build()); }
/** * Triggers a database <i>compact</i> request. */ public void compact() { HttpResponse response = null; try { response = dbc.post(buildUri(dbc.getDBUri()).path("_compact").build(), ""); } finally { close(response); } }
/** * @return All Server databases. */ public List<String> getAllDbs() { InputStream instream = null; try { Type typeOfList = new TypeToken<List<String>>() {}.getType(); instream = dbc.get(buildUri(dbc.getBaseUri()).path("_all_dbs").build()); Reader reader = new InputStreamReader(instream, Charsets.UTF_8); return dbc.getGson().fromJson(reader, typeOfList); } finally { close(instream); } }
/** * Saves a document with <tt>batch=ok</tt> query param. * @param object The object to save. */ public void batch(Object object) { assertNotEmpty(object, "object"); HttpResponse response = null; try { URI uri = buildUri(getDBUri()).query("batch", "ok").build(); response = post(uri, getGson().toJson(object)); } finally { close(response); } }
/** * 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); }
/** * Removes a document from the database given both a document <code>_id</code> and <code>_rev</code> values. * @param id The document _id field. * @param rev The document _rev field. * @throws NoDocumentException If the document is not found in the database. * @return {@link Response} */ public Response remove(String id, String rev) { assertNotEmpty(id, "id"); assertNotEmpty(rev, "rev"); final URI uri = buildUri(getDBUri()).pathEncoded(id).query("rev", rev).build(); return delete(uri); }
/** * Finds a document given id and revision and returns the result as {@link InputStream}. * <p><b>Note</b>: The stream must be closed after use to release the connection. * @param id The document _id field. * @param rev The document _rev field. * @return The result as {@link InputStream} * @throws NoDocumentException If the document is not found in the database. */ public InputStream find(String id, String rev) { assertNotEmpty(id, "id"); assertNotEmpty(rev, "rev"); final URI uri = buildUri(getDBUri()).path(id).query("rev", rev).build(); return get(uri); }
/** * Finds a document in the replicator database. * @return {@link ReplicatorDocument} */ public ReplicatorDocument find() { assertNotEmpty(replicatorDoc.getId(), "Doc id"); final URI uri = buildUri(dbURI).path(replicatorDoc.getId()).query("rev", replicatorDoc.getRevision()).build(); return dbc.get(uri, ReplicatorDocument.class); }
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); }
/** * Removes a document from the replicator database. * @return {@link Response} */ public Response remove() { assertNotEmpty(replicatorDoc.getId(), "Doc id"); assertNotEmpty(replicatorDoc.getRevision(), "Doc rev"); final URI uri = buildUri(dbURI).path(replicatorDoc.getId()).query("rev", replicatorDoc.getRevision()).build(); return dbc.delete(uri); }