public class DatabaseCache extends Database
Database
implementation with a cache.Constructor and Description |
---|
DatabaseCache(Database database,
Cache<java.lang.String,java.lang.Object> cacheInstance)
Constructor which is designed to work with a variety of different caches.
|
Modifier and Type | Method and Description |
---|---|
java.util.List<Response> |
bulk(java.util.List<?> list)
Uses the
_bulk_docs endpoint to insert multiple documents into the database in a
single HTTP request. |
boolean |
contains(java.lang.String id)
Checks if the cache contains the specified document.
|
<T> T |
find(java.lang.Class<T> classType,
java.lang.String id)
Preferentially use the cache for the find operation.
|
<T> T |
find(java.lang.Class<T> classType,
java.lang.String id,
Params params)
Preferentially use the cache for the find operation.
|
<T> T |
findAny(java.lang.Class<T> classType,
java.lang.String uri)
Preferentially use the cache for the find operation.
|
Cache<java.lang.String,java.lang.Object> |
getCache()
Returns the cache so that the application can manage it using the Cache
API methods.
|
Response |
post(java.lang.Object object)
Creates a document in the database using a HTTP
POST request. |
Response |
post(java.lang.Object object,
int writeQuorum)
Creates a document in the database similarly to
Database.post(Object) but using a
specific write quorum. |
Response |
remove(java.lang.Object object)
Removes a document from the database.
|
Response |
save(java.lang.Object object)
Saves a document in the database.
|
Response |
save(java.lang.Object object,
int writeQuorum)
Saves a document in the database similarly to
Database.save(Object) but using a
specific write quorum. |
Response |
update(java.lang.Object object)
Updates an object in the database, the object must have the correct
_id and
_rev values. |
Response |
update(java.lang.Object object,
int writeQuorum)
Updates an object in the database similarly to
Database.update(Object) , but specifying the
write quorum. |
changes, createIndex, createIndex, deleteIndex, ensureFullCommit, find, find, find, findByIndex, findByIndex, getAllDocsRequestBuilder, getDBUri, getDesignDocumentManager, getPermissions, getShard, getShards, getViewRequestBuilder, info, invokeUpdateHandler, listIndices, remove, saveAttachment, saveAttachment, search, setPermissions
public DatabaseCache(Database database, Cache<java.lang.String,java.lang.Object> cacheInstance)
database
- non-null data structure with information about the database connectioncacheInstance
- non-null cache instance which has already been created and initializedpublic Cache<java.lang.String,java.lang.Object> getCache()
public <T> T find(java.lang.Class<T> classType, java.lang.String id)
Preferentially use the cache for the find operation. Adds the retrieved T to the cache if it was not present and was found in the remote database.
Retrieve the document with the specified ID from the database and deserialize to an instance of the POJO of type T.find
in class Database
T
- object typeclassType
- the class of type Tid
- the document idDatabase.find(Class, String, String)
,
Documents -
readpublic <T> T find(java.lang.Class<T> classType, java.lang.String id, Params params)
Preferentially use the cache for the find operation. Adds the retrieved T to the cache if it was not present and was found in the remote database.
Retrieve the document with the specified ID from the database and deserialize to an instance of the POJO of type T. Uses the additional parameters specified when making theGET
request.
Example usage to get inline attachments:
Foo foo = db.find(Foo.class, "exampleId", new Params().attachments());
String attachmentData = foo.getAttachments().get("attachment.txt").getData();
find
in class Database
T
- object typeclassType
- the class of type Tid
- the document idparams
- extra parameters to appendParams
,
Documents -
readpublic <T> T findAny(java.lang.Class<T> classType, java.lang.String uri)
Preferentially use the cache for the find operation. Adds the retrieved T to the cache if it was not present and was found in the remote database.
Note that this method uses the URI as the cache key (and not document ID) since the
document may come from a different database or server. As a result even if the same object
is already stored in the cache under its document ID it would be retrieved remotely by
this method and re-stored in the cache with a URI key. This also prevents the remove
method from removing these objects from the cache so it is recommended to only use this
method with a CacheWithLifetimes
cache to avoid the cache growing unbounded.
The URI must be URI-encoded.
Example usage retrieving the Foo POJO with document ID "exampleId" from the database "exampleDb" in the "example" Cloudant account.
Foo foo = db.findAny(Foo.class, "https://example.cloudant.com/exampleDb/exampleId");
public boolean contains(java.lang.String id)
Checks if the cache contains the specified document. If it does not then checks if the database contains the specified document.
Checks if a document exists in the database.public Response save(java.lang.Object object)
If the serialized object's JSON does not contain an _id
field, then a UUID will
be generated for the document ID.
Example of inserting a JsonObject into the database:
JsonObject json = new JsonObject();
json.addProperty("_id", "test-doc-id-2");
json.add("json-array", new JsonArray());
Response response = db.save(json);
Example of inserting a POJO into the database:
Foo foo = new Foo();
Response response = db.save(foo);
Example of inserting a Map into the database:
Map<String, Object> map = new HashMap<>();
map.put("_id", "test-doc-id-1");
map.put("title", "test-doc");
Response response = db.save(map);
Note that the Java object is not modified by the save operation and so will not include the revision generated by the database server. You can obtain the server revision for this write from the response, for example:
Foo foo = new Foo();
Response response = db.save(foo);
foo.setRevision(response.getRevision());
If the operation was successful then the object is also added to the cache.
public Response save(java.lang.Object object, int writeQuorum)
Database.save(Object)
but using a
specific write quorum.
If the operation was successful then the object is also added to the cache.
save
in class Database
object
- the object to savewriteQuorum
- the write quorumResponse
Database.save(Object)
,
Documents - quorumpublic Response post(java.lang.Object object)
POST
request.
If the serialized object's JSON does not contain an _id
field, then the server
will generate a document ID.
Example of creating a document in the database for a POJO:
Foo foo = new Foo();
Response response = db.post(foo);
Note that the Java object is not modified by the create operation and so will not include the revision generated by the database server. You can obtain the server revision for this write from the response, for example:
Foo foo = new Foo();
Response response = db.post(foo);
foo.setRevision(response.getRevision());
If the operation was successful then the object is also added to the cache.
post
in class Database
object
- The object to saveResponse
public Response post(java.lang.Object object, int writeQuorum)
Database.post(Object)
but using a
specific write quorum.
If the operation was successful then the object is also added to the cache.
post
in class Database
object
- The object to savewriteQuorum
- the write QuorumResponse
Database.post(Object)
,
Documents - quorumpublic Response update(java.lang.Object object)
_id
and
_rev
values.
Example usage:
//get a Bar object from the database
Bar bar = db.find(Bar.class, "exampleId");
//change something about bar
bar.setSomeProperty(true);
//now update the remote Bar
Response responseUpdate = db.update(bar);
Note that the Java object is not modified by the update operation and so will not include the revision generated by the database server. You can obtain the server revision for this write from the response, for example:
Foo foo = new Foo();
Response response = db.update(foo);
foo.setRevision(response.getRevision());
If the operation was successful then the object is also updated in the cache.
update
in class Database
object
- the object to updateResponse
public Response update(java.lang.Object object, int writeQuorum)
Database.update(Object)
, but specifying the
write quorum.
If the operation was successful then the object is also updated in the cache.
update
in class Database
object
- the object to updatewriteQuorum
- the write quorumResponse
Database.update(Object)
,
Documents - quorumpublic Response remove(java.lang.Object object)
The object must have the correct _id
and _rev
values.
Example usage:
//get a Bar object from the database
Bar bar = db.find(Bar.class, "exampleId");
//now remove the remote Bar
Response response = db.remove(bar);
If the operation was successful then the object is also removed from the cache.
remove
in class Database
object
- the document to remove as an objectResponse
public java.util.List<Response> bulk(java.util.List<?> list)
_bulk_docs
endpoint to insert multiple documents into the database in a
single HTTP request.
Example usage:
//create a list
List<Object> newDocs = new ArrayList<Object>();
//add some objects to the list
newDocs.add(new Foo());
newDocs.add(new JsonObject());
//use the bulk insert
List<Response> responses = db.bulk(newDocs);
Objects are added to or updated in the cache if their remote operation completes successfully.
bulk
in class Database
list
- the List
of objectsList<Response>
one per object