Saturday, April 15, 2017

Understanding Meta data stored by Couchbase for each Document

Along with document, Couchbase also stores some of the important META data. This post talks about those meta details and then also shows how they can be read using N1QL query.

CAS:
CAS is acronym for check-and-set (and compare and swap) and it's a technique to do optimistic locking. This is a unique identifier associated with document which can be used to prevent the update to a document if the CAS value is not same as the original one. The intent is that, instead of (pessimistically) locking the record you read the CAS value along with document and then perform the write if and only if CAS value matches. More details on Couchbase developer pages (recent page, and a bit old link). 

CAS represents the current state of the document and each time the document is modified CAS also gets updated. Couchbase server automatically provides this value for each document and it's type is 64-bit integer. 
More details on CAS here

FLAGS:
Identifies type of data stored or formatting. 

ID:
Unique document identifier per bucket. Document ID should conform to UTF-8 encoding. Couchbase stores all keys in RAM, so keep in mind this fact before deciding the length of keys. Also, Couchbase doesn't by default generate keys for the document if you don't provide one explicitly. 


Let's see these details through queries:

N1QL Query


cbq> SELECT meta(b) FROM `bucket_name` b WHERE meta(b).id = "01674B3Z6C3H";
{
    "requestID": "c42ddb7e-75ef-461a-a9df-1b458e4b03fa",
    "signature": {
        "$1": "object"
    },
    "results": [
        {
            "$1": {
                "cas": 1491370937556664300,
                "flags": 33554432,
                "id": "01674B3Z6C3H",
                "type": "json"
            }
        }
    ],
    "status": "success",
    "metrics": {
        "elapsedTime": "100.949282ms",
        "executionTime": "100.869461ms",
        "resultCount": 1,
        "resultSize": 193
    }

}


--happy learning !




No comments:

Post a Comment