pax_global_header 0000666 0000000 0000000 00000000064 12150357216 0014514 g ustar 00root root 0000000 0000000 52 comment=b20335ef6dd4b97b8e78cd3d9679ff4fcdd0800b
LightCouch-lightcouch-0.0.6/ 0000775 0000000 0000000 00000000000 12150357216 0015677 5 ustar 00root root 0000000 0000000 LightCouch-lightcouch-0.0.6/.gitignore 0000664 0000000 0000000 00000000063 12150357216 0017666 0 ustar 00root root 0000000 0000000 .project
.settings
.classpath
target
*.log
*.log.*
LightCouch-lightcouch-0.0.6/LICENSE 0000664 0000000 0000000 00000026152 12150357216 0016712 0 ustar 00root root 0000000 0000000 Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2011 Ahmed Yehia (ahmed.yehia.m@gmail.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
LightCouch-lightcouch-0.0.6/README.md 0000664 0000000 0000000 00000000671 12150357216 0017162 0 ustar 00root root 0000000 0000000 CouchDB Java API
================
* Homepage:
LightCouch aims at providing a simple and easy-to-use APIs for accessing CouchDB databases.
It offers a powerful and lightweight persistence interface with minimal code base and dependency.
Getting Started guide is available on the website at:
API documentation and User Guide:
LightCouch-lightcouch-0.0.6/guide/ 0000775 0000000 0000000 00000000000 12150357216 0016774 5 ustar 00root root 0000000 0000000 LightCouch-lightcouch-0.0.6/guide/LICENSE 0000664 0000000 0000000 00000025040 12150357216 0020002 0 ustar 00root root 0000000 0000000 Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
Copyright 2011 Ahmed Yehia (ahmed.yehia.m@gmail.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
LightCouch-lightcouch-0.0.6/guide/README 0000664 0000000 0000000 00000000034 12150357216 0017651 0 ustar 00root root 0000000 0000000 Usage: mvn generate-sources
LightCouch-lightcouch-0.0.6/guide/pom.xml 0000664 0000000 0000000 00000002360 12150357216 0020312 0 ustar 00root root 0000000 0000000 4.0.0org.lightcouchlightcouch-guide0.0.1-SNAPSHOTcom.agilejava.docbkxdocbkx-maven-plugin2.0.13generate-htmlgenerate-pdfgenerate-sourcesorg.docbookdocbook-xml4.4runtimehttp://www.lightcouch.org/css/manual.csssrc/docbkx/resources/pdf.xslsrc/docbkx/resources/html.xsl
LightCouch-lightcouch-0.0.6/guide/src/ 0000775 0000000 0000000 00000000000 12150357216 0017563 5 ustar 00root root 0000000 0000000 LightCouch-lightcouch-0.0.6/guide/src/docbkx/ 0000775 0000000 0000000 00000000000 12150357216 0021035 5 ustar 00root root 0000000 0000000 LightCouch-lightcouch-0.0.6/guide/src/docbkx/lightcouch-guide.xml 0000664 0000000 0000000 00000070361 12150357216 0025012 0 ustar 00root root 0000000 0000000
LightCouch User Guide 0.0.6Author: Ahmed YehiaOct 24, 2012Overview
LightCouch provides a Java interface for communicating with CouchDB RESTful JSON APIs.
APIs in LightCouch is accessed by establishing a 'client' towards a CouchDB database server.
A client is the main object which performs every request to the database, under it's context, such as document persistence or querying Views.
The APIs focus on Documents CRUD, Views, Attachments, Design Documents,
Bulk operations, Changes notification, and database specific such as Compaction and Replication.
API usage and use cases examples is covered by integration unit tests, that runs against a running database.
Download.
Internally LightCouch depends on
Apache HttpClient
for handling the HTTP communications & Gson
for JSON / Java mappings.
SetupMavenorg.lightcouchlightcouch0.0.6
]]>
Alternatively download the dependencies.
LightCouch 0.0.6HttpClient 4.2.5HttpCore 4.2.4Commons Codec 1.6Commons Logging 1.1.1Gson 2.2.4Configuration and Use
LightCouch client, i.e. org.lightcouch.CouchDbClient
can be configured and constructed in several ways as preferred.
Configuration parameters can be supplied to a client via .properties files, or through convenient constructors.
Typical usage is creating a file named couchdb.properties in the default classpath of your application,
containing the following parameters:
Next, create a client instance, using the default constructor:
CouchDbClient provides additional constructors for instantiating a client.
One which accepts a .properties file name e.g, mycouchdb.properties,
another that accepts individual parameters, the final uses an object with properties CouchDbProperties.
Typically a client instance is created once per an application, and reused.
Multiple client instances within an application; can be created to handle
multiple databases, each instance runs in isolation.
After usage, it might be useful to shutdown a client's underlying connection manager,
for proper release of resources:
Spring DI
]]>
Note, the example synchronize Design documents with the databse, after a client is created.
Documents API
Documents API is accessed via a client instance which acts like JPA EntityManager.
The API provides CRUD kind of operations for documents.
Model classes that represent documents in CouchDB, can choose to extend
org.lightcouch.Document
for conventience.
Plain objects need to define id and revision fields, i.e. _id & _rev.
Gson Mapping annotations eg. @SerializedName("_id") String id; could be used to indicate the JSON name of a field.
Fields with null value are not serialized by defualt.
An instance of Gson may be obtained by a client, for use in an application when desired.
Saving/Updating Documents
Saving / Updating Documents is handled by save(Object) and update(Object).
An object can be:
1. Plain Java Object.
2. java.util.Map.
3. com.google.gson.JsonObject.
Saving a new object internally is handled by HTTP PUT,
a case where a document must be assigned an id prior to sending to the database.
If the new object does not define an id, the API will generate an
UUID similar to database generated.
UUIDs can also by obtained from the database,
an example below on Databse API section.
Documents can also be saved via batch(Object).
The Result of save or update operations returns a
org.lightcouch.Response
object that represents the database response.
map = new HashMap<>();
map.put("_id", "doc-id");
map.put("title", "value");
dbClient.save(map);
JsonObject json = new JsonObject();
json.addProperty("_id", "doc-id");
json.add("array", new JsonArray());
dbClient.save(json);
String jsonstr = "{\"title\":\"val\"}";
JsonObject jsonobj = dbClient.getGson().fromJson(jsonstr, JsonObject.class);
dbClient.save(jsonobj);
]]>Finding Documents
Finding documents API is available through find()s,
that expect the document id, or w/ rev,
and optional query parameters through org.lightcouch.Params
An additional finder findAny() gives the option to get any document from the database,
given a URI, as string, encoding may be required.
Documents returned as:
1. Plain Java Objects.
2. com.google.gson.JsonObject.
3. java.io.InputStream.
java.util.Map, however does not qualify as a return type.
Note: Input streams need to be closed properly; to release the connection.
Deleting Documents
The API provides a remove() for deleting documents,
expected parameters:
1. An object containing _id and _rev.
2. id and revision values.
Custom Serializers
Configuring a custom serializer gives the option to register classes
Gson may not be able to handle by default,
eg. JodaTimeDateTime class.
() {
public JsonElement serialize(DateTime src, Type typeOfSrc,
JsonSerializationContext context) {
return new JsonPrimitive(src.toString());
}
});
gsonBuilder.registerTypeAdapter(DateTime.class, new JsonDeserializer() {
public DateTime deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
return new DateTime(json.getAsJsonPrimitive().getAsString());
}
});
CouchDbClient dbClient = new CouchDbClient();
dbClient.setGsonBuilder(gsonBuilder);
// .. client is ready to use
]]>Attachments API
The API supports both inline and standalone Attachments.
Inline Attachmentsorg.lightcouch.Attachment
represents an inline attachment enclosed in a document.
The base64 data of an attachment may be encoded utilizing
the included dependency on Apache CodecBase64.encodeBase64String(bytes).
Model classes that extend org.lightcouch.Document
inherit the support for inline attachments.
To retrieve the base64 data of an attachment, include a parameter to the find()Standalone Attachments
Standalone attachments could be saved to an existing, or to a new document.
The attachment data is provided as java.io.InputStream.
Logging and Error HandlingLogging
LightCouch logs information about API usage, such as a request URI, and the returned HTTP status code
from the database server, such information is logged at the level INFO.
Log4j configuration:
Error Handling
In the event of API call failure, an exception of a specific type is thrown to report the failure.
Defined Exceptions:
1. NoDocumentException when a requested document could not be found (or a View with no result).
2. DocumentConflictException when a conflict is detected during a save or update.
3. CouchDbException is the super class for the former two exceptions,
conventiently it is thrown in cases of unexcpected exceptions, such as database errors 4xx or 5xx.
Views API
Views API is accessible under the context view("design_name/my_view").
Views can be queried in a number of ways:
1. ]]> A list of specified type.
2. Scalar values, int, boolean etc.
3. View entries, key/value pairs as stored in the Database B-Tree.
4. java.io.InputStream.
5. Pagination.
List list = dbClient.view("example/foo")
.includeDocs(true)
.startKey("start-key")
.endKey("end-key")
.limit(10)
.query(Foo.class);
// primitive types
int count = dbClient.view("example/by_tag").key("couchdb").queryForInt();
// View entries (keys/values), with Documents
View view = dbClient.view("example/by_date")
.key(2011, 10, 15) // complex key as: values, or array
.reduce(false)
.includeDocs(true);
ViewResult entries =
view.queryView(int[].class, String.class, Foo.class);
]]>Special Views
Views API extends to support special Views such as _all_docs & _temp_view.
Temporary Views functions may be be defined in local .js files; saved in the application classpath,
or through MapReduce object.
allDocs = dbClient.view("_all_docs").query(JsonObject.class);
// _temp_view
List list = dbClient.view("_temp_view")
.tempView("temp_1")
// .tempView(mapRedObj)
.includeDocs(true)
.reduce(false)
.query(Foo.class);
]]>
Temp views directory structure.
Pagination
Paging over a View results is offered by an API that features previous & next navigation model.
A View page is respresented by ]]>
Usage example in web application.
page = dbClient.view("example/foo").reduce(false).queryPage(resultsPerPage, param, Foo.class);
request.setAttribute("page", page);
// .. forward to a JSP for display
}
]]>
<%-- iterate over documents --%>
${foo.title}
<%-- show paging status --%>
Showing: ${page.resultFrom} - ${page.resultTo} of total ${page.totalResults}
<%-- handle navigation --%>
Bulk Documents
Bulk documents API performs two operations: Modify & Fetch for bulk documents.
newDocs = new ArrayListDesign Documents
The API for Design Documents is accessible under the context design().
A Design Document is represented by
org.lightcouch.DesignDocument.
Design Documents may be maintained on local .js files; saved in an application classpath.
An API is provides a kind of synchronization to copy from local files to the database.
org.lightcouch.CouchDbDesign
lists all the supported operations.
An example design document can be found under the test package of the source code.
Design documents directory structure:
Change NotificationsChange notifications API is accessible under the context
changes(). The API supports Changes feed of both types
normal and continuous.
A Row in the Changes feed is returned as com.google.gson.JsonObject
rows = changes.getResults();
for (ChangesResult.Row row : rows) {
String docId = row.getId()
JsonObject doc = row.getDoc();
}
// feed type continuous
Changes changes = dbClient.changes()
.includeDocs(true)
.since(since)
.heartBeat(30000)
.filter("example/filter")
.continuousChanges();
// live access to continuous feeds
while (changes.hasNext()) {
ChangesResult.Row feed = changes.next();
String docId = feed.getId();
JsonObject doc = feed.getDoc();
// changes.stop(); // stop the running feed
}
]]>Update HandlersUpdate handlers can be invoked by a client by calling invokeUpdateHandler().
Database API
Database specific APIs is accessible under the context context().
Replication
Replication is handled by two sets of API, one that replicate by POSTing to
_replicate URI, and the newer replicator database
(i.e _replicator) introduced with CouchDB 1.1.0 .
queryParams = new HashMap<>();
queryParams.put("key1", "val");
// String queryParams = "{\"key1\":\"val\"}";
// normal replication
ReplicationResult result = dbClient.replication()
.source("source-db")
.target("http://user:secret@127.0.0.1:5984/target-db")
.createTarget(true)
.sinceSeq(100) // as of CouchDB 1.2
// .cancel() // cancel a replication
.filter("example/filter1")
.queryParams(queryParams);
.trigger();
List histories = result.getHistories();
// _replicator database
Replicator replicator = dbClient.replicator()
.source("source-db")
.target("target-db")
.continuous(true)
.createTarget(true)
.sinceSeq(100) // as of CouchDB 1.2
.replicatorDB("replicator_2") // optional, defaults to _replicator
.replicatorDocId("doc-id") // optional, defaults to UUID
.filter("example/filter1")
.queryParams(queryParams)
.userCtxName("user") // for delegated requests
.userCtxRoles("admin", "manager");
Response response = replicator.save(); // triggers a replication
ReplicatorDocument findDocument = dbClient.replicator()
.replicatorDocId("doc-id")
.find();
List docs = dbClient.replicator().findAll();
Response removeResponse = dbClient.replicator()
.replicatorDocId("doc-id")
.replicatorDocRev("doc-rev")
.remove(); // cancels an ongoing replication
]]>