Tuesday 7 October 2014

Attachments to a Document


Apart from the trivial storage facilities for text there is huge requirement for the storage of  the non-textual data like media files. Couchbase lite provides a mechanism to store the content of the media files as an attachments rather as a part of the document's JSON content.

Attachments store data associated with a document but are not the part of document's JSON object. Their primary purpose is to make it efficient to store large binary data in a document. Binary data stored in JSON has to be base64-encoded into a string, which inflates its size by 33%. Also, binary data blobs are often large (think of camera images or audio files), and big JSON documents are slow to parse. 

A document can have any number of attachments, each with a different name. The MIME type information of the attachment is also stored which is only relevant for the app for interpreting its content.

In native api an attachment to a document is represented by class Attachment.


Major operations on attachment to a document are :

1. Creating or updating an attachment : 
To create an attachment, first create a mutable UnsavedRevision object by calling createRevision on the document's currentRevision. Then call setAttachment on the new revision to add an attachment. (You can of course also change the JSON by modifying the revision's properties.) Finally you call save to save the new revision.

Updating an attachment's content (or type) works exactly the same way.

final Document currentDocument = mCouchbaseDatabase.getDocument(iDocId);
UnsavedRevision newRev = currentDocument.getCurrentRevision().createRevision();
if (is == null)
{
   is = getAssets().open("droid.jpg");
   newRev.setAttachment("droid.jpg", "image/jpg", is);
   newRev.save();
}


2. Reading an attachment :
An attachment can be accessed by Revision object which in turn is accessed by Document's currentRevision. And then call getAttachment on the Revision object

final Document currentDocument = mCouchbaseDatabase.getDocument(iDocId);
final SavedRevision savedRevision = currentDocument.getCurrentRevision();
Attachment att = savedRevision.getAttachment("droid.jpg");
if (att != null)
{
 InputStream is = att.getContent();
 Drawable attachDrawable = Drawable.createFromStream(is, "droid.jpg");

 ImageView imv = new ImageView(this);
 imv.setImageDrawable(attachDrawable);
 LayoutParams pars = new LayoutParams(WIDTH_HEIGHT, WIDTH_HEIGHT);
 imv.setLayoutParams(pars);

 // Set ImageView to the Toast
 Toast testToast = new Toast(this);
 testToast.setView(imv);
 testToast.setDuration(Toast.LENGTH_LONG);
 testToast.show();
}



3. Deleting an attachment
An attachment can be deleted by calling removeAttachment on Revision object


final Document currentDocument = mCouchbaseDatabase.getDocument(iDocId);
UnsavedRevision newRev = currentDocument.getCurrentRevision().createRevision();
newRev.removeAttachment("droid.jpg");
newRev.save();


Storage without any Duplication
Attachments aren't stored in the database file itself. Instead they are individual files, contained in a directory right next to the database file. Each attachment file has a cryptic name that is actually a SHA-1 digest of its contents.

As a consequence of the naming scheme, attachments are de-duplicated: if multiple attachments in the same database have exactly the same contents, the data is only stored once in the filesystem.

                                       
Please find the complete source code here SourceCode

No comments:

Post a Comment