Exemplo n.º 1
1
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Usage: dmloader <log1> [log2] [log3]");
                Console.WriteLine("       dmloader -watch <directory>");
                return;
            }

            _debug = ConfigurationManager.AppSettings["Debug"] == "true";
            _cluster = new Cluster("couchbaseClients/couchbase");
            _bucket = _cluster.OpenBucket();

            if (args[0].StartsWith("-w"))
            {
                if (args.Length < 2 || !Directory.Exists(args[1]))
                {
                    Console.WriteLine("Please specify a valid directory.");
                    return;
                }

                GenerateViews().Wait();

                WatchDirectory(args[1]);
                Console.Read();
            }
            else
            {
                ImportFiles(args);
            }
        }
Exemplo n.º 2
0
        private void Connect()
        {
            var config = GetConnectionConfig();

            _cluster = new Cluster(config);
            _bucket = _cluster.OpenBucket();
        }
Exemplo n.º 3
0
        private static void Main(string[] args)
        {
            ClusterHelper.Initialize(new ClientConfiguration
            {
                Servers = new List<Uri>
                {
                    new Uri("http://localhost:8091/")
                }
            });
            _bucket = ClusterHelper.GetBucket("default");

            Task.Run(async () =>
            {
                var result = await InsertWithReplicateTo(new Post
                {
                    PostId = "p-0002",
                    Author = "Bingo Bailey",
                    Content = "Some nice content",
                    Published = DateTime.Now
                });

                Console.WriteLine(result);
            });

            Console.Read();
            ClusterHelper.Close();
        }
        /// <summary>
        /// Function to initialize the provider
        /// </summary>
        /// <param name="name">Name of the element in the configuration file</param>
        /// <param name="config">Configuration values for the provider from the Web.config file</param>
        public override void Initialize(
            string name,
            NameValueCollection config)
        {
            // Initialize the base class
            base.Initialize(name, config);

            // Create our Couchbase bucket instance
            _bucket = ProviderHelper.GetBucket(name, config);

            // By default use exclusive session access. But allow it to be overridden in the config file
            var exclusive = ProviderHelper.GetAndRemove(config, "exclusiveAccess", false) ?? "true";
            _exclusiveAccess = (string.Compare(exclusive, "true", StringComparison.OrdinalIgnoreCase) == 0);

            // Allow optional header and data prefixes to be used for this application
            var headerPrefix = ProviderHelper.GetAndRemove(config, "headerPrefix", false);
            if (headerPrefix != null) {
                _headerPrefix = headerPrefix;
            }
            var dataPrefix = ProviderHelper.GetAndRemove(config, "dataPrefix", false);
            if (dataPrefix != null) {
                _dataPrefix = dataPrefix;
            }

            // Make sure no extra attributes are included
            ProviderHelper.CheckForUnknownAttributes(config);
        }
Exemplo n.º 5
0
        static void ThreadPoolInsert(IBucket bucket, int n)
        {
            for (int i = 0; i < n; i++)
            {
                int i1 = i;
                Task.Factory.StartNew(() =>
                {
                    var key = "key" + i1;
                    var value = "value" + i1;

                    /*var result = bucket.Upsert(key, value);

                    if (result.Success)
                    {
                        Console.WriteLine("Write Key: {0} - Value: {1}", key, value);*/
                    var result2 = bucket.Get<string>(key);
                    if (result2.Success)
                    {
                        Console.WriteLine("Read Key: {0} - Value: {1}", key, result2.Value);
                    }
                    else
                    {
                        Console.WriteLine("Read Error: {0} - {1}", key, result2.Message);
                    }
                    /*}
                    else
                    {
                        Console.WriteLine("Write Error: {0} - {1}", key, result.Message);
                    }*/
                });
            }
        }
Exemplo n.º 6
0
        public ProtectedPackageStore(IBucket bucket, byte[] password, string prefix = null)
        {
            #region Preconditions

            if (bucket == null)
                throw new ArgumentNullException(nameof(bucket));

            if (password == null)
                throw new ArgumentNullException(nameof(password));

            #endregion

            this.bucket = bucket;
            this.password = password;

            if (prefix != null)
            {
                if (!prefix.EndsWith("/"))
                {
                    prefix = prefix + "/";
                }

                this.prefix = prefix;
            }
            else
            {
                this.prefix = "";
            }
        }
Exemplo n.º 7
0
        static void SynchronousInsert(IBucket bucket, int n)
        {
            for (int i = 0; i < n; i++)
            {
                var key = "key" + i;
                var value = "value" + i;

                var result = bucket.Upsert(key, value);
                if (result.Success)
                {
                    Console.WriteLine("Write Key: {0} - Value: {1}", key, value);
                    var result2 = bucket.Get<string>(key);
                    if (result2.Success)
                    {
                        Console.WriteLine("Read Key: {0} - Value: {1}", key, result2.Value);
                    }
                    else
                    {
                        Console.WriteLine("Read Error: {0} - {1}", key, result2.Message);
                    }
                }
                else
                {
                    Console.WriteLine("Write Error: {0} - {1}", key, result.Message);
                }
            }
        }
 public void Setup()
 {
     var config = Utils.TestConfiguration.GetCurrentConfiguration();
     config.BucketConfigs.First().Value.UseEnhancedDurability = false;
     _cluster = new Cluster(config);
     _bucket = _cluster.OpenBucket();
 }
 /// <summary>
 /// Loads a session state item from the bucket
 /// </summary>
 /// <param name="bucket">Couchbase bucket to load from</param>
 /// <param name="id">Session ID</param>
 /// <param name="metaOnly">True to load only meta data</param>
 /// <returns>Session store item read, null on failure</returns>
 public static SessionStateItem Load(
     IBucket bucket,
     string id,
     bool metaOnly)
 {
     return Load(CouchbaseSessionStateProvider.HeaderPrefix, CouchbaseSessionStateProvider.DataPrefix, bucket, id, metaOnly);
 }
Exemplo n.º 10
0
        public static async Task UpsertAllAsync(List<Person> persons, IBucket bucket)
        {
            var tasks = new List<Task<OperationResult<Person>>>();
            persons.ForEach(x => bucket.UpsertAsync(x.Id, x));

            var results = await Task.WhenAll(tasks).ConfigureAwait(false);
            Console.WriteLine("Total upserted: {0}", results.Length);
        }
Exemplo n.º 11
0
 static void Decrement(IBucket bucket, string key)
 {
     var result = bucket.Decrement(key);
     if (result.Success)
     {
         Console.WriteLine(result.Value);
     }
 }
Exemplo n.º 12
0
 private void Disconnect()
 {
     _cluster.CloseBucket(_bucket);
     _bucket.Dispose();
     _bucket = null;
     _cluster.Dispose();
     _cluster = null;
 }
        public CouchbaseEventStore(IBucket bucket, string couchbaseUser, string couchbasePwd)
        {
            if (_bucket == null)
                _bucket = bucket;
            if (bucketManager == null)
                bucketManager = _bucket.CreateManager(couchbaseUser, couchbasePwd);

        }
Exemplo n.º 14
0
 public BeerSample(IBucket bucket)
     : base(bucket)
 {
     //Two ways of applying a filter are included in this example.
     //This is by implementing IDocumentFilter and then adding explicitly.
     //adding it to the DocumentFilterManager
     DocumentFilterManager.SetFilter(new BreweryFilter());
 }
 public MemcachedBucketManager(IBucket bucket, ClientConfiguration clientConfig, IDataMapper mapper, string username, string password)
     : base(bucket,
           clientConfig,
           mapper,
           new CouchbaseHttpClient(username, password),
           username,
           password)
 {
 }
        /// <summary>
        /// Retrieves the session data from the data source. If the lockRecord parameter is true 
        /// (in the case of GetItemExclusive), then the record is locked and we return a new lockId 
        /// and lockAge.
        /// </summary>
        /// <param name="bucket">Reference to the couchbase bucket we are using</param>
        /// <param name="context">HttpContext for the current request</param>
        /// <param name="acquireLock">True to aquire the lock, false to not aquire it</param>
        /// <param name="id">Session ID for the session</param>
        /// <param name="locked">Returns true if the session item is locked, otherwise false</param>
        /// <param name="lockAge">Returns the amount of time that the item has been locked</param>
        /// <param name="lockId">Returns lock identifier for the current request</param>
        /// <param name="actions">Indicates whether the current sessions is an uninitialized, cookieless session</param>
        /// <returns>SessionStateItem object containing the session state data</returns>
        public static SessionStateItem GetSessionStoreItem(
            IBucket bucket,
            HttpContext context,
            bool acquireLock,
            string id,
            out bool locked,
            out TimeSpan lockAge,
            out object lockId,
            out SessionStateActions actions)
        {
            locked = false;
            lockId = null;
            lockAge = TimeSpan.Zero;
            actions = SessionStateActions.None;

            var e = SessionStateItem.Load(bucket, id, false);
            if (e == null)
                return null;

            if (acquireLock) {
                // repeat until we can update the retrieved
                // item (i.e. nobody changes it between the
                // time we get it from the store and updates it s attributes)
                // Save() will return false if Cas() fails
                while (true) {
                    if (e.LockId > 0)
                        break;

                    actions = e.Flag;

                    e.LockId = _exclusiveAccess ? e.HeadCas : 0;
                    e.LockTime = DateTime.UtcNow;
                    e.Flag = SessionStateActions.None;

                    // try to update the item in the store
                    if (e.SaveHeader(bucket, id, _exclusiveAccess))
                    {
                        locked = true;
                        lockId = e.LockId;

                        return e;
                    }

                    // it has been modified between we loaded and tried to save it
                    e = SessionStateItem.Load(bucket, id, false);
                    if (e == null)
                        return null;
                }
            }

            locked = true;
            lockAge = DateTime.UtcNow - e.LockTime;
            lockId = e.LockId;
            actions = SessionStateActions.None;

            return acquireLock ? null : e;
        }
        public QvCouchbaseEntityConnection(IBucket bucket, List<EntityModel> models)
            : base()
        {
            _bucket = bucket;
            _models = models;

            int.TryParse(ConfigurationManager.AppSettings["PageSize"], out PageSize);
            PageSize = PageSize > 0 ? PageSize : 1000;

        }
        public void Test_That_A_NMV_Response_Will_Force_A_Config_Update()
        {
            //TODO this test needs to be rewritten along with fake/mock
            _bucket = _cluster.OpenBucket("default");
            var operationResult = _bucket.Upsert("test", "value");

            //note that the client should be retrying the operation. Once that is in place, this 
            //test will need to be refactored.
            Assert.AreEqual(ResponseStatus.VBucketBelongsToAnotherServer, operationResult.Status);
        }
Exemplo n.º 19
0
 public SessionStore(IBucket bucket,
     IDataSerializer<AuthenticationTicket> ticketSerializer,
     ILookupNormalizer lookupNormalizer,
     IOptions<IdentityOptions> options)
 {
     _bucket = bucket;
     _timeout = options.Value.Cookies.ApplicationCookie.ExpireTimeSpan;
     _ticketSerializer = ticketSerializer;
     _lookupNormalizer = lookupNormalizer;
 }
Exemplo n.º 20
0
 internal BucketManager(IBucket bucket, ClientConfiguration clientConfig, IDataMapper mapper, HttpClient httpClient, string username, string password)
 {
     _bucket = bucket;
     BucketName = bucket.Name;
     _clientConfig = clientConfig;
     Mapper = mapper;
     _password = password;
     _username = username;
     _httpClient = httpClient;
 }
Exemplo n.º 21
0
        public static async Task UpsertAllAsync(List<Person> persons, IBucket bucket)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            var tasks = new List<Task<OperationResult<Person>>>();
            persons.ForEach(x => bucket.UpsertAsync(x.Id, x));

            var results = await Task.WhenAll(tasks).ConfigureAwait(false);
            Console.WriteLine("Total upserted: {0} in {1} seconds", results.Length, stopwatch.Elapsed.TotalSeconds);
        }
Exemplo n.º 22
0
        static void BasicQuery(IBucket bucket)
        {
            var query = bucket.CreateQuery("beer", "brewery_beers").
                Limit(5);

            var result = bucket.Query<dynamic>(query);
            foreach (var row in result.Rows)
            {
                Console.WriteLine(row);
            }
        }
Exemplo n.º 23
0
 internal BucketManager(IBucket bucket, ClientConfiguration clientConfig, HttpClient httpClient, IDataMapper mapper, string username, string password, ILogger logger)
 {
     Log = logger;
     _bucket = bucket;
     BucketName = bucket.Name;
     _clientConfig = clientConfig;
     Mapper = mapper;
     HttpClient = httpClient;
     _password = password;
     _username = username;
 }
Exemplo n.º 24
0
        public static void CreateBucketConnection()
        {
            var cluster = ClusterFactory.GetCluster();
            var cbConfig = new CouchbaseConfiguration();

            var password = cbConfig.GetPassword();
            if (!string.IsNullOrWhiteSpace(password))
                bucket = cluster.OpenBucket(cbConfig.GetBucket(), password);
            else
                bucket = cluster.OpenBucket(cbConfig.GetBucket());

            bucketManager = bucket.CreateManager("Administrator", "123456");
        }
        public void OneTimeSetUp()
        {
            _cluster = new Cluster(Utils.TestConfiguration.GetConfiguration("basic"));
            _clusterManager = _cluster.CreateManager(TestConfiguration.Settings.AdminUsername, TestConfiguration.Settings.AdminPassword);

            var createResult = _clusterManager.CreateBucket(BucketName, flushEnabled: true, bucketType:BucketTypeEnum.Memcached);
            Console.WriteLine(createResult.Success);

            // Allow time for bucket to be created and configuration to propagate before beginning operations
            Thread.Sleep(500);

            _bucket = _cluster.OpenBucket(BucketName);
            _bucketManager = _bucket.CreateManager(TestConfiguration.Settings.AdminUsername, TestConfiguration.Settings.AdminPassword);
        }
        public void OneTimeSetUp()
        {
            _cluster = new Cluster(Utils.TestConfiguration.GetConfiguration("basic"));
            _clusterManager = _cluster.CreateManager(TestConfiguration.Settings.AdminUsername, TestConfiguration.Settings.AdminPassword);

            var createResult = _clusterManager.CreateBucket(BucketName, replicaNumber:ReplicaNumber.Zero, flushEnabled: true);
            Assert.True(createResult.Success);

            // Allow time for bucket to be created and configuration to propagate before beginning operations
            Thread.Sleep(500);

            _bucket = _cluster.OpenBucket(BucketName);
            _bucketManager = _bucket.CreateManager(TestConfiguration.Settings.AdminUsername, TestConfiguration.Settings.AdminPassword);
        }
        public void OneTimeSetUp()
        {
            var mockBucket = new Mock<IBucket>();
            mockBucket.SetupGet(m => m.Name).Returns("test");
            _mockBucket = mockBucket.Object;

            _clientConfiguration = new ClientConfiguration()
            {
                BucketConfigs = new Dictionary<string, BucketConfiguration>()
                {
                    { "test", new BucketConfiguration() }
                }
            };
        }
Exemplo n.º 28
0
        public void EnsurePrimaryIndexExists(IBucket bucket)
        {
            var manager = GetBucketManager(bucket);

            var indexes = manager.ListN1qlIndexes();
            Assert.True(indexes.Success);

            if (indexes.All(p => !p.IsPrimary))
            {
                // We need to create the index

                var result = manager.CreateN1qlPrimaryIndex(false);
                Assert.True(result.Success);
            }
        }
Exemplo n.º 29
0
        public void EnsureIndexExists(IBucket bucket, string indexName, params string[] fields)
        {
            var manager = GetBucketManager(bucket);

            var indexes = manager.ListN1qlIndexes();
            Assert.True(indexes.Success);

            if (indexes.All(p => p.Name != indexName))
            {
                // We need to create the index

                var result = manager.CreateN1qlIndex(indexName, false, fields);
                Assert.True(result.Success);
            }
        }
        /// <summary>
        /// Loads a session state item from the bucket. This function is publicly accessible
        /// so that you have direct access to session data from another application if necesssary.
        /// We use this so our front end code can determine if an employee is logged into our back
        /// end application to give them special permissions, without the session data being actually common
        /// between the two applications.
        /// </summary>
        /// <param name="headerPrefix">Prefix for the header data</param>
        /// <param name="dataPrefix">Prefix for the real data</param>
        /// <param name="bucket">Couchbase bucket to load from</param>
        /// <param name="id">Session ID</param>
        /// <param name="metaOnly">True to load only meta data</param>
        /// <returns>Session store item read, null on failure</returns>
        public static SessionStateItem Load(
            string headerPrefix,
            string dataPrefix,
            IBucket bucket,
            string id,
            bool metaOnly)
        {
            // Read the header value from Couchbase
            var header = bucket.Get<byte[]>(CouchbaseSessionStateProvider.HeaderPrefix + id);
            if (header.Status != ResponseStatus.Success)
            {
                return null;
            }

            // Deserialize the header values
            SessionStateItem entry;
            using (var ms = new MemoryStream(header.Value))
            {
                entry = LoadHeader(ms);
            }
            entry.HeadCas = header.Cas;

            // Bail early if we are only loading the meta data
            if (metaOnly)
            {
                return entry;
            }

            // Read the data for the item from Couchbase
            var data = bucket.Get<byte[]>(CouchbaseSessionStateProvider.DataPrefix + id);
            if (data.Value == null)
            {
                return null;
            }
            entry.DataCas = data.Cas;

            // Deserialize the data
            using (var ms = new MemoryStream(data.Value))
            {
                using (var br = new BinaryReader(ms))
                {
                    entry.Data = SessionStateItemCollection.Deserialize(br);
                }
            }

            // Return the session entry
            return entry;
        }
Exemplo n.º 31
0
            /// <summary>
            /// Saves the session store data into Couchbase
            /// </summary>
            /// <param name="bucket">Couchbase bucket to save to</param>
            /// <param name="id">Session ID</param>
            /// <param name="useCas">True to use a check and set, false to simply store it</param>
            /// <returns>True if the value was saved, false if not</returns>
            public bool SaveData(
                IBucket bucket,
                string id,
                bool useCas)
            {
                var ts = TimeSpan.FromMinutes(Timeout);

                using (var ms = new MemoryStream())
                    using (var bw = new BinaryWriter(ms))
                    {
                        Data.Serialize(bw);

                        // Attempt to save the data and fail if the CAS fails
                        var retval = useCas
                        ? bucket.Upsert(_dataPrefix + id, ms.ToArray(), DataCas, ts)
                        : bucket.Upsert(_dataPrefix + id, ms.ToArray(), ts);

                        return(retval.Success);
                    }
            }
Exemplo n.º 32
0
        public static async Task <IEnumerable <TEntity> > QuerySlimAsync <TEntity>(this IBucket bucket,
                                                                                   Expression <Func <TEntity, bool> > expression, bool throwIfFailed = true) where TEntity : class, IBaseEntity
        {
            var result = await bucket.QueryAsync(expression);

            if (result.Success)
            {
                return(result.Rows);
            }

            if (throwIfFailed)
            {
                if (result.Exception != null)
                {
                    throw result.Exception;
                }
                throw new CouchBaseException(result);
            }
            return(new List <TEntity>());
        }
Exemplo n.º 33
0
        /// <summary>
        /// Saves the session store header into Couchbase
        /// </summary>
        /// <param name="bucket">Couchbase bucket to save to</param>
        /// <param name="id">Session ID</param>
        /// <param name="useCas">True to use a check and set, false to simply store it</param>
        /// <returns>True if the value was saved, false if not</returns>
        public bool SaveHeader(
            IBucket bucket,
            string id,
            bool useCas,
            out ResponseStatus status)
        {
            using (var ms = new MemoryStream())
            {
                WriteHeader(ms);
                var ts = TimeSpan.FromMinutes(Timeout);

                // Attempt to write the header and fail if the CAS fails
                var retval = useCas
                    ? bucket.Upsert(CouchbaseSessionStateProvider.HeaderPrefix + id, ms.ToArray(), HeadCas, ts)
                    : bucket.Upsert(CouchbaseSessionStateProvider.HeaderPrefix + id, ms.ToArray(), ts);

                status = retval.Status;
                return(retval.Success);
            }
        }
Exemplo n.º 34
0
        public static void MultiThreaded(int threadCount, int keys, IBucket bucket)
        {
            var threads = new Thread[threadCount];

            for (var i = 0; i < threadCount; i++)
            {
                var threadData = new ThreadData
                {
                    NumberOfKeysToCreate = keys,
                    Keys        = keys / threadCount,
                    Bucket      = bucket,
                    Part        = i,
                    ThreadCount = threadCount
                };

                threads[i] = new Thread(ThreadProc);
                threads[i].Start(threadData);
            }
            ResetEvent.WaitOne();
        }
Exemplo n.º 35
0
        private static void CRUD_POCO(Siaqodb siaqodb)
        {
            //register key convention, so we can work directly with POCO
            SiaqodbConfigurator.RegisterKeyConvention <Invoice>(a => a.Id);

            IBucket bucket = siaqodb.Documents["invoices"];

            Invoice invoice = BuildInvoice();

            //insert
            bucket.Store(invoice);

            Invoice invoiceLoaded = bucket.Load <Invoice>(invoice.Id);

            invoiceLoaded.InvoiceDate = invoiceLoaded.InvoiceDate.AddDays(-1);
            //update
            bucket.Store(invoiceLoaded);
            //delete
            bucket.Delete(invoiceLoaded.Id);
        }
Exemplo n.º 36
0
        public static async Task <IQueryResult <TEntity> > QueryAsync <TEntity>(this IBucket bucket,
                                                                                Expression <Func <TEntity, bool> > expression) where TEntity : class, IBaseEntity
        {
            string condition = $"type='{BaseEntity<TEntity>.DocumentType}'";
            var    request   = new QueryRequest();

            if (expression != null)
            {
                var translator = new CouchQueryTranslator();
                var command    = translator.Translate(expression);
                condition = $"{command.Command} AND {condition}";
                request.AddPositionalParameter(command.Parameters.Select(p => p.Value).ToArray());
            }

            request.Statement($"SELECT {bucket.Name}.* FROM {bucket.Name} WHERE {condition}");

            var query = await bucket.QueryAsync <TEntity>(request);

            return(query);
        }
Exemplo n.º 37
0
        private Task SetExpiryForBucketDocumentsAsync(IBucket bucket, DocumentList documentList, int?documentLimit, bool showDetails)
        {
            var runningTasks = new List <Task>();

            var processedCount = 0;

            foreach (var document in documentList.Rows)
            {
                runningTasks.Add(SetDocumentExpiryAsync(bucket, document.Id, documentList.Expiry, showDetails));
                processedCount++;

                if (documentLimit.HasValue && processedCount == documentLimit.Value)
                {
                    break;
                }
            }

            // Wait for any tasks still running to complete
            return(TaskHelper.WaitAllThrottledAsync(runningTasks, MaxActiveTasks));
        }
Exemplo n.º 38
0
        public Task ReBucket(IBucket <TValue, TCacheValue> source, TCacheValue value)
        {
            var usage = _policy.CheckValue(value);

            if (usage == 0)
            {
                return(source == _unused
                    ? value.Evict()
                    : _unused.Add(value));
            }

            if (usage > 0)
            {
                return(source == _used
                    ? value.Evict()
                    : _used.Add(value));
            }

            return(value.Evict());
        }
Exemplo n.º 39
0
        public IList <IClusterNode> Clear(IBucket bucket)
        {
            lock (_nodes)
            {
                var removed = new List <IClusterNode>(_nodes.Where(x => x.Owner == bucket));

                _nodes.RemoveWhere(x => x.Owner == bucket);
                foreach (var clusterNode in removed.Where(clusterNode => _lookupDictionary.TryRemove(clusterNode.EndPoint, out _)))
                {
                    clusterNode.Dispose();
                }

                foreach (var removedNode in removed)
                {
                    removedNode.KeyEndPointsChanged -= OnKeyEndPointsChanged;
                }

                return(removed);
            }
        }
Exemplo n.º 40
0
        private static Product GetFragmentProduct(IBucket bucket, string key, IEnumerable <string> paths)
        {
            var builder = bucket.LookupIn <dynamic>(key);

            foreach (var path in paths)
            {
                builder = builder.Get(path);
            }
            var doc    = builder.Execute();
            var result = new Product();


            result.Id               = doc.Content <string>("id");
            result.Type             = doc.Content <string>("type");
            result.ProductId        = doc.Content <int>("productId");
            result.MarketInfo       = new Dictionary <string, MarketInfo>();
            result.MarketInfo["fr"] = doc.Content <MarketInfo>("marketInfo.fr");

            return(result);
        }
Exemplo n.º 41
0
        public CouchbaseSagaRepository(
            ICluster cluster,
            ICouchbaseSagaRepositorySettings <TSaga> settings,
            ILog log)
        {
            this.cluster  = cluster;
            this.settings = settings;
            this.log      = log;

            this.cluster.Configuration.SerializationSettings   = this.settings.SerializationSettings;
            this.cluster.Configuration.DeserializationSettings = this.settings.DeserializationSettings;

            this.bucket = BucketFactory.CreateBucketAndDesignDocument(
                this.cluster,
                this.settings.BucketName,
                this.settings.ServerUsername,
                this.settings.ServerPassword,
                this.settings.DesignDocumentName,
                this.settings.DesignDocument);
        }
Exemplo n.º 42
0
        private IBucket ReadBucket(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            if (reader.GetCurrentJsonToken() != JsonToken.BeginObject)
            {
                return(null);
            }

            reader.ReadNext();             // {
            var property = reader.ReadPropertyNameSegmentRaw();

            IBucket item = null;

            if (BucketFields.TryGetValue(property, out var value))
            {
                switch (value)
                {
                case 0:
                    item = GetKeyedBucket(ref reader, formatterResolver);
                    break;

                case 1:
                case 2:
                    item = GetRangeBucket(ref reader, formatterResolver, null, property.Utf8String());
                    break;

                case 3:
                    item = GetDateHistogramBucket(ref reader, formatterResolver);
                    break;

                case 4:
                    item = GetFiltersBucket(ref reader, formatterResolver);
                    break;
                }
            }
            else
            {
                reader.ReadNextBlock();
            }

            return(item);
        }
        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);
        }
Exemplo n.º 44
0
        private static void DictionaryDemo(IBucket bucket)
        {
            //var dictionary = new Dictionary<string, PizzaEmployee>();
            var dictionary = new CouchbaseDictionary <string, PizzaEmployee>(bucket, "Dictionary_PizzaStaff");

            dictionary.Clear();

            dictionary.Add("Matt", new PizzaEmployee {
                Shift = "8-5", HourlyWage = 7.25M
            });
            dictionary.Add("Mary", new PizzaEmployee {
                Shift = "5-12", HourlyWage = 8.25M
            });

            foreach (var key in dictionary.Keys)
            {
                Console.WriteLine($"Employee '{key}' makes {dictionary[key].HourlyWage}/hour");
            }
            Console.WriteLine();

            dictionary.Add("Hiro", new PizzaEmployee {
                Shift = "5-12", HourlyWage = 10.25M
            });

            try
            {
                dictionary.Add("Matt", new PizzaEmployee {
                    Shift = "8-12", HourlyWage = 20.00M
                });
            }
            catch (ArgumentException)
            {
                Console.WriteLine("Can't add 'Matt' to the dictionary twice.");
                Console.WriteLine();
            }

            foreach (var key in dictionary.Keys)
            {
                Console.WriteLine($"Employee '{key}' makes {dictionary[key].HourlyWage}/hour");
            }
        }
Exemplo n.º 45
0
        private IBucket ReadBucket(JsonReader reader, JsonSerializer serializer)
        {
            if (reader.TokenType != JsonToken.StartObject)
            {
                return(null);
            }
            reader.Read();

            if (reader.TokenType != JsonToken.PropertyName)
            {
                return(null);
            }

            IBucket item = null;

            var property = reader.Value as string;

            switch (property)
            {
            case "key":
                item = GetKeyedBucket(reader, serializer);
                break;

            case "from":
            case "to":
                item = GetRangeBucket(reader, serializer);
                break;

            case "key_as_string":
                item = GetDateHistogramBucket(reader, serializer);
                break;

            case "doc_count":
                item = GetFiltersBucket(reader, serializer);
                break;

            default:
                return(null);
            }
            return(item);
        }
        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);
        }
Exemplo n.º 47
0
        public static void ErrorExample(IBucket bucket, string id)
        {
            var builder = bucket.LookupIn <dynamic>(id).
                          Get("type").
                          Get("somepaththatdoesntexist").
                          Get("owner");

            try
            {
                dynamic fragment = builder.Execute();
                Console.WriteLine("Generic error: {0}{1}Specific Error: {2}",
                                  fragment.Status, Environment.NewLine, fragment.OpStatus(1));

                Console.WriteLine("Generic error: {0}{1}Specific Error: {2}",
                                  fragment.Status, Environment.NewLine, fragment.OpStatus("somepaththatdoesntexist"));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Exemplo n.º 48
0
        internal string CreateN1QlQuery(IBucket bucket, Expression expression, Version clusterVersion,
                                        bool selectDocumentMetadata, out ScalarResultBehavior resultBehavior)
        {
            var queryModel = QueryParserHelper.CreateQueryParser().GetParsedQuery(expression);

            var queryGenerationContext = new N1QlQueryGenerationContext()
            {
                MemberNameResolver           = MemberNameResolver,
                MethodCallTranslatorProvider = new DefaultMethodCallTranslatorProvider(),
                Serializer             = new Core.Serialization.DefaultSerializer(),
                SelectDocumentMetadata = selectDocumentMetadata,
                ClusterVersion         = clusterVersion
            };

            var visitor = new N1QlQueryModelVisitor(queryGenerationContext);

            visitor.VisitQueryModel(queryModel);

            resultBehavior = visitor.ScalarResultBehavior;
            return(visitor.GetQuery());
        }
        protected ProductDocument Query(Expression <Func <ProductDocument, bool> > filter)
        {
            ProductDocument doc;

            do
            {
                using (IBucket bucket = _getBucket())
                {
                    try
                    {
                        doc = new BucketContext(bucket).Query <ProductDocument>().FirstOrDefault(filter);
                    }
                    catch (Exception)
                    {
                        doc = null;
                    }
                }
            }while (doc == null);

            return(doc);
        }
        /// <summary>
        /// Function to initialize the provider
        /// </summary>
        /// <param name="name">Name of the element in the configuration file</param>
        /// <param name="config">Configuration values for the provider from the Web.config file</param>
        public override void Initialize(
            string name,
            NameValueCollection config)
        {
            // Initialize the base class
            base.Initialize(name, config);

            // Create our Couchbase bucket instance
            _bucket = ProviderHelper.GetBucket(name, config);

            // Allow optional prefix to be used for this application
            var prefix = ProviderHelper.GetAndRemove(config, "prefix", false);

            if (prefix != null)
            {
                _prefix = prefix;
            }

            // Make sure no extra attributes are included
            ProviderHelper.CheckForUnknownAttributes(config);
        }
Exemplo n.º 51
0
        static Document <Person> InsertDocument(IBucket bucket)
        {
            var document = new Document <Person>
            {
                Id      = "P1",
                Content = new Person
                {
                    FirstName = "John",
                    LastName  = "Adams",
                    Age       = 21
                }
            };

            var result = bucket.Insert(document);

            if (result.Success)
            {
                Console.WriteLine("Inserted document '{0}'", document.Id);
            }
            return(document);
        }
        public void Connect()
        {
            var config = new ClientConfiguration
            {
                BucketConfigs = new Dictionary <string, BucketConfiguration> {
                    { "authenticated", new BucketConfiguration {
                          Password   = "******",
                          Username   = "******",
                          BucketName = "authenticated"
                      } }
                }
            };

            ClusterHelper.Initialize(new ClientConfiguration()
            {
                Servers = new List <Uri> {
                    new Uri(@"http://" + ConfigurationManager.AppSettings["DbIpAddress"] + ":8091")
                }
            });
            _bucket = ClusterHelper.GetBucket("default");
        }
Exemplo n.º 53
0
        /// <summary>
        /// Inserts or replaces the item at the specified index.
        /// </summary>
        /// <param name="bucket">The bucket on which to operate.</param>
        /// <param name="index">The index.</param>
        /// <param name="itemFactory">The item factory to create the item to set.</param>
        /// <param name="check">A predicate to decide if a particular item should be replaced.</param>
        /// <param name="isNew">if set to <c>true</c> the index was not previously used.</param>
        /// <returns>
        ///   <c>true</c> if the item or repalced inserted; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="System.ArgumentOutOfRangeException">index;index must be greater or equal to 0 and less than capacity</exception>
        /// <remarks>
        /// The operation will be attempted as long as check returns true - this operation may starve.
        /// </remarks>
        public static bool InsertOrUpdate <T>(this IBucket <T> bucket, int index, Func <T> itemFactory, Predicate <T> check, out bool isNew)
        {
            if (bucket == null)
            {
                throw new ArgumentNullException("bucket");
            }
            isNew = true;
            var factoryUsed = false;
            var created     = default(T);

            while (true)
            {
                if (isNew)
                {
                    T stored;
                    if (!factoryUsed)
                    {
                        created     = itemFactory.Invoke();
                        factoryUsed = true;
                    }
                    if (bucket.Insert(index, created, out stored))
                    {
                        return(true);
                    }
                    isNew = false;
                }
                else
                {
                    var result = itemFactory.Invoke();
                    if (bucket.Update(index, _ => result, check, out isNew))
                    {
                        return(true);
                    }
                    if (!isNew)
                    {
                        return(false); // returns false only when check returns false
                    }
                }
            }
        }
Exemplo n.º 54
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);
                }
            }
        }
Exemplo n.º 55
0
        public static bool TryGetOrInsert <T>(this IBucket <T> bucket, int index, Func <T> itemFactory, out T stored)
        {
            if (bucket == null)
            {
                throw new ArgumentNullException(nameof(bucket));
            }

            if (bucket.TryGet(index, out stored))
            {
                return(false);
            }

            var created = itemFactory.Invoke();

            if (!bucket.Insert(index, created, out stored))
            {
                return(false);
            }

            stored = created;
            return(true);
        }
Exemplo n.º 56
0
        public static int RemoveWhere <T>(this IBucket <T> bucket, Predicate <T> check)
        {
            if (bucket == null)
            {
                throw new ArgumentNullException(nameof(bucket));
            }
            if (check == null)
            {
                throw new ArgumentNullException(nameof(check));
            }
            var matches = bucket.WhereIndexed(check);
            var count   = 0;

            foreach (var pair in matches)
            {
                if (bucket.RemoveAt(pair.Key))
                {
                    count++;
                }
            }
            return(count);
        }
        protected override Task <WorkloadOperationResult> OnExecuteStep(IBucket bucket, int workloadIndex, int docIndex, Func <TimeSpan> getTiming)
        {
            var key = DocKeyGenerator.Generate(workloadIndex, docIndex);

            if (UseSync)
            {
                var result = Randomizer.NextDouble() <= _mutationPercentage
                    ? bucket.Upsert(key, SampleDocument)
                    : bucket.Get <string>(key);

                return(Task.FromResult(
                           new WorkloadOperationResult(result.Success, result.Message, getTiming())
                           ));
            }

            return((Randomizer.NextDouble() <= _mutationPercentage
                    ? bucket.UpsertAsync(key, SampleDocument)
                    : bucket.GetAsync <string>(key))
                   .ContinueWith(
                       task => new WorkloadOperationResult(task.Result.Success, task.Result.Message, getTiming())
                       ));
        }
Exemplo n.º 58
0
        private static void Main(string[] args)
        {
            ClusterHelper.Initialize(new ClientConfiguration
            {
                Servers = new List <Uri>
                {
                    new Uri("http://192.168.77.101:8091/")
                }
            });
            _bucket = ClusterHelper.GetBucket("default");

            Task.Run(async() =>
            {
                var tasks   = await GetTasks(1000);
                var results = await BulkUpsertAsync(tasks);
                WriteResults(results);
                Console.WriteLine();
            });

            Console.Read();
            ClusterHelper.Close();
        }
Exemplo n.º 59
0
        public static IObservable <IDocument <T> > GetDocumentObservable <T>(this IBucket bucket, IList <string> ids)
        {
            if (bucket == null)
            {
                throw new ArgumentNullException("bucket");
            }

            // Create an IObservable to get the IOperationResult for all keys
            IObservable <KeyValuePair <string, IDocumentResult <T> > > operationObserver =
                new MultiGetObservable <string, KeyValuePair <string, IDocumentResult <T> > >(
                    ids, id => new KeyValuePair <string, IDocumentResult <T> >(id, bucket.GetDocument <T>(id)));

            // Filter out KeyNotFound results
            operationObserver = operationObserver.Where(p => p.Value.Status != ResponseStatus.KeyNotFound);

            return(operationObserver.Select(p =>
            {
                CheckResultForError(p.Value);

                return p.Value.Document;
            }));
        }
Exemplo n.º 60
0
        public void Test_Dispose_On_Many_Threads()
        {
            using (var cluster = new Cluster())
            {
                Random random  = new Random(100);
                int    n       = 100;
                var    options = new ParallelOptions {
                    MaxDegreeOfParallelism = 4
                };
                Parallel.For(0, n, options, i =>
                {
                    try
                    {
                        using (IBucket bucket = cluster.OpenBucket())
                        {
                            var key = "key_" + i;
                            var set = bucket.Upsert(key, i);
                            Console.WriteLine("Inserted {0}: {1} Thread: {2}", key, set.Success,
                                              Thread.CurrentThread.ManagedThreadId);
                            var get = bucket.Get <int>(key);
                            Console.WriteLine("Getting {0} - {1}: {2} Thread: {3}", key, get.Value, get.Success,
                                              Thread.CurrentThread.ManagedThreadId);

                            var sleep = random.Next(0, 100);
                            Console.WriteLine("Sleep for {0}ms", sleep);
                            Thread.Sleep(sleep);
                        }
                    }
                    catch (AggregateException ae)
                    {
                        ae.Flatten().Handle(e =>
                        {
                            Console.WriteLine(e);
                            return(true);
                        });
                    }
                });
            }
        }