public void CustomSerializerWillSerializeJson()
        {
            var testPerson = new Person
            {
                DateOfBirth = new DateTime(1978, 12, 5, 0, 0, 0, DateTimeKind.Utc),
                Email = "*****@*****.**",
                Name = new Name
                {
                    FirstName = "OJ",
                    Surname = "Reeves"
                },
                PhoneNumbers = new List<PhoneNumber>
                {
                    new PhoneNumber
                    {
                        Number = "12345678",
                        NumberType = PhoneNumberType.Home
                    }
                }
            };

            var sots = new SerializeObjectToString<Person>(JsonConvert.SerializeObject);

            var obj = new RiakObject("bucket", "key");
            obj.SetObject(testPerson, RiakConstants.ContentTypes.ApplicationJson, sots);
            obj.Value.ShouldNotBeNull();
            obj.ContentType.ShouldEqual(RiakConstants.ContentTypes.ApplicationJson);

            var json = obj.Value.FromRiakString();
            json.ShouldEqual("{\"Name\":{\"FirstName\":\"OJ\",\"Surname\":\"Reeves\"},\"PhoneNumbers\":[{\"Number\":\"12345678\",\"NumberType\":1}],\"DateOfBirth\":\"\\/Date(281664000000)\\/\",\"Email\":\"[email protected]\"}");

            var deserialisedPerson = obj.GetObject<Person>();
            deserialisedPerson.ShouldEqual(testPerson);
        }
        public void ObjectsAreConvertedProperly()
        {
            var testPerson = new Person
            {
                DateOfBirth = new DateTime(1978, 12, 5, 0, 0, 0, DateTimeKind.Utc),
                Email = "*****@*****.**",
                Name = new Name
                {
                    FirstName = "OJ",
                    Surname = "Reeves"
                },
                PhoneNumbers = new List<PhoneNumber>
                {
                    new PhoneNumber
                    {
                        Number = "12345678",
                        NumberType = PhoneNumberType.Home
                    }
                }
            };
            var obj = new RiakObject("bucket", "key");
            obj.SetObject(testPerson);
            obj.Value.ShouldNotBeNull();
            obj.ContentType.ShouldEqual(RiakConstants.ContentTypes.ApplicationJson);

            var json = obj.Value.FromRiakString();
            json.ShouldEqual("{\"Name\":{\"FirstName\":\"OJ\",\"Surname\":\"Reeves\"},\"PhoneNumbers\":[{\"Number\":\"12345678\",\"NumberType\":1}],\"DateOfBirth\":\"1978-12-05T00:00:00Z\",\"Email\":\"[email protected]\"}");

            var deserialisedPerson = obj.GetObject<Person>();
            deserialisedPerson.ShouldEqual(testPerson);
        }
Exemplo n.º 3
0
        public void ObjectsAreConvertedProperly()
        {
            var testPerson = new Person
            {
                DateOfBirth = new DateTime(1978, 12, 5, 0, 0, 0, DateTimeKind.Utc),
                Email       = "*****@*****.**",
                Name        = new Name
                {
                    FirstName = "OJ",
                    Surname   = "Reeves"
                },
                PhoneNumbers = new List <PhoneNumber>
                {
                    new PhoneNumber
                    {
                        Number     = "12345678",
                        NumberType = PhoneNumberType.Home
                    }
                }
            };
            var obj = new RiakObject("bucket", "key");

            obj.SetObject(testPerson);
            obj.Value.ShouldNotBeNull();
            obj.ContentType.ShouldEqual(RiakConstants.ContentTypes.ApplicationJson);

            var json = obj.Value.FromRiakString();

            json.ShouldEqual("{\"Name\":{\"FirstName\":\"OJ\",\"Surname\":\"Reeves\"},\"PhoneNumbers\":[{\"Number\":\"12345678\",\"NumberType\":1}],\"DateOfBirth\":\"\\/Date(281664000000)\\/\",\"Email\":\"[email protected]\"}");

            var deserialisedPerson = obj.GetObject <Person>();

            deserialisedPerson.ShouldEqual(testPerson);
        }
Exemplo n.º 4
0
        public virtual TModel Get(string key, bool notFoundOK = false)
        {
            var riakObjectId = new RiakObjectId(BucketType, Bucket, key);
            RiakResult <RiakObject> result = client.Get(riakObjectId);

            CheckResult(result, notFoundOK);
            RiakObject value = result.Value;

            if (notFoundOK && value == null)
            {
                return(default(TModel));
            }
            else
            {
                return(value.GetObject <TModel>());
            }
        }
Exemplo n.º 5
0
        public void JsonConversionTimerTest()
        {
            var testPerson = new Person
            {
                DateOfBirth = new DateTime(1978, 12, 5, 0, 0, 0, DateTimeKind.Utc),
                Email       = "*****@*****.**",
                Name        = new Name
                {
                    FirstName = "OJ",
                    Surname   = "Reeves"
                },
                PhoneNumbers = new List <PhoneNumber>
                {
                    new PhoneNumber
                    {
                        Number     = "12345678",
                        NumberType = PhoneNumberType.Home
                    }
                }
            };
            var obj = new RiakObject("bucket", "key");

            var sw = new Stopwatch();

            sw.Start();
            const int iterations = 1000000;

            for (var i = 0; i < iterations; ++i)
            {
                obj.SetObject(testPerson);
            }
            sw.Stop();
            Console.WriteLine("Serialisation took a total of {0} - {1} per iteration", sw.Elapsed, new TimeSpan(sw.ElapsedTicks / iterations));

            sw.Reset();
            sw.Start();

            for (var i = 0; i < iterations; ++i)
            {
                var result = obj.GetObject <Person>();
            }
            sw.Stop();
            Console.WriteLine("De" +
                              "serialisation took a total of {0} - {1} per iteration", sw.Elapsed, new TimeSpan(sw.ElapsedTicks / iterations));
        }
Exemplo n.º 6
0
        private async Task <bool> UpdateTimeDb(string streamId, Func <IEnumerable <Quantum>, IEnumerable <Quantum> > updateAction)
        {
            var retries = 0;

            while (retries < 5)
            {
                var options = new RiakGetOptions {
                    BasicQuorum = true, NotFoundOk = false
                };
                options.SetRw(Quorum.WellKnown.Quorum);

                var id = new RiakObjectId("default", "Timeseries", streamId);
                _logger.Debug("Getting timeseries db of id {0}", id.Key);
                var result = await _client.Async.Get(id, options).ConfigureAwait(false);

                RiakObject timedb = result.Value;
                if (!result.IsSuccess)
                {
                    timedb = await CreateNewDb(streamId).ConfigureAwait(false);
                }
                if (timedb == null)
                {
                    _logger.Error("Could not find or create ts db for metric {0}", streamId);
                    continue;
                }

                var quantums = timedb.GetObject <IEnumerable <Quantum> >();
                quantums = updateAction(quantums);

                timedb.SetObject(quantums);

                var uptOptions = new RiakPutOptions {
                    IfNoneMatch = false, IfNotModified = true, ReturnHead = true, ReturnBody = false
                };
                uptOptions.SetW(Quorum.WellKnown.Quorum);
                var updated = await _client.Async.Put(timedb, uptOptions).ConfigureAwait(false);

                if (updated.IsSuccess)
                {
                    return(true);
                }
                retries++;
            }
            return(false);
        }
        public void JsonConversionTimerTest()
        {
            var testPerson = new Person
            {
                DateOfBirth = new DateTime(1978, 12, 5, 0, 0, 0, DateTimeKind.Utc),
                Email = "*****@*****.**",
                Name = new Name
                {
                    FirstName = "OJ",
                    Surname = "Reeves"
                },
                PhoneNumbers = new List<PhoneNumber>
                {
                    new PhoneNumber
                    {
                        Number = "12345678",
                        NumberType = PhoneNumberType.Home
                    }
                }
            };
            var obj = new RiakObject("bucket", "key");

            var sw = new Stopwatch();
            sw.Start();
            const int iterations = 1000000;

            for (var i = 0; i < iterations; ++i)
            {
                obj.SetObject(testPerson);
            }
            sw.Stop();
            Console.WriteLine("Serialisation took a total of {0} - {1} per iteration", sw.Elapsed, new TimeSpan(sw.ElapsedTicks / iterations));

            sw.Reset();
            sw.Start();

            for (var i = 0; i < iterations; ++i)
            {
                var result = obj.GetObject<Person>();
            }
            sw.Stop();
            Console.WriteLine("De" +
                "serialisation took a total of {0} - {1} per iteration", sw.Elapsed, new TimeSpan(sw.ElapsedTicks / iterations));
        }
Exemplo n.º 8
0
        public void CustomSerializerWillSerializeJson()
        {
            var testPerson = new Person
            {
                DateOfBirth = new DateTime(1978, 12, 5, 0, 0, 0, DateTimeKind.Utc),
                Email       = "*****@*****.**",
                Name        = new Name
                {
                    FirstName = "OJ",
                    Surname   = "Reeves"
                },
                PhoneNumbers = new List <PhoneNumber>
                {
                    new PhoneNumber
                    {
                        Number     = "12345678",
                        NumberType = PhoneNumberType.Home
                    }
                }
            };

            var sots = new SerializeObjectToString <Person>(JsonConvert.SerializeObject);

            var obj = new RiakObject("bucket", "key");

            obj.SetObject(testPerson, RiakConstants.ContentTypes.ApplicationJson, sots);
            obj.Value.ShouldNotBeNull();
            obj.ContentType.ShouldEqual(RiakConstants.ContentTypes.ApplicationJson);

            var json = obj.Value.FromRiakString();

            json.ShouldEqual("{\"Name\":{\"FirstName\":\"OJ\",\"Surname\":\"Reeves\"},\"PhoneNumbers\":[{\"Number\":\"12345678\",\"NumberType\":1}],\"DateOfBirth\":\"1978-12-05T00:00:00Z\",\"Email\":\"[email protected]\"}");

            var deserialisedPerson = obj.GetObject <Person>();

            deserialisedPerson.ShouldEqual(testPerson);
        }
 public void NonJsonObjectsCantBeDeserialisedFromJson()
 {
     var obj = new RiakObject("bucket", "key", "{\"Name\":{\"FirstName\":\"OJ\",\"Surname\":\"Reeves\"},\"PhoneNumbers\":[{\"Number\":\"12345678\",\"NumberType\":1}],\"DateOfBirth\":\"1978-12-05T00:00:00Z\",\"Email\":\"[email protected]\"}", RiakConstants.ContentTypes.TextPlain);
     obj.GetObject<Person>();
 }
Exemplo n.º 10
0
        public void NonJsonObjectsCantBeDeserialisedFromJson()
        {
            var obj = new RiakObject("bucket", "key", "{\"Name\":{\"FirstName\":\"OJ\",\"Surname\":\"Reeves\"},\"PhoneNumbers\":[{\"Number\":\"12345678\",\"NumberType\":1}],\"DateOfBirth\":\"\\/Date(281664000000)\\/\",\"Email\":\"[email protected]\"}", RiakConstants.ContentTypes.TextPlain);

            obj.GetObject <Person>();
        }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            const string bucket = "test";

            try
            {
                using (IRiakEndPoint endpoint = RiakCluster.FromConfig("riakConfig"))
                {
                    IRiakClient client = endpoint.CreateClient();

                    // Creating Objects In Riak
                    Console.WriteLine("Creating Objects In Riak...");

                    int val1        = 1;
                    var objectId1   = new RiakObjectId(bucket, "one");
                    var riakObject1 = new RiakObject(objectId1, val1);
                    var result      = client.Put(riakObject1);
                    CheckResult(result);

                    string val2        = "two";
                    var    objectId2   = new RiakObjectId(bucket, "two");
                    var    riakObject2 = new RiakObject(objectId2, val2);
                    result = client.Put(riakObject2);
                    CheckResult(result);

                    var val3 = new Dictionary <string, int>
                    {
                        { "myValue1", 3 },
                        { "myValue2", 4 }
                    };
                    var objectId3   = new RiakObjectId(bucket, "three");
                    var riakObject3 = new RiakObject(objectId3, val3);
                    result = client.Put(riakObject3);
                    CheckResult(result);

                    // Fetching Objects From Riak
                    Console.WriteLine("Reading Objects From Riak...");

                    var fetchResult1 = client.Get(objectId1);
                    CheckResult(fetchResult1);
                    RiakObject fetchObject1 = fetchResult1.Value;
                    int        fetchVal1    = fetchObject1.GetObject <int>();
                    Debug.Assert(val1 == fetchVal1, "Assert Failed", "val1 {0} != fetchVal1 {1}", val1, fetchVal1);

                    var fetchResult2 = client.Get(objectId2);
                    CheckResult(fetchResult2);
                    RiakObject fetchObject2 = fetchResult2.Value;
                    string     fetchVal2    = fetchObject2.GetObject <string>();
                    Debug.Assert(val2 == fetchVal2, "Assert Failed", "val2 {0} != fetchVal2 {1}", val2, fetchVal2);

                    var fetchResult3 = client.Get(objectId3);
                    CheckResult(fetchResult3);
                    RiakObject fetchObject3 = fetchResult3.Value;
                    var        fetchVal3    = fetchObject3.GetObject <Dictionary <string, int> >();
                    Debug.Assert(dictEqualityComparer.Equals(val3, fetchVal3), "Assert Failed", "val3 {0} != fetchVal3 {1}", val3, fetchVal3);

                    // Updating Objects In Riak
                    Console.WriteLine("Updating Objects In Riak");

                    fetchVal3["myValue1"] = 42;
                    var updateObject1 = new RiakObject(bucket, "three", fetchVal3);
                    var updateResult1 = client.Put(updateObject1);
                    CheckResult(updateResult1);

                    var fetchResult4 = client.Get(objectId3);
                    CheckResult(fetchResult4);
                    RiakObject fetchObject4 = fetchResult4.Value;
                    var        fetchVal4    = fetchObject4.GetObject <Dictionary <string, int> >();
                    Debug.Assert(false == dictEqualityComparer.Equals(val3, fetchVal4), "Assert Failed", "val3 {0} == fetchVal4 {1}", val3, fetchVal4);
                    Debug.Assert(fetchVal4["myValue1"] == 42, "Assert Failed", "myValue1 should have been 42");

                    // Deleting Objects From Riak
                    Console.WriteLine("Deleting Objects From Riak...");

                    RiakResult delResult1 = client.Delete(objectId1);
                    CheckResult(delResult1);

                    RiakResult delResult2 = client.Delete(objectId2);
                    CheckResult(delResult2);

                    RiakResult delResult3 = client.Delete(objectId3);
                    CheckResult(delResult3);


                    // Working With Complex Objects
                    Console.WriteLine("Working With Complex Objects...");

                    var book = new Book();
                    book.ISBN        = "1111979723";
                    book.Title       = "Moby Dick";
                    book.Author      = "Herman Melville";
                    book.Body        = "Call me Ishmael. Some years ago...";
                    book.CopiesOwned = 3;

                    var bookId        = new RiakObjectId("books", book.ISBN);
                    var bookObject    = new RiakObject(bookId, book);
                    var bookPutResult = client.Put(bookObject);
                    CheckResult(bookPutResult);

                    var fetchBookResult = client.Get(bookId);
                    CheckResult(fetchBookResult);
                    RiakObject fetchedBookObject = fetchBookResult.Value;
                    string     bookJson          = Encoding.UTF8.GetString(fetchedBookObject.Value);
                    Console.WriteLine("Serialized Object: {0}", bookJson);

                    var bookDeleteResult = client.Delete(bookId);
                    CheckResult(bookDeleteResult);
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Exception: {0}", e.Message);
            }
        }
        public void NonJsonObjectsCantBeDeserialisedFromJson()
        {
            var obj = new RiakObject("bucket", "key", "{\"Name\":{\"FirstName\":\"OJ\",\"Surname\":\"Reeves\"},\"PhoneNumbers\":[{\"Number\":\"12345678\",\"NumberType\":1}],\"DateOfBirth\":\"1978-12-05T00:00:00Z\",\"Email\":\"[email protected]\"}", RiakConstants.ContentTypes.TextPlain);

            Assert.Throws <NotSupportedException>(() => obj.GetObject <Person>());
        }