Beispiel #1
0
        public override async Task ExecuteAsync()
        {
            var key = "dotnetDevguideExampleDurability-" + DateTime.Now.Ticks;
            var data = new Data
            {
                Number = 42,
                Text = "Life, the Universe, and Everything",
                Date = DateTime.UtcNow
            };
            
            // The ReplicateTo parameter must be less than or equal to the number of replicas 
            // you have configured. Assuming that 3 replicas are configured, the following call
            // waits for replication to 3 replicas and persistence to 4 nodes in total.
            var result = await _bucket.UpsertAsync(key, data, ReplicateTo.Three, PersistTo.Four);
            Console.WriteLine("Durability status: " + result.Durability);

            if(!result.Success)
            {
                if (result.Status == ResponseStatus.NoReplicasFound)
                    Console.WriteLine("Write failed - not enough replicas configured to satisfy durability requirements");
                else
                    Console.WriteLine("An error has occured: {0}\r\n{1}", result.Message, result.Exception.ToString());
            }
            else
            {
                // It's possible for a write to succeed, but not satisfy durability.
                // For example, writing with PersistTo.Two and ReplicateTo.Zero on a 1-node cluster.
                if (result.Durability == Couchbase.IO.Operations.Durability.NotSatisfied)
                    Console.WriteLine("Write succeeded, but some durability requirements were not satisfied.");
            }

            // Wait for the write to be persisted to disk on one (normally the master) node.
            var result2 = await _bucket.UpsertAsync(key, data, ReplicateTo.Zero, PersistTo.One);
            Console.WriteLine("Durability status: " + result.Durability);
        }
Beispiel #2
0
        public override async Task ExecuteAsync()
        {
            var key = "dotnetDevguideExampleCas-" + DateTime.Now.Ticks;
            var data = new Data
            {
                Number = 0
            };

            // Set the inital number value to 0
            await _bucket.UpsertAsync(key, data);

            // Try to increment the number 1000 times without using CAS (10 threads x 100 increments)
            // We would expect the result to be Number == 1000 at the end of the process.
            var tasksWithoutCas = Enumerable.Range(1, 10).Select(i => UpdateNumberWithoutCas(key, 100));
            await Task.WhenAll(tasksWithoutCas);

            // Check if the actual result is 1000 as expected
            var result = await _bucket.GetAsync<Data>(key);
            Console.WriteLine("Expected number = 1000, actual number = " + result.Value.Number);


            // Set the inital number value back to 0
            await _bucket.UpsertAsync(key, data);

            // Now try to increment the number 1000 times with CAS
            var tasksWithCas = Enumerable.Range(1, 10).Select(i => UpdateNumberWithCas(key, 100));
            await Task.WhenAll(tasksWithCas);

            // Check if the actual result is 1000 as expected
            var result2 = await _bucket.GetAsync<Data>(key);
            Console.WriteLine("Expected number = 1000, actual number = " + result2.Value.Number);
        }
Beispiel #3
0
        public override async Task ExecuteAsync()
        {
            var key = "dotnetDevguideExampleRetrieve-" + DateTime.Now.Ticks;
            var data = new Data
            {
                Number = 42,
                Text = "Life, the Universe, and Everything",
                Date = DateTime.UtcNow
            };

            // Get non-existent document. 
            // Note that it's enough to check the Status property,
            // We're only checking all three to show they exist.
            var notFound = await _bucket.GetAsync<dynamic>(key);
            if (!notFound.Success &&
                notFound.Status == Couchbase.IO.ResponseStatus.KeyNotFound &&
                notFound.Value == null)
                Console.WriteLine("Document doesn't exist!");


            // Prepare a string value
            await _bucket.UpsertAsync(key, "Hello Couchbase!");

            // Get a string value
            var nonDocResult = await _bucket.GetAsync<string>(key);
            Console.WriteLine("Found: " + nonDocResult.Value);

            // Prepare a JSON document value
            await _bucket.UpsertAsync(key, data);

            // Get a JSON document string value
            var docResult = await _bucket.GetAsync<Data>(key);
            Console.WriteLine("Found: " + docResult.Value);
        }