Couchbase Server 7.0 now supports N1QL transactions.

Let’s try writing a C program that performs a set of transactions on a single node.

Step 1: We first decide how to call the program. The inputs will be the URL to the Couchbase bucket that we wish to run queries against, and the credentials (username followed by the password). 

Usage:

With N1QL transactions, a txid value is returned from the START TRANSACTION command. This is used with all subsequent N1QL queries within the transaction until the final COMMIT or ROLLBACK. So we must declare a transaction ID in the main function that we can pass to the remaining query requests that are part of the transaction. 

Step 2: Initialize a pointer that will be used to save the transaction ID from the BEGIN TRANSACTION query. This ID will be used in the entire transaction.


Step 3: Initialize the cluster 

A connection to a Couchbase Server cluster is represented by a lcb_INSTANCE object.

lcb_INSTANCE *instance;

The set of allowed operations depends on the type of this object, and whether the bucket is associated with it. Here we use the type Cluster. The simplest way to create a cluster object is to call lcb_create to create a Couchbase handle by passing LCB_TYPE_CLUSTER with a connection string, username, and password. Next, we schedule a connection using lcb_connect(), then we check if the bucket exists. 


Step 4: Run the queries in query.h

We have the queries corresponding to our transaction defined in queries.h. 


We need to use a JSON parsing library in C to handle the query results to extract the transaction ID. Here we can use the json-c library. In order to get the transaction ID from the BEGIN WORK statement (the first statement), we use the txid callback function. For processing and running the other queries, we call the row callback function. This will return the result rows. 


Prior to running the queries, we set three query parameters: pretty, txtimeout (transaction timeout), and the txid that we got from the first statement. These are request-level parameters. 

Now let’s take a deep dive into the callback functions 

Txid_Callback – 

Here we need to parse the JSON response from running the BEGIN WORK statement to extract the txid to pass into the subsequent statements as a query parameter using the JSONC library. For this we use the previously created pointer and set it in the lcb_respquery_cookie method. This sets the operation cookie – which means when we run lcb_query, there is a cookie argument to it and lcb_respquery_cookie gets this pointer in our callback function. (We created a pointer in the main function and set it in the callback function)


Row callback – This is used to parse and retrieve the result rows from the query run. 


Here we get the query response, get the rows and print it. 

Using the above example, we can now use N1QL transactions in the C SDK. For the full code see and instructions on how to run it see – https://github.com/ikandaswamy/CBSDK_N1QLExamples

 

Author

Posted by Isha Kandaswamy

Isha Kandaswamy is a Senior Software Engineer at Couchbase. Isha is responsible for the development of designing the different features and tools for the N1QL Query Language -SQL for Json. Also, Designing and implementing features and tools for the N1QL query language.

Leave a reply