1. Technology

C# Tutorial Two on writing Code with NoSQL databases

By

NoSQL

Continuing on from the first Tutorial, This uses C# to store data in Couchbase, modify it and then subsequently retrieve it.

Compared to relational databases, (see What is a relational database?)) Couchbase is very easy to use.  

Setting up for Visual C#

Read this page carefully. You need to download the client zip file and unzip the contents into a folder then include it in your project. Then you add the dlls as references, plus add a App.config (see mine in the project) and can then use Couchbase.

The .NET Client API

Bookmark this API list link which shows all the different methods (there are almost 60) that the API provides.

Many of these you'll probably never use (like prepending values) but there are some interesting concepts like CAS and error checking that you need to know. I'll look at them in greater depth in later tutorials.

Keys and Values

Values first. A bucket (the unit of storage, I like to think of it as the equivalent to a database instance) can hold objects up to 20 MB in size (this is thd Couchbase buckets not memcached which are 1Mb in size). Smaller objects give you greater cache hits so are faster. The value it stores is effectively a blob (again talking in RDBMS terms).

You can store single items, ints, strings etc, or you can store C# objects so long as the C# class is marked serializable which means .NET knows how to save it out in a way that it can retrieve it.

If you are working with data that's read say from a PHP or Ruby website then serialized C# classes are not going to be very useful. You should use XML or JSON to store structured data in one object. The version 2.0 server will have better support for JSON but for now this has section this page shows how to read and write JSON documents through extension methods.

So in summary, a value can be anything from an int, to an instance of a class, etc just so long as it's less than 20MB in size.

The API allows multiple values to be fetched in one call which is faster than fetching them individually.

Keys

A key is used to retrieve data. If you don't know the key, you can't get the data. (True for 1.8, but not for version 2.0 which is still at developer preview not fully released you can search by Views).

You define the keys and they can be alpha-numerics. They can be anything from names, numbers or even guids.

At it's heart, once the client is installed and you add these usings using Couchbase;
using Enyim.Caching.Memcached;

then you call this to save a value:

client.Store(StoreMode.Add, "key","value");
var value = client.Get("key") as String; // or
var value = client.Get("key");

That's about enough to get you started. You can store an instance of a class by marking it as Serializable.

[Serializable]

Couchbase is very fast at storing and retrieving values. You can call the functions many thousands of times a second. On my PC, I was able to store a 5,000 string array (each string being 4 chars) in about 3 secs. Reading them back took 3 secs and you can download the zipped up project with source code.

Tims can vary, the first time you fetch data, it's pulled from the disk. When I ran this later with 5000 x 12 char strings it took 4.3 seconds the first time then 1.4 to 2 seconds on subsequent runs.

Some code

  public void BuildAndSaveList(CouchbaseClient client)
  {
     var r = new Random() ;
    for (var i = 0; i < 5000; i++)
    {
      char[] chStr = {(char)(r.Next(32) + 64), (char) (r.Next(32) + 64),
          (char) (r.Next(32) + 64),(char)(r.Next(32) + 64)};
      var item = new MyListItem { Id = i, Value = new string(chStr) };
      var key = "index" + i;
      client.Store(StoreMode.Add, key, item) ;
    }
  }

This creates 5000 strings each made up of four random chars then puts them in the MyListItem object instance, creates a text index index0,index1,index2 etc and stores it in the Couchbase bucket with the client.Store( method.

In the next tutorial I'll look at some of the client commands including expiry, and the CAS (Check and Select) commands which are how you update data in a multi-user environment. 

  1. About.com
  2. Technology
  3. C / C++ / C#
  4. Pro. Development
  5. Programming Couchbase in C# Tutorial two

©2014 About.com. All rights reserved.