/// <summary>
        /// Adds an element with the provided key and value to the <see cref="T:Couchbase.Collections.CouchbaseDictionary`2" />.
        /// </summary>
        /// <param name="key">The object to use as the key of the element to add.</param>
        /// <param name="value">The object to use as the value of the element to add.</param>
        /// <exception cref="System.ArgumentNullException">key</exception>
        /// <exception cref="System.ArgumentException">Key exists.</exception>
        /// <exception cref="System.InvalidOperationException"></exception>
        public void Add(TKey key, TValue value)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            var sKey   = key.ToString();
            var lookup = Bucket.LookupIn <IDictionary <TKey, TValue> >(Key).
                         Exists(sKey).
                         Execute();

            if (lookup.Success)
            {
                throw new ArgumentException("Key exists.");
            }

            var insert = Bucket.MutateIn <IDictionary <TKey, TValue> >(Key).
                         Insert(sKey, value).
                         Execute();

            if (!insert.Success)
            {
                if (insert.Exception != null)
                {
                    throw insert.Exception;
                }
                throw new InvalidOperationException(insert.Status.ToString());
            }
        }
예제 #2
0
        public void Requeue()
        {
            lock (syncRoot)
            {
                IDocumentFragment <Documents.Queue> result = bucket.MutateIn <Documents.Queue>(Id)
                                                             .Remove(q => q.FetchedAt)
                                                             .Execute();

                reQueued = result.Success;
            }
        }
        public async Task CancelEvent(CancelEventCommand cancelEventCommand)
        {
            var documentResult = await _eventsBucket.MutateIn <EventDocument>(cancelEventCommand.EventId.ToString())
                                 .Replace("status", EventStatuses.CANCELLED)
                                 .ExecuteAsync();

            if (!documentResult.Success)
            {
                throw documentResult.Exception;
            }
        }
        public override void SetJobParameter(string id, string name, string value)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            bucket.MutateIn <Documents.Job>(id)
            .Upsert($"parameters.{name}", value)
            .Execute();
        }
예제 #5
0
        public async Task <bool> Patch(string id, JsonPatchDocument request)
        {
            //TODO: Refactor
            var mutateInBuilder = _bucket.MutateIn <BookEntity>(id);

            foreach (var operation in request.Operations)
            {
                switch (operation.OperationType)
                {
                case OperationType.Replace:
                    mutateInBuilder.Replace(operation.path, operation.value);
                    break;

                case OperationType.Add:
                    mutateInBuilder.Insert(operation.path, operation.value);
                    break;

                case OperationType.Remove:
                    mutateInBuilder.Remove(operation.path);
                    break;
                }
            }

            var result = await mutateInBuilder.ExecuteAsync();

            return(result.Success);
        }
        public async Task Remove(IBucket bucket, ITransactionDocument document)
        {
            await UpdateAtr(bucket, document.Key).ConfigureAwait(false);

            var result = await bucket.MutateIn <dynamic>(document.Key)
                         .Upsert(AtrIdFieldName, AtrId, SubdocPathFlags.Xattr)
                         .Upsert(AtrBucketNameFieldName, AtrBucket.Name, SubdocPathFlags.Xattr)
                         .Upsert(StagedVersionFieldName, AttemptId, SubdocPathFlags.Xattr)
                         .Upsert(StagedDataFieldName, RemovedStagedData, SubdocPathFlags.Xattr)
                         .WithCas(document.Cas)
                         .WithDurability(_config.PersistTo, _config.ReplicateTo)
                         .WithTimeout(_config.KeyValueTimeout)
                         .ExecuteAsync()
                         .ConfigureAwait(false);

            if (!result.Success)
            {
                //TODO: failed to remove
                return;
            }

            // update transaction document CAS and add to staged removes
            document.Cas = result.Cas;
            _stagedRemoves[document.Key] = document;
        }
        public override async Task <SpeechletResponse> Execute(IntentRequest intentRequest)
        {
            // get random fact from bucket
            var n1ql   = @"select m.*, meta(m).id
                            from boothduty m
                            where m.type = 'mongodbcomparison'
                            order by `number`, uuid()
                            limit 1;";
            var query  = QueryRequest.Create(n1ql);
            var result = await _bucket.QueryAsync <BoothFact>(query);

            if (result == null || !result.Rows.Any())
            {
                return(await CreateErrorResponseAsync());
            }
            var fact = result.First();

            // increment fact count
            await _bucket.MutateIn <dynamic>(fact.Id)
            .Counter("number", 1)
            .ExecuteAsync();

            // return text of fact
            return(await CreatePlainTextSpeechletReponseAsync(fact.Text));
        }
        private async Task UpdateAtr(IBucket bucket, string key)
        {
            if (string.IsNullOrWhiteSpace(AtrId))
            {
                AtrId     = AtrIdsHelper.GetAtrId(key);
                AtrBucket = bucket;
                State     = AttemptState.Pending;

                var result = await AtrBucket.MutateIn <dynamic>(AtrId)
                             .Upsert($"attempts.{AttemptId}.st", AttemptState.Pending.GetDescription(),
                                     SubdocPathFlags.CreatePath | SubdocPathFlags.Xattr, SubdocDocFlags.UpsertDocument)
                             .Upsert($"attempts.{AttemptId}.tst", MutationCasMacro,
                                     SubdocPathFlags.Xattr | SubdocPathFlags.ExpandMacroValues)
                             .Upsert($"attempts.{AttemptId}.exp", _config.Expiration.TotalMilliseconds,
                                     SubdocPathFlags.Xattr)
                             .WithDurability(_config.PersistTo, _config.ReplicateTo)
                             .WithTimeout(_config.KeyValueTimeout)
                             .ExecuteAsync()
                             .ConfigureAwait(false);

                if (!result.Success)
                {
                    //TODO: Failed to create ATR
                }
            }
        }
예제 #9
0
        public static void PushFrontExample(IBucket bucket, string id, string path, object value)
        {
            var fragment = bucket.MutateIn(id).
                           PushFront(path, value, false).
                           Execute <dynamic>();

            var status = fragment.OpStatus(path);

            Console.WriteLine(status);
        }
예제 #10
0
        // end::ExistsExample[]

        // tag::InsertExample[]
        public static void InsertExample(IBucket bucket, string id, string path, string value)
        {
            var fragment = bucket.MutateIn <dynamic>(id).
                           Insert(path, value, true). // false is the default
                           Execute();

            var status = fragment.OpStatus(path);

            Console.WriteLine(status);
        }
예제 #11
0
        // end::InsertExample[]

        // tag::RemoveExample[]
        public static void RemoveExample(IBucket bucket, string id, string path)
        {
            var fragment = bucket.MutateIn <dynamic>(id).
                           Remove(path).
                           Execute();

            var status = fragment.OpStatus(path);

            Console.WriteLine(status);
        }
예제 #12
0
        // end::ArrayInsertExample[]

        // tag::ArrayAddUniqueExample[]
        public static void ArrayAddUniqueExample(IBucket bucket, string id, string path, object value)
        {
            var fragment = bucket.MutateIn <dynamic>(id).
                           ArrayAddUnique(path, value).
                           Execute();

            var status = fragment.OpStatus(path);

            Console.WriteLine(status);
        }
예제 #13
0
        // end::ArrayAddUniqueExample[]

        // tag::CounterExample[]
        public static void CounterExample(IBucket bucket, string id, string path, long delta)
        {
            var fragment = bucket.MutateIn <dynamic>(id).
                           Counter(path, delta).
                           Execute();

            var status = fragment.OpStatus(path);

            Console.WriteLine(status);
        }
        // end::GetCartById[]

        public void AddItemToCart(Guid cartId, Item item)
        {
            // note that since I'm using the Item class
            // which is also being used for SQL in this demo
            // that there will be an "Id" field serialized to Couchbase
            // However, this Id field is completely unnecessary for Couchbase
            // and will always be '0' when in couchbase
            _bucket.MutateIn <ShoppingCart>(cartId.ToString())
            .ArrayAppend("items", item)
            .Execute();
        }
예제 #15
0
        public async Task <IActionResult> DoctorPatient([FromBody] PatientAssign assign)
        {
            IMutateInBuilder <dynamic> builder = _bucket.MutateIn <dynamic>(assign.Doctor);

            builder.ArrayAddUnique("patients", assign.Patient, true);
            var result = await builder.ExecuteAsync();

            if (result.Success)
            {
                return(Ok(assign));
            }
            return(CouchbaseError(result));
        }
예제 #16
0
        public async Task <IActionResult> Notes(string patientid, [FromBody] PatientNote notes)
        {
            // timestamp must be now, ignore/override any values posted in
            notes.Timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();

            IMutateInBuilder <dynamic> builder = _bucket.MutateIn <dynamic>(patientid)
                                                 .ArrayAppend("notes", notes, false);
            var result = await builder.ExecuteAsync();

            if (result.Success)
            {
                return(Ok(notes));
            }
            return(CouchbaseError(result));
        }
        public async Task <ITransactionDocument <T> > Insert <T>(IBucket bucket, string key, T content)
        {
            TryGetStagedWrite(key, out var document);

            await UpdateAtr(bucket, key).ConfigureAwait(false);

            var result = await bucket.MutateIn <T>(key)
                         .Upsert(AtrIdFieldName, AtrId, SubdocPathFlags.CreatePath | SubdocPathFlags.Xattr, SubdocDocFlags.InsertDocument)
                         .Upsert(AtrBucketNameFieldName, AtrBucket.Name, SubdocPathFlags.Xattr)
                         .Upsert(StagedVersionFieldName, AttemptId, SubdocPathFlags.Xattr)
                         .Upsert(StagedDataFieldName, content, SubdocPathFlags.Xattr)
                         .WithDurability(_config.PersistTo, _config.ReplicateTo)
                         .WithTimeout(_config.KeyValueTimeout)
                         .ExecuteAsync()
                         .ConfigureAwait(false);

            if (!result.Success)
            {
                //TODO: failed to insert
                return(null);
            }

            // create transaction document
            document = new TransactionDocument <T>(
                bucket,
                key,
                result.Cas,
                content,
                TransactionDocumentStatus.OwnWrite,
                new TransactionLinks <T>(
                    AtrId,
                    AtrBucket.Name,
                    AttemptId,
                    content
                    )
                );

            // add to staged inserts
            _stagedInserts[document.Key] = document;
            return((ITransactionDocument <T>)document);
        }
예제 #18
0
        public IFetchedJob Dequeue(string[] queues, CancellationToken cancellationToken)
        {
            lock (syncLock)
            {
                while (true)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    int invisibilityTimeoutEpoch = DateTime.UtcNow.Add(invisibilityTimeout.Negate()).ToEpoch();
                    logger.Trace("Looking for any jobs from the queue");

                    using (new CouchbaseDistributedLock(DISTRIBUTED_LOCK_KEY, defaultLockTimeout, storage))
                    {
                        using (IBucket bucket = storage.Client.OpenBucket(storage.Options.DefaultBucket))
                        {
                            BucketContext   context = new BucketContext(bucket);
                            Documents.Queue data    = context.Query <Documents.Queue>()
                                                      .Where(q => q.DocumentType == DocumentTypes.Queue && queues.Contains(q.Name) && (N1QlFunctions.IsMissing(q.FetchedAt) || q.FetchedAt < invisibilityTimeoutEpoch))
                                                      .OrderBy(q => q.CreatedOn)
                                                      .FirstOrDefault();

                            if (data != null)
                            {
                                IDocumentFragment <Documents.Queue> result = bucket.MutateIn <Documents.Queue>(data.Id)
                                                                             .Upsert(q => q.FetchedAt, DateTime.UtcNow.ToEpoch(), true)
                                                                             .Execute();

                                if (result.Success)
                                {
                                    logger.Trace($"Found job {data.JobId} from the queue {data.Name}");
                                    return(new FetchedJob(storage, data));
                                }
                            }
                        }
                    }

                    logger.Trace($"Unable to find any jobs in the queue. Will check the queue for jobs in {storage.Options.QueuePollInterval.TotalSeconds} seconds");
                    cancellationToken.WaitHandle.WaitOne(storage.Options.QueuePollInterval);
                }
            }
        }
        private async Task <long> GetNextJobIdAsync()
        {
            var key = JobIdentity.GetKey();

            var builder = _bucket.MutateIn <JobIdentity>(key);

            builder.Counter(p => p.Id, 1, true);

            var result = await builder.ExecuteAsync();

            if (result.Status == ResponseStatus.KeyNotFound)
            {
                var content = new JobIdentity
                {
                    Id = 0
                };

                var document = new Document <JobIdentity>
                {
                    Content = content,
                    Id      = key
                };

                var insertResult = await _bucket.InsertAsync(document);

                if (insertResult.Status == ResponseStatus.KeyExists || insertResult.Status == ResponseStatus.Success)
                {
                    // Document was created by us or by another service, try to increment again
                    return(await GetNextJobIdAsync());
                }

                // Document was not created, throw error
                insertResult.EnsureSuccess();
            }

            result.EnsureSuccess();

            return(result.Content(c => c.Id));
        }
        private static async Task <int> GetNextId(IBucket bucket)
        {
            while (true)
            {
                var builder = bucket.MutateIn <AirlineId>(AirlineId.GetKey());
                builder.Counter(p => p.Id, 1, true);

                var result = await builder.ExecuteAsync();

                if (result.Status == ResponseStatus.KeyNotFound)
                {
                    var document = new Document <AirlineId>
                    {
                        Content = new AirlineId
                        {
                            Id = 100000 // Base for new increments, probably 0 for real apps
                        },
                        Id = AirlineId.GetKey()
                    };

                    var insertResult = await bucket.InsertAsync(document);

                    if (insertResult.Status != ResponseStatus.KeyExists)
                    {
                        // Ignore failure if key exists, just means another thread created it simultaneously
                        insertResult.EnsureSuccess();
                    }

                    // Now that the document exists, try to increment again on next loop
                }
                else
                {
                    result.EnsureSuccess();

                    return(result.Content(p => p.Id));
                }
            }
        }
 public async Task UpdateOneColumnAsync(string key, KeyValuePair <string, object> filter)
 {
     await _bucket.MutateIn <T>(key)
     .Replace(filter.Key, filter.Value)
     .ExecuteAsync();
 }
예제 #22
0
        public override void ExpireJob(string jobId, TimeSpan expireIn)
        {
            if (string.IsNullOrEmpty(jobId))
            {
                throw new ArgumentNullException(nameof(jobId));
            }
            if (expireIn.Duration() != expireIn)
            {
                throw new ArgumentException(@"The `expireIn` value must be positive.", nameof(expireIn));
            }

            QueueCommand(() =>
            {
                int epoch = DateTime.UtcNow.Add(expireIn).ToEpoch();
                bucket.MutateIn <Job>(jobId)
                .Upsert(j => j.ExpireOn, epoch, true)
                .Execute();
            });
        }
예제 #23
0
 public void AddFriend(Guid userId, Guid friendId)
 {
     _bucket.MutateIn <dynamic>(userId.ToString())
     .ArrayAddUnique("friends", friendId.ToString())
     .Execute();
 }
        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);
        }
        public void MutateIn_InsertDictionary_ValidPath_ReturnsSuccess()
        {
            var key = "MutateIn_InsertDictionary_ValidPath_ReturnsSuccess";

            _bucket.Upsert(key, new { foo = "bar", bar = new Dictionary <string, string>() });

            var builder = _bucket.MutateIn <dynamic>(key).Insert("bar.baz", "faz", true);
            var result  = builder.Execute();

            Assert.AreEqual(ResponseStatus.Success, result.Status);
        }
예제 #26
0
        public async Task <IActionResult> UpdateQuant(string offeringID, int qty = 0)
        {
            var ID = GetID();

            if (ID == null)
            {
                return(BadRequest(new BadRequestError("user_id not found.")));
            }

            if (qty < 0)
            {
                return(BadRequest(new BadRequestError("Invalid quantity.")));
            }

            // using a view to only get the information from the Basket document that we need
            var query = new ViewQuery().From("dev_BasketDisc", "by_id").Key(new List <string> {
                ID, offeringID
            });
            var res = await _bucket.QueryAsync <dynamic>(query);

            // check to make sure the ViewQuery was successful and returned information
            if (!res.Success || res.Rows.Count() == 0)
            {
                return(NotFound(res.Message));
            }

            // set the variables that will be needed from the info retrieved from the query
            var           info       = res.Rows.ToList().First().Value;
            int           index      = info[0];
            OfferingsDisc offering   = JsonConvert.DeserializeObject <OfferingsDisc>(info[3].ToString());
            decimal       total_cost = Convert.ToDecimal(info[1]) - Convert.ToDecimal(offering.totalOfferingCost);

            if (qty == 0)
            {
                /*
                 * If the qty is 0 we need to remove that item from the users Basket document
                 * we also need to update the total_cost and the total_items
                 */
                var response = await _bucket.MutateIn <BasketDisc>(ID)
                               .Upsert("total_cost", total_cost.ToString())
                               .Upsert("total_items", info[2] - 1)
                               .Remove($"offeringsDisc[{index}]")
                               .ExecuteAsync();

                if (!response.Success)
                {
                    return(NotFound(new NotFoundError(response.Message)));
                }

                return(Ok(response));
            }

            // if qty > 0 update the users document
            offering.Quantity = qty;
            offering          = CalcOfferingCost(offering);
            total_cost       += Convert.ToDecimal(offering.totalOfferingCost);

            var update_res = await _bucket.MutateIn <BasketDisc>(ID)
                             .Upsert("total_cost", total_cost.ToString())
                             .Replace($"offeringsDisc[{index}]", offering)
                             .ExecuteAsync();

            if (!update_res.Success)
            {
                return(NotFound(new NotFoundError(update_res.Message)));
            }

            return(Ok(update_res));
        }
예제 #27
0
 /// <summary>
 /// Insert a subdocument for a document by property name
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="childName"></param>
 /// <param name="value"></param>
 /// <returns></returns>
 public async Task <IDocumentFragment <T> > InsertChildAsync <T, TChild>(string key, string childName, TChild value)
 {
     return(await _bucket.MutateIn <T>(key.ToLower())
            .Insert(childName, value, true)
            .ExecuteAsync());
 }
        private void Acquire(TimeSpan timeout)
        {
            logger.Trace($"Trying to acquire lock for {resource} within {timeout.TotalSeconds} seconds");

            System.Diagnostics.Stopwatch acquireStart = new System.Diagnostics.Stopwatch();
            acquireStart.Start();

            string id = $"{resource}:{DocumentTypes.Lock}".GenerateHash();

            while (string.IsNullOrEmpty(resourceId))
            {
                // default ttl for lock document
                TimeSpan ttl = DateTime.UtcNow.Add(timeout).AddMinutes(1).TimeOfDay;

                // read the document
                IDocumentResult <Lock> document = bucket.GetDocument <Lock>(id);

                // false means the document does not exists got ahead and create
                if (document.Success == false)
                {
                    Lock @lock = new Lock
                    {
                        Id       = id,
                        Name     = resource,
                        ExpireOn = DateTime.UtcNow.Add(timeout).ToEpoch()
                    };

                    IOperationResult <Lock> result = bucket.Insert(@lock.Id, @lock, ttl);
                    if (result.Success)
                    {
                        resourceId = id;
                        break;
                    }
                }
                else if (document.Content != null)
                {
                    if (document.Content.ExpireOn < DateTime.UtcNow.ToEpoch())
                    {
                        IDocumentFragment <Lock> result = bucket.MutateIn <Lock>(id)
                                                          .WithCas(document.Document.Cas)
                                                          .WithExpiry(ttl)
                                                          .Upsert(l => l.ExpireOn, DateTime.UtcNow.Add(timeout).ToEpoch(), false)
                                                          .Execute();

                        if (result.Success)
                        {
                            resourceId = id;
                            break;
                        }
                    }
                }

                // check the timeout
                if (acquireStart.ElapsedMilliseconds > timeout.TotalMilliseconds)
                {
                    throw new CouchbaseDistributedLockException($"Could not place a lock on the resource '{resource}': Lock timeout.");
                }

                // sleep for 2000 millisecond
                logger.Trace($"Unable to acquire lock for {resource}. Will check try after 2 seconds");
                System.Threading.Thread.Sleep(2000);
            }

            logger.Trace($"Acquired lock for {resource} in {acquireStart.Elapsed.TotalSeconds} seconds");
        }
예제 #29
0
 private async void OnAssigned(AssignedTask @event)
 {
     await _bucket.MutateIn <TaskDocument>(@event.TaskId.ToString())
     .Replace("assignedTo", @event.AssignedTo)
     .ExecuteAsync();
 }