public void WhenAddingAndRemovingPreAndPostCommitHooksThingsWorkAsExpected()
        {
            var props = new RiakBucketProperties()
                .AddPreCommitHook(new RiakJavascriptCommitHook("some_fun"))
                .AddPreCommitHook(new RiakJavascriptCommitHook("some_fun"))
                .AddPreCommitHook(new RiakErlangCommitHook("mod", "fun"))
                .AddPreCommitHook(new RiakErlangCommitHook("mod", "fun"))
                .AddPreCommitHook(new RiakErlangCommitHook("riak_search_kv_hook", "precommit"))
                .AddPreCommitHook(new RiakErlangCommitHook("riak_search_kv_hook", "precommit"))
                .AddPreCommitHook(RiakErlangCommitHook.RiakSearchCommitHook)
                .AddPostCommitHook(new RiakErlangCommitHook("mod", "fun"))
                .AddPostCommitHook(new RiakErlangCommitHook("mod", "fun"));

            props.PreCommitHooks.Count.ShouldEqual(3);
            props.PostCommitHooks.Count.ShouldEqual(1);

            props.SetSearch(true);
            props.PreCommitHooks.Count.ShouldEqual(3);
            props.PostCommitHooks.Count.ShouldEqual(1);

            props.SetSearch(false);
            props.PreCommitHooks.Count.ShouldEqual(2);
            props.PostCommitHooks.Count.ShouldEqual(1);

            props.PreCommitHooks.Where(x => x is RiakErlangCommitHook).Cast<RiakErlangCommitHook>()
                .Any(x => x.Function == RiakErlangCommitHook.RiakSearchCommitHook.Function
                    && x.Function == RiakErlangCommitHook.RiakSearchCommitHook.Function)
                .ShouldBeFalse();
        }
        public void WhenDisablingRiakSearchOnBucketPreCommitHookIsRemoved()
        {
            var props = new RiakBucketProperties()
                .AddPreCommitHook(new RiakErlangCommitHook("foo", "bar"))
                .SetSearch(true);

            props.PreCommitHooks.Count.ShouldEqual(2);
            props.PreCommitHooks[1].ShouldEqual(RiakErlangCommitHook.RiakSearchCommitHook);

            props.SetSearch(false);
            props.PreCommitHooks.Count.ShouldEqual(1);
            ((RiakErlangCommitHook)props.PreCommitHooks[0]).Function.ShouldEqual("bar");
        }
예제 #3
0
        public void NumberSearch() {
            Console.WriteLine("Beginning NumberSearch:");
            Console.WriteLine("\tAdding 10 items with ages between 21 and 30");

            var Cluster = RiakCluster.FromConfig("riakConfig");
            var Client = Cluster.CreateClient();
            
            var bucket = "rsds";
            
            var props = new RiakBucketProperties()
                .SetSearch(true);
            
            var setResult = Client.SetBucketProperties(bucket, props);
            
            Client.Batch(batch =>
                         {
                for (var i = 1; i < 11; i++)
                {
                    var d = DateTime.Now.AddDays(0 - i);
                    var doc = new RiakObject(bucket, i.ToString(), new { value = i, created_date = d, age_int = 20 + i });
                    
                    batch.Put(doc);
                }
            });

            Console.WriteLine("\tSearching for items between the ages of 19 and 25 (exclusive)");
            var request = new RiakSearchRequest
            {
                Query = new RiakFluentSearch(bucket, "age_int").Between("19", "25").Build()
            };
            
            var result = Client.Search(request);
            
            Console.WriteLine("\tNumberSearch is a success: {0}", result.IsSuccess);
            Console.WriteLine("\tWe found {0} items", result.Value.Documents.Count);
            
            Client.DeleteBucket(bucket);
        }
예제 #4
0
 public void SetBucketProperties(string bucket, RiakBucketProperties properties, Action<RiakResult> callback)
 {
     ExecAsync(() => callback(_client.SetBucketProperties(bucket, properties)));
 }
예제 #5
0
        private static void Run(IRiakClient client)
        {
            // is the server alive?
            Console.WriteLine("Pinging the server ...");
            var pingResult = client.Ping();
            System.Diagnostics.Debug.Assert(pingResult.IsSuccess);

            // here's how you'd go about setting the properties on a bucket
            // (there are lots more than demoed here).
            Console.WriteLine("Setting some bucket properties via REST ...");
            var restProps = new RiakBucketProperties()
                .SetAllowMultiple(true)
                .SetWVal(3);
            client.SetBucketProperties(Bucket, restProps);
            // you'll notice that this is slow, because behind the scenes the client
            // has detected properties that can't be set via the PBC interface
            // so instead it has degraded to the REST api.

            // here's a sample which uses just the PBC properties and hence runs a
            // lot faster.
            Console.WriteLine("Setting some bucket properties via PBC ...");
            var pbcProps = new RiakBucketProperties()
                .SetAllowMultiple(false);
            client.SetBucketProperties(Bucket, pbcProps);

            // we'll keep track of the keys we store as we create them
            var keys = new List<string>();

            // let's write some stuff to Riak, starting with a simple put
            Console.WriteLine("Simple Put ...");
            var simplePutData = CreateData(0);
            var simplePutResponse = client.Put(simplePutData);
            System.Diagnostics.Debug.Assert(simplePutResponse.IsSuccess);
            keys.Add(simplePutData.Key);

            // next write and pull out the resulting object at the same time,
            // and specifying a different write quorum
            var putWithBody = CreateData(1);
            Console.WriteLine("Simple Put with custom quorum ...");
            var putWithBodyResponse = client.Put(putWithBody, new RiakPutOptions { ReturnBody = true, W = 1 });
            System.Diagnostics.Debug.Assert(putWithBodyResponse.IsSuccess);
            System.Diagnostics.Debug.Assert(putWithBodyResponse.Value != null);
            System.Diagnostics.Debug.Assert(putWithBodyResponse.Value.VectorClock != null);
            keys.Add(putWithBody.Key);

            // let's bang out a few more objects to do a bulk load
            var objects = new List<RiakObject>();
            for (var i = 1; i < 11; ++i)
            {
                var obj = CreateData(i);
                objects.Add(obj);
                keys.Add(obj.Key);
            }
            Console.WriteLine("Bulk insert ...");
            var bulkInsertResults = client.Put(objects);
            // verify that they all went in
            foreach (var r in bulkInsertResults)
            {
                System.Diagnostics.Debug.Assert(r.IsSuccess);
            }

            // let's see if we can get out all the objects that we expect to retrieve
            // starting with a simple get:
            Console.WriteLine("Simple Get ...");
            var simpleGetResult = client.Get(Bucket, keys[0]);
            System.Diagnostics.Debug.Assert(simpleGetResult.IsSuccess);
            System.Diagnostics.Debug.Assert(simpleGetResult.Value != null);

            // let's do a bulk get of all the objects we've written so far, again
            // mucking with the quorum value
            var objectIds = keys.Select(k => new RiakObjectId(Bucket, k));
            Console.WriteLine("Bulk Get ...");
            var bulkGetResults = client.Get(objectIds, 1);

            // verify that we got everything
            foreach (var r in bulkGetResults)
            {
                System.Diagnostics.Debug.Assert(r.IsSuccess);
                System.Diagnostics.Debug.Assert(r.Value != null);
            }

            // let's try a map/reduce function, with javascript, to count the
            // number of objects in the bucket
            var sumMapRed = new RiakMapReduceQuery()
                .Inputs(Bucket)
                .MapJs(m => m.Source(@"function(o) {return [ 1 ];}"))
                .ReduceJs(r => r.Name(@"Riak.reduceSum").Keep(true));

            // execute this query with blocking IO, waiting for all the results to come
            // back before we process them
            Console.WriteLine("Blocking map/reduce query ...");
            var blockingMRResult = client.MapReduce(sumMapRed);
            System.Diagnostics.Debug.Assert(blockingMRResult.IsSuccess);
            // next, pull out the phase we're interested in to get the result we want
            var reducePhaseResult = blockingMRResult.Value.PhaseResults.Last().GetObjects<int[]>().SelectMany(p => p).ToArray();
            System.Diagnostics.Debug.Assert(reducePhaseResult[0] == 12);

            // now let's do the same thing, but with the blocking version that streams
            // the results back per phase, rather than waiting for all the reults to
            // be calculated first
            Console.WriteLine("Blocking streaming map/reduce query ...");
            var streamingMRResult = client.StreamMapReduce(sumMapRed);
            System.Diagnostics.Debug.Assert(streamingMRResult.IsSuccess);
            foreach (var result in streamingMRResult.Value.PhaseResults)
            {
                if (result.Phase == 1)
                {
                    var json = JArray.Parse(result.Values[0].FromRiakString());
                    System.Diagnostics.Debug.Assert(json[0].Value<int>() == 12);
                }
            }

            // each of the above methods have an async equivalent that has an extra
            // parameter to pass in which is an Action that takes the result. This
            // is executed on the worker thread that the work is done on. For the
            // sake of this example, we'll only demonstrate how to do this with
            // streaming map/reduce as applying the principle to the other functions
            // is a simple thing to do. All the async methods are exposed via the
            // 'Async' property.

            // create an event to wait on while the results are being processed
            // (usually you wouldn't worry about this in a Ui app, you'd just take
            // the result of the other thread and dispatch it to the UI when processed)
            var autoResetEvent = new AutoResetEvent(false);
            Console.WriteLine("Starting async streaming map/reduce query ...");
            client.Async.StreamMapReduce(sumMapRed, result => HandleStreamingMapReduce(result, autoResetEvent));
            Console.WriteLine("Waiting for async streaming map/reduce query result ...");
            autoResetEvent.WaitOne();

            // finally delete the bucket (this can also be done asynchronously)
            // this calls ListKeys behind the scenes, so it's a very slow process. Riak
            // doesn't currently have the ability to quickly delete a bucket.
            Console.WriteLine("Deleting the whole test bucket ...");
            client.DeleteBucket(Bucket);

            Console.WriteLine("Sample app complete!");
        }
        public void SettingPropertiesOnNewBucketWorksCorrectly()
        {
            var bucketName = string.Format("{0}_{1}", Bucket, Guid.NewGuid());

            var props = new RiakBucketProperties()
                .SetNVal(4)
                .SetSearch(true)
                .SetWVal("all")
                .SetRVal("quorum");

            var setResult = Client.SetBucketProperties(bucketName, props);
            setResult.ShouldBeTrue();

            var getResult = Client.GetBucketProperties(bucketName);
            getResult.ShouldNotBeNull();

            props = getResult;
            props.NVal.HasValue.ShouldBeTrue();
            props.NVal.Value.ShouldEqual(4U);
            props.Search.ShouldNotEqual(null);
            props.Search.Value.ShouldBeTrue();
            props.WVal.ShouldEqual(RiakConstants.QuorumOptionsLookup["all"]);
            props.RVal.ShouldEqual(RiakConstants.QuorumOptionsLookup["quorum"]);
        }
예제 #7
0
        public new void SetUp()
        {
            base.SetUp();
            Bucket = System.Guid.NewGuid().ToString();

            CreateLinkedObjects(Bucket);

            var props = new RiakBucketProperties()
                .SetAllowMultiple(true);
            Client.SetBucketProperties(Bucket, props).ShouldBeTrue();
        }
        public void WritesWithAllowMultProducesMultipleVTagsInBatch()
        {
            Client.Batch(batch =>
                {
                    // Do this via the PBC - noticable quicker than REST
                    var props = new RiakBucketProperties().SetAllowMultiple(true);
                    props.CanUsePbc.ShouldBeTrue();
                    batch.SetBucketProperties(MultiBucket, props).IsSuccess.ShouldBeTrue();

                    var doc = new RiakObject(MultiBucket, MultiKey, MultiBodyOne, RiakConstants.ContentTypes.ApplicationJson);
                    batch.Put(doc).IsSuccess.ShouldBeTrue();

                    doc = new RiakObject(MultiBucket, MultiKey, MultiBodyTwo, RiakConstants.ContentTypes.ApplicationJson);
                    batch.Put(doc).IsSuccess.ShouldBeTrue();

                    var result = batch.Get(MultiBucket, MultiKey);

                    result.Value.VTags.ShouldNotBeNull();
                    result.Value.VTags.Count.IsAtLeast(2);
                });
        }
        public void SettingPropertiesOnNewBucketWorksCorrectly()
        {
            var bucketName = Guid.NewGuid().ToString();
            var props = new RiakBucketProperties()
                .SetNVal(4)
                .SetSearch(true)
                .SetWVal("all")
                .SetRVal("quorum");

            var setResult = Client.SetBucketProperties(bucketName, props);
            setResult.IsSuccess.ShouldBeTrue(setResult.ErrorMessage);

            var getResult = Client.GetBucketProperties(bucketName, true);
            getResult.IsSuccess.ShouldBeTrue(getResult.ErrorMessage);

            props = getResult.Value;
            props.NVal.HasValue.ShouldBeTrue();
            props.NVal.Value.ShouldEqual(4U);
            props.SearchEnabled.ShouldBeTrue();
            props.WVal.Right.ShouldEqual("all");
            props.RVal.Right.ShouldEqual("quorum");
        }
        public void SettingExtendedPropertiesToBucketWithSlashesInNameShouldReturnError()
        {
            const string bucketName = "not/valid/here";
            var props = new RiakBucketProperties()
                .SetNVal(4)
                .SetSearch(true)
                .SetWVal("all")
                .SetRVal("quorum");

            var setResult = Client.SetBucketProperties(bucketName, props);
            setResult.IsSuccess.ShouldBeFalse();
        }
        public void ResettingBucketPropertiesWorksCorrectly()
        {
            const string bucket = "Schmoopy";

            var props = new RiakBucketProperties()
                .SetAllowMultiple(true)
                .SetDwVal(10)
                .SetWVal(5)
                .SetLastWriteWins(true);

            var setPropsResult = Client.SetBucketProperties(bucket, props);
            setPropsResult.IsSuccess.ShouldBeTrue(setPropsResult.ErrorMessage);

            var resetResult = Client.ResetBucketProperties(bucket);
            resetResult.IsSuccess.ShouldBeTrue(resetResult.ErrorMessage);

            var getPropsResult = Client.GetBucketProperties(bucket, true);
            getPropsResult.IsSuccess.ShouldBeTrue(getPropsResult.ErrorMessage);

            var resetProps = getPropsResult.Value;

            resetProps.AllowMultiple.ShouldNotEqual(props.AllowMultiple);
            resetProps.DwVal.ShouldNotEqual(props.DwVal);
            resetProps.WVal.ShouldNotEqual(props.WVal);
            resetProps.LastWriteWins.ShouldNotEqual(props.LastWriteWins);
        }
        public void GettingPropsOnInvalidBucketStraightAfterSettingDoesntThrowAnException()
        {
            // this bucket name must have ONE slash in it. If it has more or less then
            // errors will come out as expected. If there's one, then Riak thinks we're putting
            // a value in the cluster and so the operation works.
            const string bucketName = "slartibartfast/dentartherdent";
            var props = new RiakBucketProperties()
                .SetNVal(4)
                .SetSearch(true)
                .SetWVal("all")
                .SetRVal("quorum");

            var setResult = Client.SetBucketProperties(bucketName, props);
            setResult.IsSuccess.ShouldBeFalse();

            // this shouldn't throw any exceptions
            var getResult = Client.GetBucketProperties(bucketName, true);
            getResult.IsSuccess.ShouldBeFalse();
        }
예제 #13
0
        public void DateSearch() {
            Console.WriteLine("Beginning DateSearch:");
            Console.WriteLine("\tAdding 10 items with dates between 10 days ago and yesterday");
            var Cluster = RiakCluster.FromConfig("riakConfig");
            var Client = Cluster.CreateClient();

            var bucket = "rsds";

            var props = new RiakBucketProperties()
                .SetSearch(true);
            
            var setResult = Client.SetBucketProperties(bucket, props);
            
            Client.Batch(batch =>
            {
                for (var i = 1; i < 11; i++)
                {
                    var d = DateTime.Now.AddDays(0 - i);
                    var doc = new RiakObject(bucket, i.ToString(), new { value = i, created_date = d });
                                
                    batch.Put(doc);
                }
            });

            var start = DateTime.Now.AddDays(-4);
            var end = DateTime.Now.AddDays(-2);

            Console.WriteLine("\tSearching for items NOT between the aget of 19 and 25 (exclusive)");
            var request = new RiakSearchRequest
            {
                Query = new RiakFluentSearch(bucket, "created_date").Between(start.ToString("s"), end.ToString("s")).Build()
            };

            var result = Client.Search(request);

            Console.WriteLine("\tDateSearch is a success: {0}", result.IsSuccess);
            Console.WriteLine("\tWe found {0} items", result.Value.Documents.Count);

            Client.DeleteBucket(bucket);
        }
예제 #14
0
 public Task<RiakResult> SetBucketProperties(string bucket, RiakBucketProperties properties)
 {
     return Task.Factory.StartNew(() => _client.SetBucketProperties(bucket, properties));
 }
        public void WhenDisablingRiakSearchOnBucketWithoutSearchEnabledPreCommitHooksAreLeftAlone()
        {
            var props = new RiakBucketProperties()
                .AddPreCommitHook(new RiakErlangCommitHook("foo", "bar"))
                .SetSearch(false);

            props.PreCommitHooks.Count.ShouldEqual(1);
        }
        public void ResettingBucketPropertiesWorksCorrectly()
        {
            var bucket = string.Format("{0}_{1}", Bucket, "Schmoopy");

            var props = new RiakBucketProperties()
                .SetAllowMultiple(true)
                .SetDwVal(10)
                .SetWVal(5)
                .SetLastWriteWins(true);

            Client.SetBucketProperties(bucket, props).ShouldBeTrue();

            Client.ResetBucketProperties(bucket).ShouldBeTrue();

            var getPropsResult = Client.GetBucketProperties(bucket);
            getPropsResult.ShouldNotBeNull();

            var resetProps = getPropsResult;

            resetProps.AllowMultiple.ShouldNotEqual(props.AllowMultiple);
            resetProps.DwVal.ShouldNotEqual(props.DwVal);
            resetProps.WVal.ShouldNotEqual(props.WVal);
            resetProps.LastWriteWins.ShouldNotEqual(props.LastWriteWins);
        }
        public void WhenEnablingRiakSearchOnBucketPreCommitHookIsAdded()
        {
            var props = new RiakBucketProperties()
                .AddPreCommitHook(new RiakErlangCommitHook("foo", "bar"))
                .SetSearch(true);

            props.PreCommitHooks.Count.ShouldEqual(2);
            props.PreCommitHooks[1].ShouldEqual(RiakErlangCommitHook.RiakSearchCommitHook);
        }
        public void SettingExtendedPropertiesToBucketWithSlashesInNameShouldReturnError()
        {
            var bucketName = string.Format("{0}_{1}", Bucket, "not/valid/here");
            var props = new RiakBucketProperties()
                .SetNVal(4)
                .SetSearch(true)
                .SetWVal("all")
                .SetRVal("quorum");

            RiakException exception = null;
            try
            {
                var getResult = Client.SetBucketProperties(bucketName, props);
            }
            catch (RiakException riakException)
            {
                exception = riakException;
            }

            exception.ShouldNotBeNull();
            exception.ErrorCode.ShouldEqual((uint)ResultCode.InvalidRequest);
        }
        private static void DoAllowMultProducesMultipleTest(IRiakBatchClient client)
        {
            // delete first if something does exist
            client.Delete(MultiBucket, MultiKey);

            // Do this via the REST interface - will be substantially slower than PBC
            var props = new RiakBucketProperties().SetAllowMultiple(true).SetLastWriteWins(false);
            props.CanUsePbc.ShouldBeFalse();
            client.SetBucketProperties(MultiBucket, props).IsSuccess.ShouldBeTrue();

            var doc = new RiakObject(MultiBucket, MultiKey, MultiBodyOne, RiakConstants.ContentTypes.ApplicationJson);
            var writeResult1 = client.Put(doc);
            writeResult1.IsSuccess.ShouldBeTrue();

            doc = new RiakObject(MultiBucket, MultiKey, MultiBodyTwo, RiakConstants.ContentTypes.ApplicationJson);
            var writeResult2 = client.Put(doc);
            writeResult2.IsSuccess.ShouldBeTrue();
            writeResult2.Value.Siblings.Count.ShouldBeGreaterThan(2);

            var result = client.Get(MultiBucket, MultiKey);
            result.Value.Siblings.Count.ShouldBeGreaterThan(2);
        }
예제 #20
0
        public new void SetUp()
        {
            base.SetUp();
            CreateLinkedObjects(TestBucket);

            var props = new RiakBucketProperties()
                .SetAllowMultiple(false);

            Client.SetBucketProperties(TestBucket, props).ShouldBeTrue();
        }