org.ektorp.impl
Class StdCouchDbConnector

java.lang.Object
  extended by org.ektorp.impl.StdCouchDbConnector
All Implemented Interfaces:
CouchDbConnector

public class StdCouchDbConnector
extends Object
implements CouchDbConnector

Author:
henrik lundgren

Constructor Summary
StdCouchDbConnector(String databaseName, CouchDbInstance dbInstance)
           
StdCouchDbConnector(String databaseName, CouchDbInstance dbi, ObjectMapperFactory om)
           
 
Method Summary
 void addToBulkBuffer(Object o)
          Add the object to the bulk buffer attached to the executing thread.
 String callUpdateHandler(String designDocID, String function, String docID)
           
 String callUpdateHandler(String designDocID, String function, String docID, Map<String,String> params)
           
 String callUpdateHandler(UpdateHandlerRequest req)
           
<T> T
callUpdateHandler(UpdateHandlerRequest req, Class<T> c)
           
 List<DocumentChange> changes(ChangesCommand cmd)
          Queries the database for changes.
 StreamingChangesResult changesAsStream(ChangesCommand cmd)
          Queries the database for changes. this operation gives you the result as a iterable stream of documentchange objects, the stream should be closed when finished
 ChangesFeed changesFeed(ChangesCommand cmd)
          Sets up a continuous changes feed.
 void cleanupViews()
          View indexes on disk are named after their MD5 hash of the view definition.
 void clearBulkBuffer()
          Clears the bulk buffer attached the the executing thread.
 void compact()
          Compaction compresses the database file by removing unused sections created during updates.
 void compactViews(String designDocumentId)
          This compacts the view index from the current version of the design document.
 boolean contains(String id)
          Check if the database contains a document.
 void create(Object o)
          Creates the Object as a document in the database.
 void create(String id, Object node)
           
 String createAttachment(String docId, AttachmentInputStream data)
          Creates both the document and the attachment
 String createAttachment(String docId, String revision, AttachmentInputStream data)
          Adds an attachment to the specified document id.
 void createDatabaseIfNotExists()
          Creates a database on the configured path if it does not exists.
 String delete(Object o)
          Deletes the Object in the database.
 String delete(String id, String revision)
          Deletes the document in the database.
 String deleteAttachment(String docId, String revision, String attachmentId)
           
 void ensureFullCommit()
          Commits any recent changes to the specified database to disk.
 List<DocumentOperationResult> executeAllOrNothing(Collection<?> objects)
          Creates, updates or deletes all objects in the supplied collection.
 List<DocumentOperationResult> executeAllOrNothing(InputStream inputStream)
          Creates, updates or deletes all objects in the supplied collection.
 List<DocumentOperationResult> executeBulk(Collection<?> objects)
          Creates, updates or deletes all objects in the supplied collection.
 List<DocumentOperationResult> executeBulk(InputStream inputStream)
          Creates, updates or deletes all objects in the supplied collection.
<T> T
find(Class<T> c, String id)
          Same as get(Class c, String id) with the difference that null is return if the document was not found.
<T> T
find(Class<T> c, String id, Options options)
          Same as get(Class c, String id, Options options) with the difference that null is return if the document was not found.
 List<DocumentOperationResult> flushBulkBuffer()
          Sends the bulk buffer attached the the executing thread to the database (through a executeBulk call).
<T> T
get(Class<T> c, String id)
           
<T> T
get(Class<T> c, String id, Options options)
           
<T> T
get(Class<T> c, String id, String rev)
           
 List<String> getAllDocIds()
           
 InputStream getAsStream(String id)
          Please note that the stream has to be closed after usage, otherwise http connection leaks will occur and the system will eventually hang due to connection starvation.
 InputStream getAsStream(String id, Options options)
          Please note that the stream has to be closed after usage, otherwise http connection leaks will occur and the system will eventually hang due to connection starvation.
 InputStream getAsStream(String id, String rev)
          Please note that the stream has to be closed after usage, otherwise http connection leaks will occur and the system will eventually hang due to connection starvation.
 AttachmentInputStream getAttachment(String id, String attachmentId)
          Reads an attachment from the database.
 AttachmentInputStream getAttachment(String id, String attachmentId, String revision)
           
 HttpClient getConnection()
          Convenience method for accessing the underlying HttpClient.
 String getDatabaseName()
           
 DbInfo getDbInfo()
          Provides meta information about this database.
 DesignDocInfo getDesignDocInfo(String designDocId)
          Obtains information about a given design document, including the index, index size and current status of the design document and associated index information.
 int getRevisionLimit()
          Revision limit defines a upper bound of document revisions which CouchDB keeps track of
 List<Revision> getRevisions(String id)
           
<T> T
getWithConflicts(Class<T> c, String id)
          Will load the document with any conflicts included.
 String path()
           
 PurgeResult purge(Map<String,List<String>> revisionsToPurge)
          Permanently removes the references to deleted documents from the database.
<T> Page<T>
queryForPage(ViewQuery query, PageRequest pr, Class<T> type)
          Provides paged view results.
 InputStream queryForStream(ViewQuery query)
           
 StreamingViewResult queryForStreamingView(ViewQuery query)
          Please note that the StreamingViewResult need to be closed after usage.
 ViewResult queryView(ViewQuery query)
           
<T> List<T>
queryView(ViewQuery query, Class<T> type)
          This method requires the view result values to be document ids or documents : If the value is a document id, then the document is fetched from couchDB.
 ReplicationStatus replicateFrom(String source)
          Replicate the content in the source database into this database.
 ReplicationStatus replicateFrom(String source, Collection<String> docIds)
          Replicate the content in the source database into this database.
 ReplicationStatus replicateTo(String target)
          Replicate the content in this database into the specified target database.
 ReplicationStatus replicateTo(String target, Collection<String> docIds)
          Replicate the content in this database into the specified target database.
 void setJsonSerializer(JsonSerializer js)
           
 void setRevisionLimit(int limit)
           
 void update(Object o)
          Updates the document.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StdCouchDbConnector

public StdCouchDbConnector(String databaseName,
                           CouchDbInstance dbInstance)

StdCouchDbConnector

public StdCouchDbConnector(String databaseName,
                           CouchDbInstance dbi,
                           ObjectMapperFactory om)
Method Detail

path

public String path()
Specified by:
path in interface CouchDbConnector
Returns:

create

public void create(Object o)
Description copied from interface: CouchDbConnector
Creates the Object as a document in the database. If the id is not set it will be generated by the database. The Object's revision field will be updated through the setRevision(String s) method.

Specified by:
create in interface CouchDbConnector

create

public void create(String id,
                   Object node)
Specified by:
create in interface CouchDbConnector

contains

public boolean contains(String id)
Description copied from interface: CouchDbConnector
Check if the database contains a document.

Specified by:
contains in interface CouchDbConnector
Returns:
true if a document with the id exists in the database

createAttachment

public String createAttachment(String docId,
                               AttachmentInputStream data)
Description copied from interface: CouchDbConnector
Creates both the document and the attachment

Specified by:
createAttachment in interface CouchDbConnector
Returns:
revision of the created attachment document

createAttachment

public String createAttachment(String docId,
                               String revision,
                               AttachmentInputStream data)
Description copied from interface: CouchDbConnector
Adds an attachment to the specified document id.

Specified by:
createAttachment in interface CouchDbConnector
Returns:
the new revision of the document

getAttachment

public AttachmentInputStream getAttachment(String id,
                                           String attachmentId)
Description copied from interface: CouchDbConnector
Reads an attachment from the database. Please note that the stream has to be closed after usage, otherwise http connection leaks will occur and the system will eventually hang due to connection starvation.

Specified by:
getAttachment in interface CouchDbConnector
Returns:
the attachment in the form of an AttachmentInputStream.

getAttachment

public AttachmentInputStream getAttachment(String id,
                                           String attachmentId,
                                           String revision)
Specified by:
getAttachment in interface CouchDbConnector

delete

public String delete(Object o)
Description copied from interface: CouchDbConnector
Deletes the Object in the database.

Specified by:
delete in interface CouchDbConnector
Returns:
the revision of the deleted document

purge

public PurgeResult purge(Map<String,List<String>> revisionsToPurge)
Description copied from interface: CouchDbConnector
Permanently removes the references to deleted documents from the database.

Specified by:
purge in interface CouchDbConnector
Parameters:
revisionsToPurge - document IDs & revisions to be purged
Returns:
contains the purge sequence number, and a list of the document IDs and revisions successfully purged.

get

public <T> T get(Class<T> c,
                 String id)
Specified by:
get in interface CouchDbConnector
Parameters:
c - the target class to map to.
id - the id of the document in the database.
Returns:
the document mapped as the specified class.

get

public <T> T get(Class<T> c,
                 String id,
                 Options options)
Specified by:
get in interface CouchDbConnector
Parameters:
c - the target class to map to.
id - the id of the document in the database.
Returns:
the document mapped as the specified class.

get

public <T> T get(Class<T> c,
                 String id,
                 String rev)
Specified by:
get in interface CouchDbConnector
Parameters:
c - the target class to map to.
id - the id of the document in the database.
rev - of the object.
Returns:
the document mapped as the specified class.

getWithConflicts

public <T> T getWithConflicts(Class<T> c,
                              String id)
Description copied from interface: CouchDbConnector
Will load the document with any conflicts included.

Specified by:
getWithConflicts in interface CouchDbConnector
Parameters:
c - the target class to map to.
id - the id of the document in the database.
Returns:
the document mapped as the specified class.

find

public <T> T find(Class<T> c,
                  String id)
Description copied from interface: CouchDbConnector
Same as get(Class c, String id) with the difference that null is return if the document was not found.

Specified by:
find in interface CouchDbConnector
Returns:
null if the document was not found.

find

public <T> T find(Class<T> c,
                  String id,
                  Options options)
Description copied from interface: CouchDbConnector
Same as get(Class c, String id, Options options) with the difference that null is return if the document was not found.

Specified by:
find in interface CouchDbConnector
Returns:
null if the document was not found.

getRevisions

public List<Revision> getRevisions(String id)
Specified by:
getRevisions in interface CouchDbConnector
Returns:

getAsStream

public InputStream getAsStream(String id)
Description copied from interface: CouchDbConnector
Please note that the stream has to be closed after usage, otherwise http connection leaks will occur and the system will eventually hang due to connection starvation.

Specified by:
getAsStream in interface CouchDbConnector
Returns:
the document as raw json in an InputStream, don't forget to close the stream when finished.

getAsStream

public InputStream getAsStream(String id,
                               Options options)
Description copied from interface: CouchDbConnector
Please note that the stream has to be closed after usage, otherwise http connection leaks will occur and the system will eventually hang due to connection starvation.

Specified by:
getAsStream in interface CouchDbConnector
Returns:
the document as raw json in an InputStream, don't forget to close the stream when finished.

getAsStream

public InputStream getAsStream(String id,
                               String rev)
Description copied from interface: CouchDbConnector
Please note that the stream has to be closed after usage, otherwise http connection leaks will occur and the system will eventually hang due to connection starvation.

Specified by:
getAsStream in interface CouchDbConnector
Returns:
the document as raw json in an InputStream, don't forget to close the stream when finished.

update

public void update(Object o)
Description copied from interface: CouchDbConnector
Updates the document. The Object's revision field will be updated through the setRevision(String s) method.

Specified by:
update in interface CouchDbConnector

delete

public String delete(String id,
                     String revision)
Description copied from interface: CouchDbConnector
Deletes the document in the database.

Specified by:
delete in interface CouchDbConnector
Returns:
the revision of the deleted document.

getAllDocIds

public List<String> getAllDocIds()
Specified by:
getAllDocIds in interface CouchDbConnector
Returns:
all document ids in the database including design document ids.

createDatabaseIfNotExists

public void createDatabaseIfNotExists()
Description copied from interface: CouchDbConnector
Creates a database on the configured path if it does not exists.

Specified by:
createDatabaseIfNotExists in interface CouchDbConnector

getDatabaseName

public String getDatabaseName()
Specified by:
getDatabaseName in interface CouchDbConnector
Returns:
name

queryView

public <T> List<T> queryView(ViewQuery query,
                             Class<T> type)
Description copied from interface: CouchDbConnector
This method requires the view result values to be document ids or documents : {"_id":"_design/ExampleDoc", "views":{ "all": {"map": "function(doc) { emit(null, doc._id);}"}, "by_name": {"map": "function(doc) { emit(doc.name, doc._id);}"} // emit doc id "by_author": {"map": "function(doc) { emit(doc.author, doc);}"} // emit doc } }

Specified by:
queryView in interface CouchDbConnector
type - the type to map the result to
Returns:
the view result mapped as the specified class.

queryForPage

public <T> Page<T> queryForPage(ViewQuery query,
                                PageRequest pr,
                                Class<T> type)
Description copied from interface: CouchDbConnector
Provides paged view results. Implementation based on the recipe described in the book "CouchDB The Definitive Guide" http://guide.couchdb.org/editions/1/en/recipes.html#pagination This method has the same requirements for the view as the method queryView(ViewQuery query, Class type).

Specified by:
queryForPage in interface CouchDbConnector
Returns:

queryView

public ViewResult queryView(ViewQuery query)
Specified by:
queryView in interface CouchDbConnector
Returns:

queryForStreamingView

public StreamingViewResult queryForStreamingView(ViewQuery query)
Description copied from interface: CouchDbConnector
Please note that the StreamingViewResult need to be closed after usage.

Specified by:
queryForStreamingView in interface CouchDbConnector
Returns:
the view result as a iterable stream.

queryForStream

public InputStream queryForStream(ViewQuery query)
Specified by:
queryForStream in interface CouchDbConnector
Returns:
the view result as a raw InputStream.

deleteAttachment

public String deleteAttachment(String docId,
                               String revision,
                               String attachmentId)
Specified by:
deleteAttachment in interface CouchDbConnector
Returns:
the new revision of the document

getConnection

public HttpClient getConnection()
Description copied from interface: CouchDbConnector
Convenience method for accessing the underlying HttpClient. Preferably used wrapped in a org.ektorp.http.RestTemplate.

Specified by:
getConnection in interface CouchDbConnector
Returns:

getDbInfo

public DbInfo getDbInfo()
Description copied from interface: CouchDbConnector
Provides meta information about this database.

Specified by:
getDbInfo in interface CouchDbConnector
Returns:

getDesignDocInfo

public DesignDocInfo getDesignDocInfo(String designDocId)
Description copied from interface: CouchDbConnector
Obtains information about a given design document, including the index, index size and current status of the design document and associated index information.

Specified by:
getDesignDocInfo in interface CouchDbConnector
Returns:

compact

public void compact()
Description copied from interface: CouchDbConnector
Compaction compresses the database file by removing unused sections created during updates. This call is non-blocking, a compaction background task will be created on the CouchDB instance.

Specified by:
compact in interface CouchDbConnector

cleanupViews

public void cleanupViews()
Description copied from interface: CouchDbConnector
View indexes on disk are named after their MD5 hash of the view definition. When you change a view, old indexes remain on disk. To clean up all outdated view indexes (files named after the MD5 representation of views, that does not exist anymore) you can trigger a view cleanup

Specified by:
cleanupViews in interface CouchDbConnector

replicateFrom

public ReplicationStatus replicateFrom(String source)
Description copied from interface: CouchDbConnector
Replicate the content in the source database into this database.

Specified by:
replicateFrom in interface CouchDbConnector
Parameters:
source - database
Returns:
ReplicationStatus

replicateFrom

public ReplicationStatus replicateFrom(String source,
                                       Collection<String> docIds)
Description copied from interface: CouchDbConnector
Replicate the content in the source database into this database. Replication is restricted to the specified document ids.

Specified by:
replicateFrom in interface CouchDbConnector
Parameters:
source - database
Returns:
ReplicationStatus

replicateTo

public ReplicationStatus replicateTo(String target)
Description copied from interface: CouchDbConnector
Replicate the content in this database into the specified target database. The target must exist.

Specified by:
replicateTo in interface CouchDbConnector
Parameters:
target - database
Returns:
ReplicationStatus

replicateTo

public ReplicationStatus replicateTo(String target,
                                     Collection<String> docIds)
Description copied from interface: CouchDbConnector
Replicate the content in this database into the specified target database. Replication is restricted to the specified document ids. The target must exist.

Specified by:
replicateTo in interface CouchDbConnector
Parameters:
target - database
Returns:
ReplicationStatus

compactViews

public void compactViews(String designDocumentId)
Description copied from interface: CouchDbConnector
This compacts the view index from the current version of the design document. This call is non-blocking, a compaction background task will be created on the CouchDB instance.

Specified by:
compactViews in interface CouchDbConnector

executeAllOrNothing

public List<DocumentOperationResult> executeAllOrNothing(InputStream inputStream)
Description copied from interface: CouchDbConnector
Creates, updates or deletes all objects in the supplied collection. In the case of a power failure, when the database restarts either all the changes will have been saved or none of them. However, it does not do conflict checking, so the documents will be committed even if this creates conflicts.

Specified by:
executeAllOrNothing in interface CouchDbConnector
Returns:
The list will only contain entries for documents that has any kind of error code returned from CouchDB. i.e. the list will be empty if everything was completed successfully.

executeBulk

public List<DocumentOperationResult> executeBulk(InputStream inputStream)
Description copied from interface: CouchDbConnector
Creates, updates or deletes all objects in the supplied collection. If the json has no revision set, it will be created, otherwise it will be updated. If the json document contains a "_deleted"=true field it will be deleted. Some documents may successfully be saved and some may not. The response will tell the application which documents were saved or not. In the case of a power failure, when the database restarts some may have been saved and some not.

Specified by:
executeBulk in interface CouchDbConnector
Returns:
The list will only contain entries for documents that has any kind of error code returned from CouchDB. i.e. the list will be empty if everything was completed successfully.

executeAllOrNothing

public List<DocumentOperationResult> executeAllOrNothing(Collection<?> objects)
Description copied from interface: CouchDbConnector
Creates, updates or deletes all objects in the supplied collection. In the case of a power failure, when the database restarts either all the changes will have been saved or none of them. However, it does not do conflict checking, so the documents will be committed even if this creates conflicts.

Specified by:
executeAllOrNothing in interface CouchDbConnector
Parameters:
objects - , all objects will have their id and revision set.
Returns:
The list will only contain entries for documents that has any kind of error code returned from CouchDB. i.e. the list will be empty if everything was completed successfully.

executeBulk

public List<DocumentOperationResult> executeBulk(Collection<?> objects)
Description copied from interface: CouchDbConnector
Creates, updates or deletes all objects in the supplied collection. If the object has no revision set, it will be created, otherwise it will be updated. If the object's serialized json document contains a "_deleted"=true field it will be deleted. org.ektorp.BulkDeleteDocument.of(someObject) is the easiest way to create a delete doc for an instance. Some documents may successfully be saved and some may not. The response will tell the application which documents were saved or not. In the case of a power failure, when the database restarts some may have been saved and some not.

Specified by:
executeBulk in interface CouchDbConnector
Parameters:
objects - , all objects will have their id and revision set.
Returns:
The list will only contain entries for documents that has any kind of error code returned from CouchDB. i.e. the list will be empty if everything was completed successfully.

addToBulkBuffer

public void addToBulkBuffer(Object o)
Description copied from interface: CouchDbConnector
Add the object to the bulk buffer attached to the executing thread. A subsequent call to either flushBulkBuffer or clearBulkBuffer is expected.

Specified by:
addToBulkBuffer in interface CouchDbConnector

clearBulkBuffer

public void clearBulkBuffer()
Description copied from interface: CouchDbConnector
Clears the bulk buffer attached the the executing thread.

Specified by:
clearBulkBuffer in interface CouchDbConnector

flushBulkBuffer

public List<DocumentOperationResult> flushBulkBuffer()
Description copied from interface: CouchDbConnector
Sends the bulk buffer attached the the executing thread to the database (through a executeBulk call). The bulk buffer will be cleared when this method is finished.

Specified by:
flushBulkBuffer in interface CouchDbConnector

setJsonSerializer

public void setJsonSerializer(JsonSerializer js)

getRevisionLimit

public int getRevisionLimit()
Description copied from interface: CouchDbConnector
Revision limit defines a upper bound of document revisions which CouchDB keeps track of

Specified by:
getRevisionLimit in interface CouchDbConnector
Returns:

setRevisionLimit

public void setRevisionLimit(int limit)
Specified by:
setRevisionLimit in interface CouchDbConnector

changes

public List<DocumentChange> changes(ChangesCommand cmd)
Description copied from interface: CouchDbConnector
Queries the database for changes. This is a one-off operation. To listen to changes continuously @see changesFeed(ChangesCommand cmd).

Specified by:
changes in interface CouchDbConnector
Returns:

changesAsStream

public StreamingChangesResult changesAsStream(ChangesCommand cmd)
Description copied from interface: CouchDbConnector
Queries the database for changes. this operation gives you the result as a iterable stream of documentchange objects, the stream should be closed when finished

Specified by:
changesAsStream in interface CouchDbConnector
Returns:

changesFeed

public ChangesFeed changesFeed(ChangesCommand cmd)
Description copied from interface: CouchDbConnector
Sets up a continuous changes feed. The current update sequence in the DB will be used if ChangesCommand does not specify the since parameter. A heartbeat interval of 10 seconds will be used if ChangesCommand does not specify the heartbeat parameter.

Specified by:
changesFeed in interface CouchDbConnector
Returns:
a running changes feed that buffers incoming changes in a unbounded queue (will grow until OutOfMemoryException if not polled).

callUpdateHandler

public String callUpdateHandler(String designDocID,
                                String function,
                                String docID)
Specified by:
callUpdateHandler in interface CouchDbConnector
Returns:

callUpdateHandler

public String callUpdateHandler(String designDocID,
                                String function,
                                String docID,
                                Map<String,String> params)
Specified by:
callUpdateHandler in interface CouchDbConnector
Returns:

callUpdateHandler

public String callUpdateHandler(UpdateHandlerRequest req)
Specified by:
callUpdateHandler in interface CouchDbConnector

callUpdateHandler

public <T> T callUpdateHandler(UpdateHandlerRequest req,
                               Class<T> c)
Specified by:
callUpdateHandler in interface CouchDbConnector

ensureFullCommit

public void ensureFullCommit()
Description copied from interface: CouchDbConnector
Commits any recent changes to the specified database to disk.

Specified by:
ensureFullCommit in interface CouchDbConnector


Copyright © 2011. All Rights Reserved.