Пример #1
0
 public CosmosRepositoryBase(string collectionId)
 {
     this.docCollectionId = collectionId;
     this.cosmosClient    = new DocumentClient(new Uri(EndpointUrl), PrimaryKey);
     this.database        = new Database()
     {
         Id = databaseId
     };
     this.database      = this.cosmosClient.CreateDatabaseIfNotExistsAsync(database).Result;
     this.docCollection = new DocumentCollection()
     {
         Id = docCollectionId
     };
     this.docCollection = this.cosmosClient.CreateDocumentCollectionIfNotExistsAsync("dbs/" + databaseId, docCollection).Result;
 }
Пример #2
0
        private static async Task CreateDatabaseIfNotExistsAsync()
        {
            #region Db

            if (_documentDatabase == null)
            {
                _documentDatabase = await _documentDbClient.CreateDatabaseAsync(new Db.Database
                {
                    Id = DatabaseStore
                });
            }

            #endregion

            #region Customer Collection

            if (_customerCollection == null)
            {
                // dbs/MyDatabaseId/colls/MyCollectionId/docs/MyDocumentId
                _customerCollection =
                    _documentDbClient.CreateDocumentCollectionQuery("dbs/" + _documentDatabase.Id)
                    .Where(c => c.Id == CustomerCollectionName)
                    .AsEnumerable()
                    .FirstOrDefault();

                // ReSharper disable once ConvertIfStatementToNullCoalescingExpression
                if (_customerCollection == null)
                {
                    _customerCollection =
                        await _documentDbClient.CreateDocumentCollectionAsync("dbs/" + _documentDatabase.Id,
                                                                              new Db.DocumentCollection
                    {
                        Id             = CustomerCollectionName,
                        IndexingPolicy =
                        {
                            Automatic    = true,
                            IndexingMode = Db.IndexingMode.Lazy
                        }
                    }, new Db.Client.RequestOptions
                    {
                        OfferType = "S1"
                    });
                }
            }

            #endregion
        }
Пример #3
0
        private static async Task InitializeRangeResolverAsync()
        {
            #region Partition Collection 1

            if (_partitionCollection1 == null)
            {
                // dbs/MyDatabaseId/colls/MyCollectionId/docs/MyDocumentId
                _partitionCollection1 =
                    _documentDbClient.CreateDocumentCollectionQuery("dbs/" + _documentDatabase.Id)
                    .Where(c => c.Id == PartitionCollectionName1)
                    .AsEnumerable()
                    .FirstOrDefault();

                // ReSharper disable once ConvertIfStatementToNullCoalescingExpression
                if (_partitionCollection1 == null)
                {
                    _partitionCollection1 =
                        await _documentDbClient.CreateDocumentCollectionAsync("dbs/" + _documentDatabase.Id,
                                                                              new Db.DocumentCollection
                    {
                        Id             = PartitionCollectionName1,
                        IndexingPolicy =
                        {
                            Automatic    = true,
                            IndexingMode = Db.IndexingMode.Lazy
                        }
                    }, new Db.Client.RequestOptions
                    {
                        OfferType = "S1"
                    });
                }
            }

            #endregion

            #region Partition Collection 2

            if (_partitionCollection2 == null)
            {
                // dbs/MyDatabaseId/colls/MyCollectionId/docs/MyDocumentId
                _partitionCollection2 =
                    _documentDbClient.CreateDocumentCollectionQuery("dbs/" + _documentDatabase.Id)
                    .Where(c => c.Id == PartitionCollectionName2)
                    .AsEnumerable()
                    .FirstOrDefault();

                // ReSharper disable once ConvertIfStatementToNullCoalescingExpression
                if (_partitionCollection2 == null)
                {
                    _partitionCollection2 =
                        await _documentDbClient.CreateDocumentCollectionAsync("dbs/" + _documentDatabase.Id,
                                                                              new Db.DocumentCollection
                    {
                        Id             = PartitionCollectionName2,
                        IndexingPolicy =
                        {
                            Automatic    = true,
                            IndexingMode = Db.IndexingMode.Lazy
                        }
                    }, new Db.Client.RequestOptions
                    {
                        OfferType = "S1"
                    });
                }
            }

            #endregion

            // Initialize a partition resolver that assigns users (A-M) -> collection1, and (N-Z) -> collection2
            // and register with DocumentClient.
            // Note: \uffff is the largest UTF8 value, so M\ufff includes all strings that start with M.
            _rangeResolver = new RangePartitionResolver <string>(
                "UserId",
                new Dictionary <Range <string>, string>()
            {
                { new Range <string>("A", "M\uffff"), _partitionCollection1.SelfLink },
                { new Range <string>("N", "Z\uffff"), _partitionCollection2.SelfLink },
            });

            _documentDbClient.PartitionResolvers[_documentDatabase.SelfLink] = _rangeResolver;
        }
Пример #4
0
        private static async Task InitializeRangeResolverAsync()
        {
            #region Partition Collection 1

            if (_partitionCollection1 == null)
            {
                // dbs/MyDatabaseId/colls/MyCollectionId/docs/MyDocumentId              
                _partitionCollection1 =
                    _documentDbClient.CreateDocumentCollectionQuery("dbs/" + _documentDatabase.Id)
                        .Where(c => c.Id == PartitionCollectionName1)
                        .AsEnumerable()
                        .FirstOrDefault();

                // ReSharper disable once ConvertIfStatementToNullCoalescingExpression
                if (_partitionCollection1 == null)
                {
                    _partitionCollection1 =
                        await _documentDbClient.CreateDocumentCollectionAsync("dbs/" + _documentDatabase.Id,
                            new Db.DocumentCollection
                            {
                                Id = PartitionCollectionName1,
                                IndexingPolicy =
                                {
                                    Automatic = true,
                                    IndexingMode = Db.IndexingMode.Lazy
                                }
                            }, new Db.Client.RequestOptions
                            {
                                OfferType = "S1"
                            });
                }
            }

            #endregion

            #region Partition Collection 2

            if (_partitionCollection2 == null)
            {
                // dbs/MyDatabaseId/colls/MyCollectionId/docs/MyDocumentId              
                _partitionCollection2 =
                    _documentDbClient.CreateDocumentCollectionQuery("dbs/" + _documentDatabase.Id)
                        .Where(c => c.Id == PartitionCollectionName2)
                        .AsEnumerable()
                        .FirstOrDefault();

                // ReSharper disable once ConvertIfStatementToNullCoalescingExpression
                if (_partitionCollection2 == null)
                {
                    _partitionCollection2 =
                        await _documentDbClient.CreateDocumentCollectionAsync("dbs/" + _documentDatabase.Id,
                            new Db.DocumentCollection
                            {
                                Id = PartitionCollectionName2,
                                IndexingPolicy =
                                {
                                    Automatic = true,
                                    IndexingMode = Db.IndexingMode.Lazy
                                }
                            }, new Db.Client.RequestOptions
                            {
                                OfferType = "S1"
                            });
                }
            }

            #endregion

            // Initialize a partition resolver that assigns users (A-M) -> collection1, and (N-Z) -> collection2
            // and register with DocumentClient. 
            // Note: \uffff is the largest UTF8 value, so M\ufff includes all strings that start with M.
            _rangeResolver = new RangePartitionResolver<string>(
                "UserId",
                new Dictionary<Range<string>, string>()
                {
                    { new Range<string>("A", "M\uffff"), _partitionCollection1.SelfLink },
                    { new Range<string>("N", "Z\uffff"), _partitionCollection2.SelfLink },
                });

            _documentDbClient.PartitionResolvers[_documentDatabase.SelfLink] = _rangeResolver;
        }
Пример #5
0
        private static async Task CreateDatabaseIfNotExistsAsync()
        {
            #region Db

            if (_documentDatabase == null)
            {
                _documentDatabase = await _documentDbClient.CreateDatabaseAsync(new Db.Database
                {
                    Id = DatabaseStore
                });
            }

            #endregion

            #region Customer Collection

            if (_customerCollection == null)
            {
                // dbs/MyDatabaseId/colls/MyCollectionId/docs/MyDocumentId              
                _customerCollection =
                    _documentDbClient.CreateDocumentCollectionQuery("dbs/" + _documentDatabase.Id)
                        .Where(c => c.Id == CustomerCollectionName)
                        .AsEnumerable()
                        .FirstOrDefault();

                // ReSharper disable once ConvertIfStatementToNullCoalescingExpression
                if (_customerCollection == null)
                {
                    _customerCollection =
                        await _documentDbClient.CreateDocumentCollectionAsync("dbs/" + _documentDatabase.Id,
                            new Db.DocumentCollection
                            {
                                Id = CustomerCollectionName,
                                IndexingPolicy =
                                {
                                    Automatic = true,
                                    IndexingMode = Db.IndexingMode.Lazy
                                }
                            }, new Db.Client.RequestOptions
                            {
                                OfferType = "S1"
                            });
                }
            }

            #endregion
        }