Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RiakBucketKeyInput"/> class,
        /// and adds the attached <paramref name="riakObjectIds"/> to the collection of inputs.
        /// </summary>
        /// <param name="riakObjectIds">The object ids to add to the input collection.</param>
        /// <returns>The newly created <see cref="RiakBucketKeyInput"/>.</returns>
        public static RiakBucketKeyInput FromRiakObjectIds(IEnumerable <RiakObjectId> riakObjectIds)
        {
            var bucketKeyInput = new RiakBucketKeyInput();

            bucketKeyInput.Add(riakObjectIds);
            return(bucketKeyInput);
        }
        public void LotsOfConcurrentMapRedRequestsShouldWork()
        {
            var keys = new List<string>();

            for (var i = 1; i < 11; i++)
            {
                var key = "key" + i;
                var doc = new RiakObject(MapReduceBucket, key, new { value = i });
                keys.Add(key);

                var result = Client.Put(doc, new RiakPutOptions { ReturnBody = true });
                result.IsSuccess.ShouldBeTrue();
            }

            var input = new RiakBucketKeyInput();
            keys.ForEach(k => input.Add(new RiakObjectId(MapReduceBucket, k)));

            var query = new RiakMapReduceQuery()
                .Inputs(input)
                .MapJs(m => m.Source(@"function(o){return[1];}"))
                .ReduceJs(r => r.Name(@"Riak.reduceSum").Keep(true));
            query.Compile();

            var results = new List<RiakResult<RiakMapReduceResult>>[ThreadCount];
            Parallel.For(0, ThreadCount, i =>
                {
                    results[i] = DoMapRed(query);
                });

            var failures = 0;
            foreach (var r in results.SelectMany(l => l))
            {
                if (r.IsSuccess)
                {
                    var resultValue = JsonConvert.DeserializeObject<int[]>(r.Value.PhaseResults.ElementAt(1).Values.First().FromRiakString())[0];
                    resultValue.ShouldEqual(10);
                    //r.Value.PhaseResults.ElementAt(1).GetObject<int[]>()[0].ShouldEqual(10);
                }
                else
                {
                    // the only acceptable result is that it ran out of retries when
                    // talking to the cluster (trying to get a connection)
                    r.ResultCode.ShouldEqual(ResultCode.NoRetries);
                    ++failures;
                }
            }

            Console.WriteLine("Total of {0} out of {1} failed to execute due to connection contention", failures, ThreadCount * ActionCount);
        }
        public void RiakBucketKeyInputSerializesCorrectly()
        {
            var inputList = new List<RiakObjectId>
            {
                new RiakObjectId("foo", "baz"),
                new RiakObjectId("dooby", "scooby")
            };

            var input = new RiakBucketKeyInput()
                .Add(new RiakObjectId("foo", "bar"))
                .Add(inputList)
                .Add(inputList[0], inputList[1]);

            var s = Serialize(input.WriteJson);
            Assert.AreEqual(s, SerializedRiakBucketKeyInput);
        }
        public void RiakBucketKeyInputSerializesCorrectlyOldInterface()
        {
            var inputList = new List<Tuple<string, string>>
            { 
                new Tuple<string, string>("foo", "baz"),
                new Tuple<string, string>("dooby", "scooby")
            };

#pragma warning disable 612, 618
            var input = new RiakBucketKeyInput()
                .Add("foo", "bar")
                .Add(inputList)
                .Add(inputList[0], inputList[1]);
#pragma warning restore 612, 618

            var s = Serialize(input.WriteJson);
            Assert.AreEqual(s, SerializedRiakBucketKeyInput);
        }
        public void RiakObjectLinksAreTheSameAsLinksRetrievedViaMapReduce()
        {
            var jeremiah = Client.Get(TestBucket, Jeremiah).Value;
            var jLinks = jeremiah.Links;

            var input = new RiakBucketKeyInput()
                .Add(new RiakObjectId(TestBucket, Jeremiah));

#pragma warning disable 618
            var query = new RiakMapReduceQuery().Inputs(input).Link(l => l.AllLinks().Keep(true));
#pragma warning restore 618

            var mrResult = Client.MapReduce(query);
            mrResult.IsSuccess.ShouldBeTrue();

            // TODO: FUTURE - Is *this* chunk of code acceptable?
            // This should probably be taken care of in the RiakClient.WalkLinks
            var listOfLinks = mrResult.Value.PhaseResults.OrderBy(pr => pr.Phase)
                .ElementAt(0).Values
                    .Select(v => RiakLink.ParseArrayFromJsonString(v.FromRiakString()));

            var mrLinks = listOfLinks.SelectMany(l => l).ToList();

            mrLinks.Count().ShouldEqual(jLinks.Count);
            foreach (var link in jLinks)
            {
                mrLinks.ShouldContain(link);
            }
        }
        public void LotsOfConcurrentStreamingMapRedRequestsShouldWork()
        {
            var keys = new List<string>();

            for (var i = 1; i < 11; i++)
            {
                var key = "key" + i;
                var doc = new RiakObject(MapReduceBucket, key, new { value = i });
                keys.Add(key);

                var result = Client.Put(doc, new RiakPutOptions { ReturnBody = true });
                result.IsSuccess.ShouldBeTrue();
            }

            var input = new RiakBucketKeyInput();
            keys.ForEach(k => input.Add(new RiakObjectId(MapReduceBucket, k)));

            var query = new RiakMapReduceQuery()
                .Inputs(input)
                .MapJs(m => m.Source(@"function(o){return[1];}"))
                .ReduceJs(r => r.Name(@"Riak.reduceSum").Keep(true));
            query.Compile();

            var results = new List<RiakMapReduceResultPhase>[ThreadCount];
            Parallel.For(0, ThreadCount, i =>
                {
                    results[i] = DoStreamingMapRed(query);
                });

            var failures = 0;
            foreach (var result in results)
            {
                if (result.Count > 0)
                {
                    var lastResult = result.OrderByDescending(r => r.Phase).First();
                    var resultValue = JsonConvert.DeserializeObject<int[]>(lastResult.Values.First().FromRiakString());
                    //var resultValue = JsonConvert.DeserializeObject<int[]>(r.Value.PhaseResults.ElementAt(1).Values.First().FromRiakString())[0];
                    // due to the speed which things happen at, we can't gaurantee all 10 will be in the result set
                    resultValue[0].IsAtLeast(5);
                    //lastResult.GetObject<int[]>()[0].ShouldEqual(10);
                }
                else
                {
                    ++failures;
                }
            }
            Console.WriteLine("Total of {0} out of {1} failed to execute due to connection contention", failures, ThreadCount * ActionCount);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="RiakBucketKeyInput"/> class, 
 /// and adds the attached <paramref name="riakObjectIds"/> to the collection of inputs.
 /// </summary>
 /// <param name="riakObjectIds">The object ids to add to the input collection.</param>
 /// <returns>The newly created <see cref="RiakBucketKeyInput"/>.</returns>
 public static RiakBucketKeyInput FromRiakObjectIds(IEnumerable<RiakObjectId> riakObjectIds)
 {
     var bucketKeyInput = new RiakBucketKeyInput();
     bucketKeyInput.Add(riakObjectIds);
     return bucketKeyInput;
 }