compared with
Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (1)

View Page History
}
{code}


h2. [#Bulk Loading]

The code below is ported from the Java discussion on bulk loading in this wiki. See [Bulk Loading|http://www.couchbase.com/wiki/display/couchbase/Couchbase+Java+Client+Library] for more information.

{code:java}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Couchbase;
using Couchbase.Configuration;
using Enyim.Caching.Memcached.Results;
using Enyim.Caching.Memcached;
using System.Threading;

namespace BulkLoader
{
public class StoreHandler
{
CouchbaseClient _cbc;

public StoreHandler(IList<Uri> uris, string bucketName, string bucketPassword)
{
var config = new CouchbaseClientConfiguration();
foreach (var uri in uris)
{
config.Urls.Add(uri);
}
config.Bucket = bucketName;
config.BucketPassword = bucketPassword;

_cbc = new CouchbaseClient(config);
}

/// <summary>
/// Performa regular Store with storeMode.Set
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public IStoreOperationResult Set(string key, object value)
{
return _cbc.ExecuteStore(StoreMode.Set, key, value);
}

/// <summary>
/// Continuously try a set with exponential backoff until number of tries or
/// successful. The exponential backoff will wait a maximum of 1 second, or whatever
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="tries">Number of tries before giving up</param>
/// <returns></returns>
public IStoreOperationResult Set(string key, object value, int tries)
{
var backoffExp = 0;
var tryAgain = false;
IStoreOperationResult result = null;

try
{
do
{
if (backoffExp > tries)
{
throw new ApplicationException("Could not perform a set after " + tries + " tries.");
}

result = _cbc.ExecuteStore(StoreMode.Set, key, value);
if (result.Success) break;

if (backoffExp > 0)
{
var backOffMillis = Math.Pow(2, backoffExp);
backOffMillis = Math.Min(1000, backOffMillis); //1 sec max
Thread.Sleep((int)backOffMillis);
Console.WriteLine("Backing off, tries so far: " + backoffExp);
}
backoffExp++;

if (! result.Success)
{
var message = result.InnerResult != null ? result.InnerResult.Message : result.Message;
Console.WriteLine("Failed with status: " + message);
}

//Future versions of the .NET client will flatten the results and make checking for
//InnerResult objects unnecessary
tryAgain = (result.Message != null && result.Message.Contains("Temporary failure") ||
result.InnerResult != null && result.InnerResult.Message.Contains("Temporary failure"));

} while (tryAgain);

}
catch (Exception ex)
{
Console.WriteLine("Interrupted while trying to set. Exception:" + ex.Message);
}

// note that other failure cases fall through. status.isSuccess() can be
// checked for success or failure or the message can be retrieved.
return result;

}
}
}

{code}