/** * 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); }
/** * Find documents using a declarative JSON querying syntax. * @param <T> The class type. * @param jsonQuery The JSON query string. * @param classOfT The class of type T. * @return The result of the query as a {@code List<T> } * @throws CouchDbException If the query failed to execute or the request is invalid. */ public <T> List<T> findDocs(String jsonQuery, Class<T> classOfT) { assertNotEmpty(jsonQuery, "jsonQuery"); HttpResponse response = null; try { response = post(buildUri(getDBUri()).path("_find").build(), jsonQuery); Reader reader = new InputStreamReader(getStream(response), Charsets.UTF_8); JsonArray jsonArray = new JsonParser().parse(reader) .getAsJsonObject().getAsJsonArray("docs"); List<T> list = new ArrayList<T>(); for (JsonElement jsonElem : jsonArray) { JsonElement elem = jsonElem.getAsJsonObject(); T t = this.gson.fromJson(elem, classOfT); list.add(t); } return list; } finally { close(response); } }
/** * 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); } }
/** * Triggers a replication request. * @return {@link ReplicationResult} */ public ReplicationResult trigger() { assertNotEmpty(source, "Source"); assertNotEmpty(target, "Target"); HttpResponse response = null; try { JsonObject json = createJson(); if(log.isDebugEnabled()) { log.debug(json); } final URI uri = buildUri(dbc.getBaseUri()).path("_replicate").build(); response = dbc.post(uri, json.toString()); final InputStreamReader reader = new InputStreamReader(getStream(response), Charsets.UTF_8); return dbc.getGson().fromJson(reader, ReplicationResult.class); } finally { close(response); } }
/** * 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); } }
/** * 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); } }
/** * Performs bulk documents create and update request. * @param objects The {@link List} of documents objects. * @param newEdits If false, prevents the database from assigning documents new revision IDs. * @return {@code List<Response>} Containing the resulted entries. */ public List<Response> bulk(List<?> objects, boolean newEdits) { assertNotEmpty(objects, "objects"); HttpResponse response = null; try { final String newEditsVal = newEdits ? "\"new_edits\": true, " : "\"new_edits\": false, "; final String json = String.format("{%s%s%s}", newEditsVal, "\"docs\": ", getGson().toJson(objects)); final URI uri = buildUri(getDBUri()).path("_bulk_docs").build(); response = post(uri, json); return getResponseList(response); } finally { close(response); } }
/** * Saves an object in the database using HTTP <tt>POST</tt> request. * <p>The database will be responsible for generating the document id. * @param object The object to save * @return {@link Response} */ public Response post(Object object) { assertNotEmpty(object, "object"); HttpResponse response = null; try { URI uri = buildUri(getDBUri()).build(); response = post(uri, getGson().toJson(object)); return getResponse(response); } finally { close(response); } }