Beispiel #1
1
 public DocumentClient GetDocumentClient()
 {
     if (_documentClient != null) return _documentClient;
     _documentClient = new DocumentClient(new Uri(_endpointUrl), _authorizationKey);
     _documentClient.OpenAsync().Wait();
     return _documentClient;
 }
        /// <summary>
        /// Tests the DocumentDB connection.
        /// </summary>
        /// <param name="connectionString">DocumentDB connection string to use to connect to the account.</param>
        /// <param name="connectionMode">DocumentDB connectio mode to use when testing the connection.</param>
        /// <returns>Task that represents asynchronous connection operation.</returns>
        public async Task TestConnection(string connectionString, DocumentDbConnectionMode? connectionMode)
        {
            if (String.IsNullOrEmpty(connectionString))
                throw Errors.ConnectionStringMissing();

            var parsed = DocumentDbConnectionStringBuilder.Parse(connectionString);

            if (String.IsNullOrEmpty(parsed.AccountEndpoint))
                throw Errors.AccountEndpointMissing();

            if (String.IsNullOrEmpty(parsed.AccountKey))
                throw Errors.AccountKeyMissing();

            if (String.IsNullOrEmpty(parsed.Database))
                throw Errors.DatabaseNameMissing();

            var connectionPolicy =  DocumentDbClientHelper.ApplyConnectionMode(new ConnectionPolicy(), connectionMode);

            using (var client = new DocumentClient(new Uri(parsed.AccountEndpoint), parsed.AccountKey, connectionPolicy))
            {
                await client.OpenAsync();
            }
        }
Beispiel #3
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;
        }