public void When_Key_Does_Not_Exist_Exists_Returns_False()
        {
            var key    = "thekeythatdoesnotexists_perhaps";
            var result = _bucket.Exists(key);

            Assert.IsFalse(result);
        }
Beispiel #2
0
        public virtual Task <bool> Insert(string item)
        {
            bool result = true;

            if (!_bucket.Exists(_key))
            {
                var insert = _bucket.Insert(new Document <List <string> >
                {
                    Id      = _key,
                    Content = new List <string>()
                });

                if (!insert.Success)
                {
                    if (insert.Exception != null)
                    {
                        //throw insert.Exception;
                        result = false;
                    }
                    result = false;
                    //throw new InvalidOperationException(insert.Status.ToString());
                }
            }

            return(Task.FromResult <bool>(result));
        }
        public void SetUp()
        {
            _cluster = new Cluster(new ClientConfiguration
            {
                Servers = new List <Uri>
                {
                    new Uri("http://10.142.180.102:8091/")
                }
            });

            _cluster.Authenticate("Administrator", "password");
            _bucket = _cluster.OpenBucket();

            if (_bucket.Exists("a"))
            {
                var a = _bucket.Remove("a");
                Assert.IsTrue(a.Success);
            }

            if (_bucket.Exists("b"))
            {
                var b = _bucket.Remove("b");
                Assert.IsTrue(b.Success);
            }

            if (_bucket.Exists("c"))
            {
                var c = _bucket.Remove("c");
                Assert.IsTrue(c.Success);
            }

            if (!_bucket.Exists("counter"))
            {
                var counter = _bucket.Increment("counter");
                Assert.IsTrue(counter.Success);
            }

            var insertC = _bucket.Insert("c", new { });

            Assert.AreEqual(ResponseStatus.Success, insertC.Status);
        }
Beispiel #4
0
        private void EnsureIncrementInitialized()
        {
            // Create the increment document if it doesn't exist
            // and ensure that it's higher than the numbers in travel-sample

            if (!_bucket.Exists(IncrementDocument))
            {
                var maxId = _db.Query <Airline>().Max(p => p.Id);

                _bucket.Insert(IncrementDocument, maxId);
            }
        }
 /// <summary>
 /// 移除(键不存在,也是删除成功)
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public bool Remove(string key)
 {
     try
     {
         if (!_bucket.Exists(key))
         {
             return(true);
         }
         var result = _bucket.Remove(key);
         return(result.Success);
     }
     catch (Exception ex)
     {
         throw new Exception($"CouchBase Remove Operate Error, Message:{ex.Message}", ex);
     }
 }
Beispiel #6
0
        /// <summary>
        /// Creates the backing store.
        /// </summary>
        protected void CreateBackingStore()
        {
            if (!Bucket.Exists(Key))
            {
                var insert = Bucket.Insert(new Document <List <T> >
                {
                    Id      = Key,
                    Content = new List <T>()
                });

                if (!insert.Success)
                {
                    if (insert.Exception != null)
                    {
                        throw insert.Exception;
                    }
                    throw new InvalidOperationException(insert.Status.ToString());
                }
            }
        }
        public CarPrice GetPrice(CarPricingForm form)
        {
            var key = $"{form.Make}-{form.Model}-{form.Year}";

            if (_bucket.Exists(key))
            {
                return(_bucket.Get <CarPrice>(key).Value);
            }

            Thread.Sleep(5000);
            var result = CarPrice.Generate();

            _bucket.Upsert(new Document <CarPrice>
            {
                Id      = key,
                Content = result,
                Expiry  = 60 * 1000   // 60 seconds
            });

            return(result);
        }
        // check both the date and the filename
        // to ensure we don't import a file that's already been imported
        private bool ThisFileIsNewerThanTheLastImport(string csvFilePath)
        {
            if (!_bucket.Exists("csvImportTrackingInfo"))
            {
                return(true);
            }

            var lastCsvImportInfo = _bucket.Get <CsvImportTrackingInfo>("csvImportTrackingInfo").Value;
            var fileInfo          = new FileInfo(csvFilePath);

            if (fileInfo.LastWriteTime <= lastCsvImportInfo.ImportedDateTime)
            {
                _logger.LogInformation($"{csvFilePath} is older than the last import.");
                return(false);
            }

            if (fileInfo.Name == Path.GetFileName(lastCsvImportInfo.Filename))
            {
                _logger.LogInformation($"{csvFilePath} has already been imported.");
                return(false);
            }
            return(true);
        }
        public void PrepareTest()
        {
            var a = _bucket.Get <dynamic>("a");
            var b = _bucket.Get <dynamic>("b");
            var c = _bucket.Exists("c");

            Assert.AreEqual(ResponseStatus.KeyNotFound, a.Status);
            Assert.AreEqual(ResponseStatus.KeyNotFound, b.Status);
            Assert.IsTrue(c);

            a = _bucket.Insert("a", new { });
            Assert.IsTrue(a.Success);

            var increment = _bucket.Increment("counter");

            Assert.IsTrue(increment.Success);

            var aMeta = _bucket.MutateIn <dynamic>("a").
                        Insert("tx", new
            {
                ts    = increment.Value,
                md    = new[] { b.Id },
                value = new { name = "jeff" }
            }, SubdocPathFlags.Xattr);

            var execute = aMeta.Execute();

            Assert.IsTrue(execute.Success);

            a = _bucket.Get <dynamic>("a");
            Assert.AreEqual("", a.Value);

            b = _bucket.Insert("b", new { });
            Assert.IsTrue(b.Success);

            var bMeta = _bucket.MutateIn <dynamic>("b").
                        Insert("tx", new
            {
                ts    = increment.Value,
                md    = new[] { a.Id },
                value = new { name = "mike" }
            }, SubdocPathFlags.Xattr);

            execute = bMeta.Execute();
            Assert.IsTrue(execute.Success);

            b = _bucket.Get <dynamic>("b");
            Assert.AreEqual("", b.Value);

            dynamic aTx = _bucket.LookupIn <dynamic>("a").Get("tx", SubdocPathFlags.Xattr).Execute().Content("tx");
            dynamic bTx = _bucket.LookupIn <dynamic>("b").Get("tx", SubdocPathFlags.Xattr).Execute().Content("tx");

            //ts
            Assert.AreEqual(increment.Value, aTx.ts.Value);
            Assert.AreEqual(increment.Value, bTx.ts.Value);

            //md
            Assert.AreEqual(a.Id, bTx.md[0].Value);
            Assert.AreEqual(b.Id, aTx.md[0].Value);

            //value
            Assert.AreEqual("jeff", aTx.value.name.Value);
            Assert.AreEqual("mike", bTx.value.name.Value);
        }
        /// <inheritdoc />
        public override bool Exists(string key)
        {
            var fullKey = GetKey(key);

            return(_bucket.Exists(fullKey));
        }
 public bool IsExists(string key)
 {
     return(bucket.Exists(key));
 }