Couchbase.NET SDK v2.2.7 w/Developer Preview of Index Management now available!
Today we are releasing version 2.2.7 of the official Couchbase .NET SDK! This release notably contains supports for Index management and adds to the Sub-Document API which was released as a developer preview in v2.2.6 as well as a bug fix and improvements in future support of .NET Core.
Index Management
Global Secondary Indexes (GSIs) are an important piece of the N1QL query architecture in Couchbase server allowing for fast, low latency queries. You can place indexes on JSON elements, N1QL expressions and using filters via a WHERE clause. For v2.2.7 of the .NET SDK we have built an abstraction over the Couchbase Server indexing service which makes it very easy to create primary and secondary indexes. This abstraction extends and widens the BucketManager class which includes support for creating and managing Couchbase Buckets.
1 2 3 4 5 6 7 8 9 10 |
var configuration = new ClientConfiguration(); using (var cluster = new Cluster(configuration)) { using (var bucket = cluster.OpenBucket("beer-sample")) { var manager = bucket.CreateManager("Administrator", ""); } } |
The BucketManager is created by opening a Bucket object off a Cluster instance and passing in the correct configuration for your environment.
Creating and Dropping Primary Indexes
You can create named and unnamed primary indexes by calling the CreatePrimaryIndex and CreateNamedPrimaryIndex method once you have the BucketManager instance:
1 2 3 4 5 6 |
//create an unamed primary index on the bucket var result = manager.CreatePrimaryIndex(false); //create a named primary index called "myprimaryindex" on the bucket var result = manager.CreateNamedPrimaryIndex("myprimaryindex", false); |
Note that you are always working with the context of the bucket you have opened. So these indexes will be created on the “beer-sample” bucket we opened earlier. Also, not that both of these methods take a boolean field called “defer”, in fact, as you’ll see, all creational methods contain a “defer” parameter. If “defer” is true, the index will be created, however, it will not be built until the BuildDeferredIndexes method is called. More about deferred indexes later!
Dropping a named or unnamed primary index is pretty much the same as creating an index with the exception of the method names:
1 2 3 4 5 6 |
//drop the primary index for the bucket var result = manager.DropPrimaryIndex(); //drop a named primary index on the bucket var result = manager.DropNamedPrimaryIndex("myprimaryindex"); |
Note that if the index does not exist, then result.Success will be false.
Creating and Dropping Secondary Indexes
Creating and dropping secondary indexes is similar to primary indexes except you can index on a one or more fields as well.
1 2 3 4 5 6 |
//create a secondary index called "myindex" on the "id" and "name" fields. var result = manager.CreateIndex("myindex", true, "id", "name"); //drop the index called "myindex" var result = manager.DropIndex("myindex"); |
Note that in this case I am passing “true” so that the building of the index will be deferred. Also, if the index already exists or if it doesn’t exist and you try to drop it, result.Success will be false.
Updates to SubDocument API
This release also contains internal changes to the SubDocument API released as Developer Preview in 2.2.6. There are two major improvements related to performance were added:
- If a single mutation or lookup operation is executed the SDK will use a singular operation as opposed to a multi-operation which saves a few bytes over the wire.
- The deserialization of the response body is deferred now until you call Content
Lambda Expressions for Sub-Document path navigation
In addition to these changes, another new feature made its way into the release via a contribution from the community: Lambda expression navigation for Sub-Document paths. This feature allows you to use the familiar type-safe, intellisense supported syntax you enjoy from linq instead of just passing in string paths.
Assuming a document that looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
{ "name": "21st Amendment Brewery Cafe", "city": "San Francisco", "state": "California", "code": "94107", "country": "United States", "phone": "1-415-369-0900", "website": "http://www.21st-amendment.com/", "type": "brewery", "updated": "2010-10-24 13:54:07", "description": "The 21st Amendment Brewery offers a variety of award winning house made brews and American grilled cuisine in a comfortable loft like setting. Join us before and after Giants baseball games in our outdoor beer garden. A great location for functions and parties in our semi-private Brewers Loft. See you soon at the 21A!", "address": [ "563 Second Street" ], "geo": { "accuracy": "ROOFTOP", "lat": 37.7825, "lon": -122.393 } } |
Then you retrieve the value at the path “geo.accuracy” like this:
1 2 3 4 5 6 7 8 |
var key = "21st_amendment_brewery_cafe"; var lookup = bucket.LookupIn(key). Get(x => x.Geo.Accuracy). Execute(); var value = lookup.Content(x=>x.Geo.Accuracy); Assert.AreEqual("ROOFTOP", value); |
Of course, you could chain multiple “Gets” to retrieve each individual element from the document.
Release Notes for v2.2.7
Bug
- [NCBC-1099] – Add DataContract attribute to Error, Warning and Metrics classes
Improvement
- [NCBC-1103] – Support for async operations on subdocuments
- [NCBC-994] – Upgrade development environment and projects to VS2015
- [NCBC-1089] – Await all not succesful code paths
- [NCBC-1095] – Do not retry on NodeUnavailableException
New Feature
- [NCBC-1067] – Extend BucketManager to support Index Management
- [NCBC-1093] – Include support for Subdocument API – Part2 Single Commands DP2