Ejemplo n.º 1
0
        public DatabaseNode(DocumentClient localclient, Database db)
        {
            Text = db.Id;
            Tag = db;
            _client = localclient;
            ImageKey = "SystemFeed";
            SelectedImageKey = "SystemFeed";

            Nodes.Add(new UsersNode(_client));

            MenuItem myMenuItem3 = new MenuItem("Read Database");
            myMenuItem3.Click += new EventHandler(myMenuItemReadDatabase_Click);
            _contextMenu.MenuItems.Add(myMenuItem3);

            MenuItem myMenuItem = new MenuItem("Delete Database");
            myMenuItem.Click += new EventHandler(myMenuItemDeleteDatabase_Click);
            _contextMenu.MenuItems.Add(myMenuItem);

            _contextMenu.MenuItems.Add("-");

            MenuItem myMenuItem2 = new MenuItem("Create DocumentCollection");
            myMenuItem2.Click += new EventHandler(myMenuItemAddDocumentCollection_Click);
            _contextMenu.MenuItems.Add(myMenuItem2);
            MenuItem myMenuItem4 = new MenuItem("Refresh DocumentCollections Feed");
            myMenuItem4.Click += new EventHandler((sender, e) => Refresh(true));
            _contextMenu.MenuItems.Add(myMenuItem4);
        }
Ejemplo n.º 2
0
 static DocumentCollection FindDocumentCollection(DocumentClient client, Database database, string name)
 {
     return client.CreateDocumentCollectionQuery(database.SelfLink)
         .Where(x => x.Id == name)
         .ToList()
         .SingleOrDefault();
 }
        /// <summary>
        /// Check to verify a database with the id=FamilyRegistry does not exist
        /// If the database does not exist, create a new database
        /// </summary>
        /// <returns>Database object</returns>
        private static async Task<Database> CreateDatabaseAsync()
        {
            Database database = client
                .CreateDatabaseQuery()
                .Where(db => db.Id == "FamilyRegistry")
                .AsEnumerable()
                .FirstOrDefault();

            if (database == null)
            {
                database = await client.CreateDatabaseAsync(
                    new Database
                    {
                        Id = "FamilyRegistry"
                    });

                // Write the new database's id to the console
                Console.WriteLine("{0} created!", database.Id);
                Console.WriteLine("Press any key to continue ...");
                Console.ReadKey();
                Console.Clear();
            }

            return database;
        }
        /// <summary>
        /// Initialize a HashPartitionResolver.
        /// </summary>
        /// <param name="partitionKeyPropertyName">The property name to be used as the partition Key.</param>
        /// <param name="client">The DocumentDB client instance to use.</param>
        /// <param name="database">The database to run the samples on.</param>
        /// <param name="collectionNames">The names of collections used.</param>
        /// <returns>The created HashPartitionResolver.</returns>
        public static async Task<HashPartitionResolver> InitializeHashResolver(string partitionKeyPropertyName, DocumentClient client, Database database, string[] collectionNames)
        {
            // Set local to input.
            string[] CollectionNames = collectionNames;
            int numCollectionNames = CollectionNames.Length;

            // Create array of DocumentCollections.
            DocumentCollection[] collections = new DocumentCollection[numCollectionNames];

            // Create string array of Self Links to Collections.
            string[] selfLinks = new string[numCollectionNames];

            //Create some collections to partition data.
            for (int i = 0; i < numCollectionNames; i++)
            {
                collections[i] = await DocumentClientHelper.GetCollectionAsync(client, database, CollectionNames[i]);
                selfLinks[i] = collections[i].SelfLink;
            }

            // Join Self Link Array into a comma separated string.
            string selfLinkString = String.Join(", ", selfLinks);

            //Initialize a partition resolver that users hashing, and register with DocumentClient. 
            //Uses User Id for PartitionKeyPropertyName, could also be TenantId, or any other variable.
            HashPartitionResolver hashResolver = new HashPartitionResolver(partitionKeyPropertyName, new[] { selfLinkString });
            client.PartitionResolvers[database.SelfLink] = hashResolver;

            return hashResolver;
        }
        //new add
        private static async Task GetStart()
        {
            var client = new DocumentClient(new Uri(EndpointURL), AuthorizationKey);
            
            database = client.CreateDatabaseQuery().Where(d => d.Id == "ppweict").AsEnumerable().FirstOrDefault();
            collection = client.CreateDocumentCollectionQuery(database.SelfLink).Where(c => c.Id == "Exam_Pool").AsEnumerable().FirstOrDefault();


            var EfDs = client.CreateDocumentQuery(collection.DocumentsLink, "SELECT * FROM Exam_pool f WHERE f.verify = \"true\" ");
            foreach (exam_pool EfD in EfDs)
            {
                Console.WriteLine(EfD);
                exams.Add(new exam_pool
                {
                    id = EfD.id,
                    exam_catrgory = EfD.exam_catrgory,
                    exam_level = EfD.exam_level,
                    questions = EfD.questions,
                    answer_A = EfD.answer_A,
                    answer_B = EfD.answer_B,
                    answer_C = EfD.answer_C,
                    answer_D = EfD.answer_D,
                    answer_E = EfD.answer_E,
                    C_answer = EfD.C_answer,
                    exam_link = EfD.exam_link,
                    lang = EfD.lang,
                    verify = EfD.verify,
                });
            }
        }
Ejemplo n.º 6
0
 public static void Main(string[] args)
 {
     try
     {
         //Instantiate a new DocumentClient instance
         using (client = new DocumentClient(new Uri(endpointUrl), authorizationKey, connectionPolicy))
         {
             //Get, or Create, a reference to Database
             database = GetOrCreateDatabaseAsync(databaseId).Result;
             
             //Do operations on Collections
             RunCollectionDemo().Wait();
         }
     }            
     catch (DocumentClientException de)
     {
         Exception baseException = de.GetBaseException();
         Console.WriteLine("{0} error occurred: {1}, Message: {2}", de.StatusCode, de.Message, baseException.Message);
     }
     catch (Exception e)
     {
         Exception baseException = e.GetBaseException();
         Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message);
     }
     finally
     {
         Console.WriteLine("End of demo, press any key to exit.");
         Console.ReadKey();
     }
 }
 private static DocumentCollection GetDocumentCollection(DocumentClient client, Database database, string collectionId)
 {
     return client.CreateDocumentCollectionQuery(database.SelfLink)
         .Where(c => c.Id == collectionId)
         .AsEnumerable()
         .SingleOrDefault();
 }
        public DocumentDBDataReader()
        {
            var dict = new Dictionary<HighchartsHelper.DocumentTypes, IEnumerable<Document>>();
            _documentClient = new DocumentClient(new Uri(ConfigurationManager.AppSettings["DocumentServiceEndpoint"]), ConfigurationManager.AppSettings["DocumentKey"]);

            _database = _documentClient.CreateDatabaseQuery().Where(db => db.Id == ConfigurationManager.AppSettings["DocumentDatabase"]).AsEnumerable().FirstOrDefault();

            if (_database == null)
                throw new ApplicationException("Error: DocumentDB database does not exist");

            // Check to verify a document collection with the id=FamilyCollection does not exist
            _documentCollection = _documentClient.CreateDocumentCollectionQuery(_database.CollectionsLink).Where(c => c.Id == ConfigurationManager.AppSettings["DocumentCollection"]).AsEnumerable().FirstOrDefault();

            if (_documentCollection == null)
                throw new ApplicationException("Error: DocumentDB collection does not exist");


            try
            {
                _documentClient.CreateUserDefinedFunctionAsync(_documentCollection.SelfLink, new UserDefinedFunction
                {
                    Id = "ISDEFINED",
                    Body = @"function ISDEFINED(doc, prop) {
                            return doc[prop] !== undefined;
                        }"
                });  
            }
            catch (Exception)
            {
                //fail silently for now..
            }
        }
        private static async Task<DocumentCollection> CreateDocumentCollection(DocumentClient client, Database database, string collectionId)
        {
            var collectionSpec = new DocumentCollection { Id = collectionId };
            var requestOptions = new RequestOptions { OfferType = "S1" };

            return await client.CreateDocumentCollectionAsync(database.SelfLink, collectionSpec, requestOptions);
        }
Ejemplo n.º 10
0
 public static async Task<HttpStatusCode> CreateCollection(DocumentClient client, Database database, string collectionName)
 {
     var result = await client.CreateDocumentCollectionAsync(database.CollectionsLink, new DocumentCollection
     {
         Id = collectionName
     });
     return result.StatusCode;
 }
Ejemplo n.º 11
0
 public Dichotomy(string endpointUrl, string authorizationKey)
 {
     _client = new DocumentClient(new Uri(endpointUrl), authorizationKey);
     _database =
         _client.CreateDatabaseQuery().Where(db => db.Id == CloudConfigurationManager.GetSetting("Database"))
             .AsEnumerable().FirstOrDefault();
     _iDbService = new DbService();
 }
 // create or return a collection on a database
 public static async Task<DocumentCollection> GetCollection(Database database, string collName)
 {
     if (client.CreateDocumentCollectionQuery(database.SelfLink).Where(coll => coll.Id == collName).ToArray().Any())
     {
         return client.CreateDocumentCollectionQuery(database.SelfLink).Where(coll => coll.Id == collName).ToArray().FirstOrDefault();
     }
     return await client.CreateDocumentCollectionAsync(database.SelfLink, new DocumentCollection { Id = collName });
 }
Ejemplo n.º 13
0
 private async Task<Database> InitializeAsync(string databaseName)
 {
     if(this._database == null)
     {
         this._database = await this._dbClient.Initialize(databaseName);
     }
     return this._database;
 }
Ejemplo n.º 14
0
 public Dichotomy(string endpointUrl, string authorizationKey)
 {
     _client = new DocumentClient(new Uri(endpointUrl), authorizationKey);
     _database =
         _client.CreateDatabaseQuery().Where(db => db.SelfLink == ConfigurationManager.AppSettings["DBSelfLink"])
             .AsEnumerable().FirstOrDefault();
     _iDbService = new DbService();
 }
 public async Task<Database> Initialize()
 {
     if(this._database == null)
     {
         this._database = await this._dbClient.Initialize(this._databasename);
     }
     return this._database;
 }
 private static DocumentCollection GetDocumentCollection(DocumentClient client, Database database)
 {
     var documentCollection = client
         .CreateDocumentCollectionQuery(database.CollectionsLink)
         .Where(c => c.Id == "Documents")
         .AsEnumerable()
         .FirstOrDefault();
     return documentCollection;
 }
Ejemplo n.º 17
0
        protected void Setup()
        {
            _client = CreateClient();
            _database = CreateDatabase();
            var collection = CreateCollection(_database.SelfLink);

            AddDocuments(collection.SelfLink);
            DbSet = new DbSet<Widget>(_client, _database.ResourceId, collection.ResourceId);
        }
Ejemplo n.º 18
0
        private static async Task ReadOrCreateDatabase()
        {
            _database = Client.CreateDatabaseQuery().Where(o => o.Id == _databaseId).AsEnumerable().FirstOrDefault();

            if (_database == null)
            {
                _database = await Client.CreateDatabaseAsync(new Database {Id = _databaseId});
            }
        }
Ejemplo n.º 19
0
        public async Task<Database> GetDatabase()
        {
            if (_database == null)
            {
                _database = await GetDatabase(_config["DocumentDatabase"]);
            }

            return _database;
        }
Ejemplo n.º 20
0
        private async void ChangeCosmosRu(string databaseAccount, string primaryKey, string databasename, Collection coll, int pkr, double averageRu)
        {
            try
            {
                var endPointUrl    = $"https://{databaseAccount}.documents.azure.cn:443";
                var documentClient = new DocumentClient(new Uri(endPointUrl), primaryKey);
                Microsoft.Azure.Documents.Database database = await GetOrCreateDatabaseAsync(documentClient,
                                                                                             databasename);

                DocumentCollection collection = await GetOrCreateCollectionAsync(documentClient, database.SelfLink,
                                                                                 coll.Name);

                Offer offer = documentClient.CreateOfferQuery()
                              .Where(r => r.ResourceLink == collection.SelfLink)
                              .AsEnumerable()
                              .SingleOrDefault();
                int offerThroughput   = JsonConvert.DeserializeObject <dynamic>(offer.ToString()).content.offerThroughput; //当前设置的RU
                var currentThroughput = pkr * averageRu;                                                                   //当前实时RU
                var ruRate            = Math.Round(currentThroughput / offerThroughput, 2);                                //计算实时RU和设置的RU占
                if ((ruRate - coll.IncreaseRate >= coll.ThresholdRate || ruRate + coll.IncreaseRate < coll.ThresholdRate) && currentThroughput > 1000)
                {
                    var newOfferThroughput = Convert.ToInt32(currentThroughput / coll.ThresholdRate); //保证RU为阈值左右
                    var increaseValue      = Math.Abs(newOfferThroughput - currentThroughput);        //计算调整RU的差值
                    if (increaseValue > 100)
                    {
                        newOfferThroughput = newOfferThroughput - newOfferThroughput % 100 + 100;
                        if (newOfferThroughput < coll.DefaultValue)
                        {
                            newOfferThroughput = coll.DefaultValue;
                        }
                        offer = new OfferV2(offer, newOfferThroughput);
                        Console.WriteLine(databasename + "-" + coll.Name + ",当前设置" + offerThroughput + "RU" + ",当前消耗" +
                                          currentThroughput + "RU" + ",调整设置为" + newOfferThroughput + "RU");
                        await documentClient.ReplaceOfferAsync(offer);
                    }
                    else
                    {
                        Console.WriteLine(databasename + "-" + coll.Name + ",当前设置" + offerThroughput + "RU" + ",当前消耗" +
                                          currentThroughput + "RU" + ",无需调整设置,调整幅度最小为100RU");
                    }
                }
                else
                {
                    Console.WriteLine(databasename + "-" + coll.Name + ",当前设置" + offerThroughput + "RU" + ",当前消耗" +
                                      currentThroughput + "RU" + ",无需调整设置");
                }
            }
            catch (Exception ex)
            {
                string sErrorMsg = ex.Message;
                if (ex.InnerException != null)
                {
                    sErrorMsg += ex.InnerException.Message;
                }
                Console.WriteLine("ChangeCosmosRU-" + sErrorMsg);
            }
        }
Ejemplo n.º 21
0
        //SELECT C.id AS CourseId, C.Name AS CourseName
        //from CourseCollection AS C
        //Join Session IN C.Sessions
        //WHERE Session.Name = "Introduction"

        #region Helpers
        private static async Task<DocumentCollection> GetDocumentCollection(DocumentClient client, Database database)
        {
            DocumentCollection documentCollection = client.CreateDocumentCollectionQuery(database.CollectionsLink).Where(c => c.Id == "CourseCollection").AsEnumerable().FirstOrDefault();
            if (documentCollection == null)
            {
                documentCollection = await client.CreateDocumentCollectionAsync(database.CollectionsLink, new DocumentCollection() { Id = "CourseCollection" });
            }
            return documentCollection;
        }
		private async Task<DocumentCollection> CreateCollectionAsync(Microsoft.Azure.Documents.Database db)
		{
			var containerId = RandomName();
			var containerResponse = await _client.CreateDocumentCollectionAsync(
				db.AltLink,
				new DocumentCollection {
					Id = containerId,
					PartitionKey = new PartitionKeyDefinition{ Paths = new Collection<string>{ "/PartitionKey" } } });
			return containerResponse.Resource;
		}
Ejemplo n.º 23
0
        public DocumentClient Initialise()
        {
            var configuration = (Configuration)ConfigurationManager.GetSection("DocumentDbProvider");
            _documentClient = new DocumentClient(new Uri(configuration.Endpoint), configuration.Key);
            Database = _documentClient.CreateDatabaseQuery()
                .Where(d => d.Id == _databaseName).AsEnumerable().FirstOrDefault()
                ?? _documentClient.CreateDatabaseAsync(new Database { Id = _databaseName }).Result;

            return _documentClient;
        }
        private async Task CreateDatabaseIfNotExistsAsync(string databaseName)
        {
            _database = (await _client.ReadDatabaseFeedAsync())
                .SingleOrDefault(d => d.Id == databaseName);

            if (_database == null)
                _database = await _client.CreateDatabaseAsync(new Database
                {
                    Id = databaseName
                });
        }
Ejemplo n.º 25
0
        static void Main(string[] args)
        {
            try
            {
                _documentDbClient = new Db.Client.DocumentClient(
                    new Uri(ConfigurationManager.AppSettings["DocumentDbUri"]),
                    ConfigurationManager.AppSettings["DocumentDbKey"]);

                _documentDatabase = _documentDbClient.CreateDatabaseQuery()
                                    .Where(db => db.Id == DatabaseStore).AsEnumerable().FirstOrDefault();

                //Create database and collection if not present
                CreateDatabaseIfNotExistsAsync().Wait();

                //Create Seed Data
                CreateDocumentsAsync(_customerCollection.SelfLink).Wait();

                //Query inserted customers
                QueryAllDocuments(_customerCollection.SelfLink);

                //Query subdocuments(orders)
                QueryWithSubdocuments(_customerCollection.SelfLink);

                //Query all customer first and last name with order number with amount greate than $300.00
                QueryWithTwoJoinsAndFilter(_customerCollection.SelfLink);

                //Create partitions
                InitializeRangeResolverAsync().Wait();

                //Run queries agianst different partitions and see where the documents are stored
                QueryPartitionAsync().Wait();

                //Update and delete the document
                UpdateAndDeleteDocumentAsync().Wait();
            }
            catch (Db.DocumentClientException de)
            {
                var baseException = de.GetBaseException();
                Console.WriteLine("{0} error occurred: {1}, Message: {2}", de.StatusCode, de.Message,
                                  baseException.Message);
            }
            catch (Exception e)
            {
                var baseException = e.GetBaseException();
                Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message);
            }
            finally
            {
                //Cleanup
                CleanUpAsync().Wait();
                Console.WriteLine("End of demo, press any key to exit.");
                Console.ReadKey();
            }
        }
Ejemplo n.º 26
0
        static void Main(string[] args)
        {
            try
            {
                _documentDbClient = new Db.Client.DocumentClient(
                    new Uri(ConfigurationManager.AppSettings["DocumentDbUri"]),
                    ConfigurationManager.AppSettings["DocumentDbKey"]);

                _documentDatabase = _documentDbClient.CreateDatabaseQuery()
                    .Where(db => db.Id == DatabaseStore).AsEnumerable().FirstOrDefault();

                //Create database and collection if not present
                CreateDatabaseIfNotExistsAsync().Wait();

                //Create Seed Data
                CreateDocumentsAsync(_customerCollection.SelfLink).Wait();

                //Query inserted customers
                QueryAllDocuments(_customerCollection.SelfLink);

                //Query subdocuments(orders)
                QueryWithSubdocuments(_customerCollection.SelfLink);

                //Query all customer first and last name with order number with amount greate than $300.00
                QueryWithTwoJoinsAndFilter(_customerCollection.SelfLink);

                //Create partitions
                InitializeRangeResolverAsync().Wait();

                //Run queries agianst different partitions and see where the documents are stored
                QueryPartitionAsync().Wait();

                //Update and delete the document
                UpdateAndDeleteDocumentAsync().Wait();
            }
            catch (Db.DocumentClientException de)
            {
                var baseException = de.GetBaseException();
                Console.WriteLine("{0} error occurred: {1}, Message: {2}", de.StatusCode, de.Message,
                    baseException.Message);
            }
            catch (Exception e)
            {
                var baseException = e.GetBaseException();
                Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message);
            }
            finally
            {
                //Cleanup
                CleanUpAsync().Wait();
                Console.WriteLine("End of demo, press any key to exit.");
                Console.ReadKey();
            }
        }
		/// <summary>
		///     Constructor
		/// </summary>
		/// <param name="documentClient"></param>
		/// <param name="databaseId"></param>
		/// <param name="createDatabaseIfNonexistent"></param>
		public CollectionManager( DocumentClient documentClient ,
								  string databaseId ,
								  bool createDatabaseIfNonexistent = false ) {
			DocumentClient = documentClient;
			DatabaseId = databaseId;

			Database =
				DocumentClient.CreateDatabaseQuery().Where( item => item.Id == databaseId ).AsEnumerable().FirstOrDefault();

			Initialize( createDatabaseIfNonexistent ).Wait();
		}
Ejemplo n.º 28
0
        private async Task InitClient()
        {
            _client = new DocumentClient(new Uri(Uri), Key);
            _database = await _client.GetDatabase("reportdb");
            _documentCollection = await _client.GetCollection(_database, "reports");

            if (HasReports() == false)
            {
                await CreateDummyReportsAsync();
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DocumentClientHashPartitioningManager"/> class.
 /// </summary>
 /// <param name="partitionKeyExtractor">The partition key extractor function.</param>
 /// <param name="client">The DocumentDB client instance.</param>
 /// <param name="database">The database to partition.</param>
 /// <param name="initialPartitionCount">The number of initial partitions to create.</param>
 /// <param name="readMode">The mode to process requests in during data migrations.</param>
 public DocumentClientHashPartitioningManager(
     Func<object, string> partitionKeyExtractor,
     DocumentClient client,
     Database database,
     int initialPartitionCount,
     TransitionReadMode readMode = TransitionReadMode.ReadBoth)
 {
     this.Client = client;
     this.Database = database;
     this.Client.PartitionResolvers[database.SelfLink] = new ManagedHashPartitionResolver(partitionKeyExtractor, client, database, initialPartitionCount);
     this.ReadMode = readMode;
 }
        public SpeakerAzureDocDbRepository(Settings settings)
        {
            _settings = settings;

            _client = new DocumentClient(new Uri(endpointUrl), authorizationKey);
            
            //Get, or Create, the Database
            _database = GetOrCreateDatabaseAsync(databaseId).Result;

            //Get, or Create, the Document Collection
            _collection = GetOrCreateCollectionAsync(_database.SelfLink, collectionId).Result;
        }
        /// <summary>
        /// Initialize a RangePartitionResolver.
        /// </summary>
        /// <param name="partitionKeyPropertyName">The property name to be used as the partition Key.</param>
        /// <param name="client">The DocumentDB client instance to use.</param>
        /// <param name="database">The database to run the samples on.</param>
        /// <param name="collectionNames">The names of collections used.</param>
        /// <returns>The created HashPartitionResolver.</returns>
        public static async Task<RangePartitionResolver<string>> InitializeRangeResolver(string partitionKeyPropertyName, DocumentClient client, Database database, string[] collectionNames)
        {
            // Set local to input.
            string[] CollectionNames = collectionNames;
            int numCollectionNames = CollectionNames.Length;

            // Create array of DocumentCollections.
            DocumentCollection[] collections = new DocumentCollection[numCollectionNames];

            // Create string array of Self Links to Collections.
            string[] selfLinks = new string[numCollectionNames];

            //Create some collections to partition data.
            for (int i = 0; i < numCollectionNames; i++)
            {
                collections[i] = await DocumentClientHelper.GetCollectionAsync(client, database, CollectionNames[i]);
                selfLinks[i] = collections[i].SelfLink;
            }

            // Join Self Link Array into a comma separated string.
            string selfLinkString = String.Join(", ", selfLinks);

            // If each collection represents a range, it will have both a start and end point in its range, thus there are 2 rangeNames for each collection.
            string[] rangeNames = new string[numCollectionNames * 2];

            // Keeping track of where in the rangeNames array to add a new start/end of a range.
            int currentRangePosition = 0;

            for (int y = 0; y < numCollectionNames; y++)
            {
                string[] rangeTemp = collectionNames[y].Split('-');
                for (int z = 0; z < rangeTemp.Length; z++)
                {
                    rangeNames[currentRangePosition] = rangeTemp[z];
                    currentRangePosition++;
                }
            }

            // Dictionary needed to input into RangePartitionResolver with corresponding range and collection self link.
            Dictionary<Range<string>, string> rangeCollections = new Dictionary<Range<string>, string>();

            // TO DO:: Iterate through the ranges and add then to rangeCollections with the appropriate start/end point, with the corresponding collection self link.
            //rangeCollections.Add()
            
            RangePartitionResolver<string> rangeResolver = new RangePartitionResolver<string>(
                partitionKeyPropertyName,
                rangeCollections);

            client.PartitionResolvers[database.SelfLink] = rangeResolver;
            return rangeResolver;
        }
Ejemplo n.º 32
0
    public async Task ClearDatabaseAsync()
    {
      var db = _client.CreateDatabaseQuery()
        .Where(d => d.Id == DBID)
        .AsEnumerable()
        .FirstOrDefault();

      // If the database does not exist, create a new database
      if (db != null)
      {
        await _client.DeleteDatabaseAsync(DatabaseLink);
        _database = null;
      }
    }
Ejemplo n.º 33
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;
 }
Ejemplo n.º 34
0
        private async Task CreateDatabase()
        {
            // Check to verify a database with the id=FamilyRegistry does not exist
            database = this.client.CreateDatabaseQuery().Where(db => db.Id == "Survey").AsEnumerable().FirstOrDefault();

            // If the database does not exist, create a new database
            if (database == null)
            {
                database = await client.CreateDatabaseAsync(
                    new Database
                    {
                        Id = "Survey"
                    });
            }
        }
Ejemplo n.º 35
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
        }
Ejemplo n.º 36
0
        private static async Task <Uri> RunDemoAsync(string databaseId, string collectionId, bool addSuffix)
        {
            Microsoft.Azure.Documents.Database database = await client.CreateDatabaseIfNotExistsAsync(
                new Microsoft.Azure.Documents.Database {
                Id = databaseId
            });

            DocumentCollection collection = await GetOrCreateCollectionAsync(databaseId, collectionId);

            Uri collectionUri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);

            // await CreateDocuments(collectionUri, addSuffix);

            return(collectionUri);

            // await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId));
        }
Ejemplo n.º 37
0
        /// <summary>
        ///If DB is not avaliable in Cosmos DB create DB
        /// </summary>
        private Database GetOrCreateDatabase(string databaseName)
        {
            var client = GetOrCreateClient();

            lock (this)
            {
                if (_database == null)
                {
                    _database = client.CreateDatabaseQuery().Where(d => d.Id == databaseName).AsEnumerable().FirstOrDefault();
                    if (_database == null)
                    {
                        client.CreateDatabaseAsync(new Microsoft.Azure.Documents.Database {
                            Id = databaseName
                        }, null)
                        .ContinueWith(t => _database = t.Result);;
                    }
                }
                return(_database);
            }
        }
Ejemplo n.º 38
0
        private async Task ConnectAsync()
        {
            _lockMutex.WaitOne();
            if (_client == null)
            {
                IsConnected = false;
                if (string.IsNullOrEmpty(ConnectionUrl))
                {
                    throw new ArgumentNullException(nameof(ConnectionUrl));
                }
                if (string.IsNullOrWhiteSpace(ConnectionKey))
                {
                    throw new ArgumentNullException(nameof(ConnectionKey));
                }
                if (string.IsNullOrWhiteSpace(DatabaseName))
                {
                    throw new ArgumentNullException(nameof(DatabaseName));
                }
                try
                {
                    _client   = new DocumentClient(new Uri(ConnectionUrl), ConnectionKey);
                    _database = await GetOrCreateDatabaseAsync(DatabaseName);

                    IsConnected = true;
                }
                catch (DocumentClientException de)
                {
                    var baseException = de.GetBaseException();
                    Debug.WriteLine("{0} error occurred: {1}, Message: {2}", de.StatusCode, de.Message, baseException.Message);
                    _client = null;
                }
                catch (Exception e)
                {
                    var baseException = e.GetBaseException();
                    Debug.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message);
                    _client = null;
                }
            }
            _lockMutex.ReleaseMutex();
        }
Ejemplo n.º 39
0
        /// <summary>
        /// Create users the specified database.
        /// </summary>
        /// <param name="db">The database.</param>
        /// <param name="dbuser">Name of the user.</param>
        /// <param name="allowUpdate">if set to <c>true</c> [allow updates].</param>
        /// <returns>
        /// The Resource Response
        /// </returns>
        private async Task <Microsoft.Azure.Documents.User> CreateUserAsync(Microsoft.Azure.Documents.Database db, User dbuser, bool allowUpdate)
        {
            ResourceResponse <Microsoft.Azure.Documents.User> response = null;

            try
            {
                this.logger.Info("Checking whether {0} user exists or not", dbuser.Name);
                var existingUser = this.client.CreateUserQuery(db.UsersLink).Where(x => x.Id == dbuser.Name).AsEnumerable().SingleOrDefault();
                if (existingUser == null)
                {
                    this.logger.Info("{0} User doesn't exist. Creating it", dbuser.Name);
                    response = await this.client.CreateUserAsync(db.SelfLink, new Microsoft.Azure.Documents.User()
                    {
                        Id = dbuser.Name
                    }).ConfigureAwait(false);
                }
                else if (allowUpdate)
                {
                    this.logger.Info("{0} User exists, Updating it", dbuser.Name);
                    response = await this.client.ReplaceUserAsync(existingUser).ConfigureAwait(false);
                }
                else
                {
                    this.logger.Info("{0} user exists. Skipping update", dbuser.Name);
                    response = new ResourceResponse <Microsoft.Azure.Documents.User>(existingUser);
                }
            }
            catch (Exception exception)
            {
                this.logger.Error("An exception occured in {0} user creation/update with the error :{1} ", dbuser.Name, exception.StackTrace);
                throw;
            }

            if (response != null)
            {
                return(response.Resource);
            }

            return(null);
        }
        private async Task EnsureDatabaseCreated(string databaseName)
        {
            var dbNameToUse = Configuration.DatabaseName.GetValueOrDefault(databaseName);

            // Create new database
            DatabaseV3 = await CosmosClient.CreateDatabaseIfNotExistsAsync(dbNameToUse);

            DatabaseUri = UriFactory.CreateDatabaseUri(dbNameToUse);
            Logger?.LogDebug($"Database URI: {DatabaseUri}");
            Database = new Microsoft.Azure.Documents.Database {
                Id = databaseName
            };
            Logger?.LogDebug($"Database: {Database}");

            Logger.LogDebug($"Ensuring `{Database.Id}` exists...");
            var result = DocumentClient.CreateDatabaseIfNotExistsAsync(Database).Result;

            Logger.LogDebug($"{Database.Id} Creation Results: {result.StatusCode}");
            if (result.StatusCode.EqualsOne(HttpStatusCode.Created, HttpStatusCode.OK))
            {
                Database = result.Resource;
            }
        }
		private async Task<Microsoft.Azure.Documents.Database> CreateDatabaseAsync()
		{
			var db = new Microsoft.Azure.Documents.Database { Id = RandomName() };
			var response = await _client.CreateDatabaseAsync(db);
			return response.Resource;
		}
Ejemplo n.º 42
0
        public async Task <IEnumerable <string> > buildDB()
        {
            var results = new List <string>();

            Microsoft.Azure.Documents.Database database = await client.CreateDatabaseIfNotExistsAsync(new Database { Id = "graphdb" });

            DocumentCollection graph = await client.CreateDocumentCollectionIfNotExistsAsync(
                UriFactory.CreateDatabaseUri("graphdb"),
                new DocumentCollection { Id = "Persons" },
                new RequestOptions { OfferThroughput = 400 });

            Dictionary <string, string> gremlinQueries = new Dictionary <string, string>
            {
                { "Cleanup", "g.V().drop()" },
                { "AddAdam", "g.addV('person').property('id', 'Adam').property('Name', 'Adam')" },
                { "AddEve", "g.addV('person').property('id', 'Eve').property('Name', 'Eve')" },
                { "AddCain", "g.addV('person').property('id', 'Cain').property('Name', 'Cain')" },
                { "AddAbel", "g.addV('person').property('id', 'Abel').property('Name', 'Abel')" },
                { "AddSeth", "g.addV('person').property('id', 'Seth').property('Name', 'Seth')" },
                { "AddEnosh", "g.addV('person').property('id', 'Enosh').property('Name', 'Enosh')" },

                { "AddAdam+Eve", "g.V('Adam').addE('Married').to(g.V('Eve'))" },

                { "AddCain->Abel", "g.V('Cain').addE('Brother').to(g.V('Abel'))" },
                { "AddCain->Seth", "g.V('Cain').addE('Brother').to(g.V('Seth'))" },
                { "AddAbel->Seth", "g.V('Abel').addE('Brother').to(g.V('Seth'))" },

                { "AddEve->Abel", "g.V('Eve').addE('Mother').to(g.V('Abel'))" },
                { "AddEve->Cain", "g.V('Eve').addE('Mother').to(g.V('Cain'))" },
                { "AddEve->Seth", "g.V('Eve').addE('Mother').to(g.V('Seth'))" },

                { "AddAdam->Abel", "g.V('Adam').addE('Father').to(g.V('Abel'))" },
                { "AddAdam->Cain", "g.V('Adam').addE('Father').to(g.V('Cain'))" },
                { "AddAdam->Seth", "g.V('Adam').addE('Father').to(g.V('Seth'))" },
                { "AddSeth->Enosh", "g.V('Seth').addE('Father').to(g.V('Enosh'))" },

                { "UpdateAbel1", "g.V('Abel').property('Profession', 'Shepherd')" },
                { "UpdateCain1", "g.V('Cain').property('Profession', 'Farmer')" },

                { "UpdateAdam", "g.V('Adam').property('Max Age', '930').property('Born', '4026 BCE').property('Died', '3096 BCE').property('Name Means', 'Earthling Man; Mankind; Humankind; from a root meaning \"red\"')" },
                { "UpdateAdamFatherAge", "g.V('Adam').outE('Father').as('e').inV().has('Name', 'Seth').select('e').property('Age', '130')" }
            };

            foreach (KeyValuePair <string, string> gremlinQuery in gremlinQueries)
            {
                results.Add($"Running {gremlinQuery.Key}: {gremlinQuery.Value}");

                // The CreateGremlinQuery method extensions allow you to execute Gremlin queries and iterate
                // results asychronously
                IDocumentQuery <dynamic> query = client.CreateGremlinQuery <dynamic>(graph, gremlinQuery.Value);
                while (query.HasMoreResults)
                {
                    foreach (dynamic result in await query.ExecuteNextAsync())
                    {
                        results.Add($"\t {JsonConvert.SerializeObject(result)}");
                    }
                }

                results.Add("");
            }

            return(results);
        }
Ejemplo n.º 43
0
        /// <summary>
        /// Driver function for bulk import.
        /// </summary>
        /// <returns></returns>
        private async Task RunBulkImportAsync()
        {
            // Cleanup on start if set in config.

            DocumentCollection dataCollection = null;

            try
            {
                if (bool.Parse(ConfigurationManager.AppSettings["ShouldCleanupOnStart"]))
                {
                    Microsoft.Azure.Documents.Database database = Utils.GetDatabaseIfExists(client, DatabaseName);
                    if (database != null)
                    {
                        await client.DeleteDatabaseAsync(database.SelfLink);
                    }

                    Trace.TraceInformation("Creating database {0}", DatabaseName);
                    database = await client.CreateDatabaseAsync(new Microsoft.Azure.Documents.Database {
                        Id = DatabaseName
                    });

                    Trace.TraceInformation(String.Format("Creating collection {0} with {1} RU/s", CollectionName, CollectionThroughput));
                    dataCollection = await Utils.CreatePartitionedCollectionAsync(client, DatabaseName, CollectionName, CollectionThroughput);
                }
                else
                {
                    dataCollection = Utils.GetCollectionIfExists(client, DatabaseName, CollectionName);
                    if (dataCollection == null)
                    {
                        throw new Exception("The data collection does not exist");
                    }
                }
            }
            catch (Exception de)
            {
                Trace.TraceError("Unable to initialize, exception message: {0}", de.Message);
                throw;
            }

            // Prepare for bulk import.

            // Creating documents with simple partition key here.
            string partitionKeyProperty = dataCollection.PartitionKey.Paths[0].Replace("/", "");

            long numberOfDocumentsToGenerate = long.Parse(ConfigurationManager.AppSettings["NumberOfDocumentsToImport"]);
            int  numberOfBatches             = int.Parse(ConfigurationManager.AppSettings["NumberOfBatches"]);
            long numberOfDocumentsPerBatch   = (long)Math.Floor(((double)numberOfDocumentsToGenerate) / numberOfBatches);

            // Set retry options high for initialization (default values).
            client.ConnectionPolicy.RetryOptions.MaxRetryWaitTimeInSeconds           = 30;
            client.ConnectionPolicy.RetryOptions.MaxRetryAttemptsOnThrottledRequests = 9;

            IBulkExecutor bulkExecutor = new BulkExecutor(client, dataCollection);
            await bulkExecutor.InitializeAsync();

            // Set retries to 0 to pass control to bulk executor.
            client.ConnectionPolicy.RetryOptions.MaxRetryWaitTimeInSeconds           = 0;
            client.ConnectionPolicy.RetryOptions.MaxRetryAttemptsOnThrottledRequests = 0;

            BulkImportResponse bulkImportResponse = null;
            long   totalNumberOfDocumentsInserted = 0;
            double totalRequestUnitsConsumed      = 0;
            double totalTimeTakenSec = 0;

            var tokenSource = new CancellationTokenSource();
            var token       = tokenSource.Token;

            for (int i = 0; i < numberOfBatches; i++)
            {
                // Generate JSON-serialized documents to import.

                List <string> documentsToImportInBatch = new List <string>();
                long          prefix = i * numberOfDocumentsPerBatch;

                Trace.TraceInformation(String.Format("Generating {0} documents to import for batch {1}", numberOfDocumentsPerBatch, i));
                for (int j = 0; j < numberOfDocumentsPerBatch; j++)
                {
                    string partitionKeyValue = (prefix + j).ToString();
                    string id = partitionKeyValue + Guid.NewGuid().ToString();
                }

                // Invoke bulk import API.

                var tasks = new List <Task>();

                tasks.Add(Task.Run(async() =>
                {
                    Trace.TraceInformation(String.Format("Executing bulk import for batch {0}", i));
                    do
                    {
                        try
                        {
                            bulkImportResponse = await bulkExecutor.BulkImportAsync(
                                documents: documentsToImportInBatch,
                                enableUpsert: true,
                                disableAutomaticIdGeneration: true,
                                maxConcurrencyPerPartitionKeyRange: null,
                                maxInMemorySortingBatchSize: null,
                                cancellationToken: token);
                        }
                        catch (DocumentClientException de)
                        {
                            Trace.TraceError("Document client exception: {0}", de);
                            break;
                        }
                        catch (Exception e)
                        {
                            Trace.TraceError("Exception: {0}", e);
                            break;
                        }
                    } while (bulkImportResponse.NumberOfDocumentsImported < documentsToImportInBatch.Count);

                    Trace.WriteLine(String.Format("\nSummary for batch {0}:", i));
                    Trace.WriteLine("--------------------------------------------------------------------- ");
                    Trace.WriteLine(String.Format("Inserted {0} docs @ {1} writes/s, {2} RU/s in {3} sec",
                                                  bulkImportResponse.NumberOfDocumentsImported,
                                                  Math.Round(bulkImportResponse.NumberOfDocumentsImported / bulkImportResponse.TotalTimeTaken.TotalSeconds),
                                                  Math.Round(bulkImportResponse.TotalRequestUnitsConsumed / bulkImportResponse.TotalTimeTaken.TotalSeconds),
                                                  bulkImportResponse.TotalTimeTaken.TotalSeconds));
                    Trace.WriteLine(String.Format("Average RU consumption per document: {0}",
                                                  (bulkImportResponse.TotalRequestUnitsConsumed / bulkImportResponse.NumberOfDocumentsImported)));
                    Trace.WriteLine("---------------------------------------------------------------------\n ");

                    totalNumberOfDocumentsInserted += bulkImportResponse.NumberOfDocumentsImported;
                    totalRequestUnitsConsumed      += bulkImportResponse.TotalRequestUnitsConsumed;
                    totalTimeTakenSec += bulkImportResponse.TotalTimeTaken.TotalSeconds;
                },
                                   token));

                /*
                 * tasks.Add(Task.Run(() =>
                 * {
                 *  char ch = Console.ReadKey(true).KeyChar;
                 *  if (ch == 'c' || ch == 'C')
                 *  {
                 *      tokenSource.Cancel();
                 *      Trace.WriteLine("\nTask cancellation requested.");
                 *  }
                 * }));
                 */

                await Task.WhenAll(tasks);
            }

            Trace.WriteLine("Overall summary:");
            Trace.WriteLine("--------------------------------------------------------------------- ");
            Trace.WriteLine(String.Format("Inserted {0} docs @ {1} writes/s, {2} RU/s in {3} sec",
                                          totalNumberOfDocumentsInserted,
                                          Math.Round(totalNumberOfDocumentsInserted / totalTimeTakenSec),
                                          Math.Round(totalRequestUnitsConsumed / totalTimeTakenSec),
                                          totalTimeTakenSec));
            Trace.WriteLine(String.Format("Average RU consumption per document: {0}",
                                          (totalRequestUnitsConsumed / totalNumberOfDocumentsInserted)));
            Trace.WriteLine("--------------------------------------------------------------------- ");

            // Cleanup on finish if set in config.

            if (bool.Parse(ConfigurationManager.AppSettings["ShouldCleanupOnFinish"]))
            {
                Trace.TraceInformation("Deleting Database {0}", DatabaseName);
                await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(DatabaseName));
            }

            Trace.WriteLine("\nPress any key to exit.");
            Console.ReadKey();
        }
Ejemplo n.º 44
0
        /// <summary>
        /// Creates the collection.
        /// </summary>
        /// <param name="db">The database.</param>
        /// <param name="collection">The collection</param>
        /// <param name="allowUpdate">if set to <c>true</c> [allow updates].</param>
        /// <returns>
        /// The Resource Response for collection
        /// </returns>
        /// <exception cref="InvalidOperationException">Document collection partition key cannot be changed.</exception>
        private async Task <DocumentCollection> CreateCollectionAsync(Microsoft.Azure.Documents.Database db, Collection collection, bool allowUpdate)
        {
            ResourceResponse <DocumentCollection> response = null;

            this.logger.Info("Checking whether {0} collection exists or not", collection.Name);
            var existingColl = this.client.CreateDocumentCollectionQuery(db.SelfLink).Where(x => x.Id == collection.Name).AsEnumerable().SingleOrDefault();

            this.logger.Info("Initializing {0} collection", collection.Name);
            var documentCollection = new DocumentCollection
            {
                Id = collection.Name,
                DefaultTimeToLive = collection.Ttl
            };

            // Setting up Partition Key
            if (collection.Partitioned && !string.IsNullOrEmpty(collection.PartitionKey))
            {
                this.logger.Info("Setting up Partition Key for the {0} collection", collection.Name);
                documentCollection.PartitionKey = new PartitionKeyDefinition();
                documentCollection.PartitionKey.Paths.Add(collection.PartitionKey);
            }

            // Excluded Paths
            foreach (var path in collection.ExcludedPaths)
            {
                this.logger.Info("Setting up excluded paths for the {0} collection", collection.Name);
                var excludedPaths = new ExcludedPath()
                {
                    Path = path
                };
                documentCollection.IndexingPolicy.ExcludedPaths.Add(excludedPaths);
            }

            // Included Paths
            foreach (var path in collection.IncludedPaths)
            {
                this.logger.Info("Setting up included path {0} for the {1} collection", path, collection.Name);
                var includedPaths = new IncludedPath()
                {
                    Path = path
                };
                documentCollection.IndexingPolicy.IncludedPaths.Add(includedPaths);
            }

            // Range Index Path
            foreach (var path in collection.RangeIndexIncludedPaths)
            {
                this.logger.Info("Setting up range included path {0} for the {1} collection", path, collection.Name);
                var includedPaths = new IncludedPath()
                {
                    Path = path, Indexes = new System.Collections.ObjectModel.Collection <Index> {
                        new RangeIndex(DataType.String)
                        {
                            Precision = -1
                        }
                    }
                };
                documentCollection.IndexingPolicy.IncludedPaths.Add(includedPaths);
            }

            documentCollection.IndexingPolicy.IndexingMode = this.GetIndexingMode(collection.IndexingMode);
            if (existingColl == null)
            {
                this.logger.Info("{0} Collection doesn't exist, Creating it", collection.Name);
                response = await this.client.CreateDocumentCollectionAsync(db.SelfLink, documentCollection, new RequestOptions { OfferThroughput = collection.ResourceUnits }).ConfigureAwait(false);
            }
            else if (allowUpdate)
            {
                this.logger.Info("{0} Collection exists, Updating the collection", collection.Name);

                var validationResult = ValidatePartitionKey(existingColl.PartitionKey, documentCollection.PartitionKey);
                if (validationResult)
                {
                    existingColl.PartitionKey = documentCollection.PartitionKey;
                }
                else
                {
                    throw new InvalidOperationException("Document collection partition key cannot be changed.");
                }

                existingColl.IndexingPolicy = documentCollection.IndexingPolicy;
                response = await this.client.ReplaceDocumentCollectionAsync(existingColl).ConfigureAwait(false);

                this.logger.Info("Updating throughput of the collection");
                var offer = this.client.CreateOfferQuery().Where(t => t.ResourceLink == existingColl.SelfLink).AsEnumerable().SingleOrDefault();
                if (offer != null)
                {
                    offer = new OfferV2(offer, collection.ResourceUnits);
                    await this.client.ReplaceOfferAsync(offer).ConfigureAwait(false);
                }
            }
            else
            {
                this.logger.Info("{0} Collection exists. Skipping update", collection.Name);
                response = new ResourceResponse <DocumentCollection>(existingColl);
            }

            if (response != null)
            {
                return(response.Resource);
            }

            return(null);
        }