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.UpdateI'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<T> UpsertVersion<T>(this IBucket bucket, IDocument<T> document, bool versionIt){            if(versionIt){                IOperationResult<T> getResult = bucket.Get<T>(document.Id);                if(getResult.Success){                    // get the next version                    IOperationResult<ulong> version = bucket.Increment(document.Id + VERSION_COUNTER_SUFFIX, 1, 1);                    string keyForVersion = document.Id + VERSION_NUMBER_PREFIX + version.Value;                    try {                        bucket.Upsert<T>(keyForVersion, document.Content);                    } catch (Exception e) {                        Console.WriteLine(Cannot save version + version +  for key + document.Id + - Error:+ e.Message );                    }                    return new DocumentResult<T>(bucket.Upsert<T>(document.Id, document.Content), document.Id);                }else{                    var result = bucket.Upsert(document.Id, document.Content, document.Cas, document.Expiry.ToTtl());                    return new DocumentResult<T>(result, document.Id);                }            }else{                var result = bucket.Upsert(document.Id, document.Content, document.Cas, document.Expiry.ToTtl());                return new DocumentResult<T>(result, document.Id);            }        }        public static IDocumentResult<T> Replace<T>(this IBucket bucket, IDocument<T> document)        {            IOperationResult<T> getResult = bucket.Get<T>(document.Id);            if(getResult.Success){                // get the next version                IOperationResult<ulong> version = bucket.Increment(document.Id + VERSION_COUNTER_SUFFIX, 1, 1);                string keyForVersion = document.Id + VERSION_NUMBER_PREFIX + version.Value;                return new DocumentResult<T>(bucket.Upsert<T>(keyForVersion, document.Content), document.Id);            } else {                // if the key does not exist create it, so no version                return bucket.Replace<T>(document);            }        }        public static List<T> GetAllVersions<T>(this IBucket bucket, string key) {            List<T> returnValue = null;            List<string> keys = new List<string>();            keys.Add(key);            IOperationResult<ulong> maxVersionNum = bucket.Get<ulong>(key + VERSION_COUNTER_SUFFIX);            if ( maxVersionNum.Success) {                for(int i = 1; i<= (int)maxVersionNum.Value; i++) {                    StringBuilder sb = new StringBuilder(key);                    sb.AppendFormat({0}{1}, VERSION_NUMBER_PREFIX, i);                    returnValue.Add(bucket.Get<T>(sb.ToString()).Value);                }            } else {                returnValue.Add(bucket.Get<T>(key).Value);            }            return returnValue;        }        public static IOperationResult RemoveVersion(this IBucket bucket, string key, int version) {            return bucket.Remove(key + VERSION_NUMBER_PREFIX + version);        }        public static IOperationResult RemoveAllVersions(this IBucket bucket, string key) {            // need to delete all the version first            IOperationResult<ulong> maxVersionNum = bucket.Get<ulong>(key + VERSION_COUNTER_SUFFIX);            if (maxVersionNum.Success) {                ulong biggerVersion = maxVersionNum.Value;                try {                    // delete all the versions                    for (int i = 1; i <= (int)biggerVersion; i++) {                        string versionKey = key + VERSION_NUMBER_PREFIX + i;                        bucket.Remove(versionKey);                    }                    // delete the counter                    bucket.Remove(key + VERSION_COUNTER_SUFFIX);                } catch (Exception e) {                    Console.WriteLine(e);                }            } else {                return bucket.Remove(key);            }            return bucket.Remove(key);        }    }}
Versioning documents in Couchbase
c#;database;nosql