Beispiel #1
0
        public static async Task UpdateDcAll(string endpointUrl, string authorizationKey)
        {
            DocumentClient client = new DocumentClient(new Uri(endpointUrl), authorizationKey);
            var database = await DocumentDB.GetDB(client);

            IEnumerable<DocumentCollection> dz = client.CreateDocumentCollectionQuery(database.SelfLink)
                .AsEnumerable();


            DocumentCollection origin = client.CreateDocumentCollectionQuery(database.SelfLink)
                .Where(c => c.Id == "LMSCollection")
                .AsEnumerable()
                .FirstOrDefault();

            //search collection
            var ds =
                from d in client.CreateDocumentQuery<DcAllocate>(origin.DocumentsLink)
                where d.Type == "DisList"
                select d;
            foreach (var d in ds)
            {
                if (d.District.Contains("tst-azhang14"))
                {
                    Console.WriteLine(d.DcName);
                }
            }

            /* foreach (var x in dz)
            {
                Console.WriteLine(x.Id + x.DocumentsLink);
                await UpdateDc(x, client, database, origin);
            }*/
        }
 private static DocumentCollection GetDocumentCollection(DocumentClient client, Database database, string collectionId)
 {
     return client.CreateDocumentCollectionQuery(database.SelfLink)
         .Where(c => c.Id == collectionId)
         .AsEnumerable()
         .SingleOrDefault();
 }
        private async Task<DocumentClient> GetDocumentDbClient()
        {
            var client = new DocumentClient(new Uri(_endPointUrl), _authorizationKKry);

            var database = client.CreateDatabaseQuery().
                Where(db => db.Id == DocumentDbId).AsEnumerable().FirstOrDefault();

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

            DocumentCollection = client.CreateDocumentCollectionQuery
                ("dbs/" + database.Id).Where(c => c.Id == _collectionId).AsEnumerable().FirstOrDefault();

            if (DocumentCollection == null)
            {
                DocumentCollection = await client.CreateDocumentCollectionAsync("dbs/" + DocumentDbId,
                new DocumentCollection
                {
                    Id = _collectionId
                });

            }
           

            return client;
        }
        //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,
                });
            }
        }
 static DocumentCollection FindDocumentCollection(DocumentClient client, Database database, string name)
 {
     return client.CreateDocumentCollectionQuery(database.SelfLink)
         .Where(x => x.Id == name)
         .ToList()
         .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..
            }
        }
Beispiel #7
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 static DocumentCollection GetDocumentCollection(DocumentClient client, Database database)
 {
     var documentCollection = client
         .CreateDocumentCollectionQuery(database.CollectionsLink)
         .Where(c => c.Id == "Documents")
         .AsEnumerable()
         .FirstOrDefault();
     return documentCollection;
 }
        internal static DocumentCollection ReadOrCreateCollection(DocumentClient client, string databaseLink, string collectionId)
        {
            var collections = client.CreateDocumentCollectionQuery(databaseLink)
                            .Where(col => col.Id == collectionId).ToArray();

            if (collections.Any())
            {
                return collections.First();
            }

            var collection = new DocumentCollection { Id = collectionId };
            return client.CreateDocumentCollectionAsync(databaseLink, collection).Result;
        }
Beispiel #10
0
        private static DocumentCollection GetCollection(Microsoft.Azure.Documents.Client.DocumentClient client, Database database)
        {
            // Create a document collection.
            var documentCollection = client.CreateDocumentCollectionQuery(database.SelfLink).Where(c => c.Id == "LogEntries").ToArray().FirstOrDefault();

            if (documentCollection == null)
            {
                // Create the document collection.
                documentCollection = client.CreateDocumentCollectionAsync(database.SelfLink, new DocumentCollection {
                    Id = "LogEntries"
                }).Result;
            }
            return(documentCollection);
        }
Beispiel #11
0
        static void Main(string[] args)
        {
            if (args.Count() < 4)
            {
                Console.WriteLine("You must specify the following parameters: Document DB endpoint URL, secret key, database name, and collection name.");
                Console.WriteLine("Program terminated. Press any key to exit");
                Console.ReadKey();
                return; // exit program

            }

            // setup DocumentDB client using passed parameters
            Uri endpoint = new Uri(args[0]);
            string authKey = args[1];
            string databaseId = args[2];
            string collectionId = args[3];

            var connectionPolicy = new ConnectionPolicy()
            {
                ConnectionProtocol = Protocol.Https,
                ConnectionMode = ConnectionMode.Gateway
            };
            client = new DocumentClient(endpoint, authKey, connectionPolicy);

            Database database = client.CreateDatabaseQuery().Where(db => db.Id == databaseId).ToArray().FirstOrDefault();
            DocumentCollection collection = client.CreateDocumentCollectionQuery(database.SelfLink).Where(c => c.Id == collectionId).ToArray().FirstOrDefault();

            // get list of SP files
            var currentDir = new DirectoryInfo(Directory.GetCurrentDirectory());
            foreach (FileInfo tmpFile in currentDir.GetFiles("SP_*.js"))
            {
                string storedProecureId = tmpFile.Name.Substring(3, (tmpFile.Name.Length - 6));
                Console.WriteLine("Found File: " + storedProecureId);

                var storedProc = new StoredProcedure()
                {
                    Id = storedProecureId,
                    Body = File.ReadAllText(tmpFile.FullName)
                };

                TryDeleteStoredProcedure(collection.SelfLink, storedProc.Id); 

                client.CreateStoredProcedureAsync(collection.SelfLink, storedProc, null).Wait();
            }

            Console.WriteLine("Stored Procedure Setup completed. Press any key to continue.");
            Console.ReadKey();
        }
Beispiel #12
0
        public static async Task BackupPostAll()
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
            CloudTableClient c = storageAccount.CreateCloudTableClient();
            _table = c.GetTableReference("Post");
            _table.CreateIfNotExists();

            _client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
            var database = await DocumentDB.GetDB(_client);

            IEnumerable<DocumentCollection> dz = _client.CreateDocumentCollectionQuery(database.SelfLink)
               .AsEnumerable();
            foreach (var x in dz)
            {
               await BackupPostCollection(x);
            }
        }
        public async Task<bool>  WriteDocument(MessageType type, String jsonString)
        {
            var client = new DocumentClient(new Uri(ConfigurationManager.AppSettings["DocumentServiceEndpoint"]), ConfigurationManager.AppSettings["DocumentKey"]);

            var dbName = ConfigurationManager.AppSettings["DocumentDatabase"];
            var database = client.CreateDatabaseQuery().Where(db => db.Id == dbName).AsEnumerable().FirstOrDefault() ??
                           await client.CreateDatabaseAsync(new Database{ Id = dbName});


            // Check to verify a document collection with the does not exist
            var docCollection = ConfigurationManager.AppSettings["DocumentCollection"];
            var documentCollection = client.CreateDocumentCollectionQuery(database.CollectionsLink).Where(c => c.Id == docCollection).AsEnumerable().FirstOrDefault() ??
                                     await client.CreateDocumentCollectionAsync(database.CollectionsLink, new DocumentCollection { Id = docCollection });

            var id = Guid.NewGuid().ToString();

            var response = HttpStatusCode.Unused;
            switch (type)
            {
                case MessageType.Energy:
                    var energyDoc = JsonConvert.DeserializeObject<EnergyDocument>(jsonString);
                    energyDoc.id = id;
                    response = (await client.CreateDocumentAsync(documentCollection.DocumentsLink, energyDoc)).StatusCode;
                    break;
                case MessageType.Humidity:
                    var humidityDoc = JsonConvert.DeserializeObject<HumidityDocument>(jsonString);
                    humidityDoc.id = id;
                    response = (await client.CreateDocumentAsync(documentCollection.DocumentsLink, humidityDoc)).StatusCode;
                    break;
                case MessageType.Light:
                    var lightDoc = JsonConvert.DeserializeObject<LightDocument>(jsonString);
                    lightDoc.id = id;
                    response = (await client.CreateDocumentAsync(documentCollection.DocumentsLink, lightDoc)).StatusCode;
                    break;
                case MessageType.Temperature:
                    var tempDoc = JsonConvert.DeserializeObject<TemperatureDocument>(jsonString);
                    tempDoc.id = id;
                    response = (await client.CreateDocumentAsync(documentCollection.DocumentsLink, tempDoc)).StatusCode;
                    break;
            }

            return response == HttpStatusCode.Created;
        }
Beispiel #14
0
        static void Main(string[] args)
        {
            ConnectionPolicy policy = new ConnectionPolicy()
            {
                ConnectionMode = ConnectionMode.Direct,
                ConnectionProtocol = Protocol.Tcp
            };
            Uri endPoint = new Uri(EndpointUrl);
            using (DocumentClient client = new DocumentClient(endPoint, AuthKey, policy))
            {
                Database database =
                    client.CreateDatabaseQuery().Where(db => db.Id == DatabasebId).AsEnumerable().First();
                DocumentCollection collection =
                    client.CreateDocumentCollectionQuery(database.SelfLink)
                        .Where(c => c.Id == CollectionId)
                        .AsEnumerable()
                        .First();

                IEnumerable<DataClass> dataSet = GetDataObjects(100);

                Task<ResourceResponse<Document>>[] documentTasks =
                    dataSet.Select(d => client.CreateDocumentAsync(collection.DocumentsLink, d)).ToArray();

                Task.WaitAll(documentTasks);

                Document[] docs = documentTasks.Select(t => t.Result.Resource).ToArray();

                Document doc = docs.First();

                Task<ResourceResponse<Attachment>>[] attachmentTasks =
                    docs.Select(d => client.CreateAttachmentAsync(doc.AttachmentsLink, GetStream(1024))).ToArray();

                Task.WaitAll(attachmentTasks);

                DataClass[] data =
                    client.CreateDocumentQuery<DataClass>(collection.DocumentsLink).Select(d => d).ToArray();
                Attachment[] attachments = client.CreateAttachmentQuery(doc.AttachmentsLink).ToArray();

                string sql = "SELECT c.Name FROM c Where c.ObjectKey < 30 AND c.ObjectKey > 20";
                dynamic[] dataArray = client.CreateDocumentQuery(collection.DocumentsLink, sql).ToArray();
            }
        }
Beispiel #15
0
        private static DocumentCollection GetOrCreateCollection(DocumentClient client, string databaseSelfLink, string collectionName)
        {
            // Try to retrieve the collection (Microsoft.Azure.Documents.DocumentCollection) whose Id is equal to collectionName
            var collection = client.CreateDocumentCollectionQuery(databaseSelfLink)
                .Where(c => c.Id == collectionName)
                .ToArray()
                .FirstOrDefault()
                             ??
                             client.CreateDocumentCollectionAsync(databaseSelfLink,
                                 new DocumentCollection { Id = collectionName }).Result;

            if (collection == null)
            {
                collection = new DocumentCollection { Id = collectionName };
                collection.IndexingPolicy.IncludedPaths.Add(new IndexingPath { IndexType = IndexType.Range, NumericPrecision = 5, Path = "/" });

                collection = client.CreateDocumentCollectionAsync(databaseSelfLink, collection).Result;
            }
            return collection;
        }
        // GET /api/search/?criterias=TOTO
        public List<FeedItem> Get([FromUri]string criterias = "")
        {
            // si criterias = LAST10 -> renvoyé les dix derniere articles

            var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);

            Database database = client.CreateDatabaseQuery()
                .Where(db => db.Id == databaseId)
                .AsEnumerable()
                .FirstOrDefault();

            DocumentCollection collectionPosts = client.CreateDocumentCollectionQuery(database.CollectionsLink)
                .Where(doccoll => doccoll.Id == collectionIdPost)
                .AsEnumerable()
                .FirstOrDefault();

            var posts  = (
                from b in client.CreateDocumentQuery<FeedItem>(collectionPosts.SelfLink)
                where b.Year == 2015 && b.Month >= 1 && b.Day >= 8
                select b).ToList();

            return posts;
        }
        public static async Task SaveTestResults(PerformanceTestResult testResults)
        {
            
            // Make sure to call client.Dispose() once you've finished all DocumentDB interactions
            // Create a new instance of the DocumentClient
            var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);

            // Check to verify a database with the id=FamilyRegistry does not exist
            Database database = client.CreateDatabaseQuery().Where(db => db.Id == "DistributedWebTest").AsEnumerable().FirstOrDefault();

            if (database == null)
            {
                // Create a database
                database = await client.CreateDatabaseAsync(
                    new Database
                    {
                        Id = "DistributedWebTest"
                    });
            }
           
            // Check to verify a document collection with the id=FamilyCollection does not exist
            DocumentCollection documentCollection = client.CreateDocumentCollectionQuery(database.CollectionsLink).Where(c => c.Id == "DistributedTestResults").AsEnumerable().FirstOrDefault();

            if (documentCollection == null)
            {
                // Create a document collection using the lowest performance tier available (currently, S1)
                documentCollection = await client.CreateDocumentCollectionAsync(database.CollectionsLink,
                    new DocumentCollection { Id = "DistributedTestResults" },
                    new RequestOptions { OfferType = "S1" });
            }

            await client.CreateDocumentAsync(documentCollection.DocumentsLink, testResults);

  

        }
        public static DocumentCollection GetOrCreateDocumentCollection(DocumentClient client, string databaseLink,
            string collectionId, IndexingPolicy indexingPolicy)
        {
            IQueryable<DocumentCollection> collectionQuery =
                from coll in client.CreateDocumentCollectionQuery(databaseLink)
                where coll.Id == collectionId
                select coll;

            IEnumerable<DocumentCollection> enumerable = collectionQuery.AsEnumerable();
            if (!enumerable.Any())
            {
                DocumentCollection collection = new DocumentCollection() { Id = collectionId };

                if (indexingPolicy != null)
                {
                    collection.IndexingPolicy.Automatic = indexingPolicy.Automatic;
                    collection.IndexingPolicy.IndexingMode = indexingPolicy.IndexingMode;

                    foreach (var path in indexingPolicy.IncludedPaths)
                    {
                        collection.IndexingPolicy.IncludedPaths.Add(path);
                    }

                    foreach (var path in indexingPolicy.ExcludedPaths)
                    {
                        collection.IndexingPolicy.ExcludedPaths.Add(path);
                    }
                }

                return client.CreateDocumentCollectionAsync(databaseLink, collection).Result.Resource;
            }
            else
            {
                return enumerable.First();
            }
        }
Beispiel #19
0
        private static async Task sp2(DocumentCollection dc, DocumentClient client, Database database)
        {
            DocumentCollection dc2 = client.CreateDocumentCollectionQuery(database.SelfLink)
                .Where(c => c.Id == "LMSCollection1444075919174")
                .AsEnumerable()
                .FirstOrDefault();
            await client.OpenAsync();
            //await _iDbService.CollectionTransfer(client, dc2, dc);
            var mn=await client.ReadDatabaseAsync(database.SelfLink);

          /*  await client.CreateDocumentAsync(dc.SelfLink, new CurrentCollection
            {
                id = "CurrentCollection",
                name=dc.Id
            });*/
          /*  var resolver = _iDbService.GetResolver(client, dc);
            foreach (var d in resolver.PartitionMap)
            {
                Console.WriteLine(d.Value);
                Offer offer = client.CreateOfferQuery()
                                      .Where(r => r.ResourceLink == d.Value)
                                      .AsEnumerable()
                                      .SingleOrDefault();
            }*/




           /* HashPartitionResolver hashResolver = new HashPartitionResolver(
                u => ((PostMessage) u).Path.District,
                new string[] {dc.SelfLink, dc2.SelfLink});

            client.PartitionResolvers[database.SelfLink] = hashResolver;*/

            var rangeResolver = _iDbService.GetResolver(client);
            client.PartitionResolvers[database.SelfLink] = rangeResolver;

            
            var created = await _iDbService.InitResolver("");
          
            
            while (true)
            {
                var re2 = await _iDbService.UpdateResolver(dc2);
                var p = re2;
                await Task.Delay(TimeSpan.FromSeconds(4));
            }

            var z1 = rangeResolver.GetPartitionKey(new PostMessage
                    {
                        Type = "Post",
                        Info = new Info
                        {
                            user = "******",
                            uid = "1210808",
                            message = "java",
                            timestamp = 7
                        },
                        Path = new PostPath
                        {
                            District = "tst-azhang" 
                        }
                    });

            //search global
            IQueryable<PostMessage> query = client.CreateDocumentQuery<PostMessage>(database.SelfLink)
                .Where(u => u.Info.timestamp>1);

            var now = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;

            //search on partition

            var t1 = DateTime.Now;
            var query2 = client.CreateDocumentQuery<PostMessage>(database.SelfLink, new FeedOptions
            {
                MaxItemCount = 1200
            }, now);
            var query3 = client.CreateDocumentQuery<PostMessage>(dc2.SelfLink).AsDocumentQuery();



            var t2 = DateTime.Now;
            Console.WriteLine(t2-t1);
              //.Where(u => u.Path.District=="tst-azhang");
          
            foreach (PostMessage a in query2)
            {
                Console.WriteLine(a.Info.timestamp);
            }

          /*  double totalRequestCharge = 0;
            
            while (query3.HasMoreResults)
            {
                FeedResponse<dynamic> queryResponse = await query3.ExecuteNextAsync<dynamic>();
                Console.WriteLine("Query batch consumed {0} request units {1} doc", queryResponse.RequestCharge, queryResponse.Count);
                totalRequestCharge += queryResponse.RequestCharge;
            }
            Console.WriteLine(DateTime.Now - t2);
            Console.WriteLine("Query consumed {0} request units in total", totalRequestCharge);*/
            Console.ReadLine();
       

            int n = 19;
            try
            {
                while (n < 10)
                {
                    n++;
                    var timestamp=(long) (DateTime.UtcNow.AddHours(-1).Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
                    PostMessage x = new PostMessage
                    {
                        Type = "Post",
                        Info = new Info
                        {
                            user = "******",
                            uid = "1210808",
                            message = "java",
                            timestamp = timestamp
                        },
                        Path = new PostPath
                        {
                            District = "tst-azhang" 
                        }
                    };

                    await client.CreateDocumentAsync(database.SelfLink, x);
                }
            }
            catch (Exception e)
            {
                var t = e;
            }

            var z = 4;
        }
Beispiel #20
0
 public static DocumentCollection GetCollection(DocumentClient client, Database db, string collectionName)
 {
     return
         client.CreateDocumentCollectionQuery(db.CollectionsLink)
             .Where(w => w.Id == collectionName)
             .AsEnumerable()
             .FirstOrDefault();
 }
Beispiel #21
0
        private static async Task DeleteAll(DocumentClient client, Database database,
            DocumentCollection dc)
        {
            IEnumerable<DocumentCollection> dz = client.CreateDocumentCollectionQuery(database.SelfLink)
                .AsEnumerable();
            var nnn = 0;
            foreach (var z in dz)
            {
                var families =
                    from f in client.CreateDocumentQuery(z.SelfLink)
                    select f;

                try
                {
                    var mn = 0;
                    List<dynamic> batch = new List<dynamic>();

                    foreach (var family in families)
                    {
                        nnn++;

                        if (mn < 100)
                        {
                            batch.Add(family);
                            mn++;
                        }
                        else
                        {
                            await _iDbService.DocumentDb.BatchDelete(z, batch);
                            mn = 0;
                            batch = new List<dynamic>();
                        }

                        /* dynamic d = JsonConvert.DeserializeObject(family.ToString());
                            //Console.WriteLine(d.Path.school);  
                            try
                            {
                                //var res = await client.DeleteDocumentAsync(family.SelfLink);
                                var family1 = family;
                                var response =
                                    await
                                        _iDbService.ExecuteWithRetries(5,
                                            () => client.DeleteDocumentAsync(family1.SelfLink));
                            }
                            catch (DocumentClientException e)
                            {
                                Console.WriteLine(e.Message);
                            }

                            Console.WriteLine(family.SelfLink);*/
                    }
                    await _iDbService.DocumentDb.BatchDelete(z, batch);
                }
                catch (Exception eee)
                {
                    Console.WriteLine(eee.Message);
                }
            }

            Console.WriteLine(nnn);
        }
Beispiel #22
0
        private static async Task Resolver(DocumentCollection dc, DocumentClient client, Database database)
        {
            //transfer collection data
            DocumentCollection dc2 = client.CreateDocumentCollectionQuery(database.SelfLink)
                .Where(c => c.Id == "LMSCollection1444075919174")
                .AsEnumerable()
                .FirstOrDefault();
            //await _iDbService.CollectionTransfer(dc2, dc);
   
            /*await client.CreateDocumentAsync(dc.SelfLink, new CurrentCollection
            {
                id = "CurrentCollection",
                name = dc.Id
            });*/

            //resolver
            /*HashPartitionResolver hashResolver = new HashPartitionResolver(
                u => ((PostMessage) u).Path.District,
                new string[] {dc.SelfLink, dc2.SelfLink});

            client.PartitionResolvers[database.SelfLink] = hashResolver;

            var rangeResolver = _iDbService.RangePartitionResolver().GetResolver();
            client.PartitionResolvers[database.SelfLink] = rangeResolver;
            var z1 = rangeResolver.GetPartitionKey(new PostMessage
            {
                Type = "Post",
                Info = new Info
                {
                    user = "******",
                    uid = "1210808",
                    message = "java",
                    timestamp = 7
                },
                Path = new PostPath
                {
                    District = "tst-azhang"
                }
            });*/

            var created = await _iDbService.DocumentDb.InitResolver();


            if (dc != null)
            {
                while (true)
                {
                    var re2 = await _iDbService.DocumentDb.UpdateResolver(dc2);
                    var p = re2;
                    await Task.Delay(TimeSpan.FromSeconds(4));
                }
            }


            //search global
            IQueryable<PostMessage> query = client.CreateDocumentQuery<PostMessage>(database.SelfLink)
                .Where(u => u.Info.timestamp > 1);


            //search on partition
            var partitionKey = (long) (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
            var query2 = client.CreateDocumentQuery<PostMessage>(database.SelfLink, new FeedOptions
            {
                MaxItemCount = 1200
            }, partitionKey);

            foreach (PostMessage a in query2)
            {
                Console.WriteLine(a.Info.timestamp);
            }


            //check search RU
            /*  double totalRequestCharge = 0;
            var query3 = client.CreateDocumentQuery<PostMessage>(dc2.SelfLink).AsDocumentQuery();
            while (query3.HasMoreResults)
            {
                FeedResponse<dynamic> queryResponse = await query3.ExecuteNextAsync<dynamic>();
                Console.WriteLine("Query batch consumed {0} request units {1} doc", queryResponse.RequestCharge, queryResponse.Count);
                totalRequestCharge += queryResponse.RequestCharge;
            }
            Console.WriteLine("Query consumed {0} request units in total", totalRequestCharge);*/

            //await AddTestData(client,database,5);

            Console.ReadLine();
        }
        private DocumentConnection CreateDocumentDbConnection()
        {
            var client = new DocumentClient(new Uri(EndpointUri), AccountKey);

            // get the database and if it doesn't exist create it

            Database database = client.CreateDatabaseQuery()
                .Where(db => db.Id == Database)
                .AsEnumerable()
                .FirstOrDefault();

            if (database == null)
            {
                Log.LogMessage(MessageImportance.Low, "The database {0} does not exist, will create it.", Database);
                var task = client.CreateDatabaseAsync(new Database { Id = Database });
                database = task.Result;
            }

            // get the document collection and if it doesn't exist create it

            DocumentCollection collection = client.CreateDocumentCollectionQuery(database.SelfLink)
                .Where(c => c.Id == Collection)
                .AsEnumerable()
                .FirstOrDefault();

            if (collection == null)
            {
                Log.LogMessage(MessageImportance.Low, "The collection {0} does not exist, will create it.", Collection);
                var task = client.CreateDocumentCollectionAsync(database.SelfLink, new DocumentCollection { Id = Collection });
                collection = task.Result;
            }

            Log.LogMessage(MessageImportance.Normal, "Connected to DocumentDB database {0}, collection {1}.", Database, Collection);
            return new DocumentConnection(database, client, collection);
        }
Beispiel #24
0
        private static async Task<DocumentCollection> GetNewCollection(DocumentClient client, string databaseSelfLink)
        {
            //create new collection
            /*var t = (long) (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
            DocumentCollection newDc=await _client.CreateDocumentCollectionAsync(_database.CollectionsLink,
                                            new DocumentCollection
                                            {
                                                Id = "LMSCollection"+t
                                            });
            //Fetch the resource to be updated
            Offer offer = _client.CreateOfferQuery()
                                      .Where(r => r.ResourceLink == newDc.SelfLink)
                                      .AsEnumerable()
                                      .SingleOrDefault();

            //Change the user mode to All
            if (offer != null)
            {
                offer.OfferType = "S3";

                //Now persist these changes to the database by replacing the original resource
                Offer updated = await _client.ReplaceOfferAsync(offer);
            }*/

            //search collection
            DocumentCollection newDc = client.CreateDocumentCollectionQuery(databaseSelfLink)
                .Where(c => c.Id == "LMSCollection1444075919174")
                .AsEnumerable()
                .FirstOrDefault();


            return newDc;
        }
        /// <summary>
        /// Initialize the DocumentDb settings and connections
        /// </summary>
        public void InitializeDocumentDb()
        {
            this.DocumentDbEndPointUrl = ConfigurationManager.AppSettings["DocumentDbEndPointUrl"];
            if (String.IsNullOrWhiteSpace(this.DocumentDbEndPointUrl))
            {
                throw new ArgumentException("A required AppSetting cannot be null or empty", "DocumentDbEndPointUrl");
            }

            this.DocumentDbAuthorizationKey = ConfigurationManager.AppSettings["DocumentDbAuthorizationKey"];
            if (String.IsNullOrWhiteSpace(this.DocumentDbAuthorizationKey))
            {
                throw new ArgumentException("A required AppSetting cannot be null or empty", "DocumentDbAuthorizationKey");
            }

            this.DocumentDbDatabase = ConfigurationManager.AppSettings["DocumentDbDatabase"];
            if (String.IsNullOrWhiteSpace(this.DocumentDbDatabase))
            {
                throw new ArgumentException("A required AppSetting cannot be null or empty", "DocumentDbDatabase");
            }

            this.DocumentDbCollection = ConfigurationManager.AppSettings["DocumentDbCollection"];
            if (String.IsNullOrWhiteSpace(this.DocumentDbCollection))
            {
                throw new ArgumentException("A required AppSetting cannot be null or empty", "DocumentDbCollection");
            }

            // Create a new instance of the DocumentClient
            documentClient = new DocumentClient(new Uri(this.DocumentDbEndPointUrl), this.DocumentDbAuthorizationKey);

            // Check to verify if database already exists
            Database database = documentClient.CreateDatabaseQuery().
                Where(db => db.Id == this.DocumentDbDatabase).AsEnumerable().FirstOrDefault();

            if (database == null)
            {
                Context.Logger.Info("Creating a new DocumentDb database: {0}", this.DocumentDbDatabase);
                // Create a database
                var task = documentClient.CreateDatabaseAsync(
                    new Database
                    {
                        Id = this.DocumentDbDatabase
                    });

                task.Wait();
                database = task.Result;
            }
            else
            {
                Context.Logger.Info("Found an existing DocumentDb database: {0}", database.Id);
            }

            // Check to verify a document collection already exists
            documentCollection = documentClient.CreateDocumentCollectionQuery(database.CollectionsLink).
                Where(c => c.Id == this.DocumentDbCollection).AsEnumerable().FirstOrDefault();

            if (documentCollection == null)
            {
                Context.Logger.Info("Creating a new DocumentDb collection: {0}", this.DocumentDbCollection);
                // Create a document collection
                var task = documentClient.CreateDocumentCollectionAsync(database.CollectionsLink,
                    new DocumentCollection
                    {
                        Id = this.DocumentDbCollection,
                    });

                task.Wait();
                documentCollection = task.Result;
            }
            else
            {
                Context.Logger.Info("Found an existing DocumentDb collection: {0}", documentCollection.Id);
            }
        }
        /// <summary>
        /// Initialize the DocumentDb settings and connections
        /// </summary>
        public void InitializeDocumentDb()
        {
            this.DocumentDbEndPointUrl = ConfigurationManager.AppSettings["DocumentDbEndPointUrl"];
            if (String.IsNullOrWhiteSpace(this.DocumentDbEndPointUrl))
            {
                throw new ArgumentException("A required AppSetting cannot be null or empty", "DocumentDbEndPointUrl");
            }

            this.DocumentDbAuthorizationKey = ConfigurationManager.AppSettings["DocumentDbAuthorizationKey"];
            if (String.IsNullOrWhiteSpace(this.DocumentDbAuthorizationKey))
            {
                throw new ArgumentException("A required AppSetting cannot be null or empty", "DocumentDbAuthorizationKey");
            }

            this.DocumentDbDatabase = ConfigurationManager.AppSettings["DocumentDbDatabase"];
            if (String.IsNullOrWhiteSpace(this.DocumentDbDatabase))
            {
                throw new ArgumentException("A required AppSetting cannot be null or empty", "DocumentDbDatabase");
            }

            this.DocumentDbCollection = ConfigurationManager.AppSettings["DocumentDbCollection"];
            if (String.IsNullOrWhiteSpace(this.DocumentDbCollection))
            {
                throw new ArgumentException("A required AppSetting cannot be null or empty", "DocumentDbCollection");
            }

            this.DocumentDbLookupField = ConfigurationManager.AppSettings["DocumentDbLookupField"];
            if (String.IsNullOrWhiteSpace(this.DocumentDbLookupField))
            {
                throw new ArgumentException("A required AppSetting cannot be null or empty", "DocumentDbLookupField");
            }

            // Create a new instance of the DocumentClient
            documentClient = new DocumentClient(new Uri(this.DocumentDbEndPointUrl), this.DocumentDbAuthorizationKey);

            // Check to verify if database already exists
            Database database = documentClient.CreateDatabaseQuery().Where(db => db.Id == this.DocumentDbDatabase).AsEnumerable().FirstOrDefault();

            if (database == null)
            {
                throw new Exception("DocumentDb database '" + this.DocumentDbDatabase + "' does not exist.");
            }

            // Check to verify a document collection already exists
            documentCollection = documentClient.CreateDocumentCollectionQuery(database.CollectionsLink).Where(c => c.Id == this.DocumentDbCollection).AsEnumerable().FirstOrDefault();

            if (documentCollection == null)
            {
                throw new Exception("DocumentDb database collection '" + this.DocumentDbCollection + "' does not exist.");
            }
        }
Beispiel #27
0
        public static async Task<DocumentCollection> GetDC(DocumentClient client, Database database)
        {
            DocumentCollection documentCollection = client.CreateDocumentCollectionQuery(database.SelfLink)
                .Where(c => c.Id == "LMSCollection")
                .AsEnumerable()
                .FirstOrDefault()
                                                    ??
                                                    await client.CreateDocumentCollectionAsync(database.CollectionsLink,
                                                        new DocumentCollection
                                                        {
                                                            Id = "LMSCollection"
                                                        });

            Console.WriteLine(documentCollection.SelfLink);
            return documentCollection;
        }
        private static async Task GetStartedDemo()
        {
            // Create a new instance of the DocumentClient
            var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);

            // Check to verify a database with the id=FamilyRegistry does not exist
            Database database = client.CreateDatabaseQuery().Where(db => db.Id == "FamilyRegistry").AsEnumerable().FirstOrDefault();

            // If the database does not exist, create a new database
            if (database == null)
            {
                database = await client.CreateDatabaseAsync(
                    new Database
                    {
                        Id = "FamilyRegistry"
                    });

                WriteMessage("Created dbs/FamilyRegistry");
            }

            // Check to verify a document collection with the id=FamilyCollection does not exist
            DocumentCollection documentCollection = client.CreateDocumentCollectionQuery("dbs/" + database.Id).Where(c => c.Id == "FamilyCollection").AsEnumerable().FirstOrDefault();

            // If the document collection does not exist, create a new collection
            if (documentCollection == null)
            {
                documentCollection = await client.CreateDocumentCollectionAsync("dbs/" + database.Id,
                    new DocumentCollection
                    {
                        Id = "FamilyCollection"
                    });

                WriteMessage("Created dbs/FamilyRegistry/colls/FamilyCollection");
            }

            // Check to verify a document with the id=AndersenFamily does not exist
            Document document = client.CreateDocumentQuery("dbs/" + database.Id + "/colls/" + documentCollection.Id).Where(d => d.Id == "AndersenFamily").AsEnumerable().FirstOrDefault();

            // If the document does not exist, create a new document
            if (document == null)
            {
                // Create the Andersen Family document
                Family andersonFamily = new Family
                {
                    Id = "AndersenFamily",
                    LastName = "Andersen",
                    Parents = new Parent[] {
                        new Parent { FirstName = "Thomas" },
                        new Parent { FirstName = "Mary Kay"}
                    },
                    Children = new Child[] {
                        new Child
                        { 
                            FirstName = "Henriette Thaulow", 
                            Gender = "female", 
                            Grade = 5, 
                            Pets = new Pet[] {
                                new Pet { GivenName = "Fluffy" } 
                            }
                        } 
                    },
                    Address = new Address { State = "WA", County = "King", City = "Seattle" },
                    IsRegistered = true
                };

                // id based routing for the first argument, "dbs/FamilyRegistry/colls/FamilyCollection"
                await client.CreateDocumentAsync("dbs/" + database.Id + "/colls/" + documentCollection.Id, andersonFamily);

                WriteMessage("Created dbs/FamilyRegistry/colls/FamilyCollection/docs/AndersenFamily");
            }

            // Check to verify a document with the id=AndersenFamily does not exist
            document = client.CreateDocumentQuery("dbs/" + database.Id + "/colls/" + documentCollection.Id).Where(d => d.Id == "WakefieldFamily").AsEnumerable().FirstOrDefault();

            if (document == null)
            {
                // Create the WakeField document
                Family wakefieldFamily = new Family
                {
                    Id = "WakefieldFamily",
                    Parents = new Parent[] {
                        new Parent { FamilyName= "Wakefield", FirstName= "Robin" },
                        new Parent { FamilyName= "Miller", FirstName= "Ben" }
                    },
                    Children = new Child[] {
                        new Child {
                            FamilyName= "Merriam", 
                            FirstName= "Jesse", 
                            Gender= "female", 
                            Grade= 8,
                            Pets= new Pet[] {
                                new Pet { GivenName= "Goofy" },
                                new Pet { GivenName= "Shadow" }
                            }
                        },
                        new Child {
                            FamilyName= "Miller", 
                            FirstName= "Lisa", 
                            Gender= "female", 
                            Grade= 1
                        }
                    },
                    Address = new Address { State = "NY", County = "Manhattan", City = "NY" },
                    IsRegistered = false
                };

                // id based routing for the first argument, "dbs/FamilyRegistry/colls/FamilyCollection"
                await client.CreateDocumentAsync("dbs/" + database.Id + "/colls/" + documentCollection.Id, wakefieldFamily);

                WriteMessage("Created dbs/FamilyRegistry/colls/FamilyCollection/docs/WakefieldFamily");
            }

            // Query the documents using DocumentDB SQL for the Andersen family
            var families = client.CreateDocumentQuery("dbs/" + database.Id + "/colls/" + documentCollection.Id,
                "SELECT * " +
                "FROM Families f " +
                "WHERE f.id = \"AndersenFamily\"");

            foreach (var family in families)
            {
                Console.WriteLine("\tRead {0} from SQL", family);
            }

            // Query the documents using LINQ for the Andersen family
            families =
                from f in client.CreateDocumentQuery("dbs/" + database.Id + "/colls/" + documentCollection.Id)
                where f.Id == "AndersenFamily"
                select f;

            foreach (var family in families)
            {
                Console.WriteLine("Read {0} from LINQ", family);
            }

            // Query the documents using LINQ lambdas for the Andersen family
            families = client.CreateDocumentQuery("dbs/" + database.Id + "/colls/" + documentCollection.Id)
                .Where(f => f.Id == "AndersenFamily")
                .Select(f => f);

            foreach (var family in families)
            {
                Console.WriteLine("\tRead {0} from LINQ query", family);
            }

            // Clean up/delete the database and client
            await client.DeleteDatabaseAsync("dbs/" + database.Id);
            client.Dispose();
        }
 /// <summary>
 /// Internal Member to get or return a Azure DocumentDB Collection
 /// </summary>
 /// <param name="client"></param>
 /// <param name="dbLink"></param>
 /// <param name="id"></param>
 /// <returns></returns>
 private async Task<DocumentCollection> GetOrCreateCollectionAsync(DocumentClient client, string dbLink, string id)
 {
     DocumentCollection collection = client.CreateDocumentCollectionQuery(dbLink).Where(c => c.Id == id).ToArray().FirstOrDefault();
     if (collection == null)
     {
         collection = await client.CreateDocumentCollectionAsync(dbLink, new DocumentCollection { Id = id });
     }
     return collection;
 }
Beispiel #30
0
 /// <summary>
 /// Internal Member to get or return a Azure DocumentDB Collection
 /// </summary>
 /// <param name="client"></param>
 /// <param name="dbLink"></param>
 /// <param name="id"></param>
 /// <returns></returns>
 private async Task<DocumentCollection> GetOrCreateCollectionAsync(DocumentClient client, string dbLink, string id)
 {
     DocumentCollection collection = client.CreateDocumentCollectionQuery(dbLink).Where(c => c.Id == id).ToArray().FirstOrDefault();
     if (collection == null)
     {
         collection = await client.CreateDocumentCollectionAsync(dbLink, new DocumentCollection { Id = id });
        Log.Info("AzureDocDbRequestManager.GetOrCreateCollectionAsync", "Created Document DB Collection {0}", dbLink);
     }
     return collection;
 }
Beispiel #31
0
        private static async Task UpdateDc(DocumentCollection oldDc, DocumentClient client,
            Database database, DocumentCollection origin)
        {
            var res = await client.ReadDocumentCollectionAsync(oldDc.SelfLink);
            var size = res.CollectionSizeUsage;
            var totalSize = res.CollectionSizeQuota;
            if (size > totalSize*2/100000)
            {
                var ds =
                    from d in client.CreateDocumentQuery<PostMessage>(oldDc.DocumentsLink)
                    where d.Type == "Post"
                    select d;

                Hashtable hs = new Hashtable();
                var n = ds.ToList().Count;
                foreach (var d in ds)
                {
                    if (!hs.ContainsKey(d.Path.District))
                    {
                        hs.Add(d.Path.District, 1);
                    }
                    else
                    {
                        hs[d.Path.District] = (int) hs[d.Path.District] + 1;
                    }
                }
                Hashtable newList = new Hashtable();
                Hashtable oldList = new Hashtable();

                foreach (DictionaryEntry h in hs)
                {
                    var c = 0;
                    foreach (DictionaryEntry hh in newList)
                    {
                        c = c + (int) hh.Value;
                    }
                    if (c < n*0.45 && (c + (int) h.Value < n*0.55))
                    {
                        newList.Add(h.Key, h.Value);
                    }
                    else
                    {
                        oldList.Add(h.Key, h.Value);
                    }
                }


                if (newList.Count > 0)
                {
                    //create new collection
                    /*var t = (long) (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
                  DocumentCollection newDc=await client.CreateDocumentCollectionAsync(database.CollectionsLink,
                                                        new DocumentCollection
                                                        {
                                                            Id = "LMSCollection"+t
                                                        });*/

                    //search collection
                    DocumentCollection newDc = client.CreateDocumentCollectionQuery(database.SelfLink)
                        .Where(c => c.Id == "LMSCollection1444075919174")
                        .AsEnumerable()
                        .FirstOrDefault();

                    await SaveDisList(newList, oldList, origin, oldDc, newDc, client);

                    await TransferDc(oldDc, newDc, client, database, newList);
                }
            }
        }