コード例 #1
0
        public void EqualsFindsOneKey()
        {
            for (int i = 0; i < 10; i++)
            {
                Client.Put(new RiakObject(Bucket, string.Format("time_{0}", i), EmptyBody, RiakConstants.ContentTypes.ApplicationJson));
            }

            var mr = new RiakMapReduceQuery {ContentType = MrContentType};

            mr.Inputs(Bucket)
                .Filter(f => f.Equal("time_8"))
                .MapJs(m => m.Source("function (o) { return [1]; }"))
                .ReduceJs(r => r.Name("Riak.reduceSum").Keep(true));

            var result = Client.MapReduce(mr);
            result.ShouldNotBeNull();

            var mrResult = result;
            mrResult.PhaseResults.ShouldNotBeNull();
            mrResult.PhaseResults.Count().ShouldEqual(2);

            mrResult.PhaseResults.ElementAt(0).Phase.ShouldEqual(0u);
            mrResult.PhaseResults.ElementAt(1).Phase.ShouldEqual(1u);

            mrResult.PhaseResults.ElementAt(0).Values.Count().ShouldEqual(0);
            mrResult.PhaseResults.ElementAt(1).Values.Count().ShouldNotEqual(0);

            var values = JsonConvert.DeserializeObject<int[]>(mrResult.PhaseResults.ElementAt(1).Values.First().FromRiakString());
            values[0].ShouldEqual(1);
        }
コード例 #2
0
        public void SearchingByNameReturnsTheObjectId()
        {
            Client.Put(new RiakObject(Bucket, RiakSearchKey, RiakSearchDoc, RiakConstants.ContentTypes.ApplicationJson));
            Client.Put(new RiakObject(Bucket, RiakSearchKey2, RiakSearchDoc2, RiakConstants.ContentTypes.ApplicationJson));

            var mr = new RiakMapReduceQuery();

            var modFunArg = new RiakModuleFunctionArgInput
                                {
                Module = "riak_search",
                Function = "mapred_search",
                Arg = new[] {Bucket, "name:Al*"}
            };

            mr.Inputs(modFunArg);

            var result = Client.MapReduce(mr);
            result.IsSuccess.ShouldBeTrue();

            var mrResult = result.Value;
            mrResult.PhaseResults.Count().ShouldEqual(1);

            mrResult.PhaseResults.ElementAt(0).Values.ShouldNotBeNull();
            // TODO Add data introspection to test - need to verify the results, after all.
        }
コード例 #3
0
ファイル: SolrTests.cs プロジェクト: taliesins/CorrugatedIron
        public void SearchingByNameReturnsTheObjectId()
        {
            Client.Put(new RiakObject(Bucket, RiakSearchKey, RiakSearchDoc, RiakConstants.ContentTypes.ApplicationJson));
            Client.Put(new RiakObject(Bucket, RiakSearchKey2, RiakSearchDoc2, RiakConstants.ContentTypes.ApplicationJson));

            var mr = new RiakMapReduceQuery();

            var token = new RiakSearchPhraseToken("Al*");
            var solr = new SolrQuery { Fieldname = "name", QueryPart = token };

            var modFunArg = new RiakModuleFunctionArgInput
                                {
                                    Module = "riak_search",
                                    Function = "mapred_search",
                                    Arg = new[] {Bucket, solr.ToString()}
                                };

            mr.Inputs(modFunArg)
                .MapJs(m => m.Source(@"function(value, keydata, arg) { return [value]; }").Keep(true))
                .ReduceJs(r => r.Source(@"function(values, arg) { return values; }").Keep(true));

            var result = Client.MapReduce(mr);
            result.IsSuccess.ShouldBeTrue();

            var mrResult = result.Value;
            mrResult.PhaseResults.Count().ShouldEqual(2);

            mrResult.PhaseResults.ElementAt(0).Values.ShouldNotBeNull();
            mrResult.PhaseResults.ElementAt(1).Values.ShouldNotBeNull();
            // TODO Add data introspection to test - need to verify the results, after all.
        }
コード例 #4
0
        public void RangeQuery()
        {
            var Cluster = RiakCluster.FromConfig("riakConfig");
            var Client = Cluster.CreateClient();

            var bucket = "rsds";

            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 });

                    var position = 100 + i;

                    doc.BinIndex("position").Set(position.ToString());
                    
                    batch.Put(doc);
                }
            });

            var query = new RiakMapReduceQuery()
                    .Inputs(RiakIndex.Range("bucket", "position", 100, 200))
                    .MapJs(m => m.Name("Riak.mapValuesJson").Keep(true));

            var result = Client.MapReduce(query);
            //var items = result.Value.PhaseResults.SelectMany(x => x.GetObjects<dynamic>);
        }
コード例 #5
0
        public void AllKeysReturnsListOfKeys()
        {
            var bucket = Bucket + "_" + Guid.NewGuid().ToString();
            var originalKeys = new List<string>();

            for (var i = 0; i < 10; i++)
            {
                var o = new RiakObject(bucket, Guid.NewGuid().ToString(), "{ value: \"this is an object\" }");
                originalKeys.Add(o.Key);

                Client.Put(o);
            }

            var mr = new RiakMapReduceQuery()
                .Inputs(RiakIndex.AllKeys(bucket));

            var result = Client.MapReduce(mr);
            var keys = result.Value.PhaseResults.SelectMany(x => x.GetObjectIds()).ToList();

            result.IsSuccess.ShouldBeTrue(result.ErrorMessage);
            keys.Count.ShouldEqual(10);

            foreach (var key in keys)
            {
                key.Bucket.ShouldNotBeNullOrEmpty();
                key.Key.ShouldNotBeNullOrEmpty();
                originalKeys.Contains(key.Key).ShouldBeTrue();
            }
        }
コード例 #6
0
        public void BuildingComplexMapReduceJobsWithTheApiProducesTheCorrectCommand()
        {
            var query = new RiakMapReduceQuery
                {
                    ContentType = MrContentType
                }
                .Inputs("animals")
                .MapJs(m => m.Source("function(o) { if (o.key.indexOf('spider') != -1) return [1]; return []; }"))
                .ReduceJs(r => r.Name("Riak.reduceSum").Keep(true));

            var request = query.ToMessage();
            request.request.ShouldEqual(ComplexMrJobText.ToRiakString());
        }
コード例 #7
0
        public void BuildingComplexMapReduceJobsWithFiltersProducesTheCorrectCommand()
        {
            var query = new RiakMapReduceQuery
                {
                    ContentType = MrContentType
                }
                .Inputs("animals")
                //.Filter(new Matches<string>("spider"))
                .Filter(f => f.Matches("spider"))
                .MapJs(m => m.Source("function(o) { return [1]; }"))
                .ReduceJs(r => r.Name("Riak.reduceSum").Keep(true));

            var request = query.ToMessage();
            request.request.ShouldEqual(ComplexMrJobWithFilterText.ToRiakString());
        }
コード例 #8
0
        public void SearchingByNameReturnsTheObjectId()
        {
            Client.Put(new RiakObject(Bucket, RiakSearchKey, RiakSearchDoc, RiakConstants.ContentTypes.ApplicationJson));
            Client.Put(new RiakObject(Bucket, RiakSearchKey2, RiakSearchDoc2, RiakConstants.ContentTypes.ApplicationJson));

            var mr = new RiakMapReduceQuery()
                .Inputs(new RiakBucketSearchInput(Bucket, "name:A1*"));

            var result = Client.MapReduce(mr);
            result.IsSuccess.ShouldBeTrue(result.ErrorMessage);

            var mrResult = result.Value;
            mrResult.PhaseResults.Count().ShouldEqual(1);

            mrResult.PhaseResults.ElementAt(0).Values.ShouldNotBeNull();
            // TODO Add data introspection to test - need to verify the results, after all.
        }
コード例 #9
0
        public void RiakObjectLinksAreTheSameAsLinksRetrievedViaMapReduce()
        {
            var jeremiah = Client.Get(TestBucket, Jeremiah).Value;
            var jLinks = jeremiah.Links;

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

            var query = new RiakMapReduceQuery()
                .Inputs(input)
                .Link(l => l.AllLinks().Keep(true));

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

            // TODO 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 = from link in listOfLinks
                          from item in link
                          select item;

            mrLinks.Count().ShouldEqual(jLinks.Count);
            foreach (var link in jLinks)
            {
                mrLinks.ShouldContain(link);
            }
        }
コード例 #10
0
        public void LinksAreRetrievedWithAMapReducePhase()
        {
            var query = new RiakMapReduceQuery()
                .Inputs(TestBucket)
                //.Filter(new Matches<string>(Jeremiah))
                .Filter(f => f.Matches(Jeremiah))
                .Link(l => l.Tag("friends").Bucket(TestBucket).Keep(false))
                .ReduceErlang(r => r.ModFun("riak_kv_mapreduce", "reduce_set_union").Keep(true));

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

            var mrResult = result.Value;
            mrResult.PhaseResults.ShouldNotBeNull();
            mrResult.PhaseResults.Count().ShouldEqual(2);
        }
コード例 #11
0
        public void QueryingByIntIndexReturnsAListOfKeys()
        {
            for (var i = 0; i < 10; i++)
            {
                var o = new RiakObject(Bucket, Guid.NewGuid().ToString(), "{\"value\":\"this is an object\"}");
                o.IntIndex("age").Set(32, 20);

                Client.Put(o);
            }

            var mr = new RiakMapReduceQuery()
                .Inputs(RiakIndex.Match(Bucket, "age", 32));

            var result = Client.MapReduce(mr);
            result.IsSuccess.ShouldBeTrue(result.ErrorMessage);

            var keys = result.Value.PhaseResults.SelectMany(x => x.GetObjectIds()).ToList();

            keys.Count().ShouldEqual(10);

            foreach (var key in keys)
            {
                key.Bucket.ShouldNotBeNullOrEmpty();
                key.Key.ShouldNotBeNullOrEmpty();
            }
        }
コード例 #12
0
        private void InvokeExpireCallbackAndDeleteSession()
        {
            // MR to get sessions to delete
            var query = new RiakMapReduceQuery();
            query.Inputs(ApplicationName)
                .MapJs(
                    m =>
                    m.Source(@"
function (value, keyData, arg) {
    var now = new Date(Date.parse(arg));
    var metadata = value.values[0].metadata;
    var expires = metadata['X-Riak-Meta']['X-Riak-Meta-Expires'];

    if (arg > expires) 
    {
        return [value.key, expires];
    }
    else
    {
        return [];
    }
}")
                        .Argument(DateTime.Now.ToString("R")));

            var results = _client.MapReduce(query);
            
            if (results.IsSuccess)
            {
                var keys = results.Value.PhaseResults.ElementAt(results.Value.PhaseResults.Count() - 1).Value.FromRiakString();
                var keyList = JsonConvert.DeserializeObject<string[]>(keys);

                var riakObjectIdList = keyList.Select(key => new RiakObjectId(ApplicationName, key)).ToList();

                // for some stupid reason, we have to retrieve all of the deleted keys, process them, and THEN delete them
                var riakSessionObjects = _client.Get(riakObjectIdList);
                foreach (var riakSessionObject in riakSessionObjects)
                {
                    var value = riakSessionObject.Value;
                    var session = new RiakSessionItem(value);
                    _expireCallBack.Invoke(value.Key, Deserialize(session.SessionStoreItems));
                }

                _client.Async.Delete(riakObjectIdList, deleteResults => { return; });
            }
        }
コード例 #13
0
        public void BuildingSimpleMapReduceJobsWithTheApiProducesByteArrays()
        {
            var query = new RiakMapReduceQuery
                {
                    ContentType = MrContentType
                }
                .Inputs("animals")
                .MapJs(m => m.Source("function(v) { return [v]; }").Keep(true));

            var request = query.ToMessage();
            request.content_type.ShouldEqual(MrContentType.ToRiakString());
            request.request.ShouldEqual(MrJobText.ToRiakString());
        }
コード例 #14
0
        public IQueryable<Dinner> FindByLocation(float latitude, float longitude)
        {
            var mr = new RiakMapReduceQuery { ContentType = RiakConstants.ContentTypes.ApplicationJson };

            var input = new RiakIntIndexRangeInput(DinnerBucket, "EventDate_int", int.Parse(DateTime.Now.ToString("yyyyMMdd")), int.MaxValue);

            mr.Inputs(input)
                .MapJs(m => m.Name("Riak.mapValuesJson").Keep(false))
                .MapJs(
                    m =>
                    m.Source(
                        @"
            function (value, keyData, arg) {
            // From http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points
            var lat, long = arg.split(""|"");

            var R = 6371; // Radius of the earth in km
            var dLat = (lat2-lat).toRad();  // Javascript functions in radians
            var dLon = (lon2-lon).toRad();
            var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
            Math.sin(dLon/2) * Math.sin(dLon/2);
            var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
            var d = R * c; // Distance in km
            var m = d * 1.609344; // Distance in miles

            if (m < 100) {
            return [value];
            }
            else
            {
            return [];
            }
            }
            ")
                        .Argument(string.Format("{0}|{1}", latitude, longitude))
                        .Keep(true));

            return MapReduceDinners(mr);

            //var dinners = from dinner in FindUpcomingDinners()
            //              join i in NearestDinners(latitude, longitude)
            //              on dinner.DinnerID equals i.DinnerID
            //              select dinner;

            //return dinners;
        }
コード例 #15
0
        public IQueryable<Dinner> NearestDinners(double latitude, double longitude)
        {
            var mr = new RiakMapReduceQuery {ContentType = RiakConstants.ContentTypes.ApplicationJson};

            mr.Inputs(DinnerBucket)
                .MapJs(
                    m =>
                    m.Source(
                        @"
            function (value, keyData, arg) {
            // From http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points
            var lat, long = arg.split(""|"");

            var R = 6371; // Radius of the earth in km
            var dLat = (lat2-lat).toRad();  // Javascript functions in radians
            var dLon = (lon2-lon).toRad();
            var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
            Math.sin(dLon/2) * Math.sin(dLon/2);
            var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
            var d = R * c; // Distance in km
            var m = d * 1.609344; // Distance in miles

            if (m < 100) {
            return [value];
            }
            else
            {
            return [];
            }
            }
            ")
                        .Argument(string.Format("{0}|{1}", latitude, longitude))
                        .Keep(true));

            return MapReduceDinners(mr);

            //return from d in _db.Dinners
            //       where DistanceBetween(latitude, longitude, d.Latitude, d.Longitude) < 100
            //       select d;
        }
コード例 #16
0
        public void BuildingMapReducePhaseWithObjectArgumentProducesCorrectResult()
        {
            var query = new RiakMapReduceQuery { ContentType = MrContentType }
                .Inputs("animals")
                .ReduceJs(c => c.Name("Riak.reduceSlice").Keep(true).Argument(new { reduce_phase_only_1 = true }));

            var request = query.ToMessage();
            request.request.ShouldEqual(MrJobWithObjectArgument.ToRiakString());
        }
コード例 #17
0
        public void QueryingDollarKeyDoesNotAppendBinIndexSuffix()
        {
            var query = new RiakMapReduceQuery
                {
                    ContentType = MrContentType
                }
                .Inputs(RiakIndex.Range("animals", "$key", "0", "zzzzz"));

            var request = query.ToMessage();
            var requestString = request.request.FromRiakString();

            requestString.Contains("$key").ShouldBeTrue();
            requestString.Contains("$key_bin").ShouldBeFalse();
        }
コード例 #18
0
        public void BuildingSimpleMapReduceJobsWithTimeoutProducesTheCorrectCommand()
        {
            var query = new RiakMapReduceQuery
            {
                ContentType = MrContentType,
                Timeout = 100200
            }
                .Inputs("animals")
                .MapJs(m => m.Source("function(v) { return [v]; }").Keep(true));

            var request = query.ToMessage();
            request.content_type.ShouldEqual(MrContentType.ToRiakString());
            request.request.ShouldEqual(MrJobWithTimeoutText.ToRiakString());
        }
コード例 #19
0
        //
        // Query Methods
        public IQueryable<Dinner> FindDinnersByText(string q)
        {
            var mr = new RiakMapReduceQuery { ContentType = RiakConstants.ContentTypes.ApplicationJson };

            mr.Inputs(DinnerBucket)
                .MapJs(m => m.Source(@"
            function (value, keyData, arg) {
            var o = Riak.mapValuesJson(value)[0];

            if (o.Title.Contains(arg.keyword)
            || o.Description.Contains(arg.keyword)
            || o.HostedBy.Contains(arg.keyword))
            {
            return [o];
            }
            }
            ").Argument(q).Keep(true));

            // TODO create a version of this using RiakSearch

            var mrResult = _client.MapReduce(mr);
            if (mrResult.IsSuccess)
            {
                var results = mrResult.Value.PhaseResults.Last().GetObjects<Dinner>();
                return results.AsQueryable();
            }

            throw new ApplicationException(mrResult.ErrorMessage);
        }
コード例 #20
0
        private void Poll(RiakResult<RiakStreamedMapReduceResult> result)
        {
            if (result.IsSuccess)
            {
                foreach (var phase in result.Value.PhaseResults)
                {
                    // make sure we get hold of the phase result which has data
                    if (phase.Value != null)
                    {
                        // deserialize into an array of messages
                        var messages = phase.GetObject<YakMessage[]>();

                        // throw them on screen
                        foreach (var m in messages)
                        {
                            DisplayMessage(m);
                            _since = m.timestamp + 1;
                        }
                    }
                }
            }

            // create the next map reduce job
            var pollQuery = new RiakMapReduceQuery()
                .Inputs("messages")
                .MapJs(m => m.BucketKey("yakmr", "mapMessageSince").Argument(_since))
                .ReduceJs(r => r.BucketKey("yakmr", "reduceSortTimestamp").Keep(true));

            // do the usual wait
            WaitRandom();

            // and off we go again.
            _riakClient.Async.StreamMapReduce(pollQuery, Poll);
        }
コード例 #21
0
        public IQueryable<Dinner> FindUpcomingDinners()
        {
            var mr = new RiakMapReduceQuery { ContentType = RiakConstants.ContentTypes.ApplicationJson };

            // TODO: Modify DinnerId to be a string.

            var input = new RiakIntIndexRangeInput(DinnerBucket, "EventDate_int", int.Parse(DateTime.Now.ToString("yyyyMMdd")), int.MaxValue);

            mr.Inputs(input)
                .MapJs(m => m.Name("Riak.mapValuesJson").Keep(true));

            return MapReduceDinners(mr);

            //return from dinner in FindAllDinners()
            //       where dinner.EventDate >= DateTime.Now
            //       orderby dinner.EventDate
            //       select dinner;
        }
コード例 #22
0
        public void BuildingMapReducePhaseWithVaueTypeArgumentProducesCorrectResult()
        {
            var query = new RiakMapReduceQuery { ContentType = MrContentType }
                .Inputs("animals")
                .ReduceJs(c => c.Name("Riak.reduceSlice").Keep(true).Argument("slartibartfast"));

            var request = query.ToMessage();
            request.request.ShouldEqual(MrJobWithValueTypeArgument.ToRiakString());
        }
コード例 #23
0
        private IQueryable<Dinner> MapReduceDinners(RiakMapReduceQuery mr)
        {
            var mrResult = _client.MapReduce(mr);
            if (mrResult.IsSuccess)
            {
                var results = mrResult.Value.PhaseResults.Last().GetObjects<Dinner>();
                return results.AsQueryable();
            }

            throw new ApplicationException(mrResult.ErrorMessage);
        }
コード例 #24
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!");
        }
コード例 #25
0
        public void MapReduceQueriesReturnDataInBatch()
        {
            var bucket = Guid.NewGuid().ToString();

            Client.Batch(batch =>
                {
                    for (var i = 1; i < 11; i++)
                    {
                        var doc = new RiakObject(bucket, i.ToString(), new { value = i });
                        batch.Put(doc).IsSuccess.ShouldBeTrue();
                    }

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

                    var result = batch.MapReduce(query);
                    result.IsSuccess.ShouldBeTrue();

                    var mrRes = result.Value;
                    mrRes.PhaseResults.ShouldNotBeNull();
                    mrRes.PhaseResults.Count().ShouldEqual(2);

                    mrRes.PhaseResults.ElementAt(0).Phase.ShouldEqual(0u);
                    mrRes.PhaseResults.ElementAt(1).Phase.ShouldEqual(1u);

                    //mrRes.PhaseResults.ElementAt(0).Values.ShouldBeNull();
                    foreach(var v in mrRes.PhaseResults.ElementAt(0).Values)
                    {
                        v.ShouldBeNull();
                    }
                    mrRes.PhaseResults.ElementAt(1).Values.ShouldNotBeNull();

                    var values = result.Value.PhaseResults.ElementAt(1).GetObjects<int[]>().First();
                    values[0].ShouldEqual(10);
                });
        }
コード例 #26
0
 public void StreamMapReduce(RiakMapReduceQuery query, Action<RiakResult<RiakStreamedMapReduceResult>> callback)
 {
     ExecAsync(() => callback(_client.StreamMapReduce(query)));
 }
コード例 #27
0
        private void StartPolling()
        {
            // when we start polling we look to see any items from the last hour..
            _since = Since(DateTime.UtcNow.AddHours(-1));

            // and do a simple map reduce which gets the last 25 elements that appear in that
            // time frame.
            var pollQuery = new RiakMapReduceQuery()
                .Inputs("messages")
                .MapJs(m => m.BucketKey("yakmr", "mapMessageSince").Argument(_since))
                .ReduceJs(r => r.BucketKey("yakmr", "reduceSortTimestamp"))
                .ReduceJs(r => r.BucketKey("yakmr", "reduceLimitLastN").Argument(25).Keep(true));

            // make sure we don't have our MILLIONS of clients smashing the cluster at exactly
            // the same time
            WaitRandom();

            // then asyncrhonously stream the result set
            _riakClient.Async.StreamMapReduce(pollQuery, Poll);
        }
コード例 #28
0
        public void IntRangeQueriesReturnMultipleKeys()
        {
            for (var i = 0; i < 10; i++)
            {
                var o = new RiakObject(Bucket, Guid.NewGuid().ToString(), "{ value: \"this is an object\" }");
                o.IntIndex("age").Set(25 + i);

                Client.Put(o);
            }

            var mr = new RiakMapReduceQuery()
                .Inputs(RiakIndex.Range(Bucket, "age", 27, 30));

            var result = Client.MapReduce(mr);
            result.IsSuccess.ShouldBeTrue(result.ErrorMessage);
            result.Value.PhaseResults.SelectMany(x => x.GetObjectIds()).Count().ShouldEqual(4);

            // TODO write tests verifying results
        }
コード例 #29
0
        private void DeleteExpiredSessions()
        {
            // MR to get sessions to delete
            var query = new RiakMapReduceQuery();
            query.Inputs(ApplicationName)
                .MapJs(
                    m =>
                    m.Source(@"
function (value, keyData, arg) {
    var now = new Date(Date.parse(arg));
    var metadata = value.values[0].metadata;
    var expires = metadata['X-Riak-Meta']['X-Riak-Meta-Expires'];

    if (arg > expires) 
    {
        return [value.key, expires];
    }
    else
    {
        return [];
    }
}")
                        .Argument(DateTime.Now.ToString("R")));

            var results = _client.MapReduce(query);

            if (results.IsSuccess)
            {
                var keys = results.Value.PhaseResults.ElementAt(results.Value.PhaseResults.Count() - 1).Value.FromRiakString();
                var keyList = JsonConvert.DeserializeObject<string[]>(keys);

                var riakObjectIdList = keyList.Select(key => new RiakObjectId(ApplicationName, key)).ToList();

                _client.Async.Delete(riakObjectIdList, deleteResults => { return; });
            }
        }
コード例 #30
0
        public void KeysReturnsSelectiveListOfKeys()
        {
            var bucket = Bucket + "_" + Guid.NewGuid().ToString();
            var originalKeys = new List<string>();

            for (var i = 0; i < 10; i++)
            {
                var o = new RiakObject(bucket, i.ToString(), "{ value: \"this is an object\" }");
                originalKeys.Add(o.Key);

                Client.Put(o);
            }

            var mr = new RiakMapReduceQuery()
                .Inputs(RiakIndex.Keys(bucket, "2", "6"))
                .ReduceErlang(r => r.ModFun("riak_kv_mapreduce", "reduce_identity")
                .Argument("do_prereduce")
                .Keep(true));

            var result = Client.MapReduce(mr);
            var keys = result.Value.PhaseResults.SelectMany(x => x.GetObjectIds()).ToList();

            result.IsSuccess.ShouldBeTrue(result.ErrorMessage);
            keys.Count.ShouldEqual(5);

            foreach (var key in keys)
            {
                key.Bucket.ShouldNotBeNullOrEmpty();
                key.Key.ShouldNotBeNullOrEmpty();
                originalKeys.Contains(key.Key).ShouldBeTrue();
            }
        }