public static Task <IGetResult> GetAsync(this ICouchbaseCollection collection, string id)
 {
     return(collection.GetAsync(id, new GetOptions()));
 }
        /// <summary>
        /// Executes a query and ingests the results as documents into Couchbase server for further analytics.
        /// <para>
        /// NOTE: This is an experimental API and may change in the future.
        /// </para>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cluster"></param>
        /// <param name="collection"></param>
        /// <param name="statement"></param>
        /// <param name="ingestOptions"></param>
        /// <returns></returns>
        public static async Task <IEnumerable <IMutationResult> > IngestAsync <T>(this ICluster cluster, string statement, ICouchbaseCollection collection, IngestOptions ingestOptions = null)
        {
            //use defaults if not options not explicitly passed
            ingestOptions ??= new IngestOptions();

            if (ingestOptions.TokenValue.IsCancellationRequested)
            {
                ingestOptions.TokenValue.ThrowIfCancellationRequested();
            }

            //execute the analytics query
            var result = await cluster.AnalyticsQueryAsync <T>(
                statement,
                options => options.CancellationToken(ingestOptions.TokenValue)
                ).ConfigureAwait(false);

            // ingest result into collection
            var results = new ConcurrentBag <Task <IMutationResult> >();

            await foreach (var row in result.WithCancellation(ingestOptions.TokenValue).ConfigureAwait(false))
            {
                Task <IMutationResult> op;
                switch (ingestOptions.IngestMethodValue)
                {
                case IngestMethod.Insert:
                    op = collection.InsertAsync(
                        ingestOptions.IdGeneratorValue(row),
                        row,
                        options =>
                    {
                        options.Expiry(ingestOptions.ExpiryValue);
                        options.Timeout(ingestOptions.TimeoutValue);
                    });
                    break;

                case IngestMethod.Upsert:
                    op = collection.UpsertAsync(
                        ingestOptions.IdGeneratorValue(row),
                        row,
                        options =>
                    {
                        options.Expiry(ingestOptions.ExpiryValue);
                        options.Timeout(ingestOptions.TimeoutValue);
                    });
                    break;

                case IngestMethod.Replace:
                    op = collection.ReplaceAsync(
                        ingestOptions.IdGeneratorValue(row),
                        row,
                        options =>
                    {
                        options.Expiry(ingestOptions.ExpiryValue);
                        options.Timeout(ingestOptions.TimeoutValue);
                    });
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                results.Add(op);
            }

            return(await Task.WhenAll(results).ConfigureAwait(false));
        }
Ejemplo n.º 3
0
 public static IAsyncDisposable RemoveDocument(ICouchbaseCollection collection, string docId,
                                               ITestOutputHelper outputHelper)
 {
     return(new DisposeCleanerAsync(() => collection.RemoveAsync(docId), outputHelper));
 }
 public async Task UnstageRemove(ICouchbaseCollection collection, string docId, ulong cas = 0) => _ = await LookupDocumentAsync(collection, docId);
 internal PersistentQueue(ICouchbaseCollection collection, string key, ILogger?logger, IRedactor?redactor)
     : base(collection, key, logger, redactor, new object(), false)
 {
 }
Ejemplo n.º 6
0
        public async Task <(ulong updatedCas, MutationToken mutationToken)> MutateStagedInsert(ICouchbaseCollection collection, string docId, object content, IAtrRepository atr, ulong?cas = null)
        {
            List <MutateInSpec> specs = CreateMutationSpecs(atr, "insert", content);
            var opts = GetMutateInOptions(StoreSemantics.Insert)
                       .AccessDeleted(true)
                       .CreateAsDeleted(true);

            if (cas.HasValue)
            {
                opts.Cas(cas.Value).StoreSemantics(StoreSemantics.Replace);
            }
            else
            {
                opts.Cas(0);
            }

            var mutateResult = await collection.MutateInAsync(docId, specs, opts).CAF();

            return(mutateResult.Cas, mutateResult.MutationToken);
        }
        public Task <(ulong updatedCas, MutationToken mutationToken)> MutateStagedInsert(ICouchbaseCollection collection, string docId, object content, IAtrRepository atr, ulong?cas = null)
        {
            var mockLookupInResult = new Mock <ILookupInResult>(MockBehavior.Strict);

            mockLookupInResult.SetupGet(l => l.IsDeleted).Returns(false);
            mockLookupInResult.SetupGet(l => l.Cas).Returns(5);

            Add(collection, docId, new DocumentLookupResult(docId, null, new JObjectContentWrapper(content), mockLookupInResult.Object, new Components.DocumentMetadata(), collection));
            Interlocked.Increment(ref _rollingCas);
            return(Task.FromResult(((ulong)_rollingCas, new MutationToken("fake", 1, 2, _rollingCas))));
        }
 public static Task <IGetReplicaResult> GetAnyReplicaAsync(this ICouchbaseCollection collection, string id)
 {
     return(collection.GetAnyReplicaAsync(id, GetAnyReplicaOptions.Default));
 }
 public static Task <IMutateInResult> MutateInAsync(this ICouchbaseCollection collection, string id,
                                                    IEnumerable <MutateInSpec> specs)
 {
     return(collection.MutateInAsync(id, specs, new MutateInOptions()));
 }
 public static Task <IGetResult> GetAndLockAsync(this ICouchbaseCollection collection, string id, TimeSpan expiry)
 {
     return(collection.GetAndLockAsync(id, expiry, new GetAndLockOptions()));
 }
 public static Task <ILookupInResult> LookupInAsync(this ICouchbaseCollection collection, string id,
                                                    IEnumerable <LookupInSpec> specs)
 {
     return(collection.LookupInAsync(id, specs, new LookupInOptions()));
 }
 public static Task TouchAsync(this ICouchbaseCollection collection, string id, TimeSpan expiry)
 {
     return(collection.TouchAsync(id, expiry, new TouchOptions()));
 }
 public static Task UnlockAsync <T>(this ICouchbaseCollection collection, string id, ulong cas)
 {
     return(collection.UnlockAsync <T>(id, cas, new UnlockOptions()));
 }
 public static Task RemoveAsync(this ICouchbaseCollection collection, string id)
 {
     return(collection.RemoveAsync(id, new RemoveOptions()));
 }
Ejemplo n.º 15
0
 public async Task UnstageRemove(ICouchbaseCollection collection, string docId, ulong cas = 0)
 {
     var opts = new RemoveOptions().Defaults(_durability, _keyValueTimeout).Cas(cas);
     await collection.RemoveAsync(docId, opts).CAF();
 }
 public static IPersistentQueue <T> Queue <T>(this ICouchbaseCollection collection, string docId)
 {
     return(new PersistentQueue <T>(collection, docId, (collection as CouchbaseCollection)?.Logger, (collection as CouchbaseCollection)?.Redactor));
 }
Ejemplo n.º 17
0
 public async Task <DocumentLookupResult> LookupDocumentAsync(ICouchbaseCollection collection, string docId, bool fullDocument = true) => await LookupDocumentAsync(collection, docId, _keyValueTimeout, fullDocument).CAF();
 public static IPersistentDictionary <TValue> Dictionary <TValue>(this ICouchbaseCollection collection, string docId)
 {
     return(new PersistentDictionary <TValue>(collection, docId, (collection as CouchbaseCollection)?.Logger, (collection as CouchbaseCollection)?.Redactor));
 }
 public void Add(ICouchbaseCollection collection, string docId, DocumentLookupResult doc) => Docs.Add(collection.GetKey(docId), doc);
 public static IEnumerable <Task <IGetReplicaResult> > GetAllReplicasAsync(this ICouchbaseCollection collection, string id)
 {
     return(collection.GetAllReplicasAsync(id, GetAllReplicasOptions.Default));
 }
        public async Task <(ulong updatedCas, MutationToken mutationToken)> UnstageInsertOrReplace(ICouchbaseCollection collection, string docId, ulong cas, object finalDoc, bool insertMode)
        {
            _ = await LookupDocumentAsync(collection, docId);

            Interlocked.Increment(ref _rollingCas);
            return((ulong)_rollingCas, new MutationToken("fake", 1, 2, _rollingCas));
        }
 public static Task <IExistsResult> ExistsAsync(this ICouchbaseCollection collection, string id)
 {
     return(collection.ExistsAsync(id, new ExistsOptions()));
 }
 /// <summary>
 /// Set <see cref="ICouchbaseCollection"/> to use for Active Transaction Record metadata.
 /// </summary>
 /// <param name="metadataCollection">The collection to use.</param>
 /// <returns>The builder.</returns>
 /// <remarks>If this is not set, then the metadata collection will be chosen based on the VBucket of the first document modification in the transaction.</remarks>
 public TransactionConfigBuilder MetadataCollection(ICouchbaseCollection metadataCollection)
 {
     _config.MetadataCollection = metadataCollection;
     return(this);
 }
 public static Task <IMutationResult> UpsertAsync <T>(this ICouchbaseCollection collection, string id, T content)
 {
     return(collection.UpsertAsync(id, content, new UpsertOptions()));
 }
        /// <summary>
        /// Executes a query and ingests the results as documents into Couchbase server for further analytics.
        /// <para>
        /// NOTE: This is an experimental API and may change in the future.
        /// </para>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cluster"></param>
        /// <param name="collection"></param>
        /// <param name="statement"></param>
        /// <param name="configureOptions"></param>
        /// <returns></returns>
        public static Task <IEnumerable <IMutationResult> > IngestAsync <T>(this ICluster cluster, string statement, ICouchbaseCollection collection, Action <IngestOptions> configureOptions)
        {
            var options = new IngestOptions();

            configureOptions(options);

            return(IngestAsync <T>(
                       cluster,
                       statement,
                       collection,
                       options
                       ));
        }
Ejemplo n.º 26
0
        static async Task Main(string[] args)
        {
            // SETUP: connect to Couchbase
            _cluster = await Cluster.ConnectAsync(
                "couchbase://localhost",
                "Administrator",
                "password");

            _bucket = await _cluster.BucketAsync("matt");

            _scope = await _bucket.ScopeAsync("myScope");

            _coll = await _scope.CollectionAsync("myCollection");

            // SETUP: create a 'conference' document and a 'conference activities' document
            await SetupInitialDocuments();

            // STEP 1: create transactions object
            var transactions = Transactions.Create(_cluster,
                                                   TransactionConfigBuilder.Create()
                                                   .DurabilityLevel(DurabilityLevel.MajorityAndPersistToActive) // since I have 1 node, replication must be 0, or this will throw exception
                                                   .Build());

            Console.WriteLine("Press ENTER to continue");
            Console.ReadLine();

            // STEP 2: transaction operations
            await transactions.RunAsync(async (ctx) =>
            {
                var now = DateTime.Now;

                // FIRST: get the two document I want to change
                var confDoc = await ctx.GetAsync(_coll, "dataLove2021");
                var actsDoc = await ctx.GetAsync(_coll, "dataLove2021::activities");
                var conf    = confDoc.ContentAs <Conference>();
                var acts    = actsDoc.ContentAs <ConferenceActivities>();

                // SECOND: add an event to the "activities" document
                acts.Events.Add(new ConferenceEvent
                {
                    Type       = "CFP",
                    DtActivity = now,
                    Desc       = "Submitted to the CFP"
                });
                // acts.Events.Add(new ConferenceEvent
                // {
                //     Type = "PRESENTATION",
                //     DtActivity = now,
                //     Desc = "Delivered ACID presentation"
                // });
                // acts.Events.Add(new ConferenceEvent
                // {
                //     Type = "SPATIAL",
                //     DtActivity = now,
                //     Desc = "Answered questions in Spatial Chat"
                // });

                // THIRD: change the "conference" document
                conf.Followups    = (conf.Followups ?? 0) + 1;
                conf.LastActivity = now;

                // FOURTH: write the changes
                await ctx.ReplaceAsync(confDoc, conf);

                // OPTIONAL STEP: fail right in the middle of the transaction making two writes
                // var fail = true;
                // if(fail) throw new Exception("Something went wrong!");

                await ctx.ReplaceAsync(actsDoc, acts);

                // FIFTH: commit (implied)
            });

            await _cluster.DisposeAsync();
        }
Ejemplo n.º 27
0
 private static Task Upsert(ICouchbaseCollection collection, KeyValuePair <string, object> document)
 {
     return(collection.UpsertAsync(document.Key, document.Value));
 }
Ejemplo n.º 28
0
        public async Task <(ulong updatedCas, MutationToken?mutationToken)> UnstageInsertOrReplace(ICouchbaseCollection collection, string docId, ulong cas, object finalDoc, bool insertMode)
        {
            if (insertMode)
            {
                var opts         = new InsertOptions().Defaults(_durability, _keyValueTimeout);
                var mutateResult = await collection.InsertAsync(docId, finalDoc, opts).CAF();

                return(mutateResult.Cas, mutateResult?.MutationToken);
            }
            else
            {
                var opts         = GetMutateInOptions(StoreSemantics.Replace).Cas(cas);
                var mutateResult = await collection.MutateInAsync(docId, specs =>
                                                                  specs.Upsert(TransactionFields.TransactionInterfacePrefixOnly, string.Empty,
                                                                               isXattr : true, createPath : true)
                                                                  .Remove(TransactionFields.TransactionInterfacePrefixOnly, isXattr : true)
                                                                  .SetDoc(finalDoc), opts).CAF();

                return(mutateResult.Cas, mutateResult?.MutationToken);
            }
        }
 /// <summary>
 /// Request a distributed Couchbase Mutex that expires after <paramref name="expiration"/>.
 /// </summary>
 /// <remarks>
 /// Uses the default holder from <see cref="LockHolder"/>.
 ///
 /// The <see cref="ICouchbaseMutex"/> should be disposed once the lock is no longer needed.
 /// </remarks>
 /// <param name="collection">Couchbase collection.</param>
 /// <param name="name">Name of the lock.</param>
 /// <param name="expiration">Time until mutex expires, if not renewed.</param>
 /// <param name="cancellationToken">Cancellation token.</param>
 /// <returns>The <see cref="ICouchbaseMutex"/>.</returns>
 /// <exception cref="CouchbaseLockUnavailableException">Thrown if the lock could not be acquired.</exception>
 /// <exception cref="CouchbaseException">Thrown on general Couchbase communication errors.</exception>
 /// <exception cref="ArgumentNullException">Thrown if bucket is null.</exception>
 /// <exception cref="ArgumentException">Thrown for invalid name.</exception>
 /// <exception cref="ArgumentOutOfRangeException">Thrown for non-positive expiration.</exception>
 public static Task <ICouchbaseMutex> RequestMutexAsync(this ICouchbaseCollection collection, string name, TimeSpan expiration,
                                                        CancellationToken cancellationToken = default)
 {
     return(collection.RequestMutexAsync(name, LockHolder.Default, expiration, cancellationToken));
 }
 public static Task <IMutationResult> ReplaceAsync <T>(this ICouchbaseCollection collection, string id, T content)
 {
     return(collection.ReplaceAsync(id, content, new ReplaceOptions()));
 }