patterncsharpMinor
Versioning documents in Couchbase
Viewed 0 times
versioningdocumentscouchbase
Problem
I've just converted a class for versioning Couchbase documents from Java to C#, but I'm not 100% familiar with the Couchbase API so would like some feedback.
The original class can be found here.
Update
I'm specifically looking for help in the areas of what exceptions are thrown and where should I be checking for them?
There is a loop for removing multiple documents, I'm not sure if this is the correct way to remove documents. It feels like bad practice and there should be something in N1QL to support this. Is there and if so how should it be used?
```
using Couchbase;
using Couchbase.Core;
using Couchbase.Utils;
using System.Collections.Generic;
using System;
using System.Text;
namespace CouchbaseExtensions
{
public static class CouchbaseBucket {
public static string VERSION_NUMBER_PREFIX = "::v";
public static string VERSION_COUNTER_SUFFIX = "_version";
public static IDocumentResult UpsertVersion(this IBucket bucket, IDocument document, bool versionIt){
if(versionIt){
IOperationResult getResult = bucket.Get(document.Id);
if(getResult.Success){
// get the next version
IOperationResult version = bucket.Increment(document.Id + VERSION_COUNTER_SUFFIX, 1, 1);
string keyForVersion = document.Id + VERSION_NUMBER_PREFIX + version.Value;
try {
bucket.Upsert(keyForVersion, document.Content);
} catch (Exception e) {
Console.WriteLine("Cannot save version "+ version + " for key "+ document.Id +" - Error:"+ e.Message );
}
return new DocumentResult(bucket.Upsert(document.Id, document.Content), document.Id);
}else{
var result = bucket.Upsert(document.Id, document.Content, document.Cas, document.Expiry.ToTtl());
return new DocumentResult(result, document.Id);
The original class can be found here.
Update
I'm specifically looking for help in the areas of what exceptions are thrown and where should I be checking for them?
There is a loop for removing multiple documents, I'm not sure if this is the correct way to remove documents. It feels like bad practice and there should be something in N1QL to support this. Is there and if so how should it be used?
```
using Couchbase;
using Couchbase.Core;
using Couchbase.Utils;
using System.Collections.Generic;
using System;
using System.Text;
namespace CouchbaseExtensions
{
public static class CouchbaseBucket {
public static string VERSION_NUMBER_PREFIX = "::v";
public static string VERSION_COUNTER_SUFFIX = "_version";
public static IDocumentResult UpsertVersion(this IBucket bucket, IDocument document, bool versionIt){
if(versionIt){
IOperationResult getResult = bucket.Get(document.Id);
if(getResult.Success){
// get the next version
IOperationResult version = bucket.Increment(document.Id + VERSION_COUNTER_SUFFIX, 1, 1);
string keyForVersion = document.Id + VERSION_NUMBER_PREFIX + version.Value;
try {
bucket.Upsert(keyForVersion, document.Content);
} catch (Exception e) {
Console.WriteLine("Cannot save version "+ version + " for key "+ document.Id +" - Error:"+ e.Message );
}
return new DocumentResult(bucket.Upsert(document.Id, document.Content), document.Id);
}else{
var result = bucket.Upsert(document.Id, document.Content, document.Cas, document.Expiry.ToTtl());
return new DocumentResult(result, document.Id);
Solution
I don't think you need either
These lines are repeated and thus I'd move them to a separate method:
Those two combined result in something like this:
.... could be rewritten as:
I'm not happy about the variable name
else to be stated explicitly, since the previous code will always return a value (or throw an exception).These lines are repeated and thus I'd move them to a separate method:
var result = bucket.Upsert(document.Id, document.Content, document.Cas, document.Expiry.ToTtl());
return new DocumentResult(result, document.Id);Those two combined result in something like this:
public static IDocumentResult UpsertVersion(this IBucket bucket, IDocument document, bool versionIt)
{
if(versionIt)
{
IOperationResult getResult = bucket.Get(document.Id);
if(getResult.Success)
{
// code omitted for brevity
}
return GetDocumentResult(bucket, document);
}
return GetDocumentResult(bucket, document);
}
private static DocumentResult GetDocumentResult(IBucket bucket, IDocument document)
{
var result = bucket.Upsert(document.Id, document.Content, document.Cas, document.Expiry.ToTtl());
return new DocumentResult(result, document.Id);
}Console.WriteLine has at least one version that has composite formatting built in, so this line:Console.WriteLine("Cannot save version "+ version + " for key "+ document.Id +" - Error:"+ e.Message );.... could be rewritten as:
Console.WriteLine("Cannot save version {0} for key {1} - Error: {2}",
version, document.Id, e.Message);I'm not happy about the variable name
versionIt. Perhaps something like applyVersioning would be more appropriate?Code Snippets
var result = bucket.Upsert(document.Id, document.Content, document.Cas, document.Expiry.ToTtl());
return new DocumentResult<T>(result, document.Id);public static IDocumentResult<T> UpsertVersion<T>(this IBucket bucket, IDocument<T> document, bool versionIt)
{
if(versionIt)
{
IOperationResult<T> getResult = bucket.Get<T>(document.Id);
if(getResult.Success)
{
// code omitted for brevity
}
return GetDocumentResult(bucket, document);
}
return GetDocumentResult(bucket, document);
}
private static DocumentResult<T> GetDocumentResult(IBucket bucket, IDocument<T> document)
{
var result = bucket.Upsert(document.Id, document.Content, document.Cas, document.Expiry.ToTtl());
return new DocumentResult<T>(result, document.Id);
}Console.WriteLine("Cannot save version "+ version + " for key "+ document.Id +" - Error:"+ e.Message );Console.WriteLine("Cannot save version {0} for key {1} - Error: {2}",
version, document.Id, e.Message);Context
StackExchange Code Review Q#98842, answer score: 2
Revisions (0)
No revisions yet.