public ContainerResponse Delete(Container container) { var response = new ContainerResponse(); using (_session) { try { string id = string.Format("containers/{0}", container.Id); var dbcontainer = _session.Load<Container>(id); if (dbcontainer != null) { _session.Delete(dbcontainer); _session.SaveChanges(); } } catch (Exception ex) { Debug.WriteLine("Delete(Container): {0}", ex.Message); throw; } } return response; }
public ContainerResponse Post(Container container) { var response = new ContainerResponse(); using (_session) { try { _session.Store(container); _session.SaveChanges(); response.Id = container.Id; } catch (Exception ex) { Debug.WriteLine("Post(Container): {0}", ex.Message); throw; } } return response; }
public async Task ReadReplaceThroughputResponseTests() { int toStreamCount = 0; int fromStreamCount = 0; CosmosSerializerHelper mockJsonSerializer = new CosmosSerializerHelper( null, (x) => fromStreamCount++, (x) => toStreamCount++); //Create a new cosmos client with the mocked cosmos json serializer CosmosClient client = TestCommon.CreateCosmosClient( (cosmosClientBuilder) => cosmosClientBuilder.WithCustomSerializer(mockJsonSerializer)); string databaseId = Guid.NewGuid().ToString(); int throughput = 10000; DatabaseResponse createResponse = await client.CreateDatabaseAsync(databaseId, throughput, null); Assert.AreEqual(HttpStatusCode.Created, createResponse.StatusCode); Cosmos.Database cosmosDatabase = createResponse; ThroughputResponse readThroughputResponse = await cosmosDatabase.ReadThroughputAsync(new RequestOptions()); Assert.IsNotNull(readThroughputResponse); Assert.IsNotNull(readThroughputResponse.Resource); Assert.IsNotNull(readThroughputResponse.MinThroughput); Assert.IsNotNull(readThroughputResponse.Resource.Throughput); Assert.AreEqual(throughput, readThroughputResponse.Resource.Throughput.Value); // Implicit ThroughputProperties throughputProperties = await cosmosDatabase.ReadThroughputAsync(new RequestOptions()); Assert.IsNotNull(throughputProperties); Assert.AreEqual(throughput, throughputProperties.Throughput); // Simple API int?readThroughput = await cosmosDatabase.ReadThroughputAsync(); Assert.AreEqual(throughput, readThroughput); // Database must have a container before it can be scaled string containerId = Guid.NewGuid().ToString(); string partitionPath = "/users"; ContainerResponse containerResponse = await cosmosDatabase.CreateContainerAsync(containerId, partitionPath, throughput : null); Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode); ThroughputResponse replaceThroughputResponse = await cosmosDatabase.ReplaceThroughputAsync(readThroughputResponse.Resource.Throughput.Value + 1000); Assert.IsNotNull(replaceThroughputResponse); Assert.IsNotNull(replaceThroughputResponse.Resource); Assert.AreEqual(readThroughputResponse.Resource.Throughput.Value + 1000, replaceThroughputResponse.Resource.Throughput.Value); await cosmosDatabase.DeleteAsync(); Database databaseNoThroughput = await client.CreateDatabaseAsync(Guid.NewGuid().ToString(), throughput : null); try { ThroughputResponse throughputResponse = await databaseNoThroughput.ReadThroughputAsync(new RequestOptions()); Assert.Fail("Should through not found exception as throughput is not configured"); } catch (CosmosException exception) { Assert.AreEqual(HttpStatusCode.NotFound, exception.StatusCode); } try { ThroughputResponse throughputResponse = await databaseNoThroughput.ReplaceThroughputAsync(2000, new RequestOptions()); Assert.Fail("Should through not found exception as throughput is not configured"); } catch (CosmosException exception) { Assert.AreEqual(HttpStatusCode.NotFound, exception.StatusCode); } int?dbThroughput = await databaseNoThroughput.ReadThroughputAsync(); Assert.IsNull(dbThroughput); Assert.AreEqual(0, toStreamCount, "Custom serializer to stream should not be used for offer operations"); Assert.AreEqual(0, fromStreamCount, "Custom serializer from stream should not be used for offer operations"); await databaseNoThroughput.DeleteAsync(); }
public async Task TimeToLivePropertyPath() { string containerName = Guid.NewGuid().ToString(); string partitionKeyPath = "/user"; int timeToLivetimeToLiveInSeconds = 10; CosmosContainerSettings setting = new CosmosContainerSettings() { Id = containerName, PartitionKey = new PartitionKeyDefinition() { Paths = new Collection <string> { partitionKeyPath }, Kind = PartitionKind.Hash }, TimeToLivePropertyPath = "/creationDate", }; ContainerResponse containerResponse = null; try { containerResponse = await this.cosmosDatabase.CreateContainerIfNotExistsAsync(setting); Assert.Fail("CreateColleciton with TtlPropertyPath and with no DefaultTimeToLive should have failed."); } catch (CosmosException exeption) { // expected because DefaultTimeToLive was not specified Assert.AreEqual(HttpStatusCode.BadRequest, exeption.StatusCode); } // Verify the container content. setting.DefaultTimeToLive = timeToLivetimeToLiveInSeconds; containerResponse = await this.cosmosDatabase.CreateContainerIfNotExistsAsync(setting); CosmosContainer cosmosContainer = containerResponse; Assert.AreEqual(timeToLivetimeToLiveInSeconds, containerResponse.Resource.DefaultTimeToLive); Assert.AreEqual("/creationDate", containerResponse.Resource.TimeToLivePropertyPath); //verify removing the ttl property path setting.TimeToLivePropertyPath = null; containerResponse = await cosmosContainer.ReplaceAsync(setting); cosmosContainer = containerResponse; Assert.AreEqual(timeToLivetimeToLiveInSeconds, containerResponse.Resource.DefaultTimeToLive); Assert.IsNull(containerResponse.Resource.TimeToLivePropertyPath); //adding back the ttl property path setting.TimeToLivePropertyPath = "/creationDate"; containerResponse = await cosmosContainer.ReplaceAsync(setting); cosmosContainer = containerResponse; Assert.AreEqual(containerResponse.Resource.TimeToLivePropertyPath, "/creationDate"); //Creating an item and reading before expiration var payload = new { id = "testId", user = "******", creationDate = ToEpoch(DateTime.UtcNow) }; ItemResponse <dynamic> createItemResponse = await cosmosContainer.CreateItemAsync <dynamic>(payload); Assert.IsNotNull(createItemResponse.Resource); Assert.AreEqual(createItemResponse.StatusCode, HttpStatusCode.Created); ItemResponse <dynamic> readItemResponse = await cosmosContainer.ReadItemAsync <dynamic>(new Cosmos.PartitionKey(payload.user), payload.id); Assert.IsNotNull(readItemResponse.Resource); Assert.AreEqual(readItemResponse.StatusCode, HttpStatusCode.OK); containerResponse = await cosmosContainer.DeleteAsync(); Assert.AreEqual(HttpStatusCode.NoContent, containerResponse.StatusCode); }
/// <summary> /// Run samples for Order By queries. /// </summary> /// <returns>a Task object.</returns> private async Task <RunSummary> ExecuteAsync(BenchmarkConfig config) { using (CosmosClient cosmosClient = config.CreateCosmosClient(config.Key)) { Microsoft.Azure.Cosmos.Database database = cosmosClient.GetDatabase(config.Database); if (config.CleanupOnStart) { await database.DeleteStreamAsync(); } ContainerResponse containerResponse = await Program.CreatePartitionedContainerAsync(config, cosmosClient); Container container = containerResponse; int?currentContainerThroughput = await container.ReadThroughputAsync(); if (!currentContainerThroughput.HasValue) { // Container throughput is not configured. It is shared database throughput ThroughputResponse throughputResponse = await database.ReadThroughputAsync(requestOptions : null); throw new InvalidOperationException($"Using database {config.Database} with {throughputResponse.Resource.Throughput} RU/s. " + $"Container {config.Container} must have a configured throughput."); } Console.WriteLine($"Using container {config.Container} with {currentContainerThroughput} RU/s"); int taskCount = config.GetTaskCount(currentContainerThroughput.Value); Console.WriteLine("Starting Inserts with {0} tasks", taskCount); Console.WriteLine(); string partitionKeyPath = containerResponse.Resource.PartitionKeyPath; int opsPerTask = config.ItemCount / taskCount; // TBD: 2 clients SxS some overhead RunSummary runSummary; using (Microsoft.Azure.Documents.Client.DocumentClient documentClient = config.CreateDocumentClient(config.Key)) { Func <IBenchmarkOperation> benchmarkOperationFactory = this.GetBenchmarkFactory( config, partitionKeyPath, cosmosClient, documentClient); if (config.DisableCoreSdkLogging) { // Do it after client initialization (HACK) Program.ClearCoreSdkListeners(); } IExecutionStrategy execution = IExecutionStrategy.StartNew(config, benchmarkOperationFactory); runSummary = await execution.ExecuteAsync(taskCount, opsPerTask, config.TraceFailures, 0.01); } if (config.CleanupOnFinish) { Console.WriteLine($"Deleting Database {config.Database}"); await database.DeleteStreamAsync(); } runSummary.WorkloadType = config.WorkloadType; runSummary.id = $"{DateTime.UtcNow:yyyy-MM-dd:HH-mm}-{config.CommitId}"; runSummary.Commit = config.CommitId; runSummary.CommitDate = config.CommitDate; runSummary.CommitTime = config.CommitTime; runSummary.Date = DateTime.UtcNow.ToString("yyyy-MM-dd"); runSummary.Time = DateTime.UtcNow.ToString("HH-mm"); runSummary.BranchName = config.BranchName; runSummary.TotalOps = config.ItemCount; runSummary.Concurrency = taskCount; runSummary.Database = config.Database; runSummary.Container = config.Container; runSummary.AccountName = config.EndPoint; runSummary.pk = config.ResultsPartitionKeyValue; runSummary.MaxTcpConnectionsPerEndpoint = config.MaxTcpConnectionsPerEndpoint; runSummary.MaxRequestsPerTcpConnection = config.MaxRequestsPerTcpConnection; string consistencyLevel = config.ConsistencyLevel; if (string.IsNullOrWhiteSpace(consistencyLevel)) { AccountProperties accountProperties = await cosmosClient.ReadAccountAsync(); consistencyLevel = accountProperties.Consistency.DefaultConsistencyLevel.ToString(); } runSummary.ConsistencyLevel = consistencyLevel; if (config.PublishResults) { runSummary.Diagnostics = CosmosDiagnosticsLogger.GetDiagnostics(); await this.PublishResults( config, runSummary, cosmosClient); } return(runSummary); } }
public async Task CreateContainerIfNotExistsAsyncTest() { string containerName = Guid.NewGuid().ToString(); string partitionKeyPath1 = "/users"; ContainerProperties settings = new ContainerProperties(containerName, partitionKeyPath1); ContainerResponse containerResponse = await this.cosmosDatabase.CreateContainerIfNotExistsAsync(settings); Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode); Assert.AreEqual(containerName, containerResponse.Resource.Id); Assert.AreEqual(partitionKeyPath1, containerResponse.Resource.PartitionKey.Paths.First()); //Creating container with same partition key path containerResponse = await this.cosmosDatabase.CreateContainerIfNotExistsAsync(settings); Assert.AreEqual(HttpStatusCode.OK, containerResponse.StatusCode); Assert.AreEqual(containerName, containerResponse.Resource.Id); Assert.AreEqual(partitionKeyPath1, containerResponse.Resource.PartitionKey.Paths.First()); //Creating container with different partition key path string partitionKeyPath2 = "/users2"; try { settings = new ContainerProperties(containerName, partitionKeyPath2); containerResponse = await this.cosmosDatabase.CreateContainerIfNotExistsAsync(settings); Assert.Fail("Should through ArgumentException on partition key path"); } catch (ArgumentException ex) { Assert.AreEqual(nameof(settings.PartitionKey), ex.ParamName); Assert.IsTrue(ex.Message.Contains(string.Format( ClientResources.PartitionKeyPathConflict, partitionKeyPath2, containerName, partitionKeyPath1))); } containerResponse = await containerResponse.Container.DeleteContainerAsync(); Assert.AreEqual(HttpStatusCode.NoContent, containerResponse.StatusCode); //Creating existing container with partition key having value for SystemKey //https://github.com/Azure/azure-cosmos-dotnet-v3/issues/623 string v2ContainerName = "V2Container"; PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition(); partitionKeyDefinition.Paths.Add("/test"); partitionKeyDefinition.IsSystemKey = false; ContainerProperties containerPropertiesWithSystemKey = new ContainerProperties() { Id = v2ContainerName, PartitionKey = partitionKeyDefinition, }; await this.cosmosDatabase.CreateContainerAsync(containerPropertiesWithSystemKey); ContainerProperties containerProperties = new ContainerProperties(v2ContainerName, "/test"); containerResponse = await this.cosmosDatabase.CreateContainerIfNotExistsAsync(containerProperties); Assert.AreEqual(HttpStatusCode.OK, containerResponse.StatusCode); Assert.AreEqual(v2ContainerName, containerResponse.Resource.Id); Assert.AreEqual("/test", containerResponse.Resource.PartitionKey.Paths.First()); containerResponse = await containerResponse.Container.DeleteContainerAsync(); Assert.AreEqual(HttpStatusCode.NoContent, containerResponse.StatusCode); containerPropertiesWithSystemKey.PartitionKey.IsSystemKey = true; await this.cosmosDatabase.CreateContainerAsync(containerPropertiesWithSystemKey); containerProperties = new ContainerProperties(v2ContainerName, "/test"); containerResponse = await this.cosmosDatabase.CreateContainerIfNotExistsAsync(containerProperties); Assert.AreEqual(HttpStatusCode.OK, containerResponse.StatusCode); Assert.AreEqual(v2ContainerName, containerResponse.Resource.Id); Assert.AreEqual("/test", containerResponse.Resource.PartitionKey.Paths.First()); containerResponse = await containerResponse.Container.DeleteContainerAsync(); Assert.AreEqual(HttpStatusCode.NoContent, containerResponse.StatusCode); }
public async Task ChangeFeed_FeedRange_FromV0Token() { ContainerResponse largerContainer = await this.database.CreateContainerAsync( new ContainerProperties(id : Guid.NewGuid().ToString(), partitionKeyPath : "/pk"), throughput : 20000, cancellationToken : this.cancellationToken); ContainerInternal container = (ContainerInlineCore)largerContainer; int expected = 100; int count = 0; await this.CreateRandomItems((ContainerCore)container, expected, randomPartitionKey : true); IReadOnlyList <FeedRange> feedRanges = await container.GetFeedRangesAsync(); List <string> continuations = new List <string>(); // First do one request to construct the old model information based on Etag foreach (FeedRange feedRange in feedRanges) { IEnumerable <string> pkRangeIds = await container.GetPartitionKeyRangesAsync(feedRange); ChangeFeedRequestOptions requestOptions = new ChangeFeedRequestOptions() { PageSizeHint = 1 }; ChangeFeedIteratorCore feedIterator = container.GetChangeFeedStreamIterator( changeFeedStartFrom: ChangeFeedStartFrom.Beginning(feedRange), changeFeedMode: ChangeFeedMode.Incremental, changeFeedRequestOptions: requestOptions) as ChangeFeedIteratorCore; ResponseMessage firstResponse = await feedIterator.ReadNextAsync(); // Construct the continuation's range, using PKRangeId + ETag List <dynamic> ct = new List <dynamic>() { new { FeedRange = new { type = "Physical Partition Key Range Id", value = pkRangeIds.First() }, State = new { type = "continuation", value = JObject.Parse(firstResponse.ContinuationToken)["Continuation"][0]["State"]["value"].ToString() } } }; if (firstResponse.Content != null) { Collection <ToDoActivity> response = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(firstResponse.Content).Data; count += response.Count; } // Extract Etag and manually construct the continuation dynamic oldContinuation = new { V = 2, Rid = await container.GetCachedRIDAsync(cancellationToken : this.cancellationToken), Continuation = ct }; continuations.Add(JsonConvert.SerializeObject(oldContinuation)); } // Now start the new iterators with the constructed continuations from migration foreach (string continuation in continuations) { ChangeFeedRequestOptions requestOptions = new ChangeFeedRequestOptions() { PageSizeHint = 100, }; ChangeFeedIteratorCore feedIterator = container.GetChangeFeedStreamIterator( changeFeedStartFrom: ChangeFeedStartFrom.ContinuationToken(continuation), changeFeedMode: ChangeFeedMode.Incremental, changeFeedRequestOptions: requestOptions) as ChangeFeedIteratorCore; ResponseMessage firstResponse = await feedIterator.ReadNextAsync(); if (firstResponse.IsSuccessStatusCode) { Collection <ToDoActivity> response = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(firstResponse.Content).Data; count += response.Count; string migratedContinuation = firstResponse.ContinuationToken; TryCatch <CosmosElement> monadicParsedToken = CosmosElement.Monadic.Parse(migratedContinuation); Assert.IsFalse(monadicParsedToken.Failed); TryCatch <VersionedAndRidCheckedCompositeToken> monadicVersionedToken = VersionedAndRidCheckedCompositeToken .MonadicCreateFromCosmosElement(monadicParsedToken.Result); Assert.IsFalse(monadicVersionedToken.Failed); VersionedAndRidCheckedCompositeToken versionedAndRidCheckedCompositeToken = monadicVersionedToken.Result; Assert.AreEqual(VersionedAndRidCheckedCompositeToken.Version.V2, versionedAndRidCheckedCompositeToken.VersionNumber); } } Assert.AreEqual(expected, count); }
public async Task ParallelizeQueryThroughTokens_OfT() { ContainerInternal container = null; try { // Create a container large enough to have at least 2 partitions ContainerResponse containerResponse = await this.database.CreateContainerAsync( id : Guid.NewGuid().ToString(), partitionKeyPath : "/id", throughput : 15000); container = (ContainerInlineCore)containerResponse; List <string> generatedIds = Enumerable.Range(0, 1000).Select(n => $"BasicItem{n}").ToList(); foreach (string id in generatedIds) { string item = $@" {{ ""id"": ""{id}"" }}"; using (ResponseMessage createResponse = await container.CreateItemStreamAsync( QueryFeedRangeTests.GenerateStreamFromString(item), new Cosmos.PartitionKey(id))) { Assert.IsTrue(createResponse.IsSuccessStatusCode); } } IReadOnlyList <FeedRange> feedTokens = await container.GetFeedRangesAsync(); Assert.IsTrue(feedTokens.Count > 1, " RUs of the container needs to be increased to ensure at least 2 partitions."); List <Task <List <string> > > tasks = feedTokens.Select(async feedToken => { List <string> results = new List <string>(); FeedIterator <ToDoActivity> feedIterator = container.GetItemQueryIterator <ToDoActivity>(queryDefinition: new QueryDefinition("select * from T where STARTSWITH(T.id, \"BasicItem\")"), feedRange: feedToken, requestOptions: new QueryRequestOptions() { MaxItemCount = 10 }); string continuation = null; while (feedIterator.HasMoreResults) { FeedResponse <ToDoActivity> response = await feedIterator.ReadNextAsync(); foreach (ToDoActivity toDoActivity in response) { results.Add(toDoActivity.id); } continuation = response.ContinuationToken; break; } feedIterator = container.GetItemQueryIterator <ToDoActivity>(queryDefinition: new QueryDefinition("select * from T where STARTSWITH(T.id, \"BasicItem\")"), feedRange: feedToken, continuationToken: continuation, requestOptions: new QueryRequestOptions() { MaxItemCount = 10 }); while (feedIterator.HasMoreResults) { FeedResponse <ToDoActivity> response = await feedIterator.ReadNextAsync(); foreach (ToDoActivity toDoActivity in response) { results.Add(toDoActivity.id); } } return(results); }).ToList(); await Task.WhenAll(tasks); CollectionAssert.AreEquivalent(generatedIds, tasks.SelectMany(t => t.Result).ToList()); } finally { await container?.DeleteContainerAsync(); } }
// Async main requires c# 7.1 which is set in the csproj with the LangVersion attribute // <Main> public static async Task Main(string[] args) { IConfigurationRoot configuration = new ConfigurationBuilder() .AddJsonFile("appSettings.json") .Build(); string endpoint = configuration["EndPointUrl"]; if (string.IsNullOrEmpty(endpoint)) { throw new ArgumentNullException("Please specify a valid endpoint in the appSettings.json"); } string authKey = configuration["AuthorizationKey"]; if (string.IsNullOrEmpty(authKey) || string.Equals(authKey, "Super secret key")) { throw new ArgumentException("Please specify a valid AuthorizationKey in the appSettings.json"); } // Connecting to Emulator. Change if you want a live account CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder(endpoint, authKey); // Declare a JSON schema to use with the schema validation handler var myContainerSchema = JSchema.Parse(@"{ 'type': 'object', 'properties': { 'name': {'type': 'string'} }, 'required': ['name'] }"); cosmosClientBuilder.AddCustomHandlers( new LoggingHandler(), new ConcurrencyHandler(), new ThrottlingHandler(), new SchemaValidationHandler((database: "mydb", container: "mycoll2", schema: myContainerSchema)) ); CosmosClient client = cosmosClientBuilder.Build(); DatabaseResponse databaseResponse = await client.CreateDatabaseIfNotExistsAsync("mydb"); Database database = databaseResponse.Database; ContainerResponse containerResponse = await database.CreateContainerIfNotExistsAsync("mycoll", "/id"); Container container = containerResponse.Container; Item item = new Item() { Id = Guid.NewGuid().ToString(), Name = "Test Item", Description = "Some random test item", Completed = false }; // Create await container.CreateItemAsync <Item>(item, new PartitionKey(item.Id)); item.Completed = true; // Replace await container.ReplaceItemAsync <Item>(item, item.Id, new PartitionKey(item.Id)); // Querying List <Item> results = new List <Item>(); using (FeedIterator <Item> query = container.GetItemQueryIterator <Item>(new QueryDefinition("SELECT * FROM c"), requestOptions: new QueryRequestOptions() { MaxConcurrency = 1 })) { while (query.HasMoreResults) { FeedResponse <Item> response = await query.ReadNextAsync(); results.AddRange(response.ToList()); } } // Read Item ItemResponse <Item> cosmosItemResponse = await container.ReadItemAsync <Item>(item.Id, new PartitionKey(item.Id)); ItemRequestOptions itemRequestOptions = new ItemRequestOptions() { IfMatchEtag = cosmosItemResponse.ETag }; // Concurrency List <Task <ItemResponse <Item> > > tasks = new List <Task <ItemResponse <Item> > > { UpdateItemForConcurrency(container, itemRequestOptions, item), UpdateItemForConcurrency(container, itemRequestOptions, item) }; try { await Task.WhenAll(tasks); } catch (CosmosException ex) { // Verify that our custom handler caught the scenario Debug.Assert(999.Equals(ex.SubStatusCode)); } // Delete await container.DeleteItemAsync <Item>(item.Id, new PartitionKey(item.Id)); // Schema validation containerResponse = await database.CreateContainerIfNotExistsAsync("mycoll2", "/id"); container = containerResponse.Container; // Insert an item with invalid schema var writeSucceeded = true; try { await container.CreateItemAsync(new { id = "12345" }); } catch (InvalidItemSchemaException) { writeSucceeded = false; } Debug.Assert(!writeSucceeded); // Insert an item with valid schema try { await container.CreateItemAsync(new { id = "12345", name = "Youri" }); writeSucceeded = true; } catch (InvalidItemSchemaException) { writeSucceeded = false; } Debug.Assert(writeSucceeded); // Update an item with invalid schema try { await container.ReplaceItemAsync(new { id = "12345" }, "12345"); writeSucceeded = true; } catch (InvalidItemSchemaException) { writeSucceeded = false; } Debug.Assert(!writeSucceeded); // Update an item with valid schema try { await container.ReplaceItemAsync(new { id = "12345", name = "Vladimir" }, "12345"); writeSucceeded = true; } catch (InvalidItemSchemaException) { writeSucceeded = false; } Debug.Assert(writeSucceeded); }
public async Task ChangeFeed_FeedRange_FromV2SDK() { ContainerResponse largerContainer = await this.database.CreateContainerAsync( new ContainerProperties(id : Guid.NewGuid().ToString(), partitionKeyPath : "/status"), throughput : 20000, cancellationToken : this.cancellationToken); ContainerCore container = (ContainerInlineCore)largerContainer; int expected = 100; int count = 0; await this.CreateRandomItems(container, expected, randomPartitionKey : true); IReadOnlyList <FeedRange> feedRanges = await container.GetFeedRangesAsync(); List <string> continuations = new List <string>(); // First do one request to construct the old model information based on Etag foreach (FeedRange feedRange in feedRanges) { IEnumerable <string> pkRangeIds = await container.GetPartitionKeyRangesAsync(feedRange); ChangeFeedRequestOptions requestOptions = new ChangeFeedRequestOptions() { StartTime = DateTime.MinValue.ToUniversalTime(), MaxItemCount = 1 }; ChangeFeedIteratorCore feedIterator = container.GetChangeFeedStreamIterator(feedRange: feedRange, changeFeedRequestOptions: requestOptions) as ChangeFeedIteratorCore; ResponseMessage firstResponse = await feedIterator.ReadNextAsync(); if (firstResponse.IsSuccessStatusCode) { Collection <ToDoActivity> response = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(firstResponse.Content).Data; count += response.Count; } // Construct the continuation's range, using PKRangeId + ETag List <dynamic> ct = new List <dynamic>() { new { min = string.Empty, max = string.Empty, token = firstResponse.Headers.ETag } }; // Extract Etag and manually construct the continuation dynamic oldContinuation = new { V = 0, PKRangeId = pkRangeIds.First(), Continuation = ct }; continuations.Add(JsonConvert.SerializeObject(oldContinuation)); } // Now start the new iterators with the constructed continuations from migration foreach (string continuation in continuations) { ChangeFeedRequestOptions requestOptions = new ChangeFeedRequestOptions() { MaxItemCount = 100 }; ChangeFeedIteratorCore feedIterator = container.GetChangeFeedStreamIterator(continuationToken: continuation, changeFeedRequestOptions: requestOptions) as ChangeFeedIteratorCore; ResponseMessage firstResponse = await feedIterator.ReadNextAsync(); if (firstResponse.IsSuccessStatusCode) { Collection <ToDoActivity> response = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(firstResponse.Content).Data; count += response.Count; string migratedContinuation = firstResponse.ContinuationToken; Assert.IsTrue(FeedRangeContinuation.TryParse(migratedContinuation, out FeedRangeContinuation feedRangeContinuation)); Assert.IsTrue(feedRangeContinuation.FeedRange is FeedRangeEPK); } } Assert.AreEqual(expected, count); }
public async Task ReadFeedIteratorCore_CrossPartitionBiDirectional(bool useStatelessIteration) { ContainerInternal container = null; try { ContainerResponse containerResponse = await this.database.CreateContainerAsync( new ContainerProperties(id : Guid.NewGuid().ToString(), partitionKeyPath : "/id"), throughput : 50000, cancellationToken : this.cancellationToken); container = (ContainerInlineCore)containerResponse; // Create Items const int total = 30; QueryRequestOptions requestOptions = new QueryRequestOptions() { MaxItemCount = 10 }; List <string> items = new List <string>(); for (int i = 0; i < total; i++) { string item = $@" {{ ""id"": ""{i}"" }}"; using (ResponseMessage createResponse = await container.CreateItemStreamAsync( ReadFeedRangeTests.GenerateStreamFromString(item), new Cosmos.PartitionKey(i.ToString()))) { Assert.IsTrue(createResponse.IsSuccessStatusCode); } } string continuation = null; FeedIterator iter = container.GetItemQueryStreamIterator( continuationToken: continuation, requestOptions: requestOptions); int count = 0; List <string> forwardOrder = new List <string>(); while (iter.HasMoreResults) { if (useStatelessIteration) { iter = container.GetItemQueryStreamIterator( continuationToken: continuation, requestOptions: requestOptions); } using (ResponseMessage response = await iter.ReadNextAsync()) { Assert.IsNotNull(response); continuation = response.ContinuationToken; using (StreamReader reader = new StreamReader(response.Content)) { string json = await reader.ReadToEndAsync(); JArray documents = (JArray)JObject.Parse(json).SelectToken("Documents"); count += documents.Count; if (documents.Any()) { forwardOrder.Add(documents.First().SelectToken("id").ToString()); } } } } Assert.IsNotNull(forwardOrder); Assert.AreEqual(total, count); Assert.IsFalse(forwardOrder.Where(x => string.IsNullOrEmpty(x)).Any()); requestOptions.Properties = requestOptions.Properties = new Dictionary <string, object>() { { Documents.HttpConstants.HttpHeaders.EnumerationDirection, (byte)BinaryScanDirection.Reverse }, }; count = 0; List <string> reverseOrder = new List <string>(); continuation = null; iter = container .GetItemQueryStreamIterator(queryDefinition: null, continuationToken: continuation, requestOptions: requestOptions); while (iter.HasMoreResults) { if (useStatelessIteration) { iter = container .GetItemQueryStreamIterator(queryDefinition: null, continuationToken: continuation, requestOptions: requestOptions); } using (ResponseMessage response = await iter.ReadNextAsync()) { continuation = response.ContinuationToken; Assert.IsNotNull(response); using (StreamReader reader = new StreamReader(response.Content)) { string json = await reader.ReadToEndAsync(); JArray documents = (JArray)JObject.Parse(json).SelectToken("Documents"); count += documents.Count; if (documents.Any()) { reverseOrder.Add(documents.First().SelectToken("id").ToString()); } } } } Assert.IsNotNull(reverseOrder); Assert.AreEqual(total, count); forwardOrder.Reverse(); CollectionAssert.AreEqual(forwardOrder, reverseOrder); Assert.IsFalse(reverseOrder.Where(x => string.IsNullOrEmpty(x)).Any()); } finally { await container?.DeleteContainerStreamAsync(); } }
/// <summary> /// Creates a container in the [TangledServices.ServicePortal] database. /// </summary> /// <param name="database">The [TangledServices.ServicePortal] database object.</param> /// <param name="container">The container represented as a ResetContainerModel object.</param> /// <returns>ContainerResponse object</returns> private async Task <ContainerResponse> CreateContainer(SystemLookupItem group) { ContainerResponse containerResponse = await _adminManager.CreateContainer(group.CanonicalName, group.CanonicalName); return(containerResponse); }
public async Task PermissionTests(bool directMode) { CosmosClient client = directMode ? DirectCosmosClient : GatewayCosmosClient; Database database = client.GetDatabase(DatabaseId); List <string> createdPermissionIds = new List <string>(); List <string> createdContainerIds = new List <string>(); string userId = Guid.NewGuid().ToString(); UserCore user = null; try { UserResponse createUserResponse = await database.CreateUserAsync(userId); Assert.AreEqual(HttpStatusCode.Created, createUserResponse.StatusCode); user = (UserInlineCore)createUserResponse.User; ContainerResponse createContainerResponse = await database.CreateContainerIfNotExistsAsync(Guid.NewGuid().ToString(), partitionKeyPath : "/pk"); Container container = createContainerResponse.Container; PermissionResponse permissionResponse = await user.CreatePermissionAsync(new PermissionProperties("BasicQueryPermission1", PermissionMode.All, container)); createdContainerIds.Add(createContainerResponse.Container.Id); createdPermissionIds.Add(permissionResponse.Permission.Id); createContainerResponse = await database.CreateContainerIfNotExistsAsync(Guid.NewGuid().ToString(), partitionKeyPath : "/pk"); container = createContainerResponse.Container; permissionResponse = await user.CreatePermissionAsync(new PermissionProperties("BasicQueryPermission2", PermissionMode.All, container)); createdContainerIds.Add(createContainerResponse.Container.Id); createdPermissionIds.Add(permissionResponse.Permission.Id); createContainerResponse = await database.CreateContainerIfNotExistsAsync(Guid.NewGuid().ToString(), partitionKeyPath : "/pk"); container = createContainerResponse.Container; permissionResponse = await user.CreatePermissionAsync(new PermissionProperties("BasicQueryPermission3", PermissionMode.All, container)); createdContainerIds.Add(createContainerResponse.Container.Id); createdPermissionIds.Add(permissionResponse.Permission.Id); //Read All List <PermissionProperties> results = await this.ToListAsync( user.GetPermissionQueryStreamIterator, user.GetPermissionQueryIterator <PermissionProperties>, null, CosmosBasicQueryTests.RequestOptions ); CollectionAssert.IsSubsetOf(createdPermissionIds, results.Select(x => x.Id).ToList()); //Basic query List <PermissionProperties> queryResults = await this.ToListAsync( user.GetPermissionQueryStreamIterator, user.GetPermissionQueryIterator <PermissionProperties>, "select * from T where STARTSWITH(T.id, \"BasicQueryPermission\")", CosmosBasicQueryTests.RequestOptions ); CollectionAssert.AreEquivalent(createdPermissionIds, queryResults.Select(x => x.Id).ToList()); } finally { foreach (string id in createdPermissionIds) { await user.GetPermission(id).DeleteAsync(); } foreach (string id in createdContainerIds) { await database.GetContainer(id).DeleteContainerAsync(); } await user?.DeleteAsync(); } }
private async void GetInfoAboutBox(string dir_path) { try { ContainerResponse container = new ContainerResponse(); //извлечение данных контейнера из файла //using (FileStream fs = new FileStream(dir_path + "box_data.txt", FileMode.OpenOrCreate)) //{ // container = await System.Text.Json.JsonSerializer.DeserializeAsync<ContainerResponse>(fs); //} //пример чтения данных с файла string file_data_remember; using (FileStream file = new FileStream(dir_path + "box_data.txt", FileMode.Open, FileAccess.Read)) { // преобразуем строку в байты byte[] array = new byte[file.Length]; // считываем данные file.Read(array, 0, array.Length); // декодируем байты в строку file_data_remember = Encoding.Default.GetString(array); file.Close(); } container = JsonConvert.DeserializeObject <ContainerResponse>(file_data_remember); string name = container.Name;//!!!! //if(s_payment.Text == "Оплачено") //{ var myHttpClient = new HttpClient(); var uri = new Uri("http://iot.tmc-centert.ru/api/container/getbox?id=" + container.SmartBoxId); HttpResponseMessage response = await myHttpClient.GetAsync(uri); AuthApiData <BoxDataResponse> o_data = new AuthApiData <BoxDataResponse>(); string s_result; using (HttpContent responseContent = response.Content) { s_result = await responseContent.ReadAsStringAsync(); } o_data = JsonConvert.DeserializeObject <AuthApiData <BoxDataResponse> >(s_result); if (response.StatusCode == HttpStatusCode.OK) { if (o_data.Status == "0") { Toast.MakeText(this, o_data.Message, ToastLength.Long).Show(); BoxDataResponse exported_data = new BoxDataResponse(); exported_data = o_data.ResponseData; StaticBox.AddInfoBox(exported_data); //добавляем инфу о найденном контейнере //container_name.Text = exported_data.Name.ToString(); //container_name.Text = name; s_temperature.Text = exported_data.Temperature; s_light.Text = exported_data.Light.ToString(); s_humidity.Text = exported_data.Wetness; StaticBox.Longitude = exported_data.Longitude; StaticBox.Latitude = exported_data.Latitude; s_longitude.Text = StaticBox.Longitude.ToString(); s_latitude.Text = StaticBox.Latitude.ToString(); //coordinates lat lon s_weight.Text = "100.0"; //progressBar.Progress = 6; Text3.Text = "ПИН-код доступа отобразится после оплаты"; //status_view.Text = "6. Ожидание выгрузки"; if (s_payment.Text == "Оплачено") { s_pin_access_code.Text = (exported_data.Code == null) ? "0000" : "1324";// !!!! } else { s_pin_access_code.Text = "****"; } if (exported_data.IsOpenedDoor.ToString() == "true") { s_lock_unlock_door.Text = "разблокирована"; } else { s_lock_unlock_door.Text = "заблокирована"; } s_cost.Text = "39537.5"; //var boxState = s_open_close_container.Text; //var doorState = s_lock_unlock_door.Text; } else { Toast.MakeText(this, o_data.Message, ToastLength.Long).Show(); } } //else if(s_payment.Text == "Не оплачено") //{ // s_temperature.Text = "****"; // s_light.Text = "****"; // s_humidity.Text = "****"; // s_weight.Text = "****"; // s_pin_access_code.Text = "****"; // s_lock_unlock_door.Text = "****"; // s_cost.Text = "1000"; // container_name.Text = name; // s_latitude.Text = "****"; // s_longitude.Text = "****"; //} } catch (Exception ex) { Toast.MakeText(this, "" + ex.Message, ToastLength.Long).Show(); } }
public async Task ContainerContractTest() { ContainerProperties containerProperties = new ContainerProperties(Guid.NewGuid().ToString(), "/users") { IndexingPolicy = new IndexingPolicy() { Automatic = true, IndexingMode = IndexingMode.Consistent, IncludedPaths = new Collection <IncludedPath>() { new IncludedPath() { Path = "/*" } }, ExcludedPaths = new Collection <ExcludedPath>() { new ExcludedPath() { Path = "/test/*" } }, CompositeIndexes = new Collection <Collection <CompositePath> >() { new Collection <CompositePath>() { new CompositePath() { Path = "/address/city", Order = CompositePathSortOrder.Ascending }, new CompositePath() { Path = "/address/zipcode", Order = CompositePathSortOrder.Descending } } }, SpatialIndexes = new Collection <SpatialPath>() { new SpatialPath() { Path = "/address/spatial/*", SpatialTypes = new Collection <SpatialType>() { SpatialType.LineString } } } } }; CosmosJsonDotNetSerializer serializer = new CosmosJsonDotNetSerializer(); Stream stream = serializer.ToStream(containerProperties); ContainerProperties deserialziedTest = serializer.FromStream <ContainerProperties>(stream); ContainerResponse response = await this.database.CreateContainerAsync(containerProperties); Assert.IsNotNull(response); Assert.IsTrue(response.RequestCharge > 0); Assert.IsNotNull(response.Headers); Assert.IsNotNull(response.Headers.ActivityId); ContainerProperties responseProperties = response.Resource; Assert.IsNotNull(responseProperties.Id); Assert.IsNotNull(responseProperties.ResourceId); Assert.IsNotNull(responseProperties.ETag); Assert.IsTrue(responseProperties.LastModified.HasValue); Assert.IsTrue(responseProperties.LastModified.Value > new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), responseProperties.LastModified.Value.ToString()); Assert.AreEqual(1, responseProperties.IndexingPolicy.IncludedPaths.Count); IncludedPath includedPath = responseProperties.IndexingPolicy.IncludedPaths.First(); Assert.AreEqual("/*", includedPath.Path); Assert.AreEqual("/test/*", responseProperties.IndexingPolicy.ExcludedPaths.First().Path); Assert.AreEqual(1, responseProperties.IndexingPolicy.CompositeIndexes.Count); Assert.AreEqual(2, responseProperties.IndexingPolicy.CompositeIndexes.First().Count); CompositePath compositePath = responseProperties.IndexingPolicy.CompositeIndexes.First().First(); Assert.AreEqual("/address/city", compositePath.Path); Assert.AreEqual(CompositePathSortOrder.Ascending, compositePath.Order); Assert.AreEqual(1, responseProperties.IndexingPolicy.SpatialIndexes.Count); SpatialPath spatialPath = responseProperties.IndexingPolicy.SpatialIndexes.First(); Assert.AreEqual("/address/spatial/*", spatialPath.Path); Assert.AreEqual(4, spatialPath.SpatialTypes.Count); // All SpatialTypes are returned }
public async Task ChangeFeed_FeedRange_FromV0Token() { ContainerResponse largerContainer = await this.database.CreateContainerAsync( new ContainerProperties(id : Guid.NewGuid().ToString(), partitionKeyPath : "/status"), throughput : 20000, cancellationToken : this.cancellationToken); ContainerInternal container = (ContainerInlineCore)largerContainer; int expected = 100; int count = 0; await this.CreateRandomItems((ContainerCore)container, expected, randomPartitionKey : true); IReadOnlyList <FeedRange> feedRanges = await container.GetFeedRangesAsync(); List <string> continuations = new List <string>(); // First do one request to construct the old model information based on Etag foreach (FeedRange feedRange in feedRanges) { IEnumerable <string> pkRangeIds = await container.GetPartitionKeyRangesAsync(feedRange); ChangeFeedRequestOptions requestOptions = new ChangeFeedRequestOptions() { PageSizeHint = 1 }; ChangeFeedIteratorCore feedIterator = container.GetChangeFeedStreamIterator( changeFeedStartFrom: ChangeFeedStartFrom.Beginning(feedRange), changeFeedMode: ChangeFeedMode.Incremental, changeFeedRequestOptions: requestOptions) as ChangeFeedIteratorCore; ResponseMessage firstResponse = await feedIterator.ReadNextAsync(); FeedRangeEpk FeedRangeEpk = feedRange as FeedRangeEpk; // Construct the continuation's range, using PKRangeId + ETag List <dynamic> ct = new List <dynamic>() { new { min = FeedRangeEpk.Range.Min, max = FeedRangeEpk.Range.Max, token = (string)null } }; // Extract Etag and manually construct the continuation dynamic oldContinuation = new { V = 0, Rid = await container.GetCachedRIDAsync(cancellationToken : this.cancellationToken), Continuation = ct }; continuations.Add(JsonConvert.SerializeObject(oldContinuation)); } // Now start the new iterators with the constructed continuations from migration foreach (string continuation in continuations) { ChangeFeedRequestOptions requestOptions = new ChangeFeedRequestOptions() { PageSizeHint = 100, EmitOldContinuationToken = true, }; ChangeFeedIteratorCore feedIterator = container.GetChangeFeedStreamIterator( changeFeedStartFrom: ChangeFeedStartFrom.ContinuationToken(continuation), changeFeedMode: ChangeFeedMode.Incremental, changeFeedRequestOptions: requestOptions) as ChangeFeedIteratorCore; ResponseMessage firstResponse = await feedIterator.ReadNextAsync(); if (firstResponse.IsSuccessStatusCode) { Collection <ToDoActivity> response = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(firstResponse.Content).Data; count += response.Count; string migratedContinuation = firstResponse.ContinuationToken; Assert.IsTrue(FeedRangeContinuation.TryParse(migratedContinuation, out FeedRangeContinuation feedRangeContinuation)); Assert.IsTrue(feedRangeContinuation.FeedRange is FeedRangeEpk); } } Assert.AreEqual(expected, count); }
static void Main(string[] args) { CosmosClientOptions options = new CosmosClientOptions { ConsistencyLevel = ConsistencyLevel.Eventual }; client = new CosmosClient(connectionString, options); DatabaseResponse dr = client.CreateDatabaseIfNotExistsAsync("TestDatabase", 500).GetAwaiter().GetResult(); UniqueKey key1 = new UniqueKey(); key1.Paths.Add("/lastname"); UniqueKeyPolicy policy = new UniqueKeyPolicy(); policy.UniqueKeys.Add(key1); ContainerProperties cp = new ContainerProperties { Id = "testContainer", PartitionKeyPath = "/email", UniqueKeyPolicy = policy, }; cp.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath() { Path = "/*" }); cp.IndexingPolicy.IncludedPaths.Add(new IncludedPath() { Path = "/firstname/*" }); //Will provision 600 to new container if not then uses shared or 400 as specified at db level ContainerResponse cr = dr.Database.CreateContainerIfNotExistsAsync(cp, 600).GetAwaiter().GetResult(); Employee e1 = new Employee { age = 11, email = "*****@*****.**", firstName = "microsoft", lastName = "Da", id = "002" }; ItemResponse <Employee> item = cr.Container.CreateItemAsync(e1, new PartitionKey(e1.email)).GetAwaiter().GetResult(); Console.WriteLine(item.Resource.email.ToString()); Console.WriteLine(item.RequestCharge); //Querying QueryRequestOptions ro = new QueryRequestOptions { MaxItemCount = 1 //Pagination settings }; FeedIterator <Employee> fi = cr.Container.GetItemQueryIterator <Employee>("select * from c", null, ro); //By default Query fetches only 100 records if it has more than 100 then it fetches next hundred with other set of query execution //Pagination while (fi.HasMoreResults) { FeedResponse <Employee> fr = fi.ReadNextAsync().GetAwaiter().GetResult(); foreach (var efr in fr) { Console.WriteLine($"Record : {efr.email} --- {efr.firstName}"); } } }
private static async Task ProcessNavaidData(ContainerResponse container, string path) { var selector = new FixedLengthTypeMapperSelector(); selector.When(s => s.StartsWith("NAV1")).Use(NavaidMapper.GetNavaidMapper()); selector.When(s => s.StartsWith("NAV2")) .Use(NavaidMapper.GetNavaidRemarksMapper()); selector.When(s => s.StartsWith("NAV3")).Use(NavaidMapper.GetAirspaceFixesAssociatedWithNavaidMapper()); selector.When(s => s.StartsWith("NAV4")) .Use(NavaidMapper.GetHoldingPatternMapper()); selector.When(s => s.StartsWith("NAV5")).Use(NavaidMapper.GetFanmarkerMapper()); selector.When(s => s.StartsWith("NAV6")).Use(NavaidMapper.GetReceiverCheckpointMapper()); var mapit = new StringBuilder(); using var reader = new StreamReader(File.OpenRead($@"{path}\NAV.txt")); var flr = selector.GetReader(reader); Navaid navaid = null; while (flr.Read()) { switch (flr.Current) { case Navaid navaidData: if (navaid != null) { if (Coordinate.TryParse($"{navaid.NavaidLongitudeFormatted} {navaid.NavaidLatitudeFormatted}", out Coordinate c)) { navaid.Location = new Point(c.Longitude.DecimalDegree, c.Latitude.DecimalDegree); } navaid.id = $"{navaid.NavaidFacilityIdentifier}|{navaid.NavaidFacilityType.Replace("/", string.Empty)}|{navaid.CityAssociatedWithTheNavaid}"; navaid.RecordTypeIndicator = "NAVAID"; try { var readResponse = await container.Container.ReadItemAsync <Navaid>(navaid.id, new PartitionKey("NAVAID")); var replaceResponse = await container.Container.ReplaceItemAsync <Navaid>(navaid, navaid.id, new PartitionKey("NAVAID")); Console.WriteLine($"Replaced {navaid.NavaidFacilityIdentifier}"); } catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound) { // Save the record to the DB var navaidResponse = await container.Container.CreateItemAsync <Navaid>(navaid, new PartitionKey("NAVAID")); Console.WriteLine($"Created {navaid.NavaidFacilityIdentifier}"); } } navaid = navaidData; break; case NavaidRemark remark: navaid.Remarks ??= new List <NavaidRemark>(); navaid.Remarks.Add(remark); break; case AirspaceFixAssociatedWithNavaid asf: navaid.AirspaceFixes ??= new List <AirspaceFixAssociatedWithNavaid>(); navaid.AirspaceFixes.Add(asf); break; case HoldingPattern holdingPattern: navaid.HoldingPatterns ??= new List <HoldingPattern>(); navaid.HoldingPatterns.Add(holdingPattern); break; case FanMarkerAssociatedwithnavaid fanMarker: navaid.FanMarkers ??= new List <FanMarkerAssociatedwithnavaid>(); navaid.FanMarkers.Add(fanMarker); break; case VorReceiverCheckpointAssociatedWithNavaid rcvr: navaid.VorReceivers ??= new List <VorReceiverCheckpointAssociatedWithNavaid>(); navaid.VorReceivers.Add(rcvr); break; default: throw new Exception("Not found"); } } }
private async Task ValidateCollectionIndexProgressHeaders(CosmosClient client) { CosmosDatabase db = await client.CreateDatabaseAsync(Guid.NewGuid().ToString()); try { PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition { Paths = new System.Collections.ObjectModel.Collection <string>(new[] { "/id" }), Kind = PartitionKind.Hash }; var lazyCollection = new CosmosContainerSettings() { Id = Guid.NewGuid().ToString(), PartitionKey = partitionKeyDefinition }; lazyCollection.IndexingPolicy.IndexingMode = Cosmos.IndexingMode.Lazy; CosmosContainer lazyContainer = await db.CreateContainerAsync(lazyCollection); var consistentCollection = new CosmosContainerSettings() { Id = Guid.NewGuid().ToString(), PartitionKey = partitionKeyDefinition }; consistentCollection.IndexingPolicy.IndexingMode = Cosmos.IndexingMode.Consistent; CosmosContainer consistentContainer = await db.CreateContainerAsync(consistentCollection); var noneIndexCollection = new CosmosContainerSettings() { Id = Guid.NewGuid().ToString(), PartitionKey = partitionKeyDefinition }; noneIndexCollection.IndexingPolicy.Automatic = false; noneIndexCollection.IndexingPolicy.IndexingMode = Cosmos.IndexingMode.None; CosmosContainer noneIndexContainer = await db.CreateContainerAsync(noneIndexCollection); var doc = new Document() { Id = Guid.NewGuid().ToString() }; await lazyContainer.CreateItemAsync <Document>(doc); await consistentContainer.CreateItemAsync <Document>(doc); await noneIndexContainer.CreateItemAsync <Document>(doc); // Lazy-indexing collection. { ContainerResponse collectionResponse = await lazyContainer.ReadAsync(requestOptions : new ContainerRequestOptions { PopulateQuotaInfo = true }); Assert.IsTrue(int.Parse(collectionResponse.Headers[HttpConstants.HttpHeaders.CollectionLazyIndexingProgress], CultureInfo.InvariantCulture) >= 0, "Expect lazy indexer progress when reading lazy collection."); Assert.AreEqual(100, int.Parse(collectionResponse.Headers[HttpConstants.HttpHeaders.CollectionIndexTransformationProgress], CultureInfo.InvariantCulture), "Expect reindexer progress when reading lazy collection."); } // Consistent-indexing collection. { ContainerResponse collectionResponse = await consistentContainer.ReadAsync(requestOptions : new ContainerRequestOptions { PopulateQuotaInfo = true }); Assert.IsFalse(collectionResponse.Headers.AllKeys().Contains(HttpConstants.HttpHeaders.CollectionLazyIndexingProgress), "No lazy indexer progress when reading consistent collection."); Assert.AreEqual(100, int.Parse(collectionResponse.Headers[HttpConstants.HttpHeaders.CollectionIndexTransformationProgress], CultureInfo.InvariantCulture), "Expect reindexer progress when reading consistent collection."); } // None-indexing collection. { ContainerResponse collectionResponse = await noneIndexContainer.ReadAsync(requestOptions : new ContainerRequestOptions { PopulateQuotaInfo = true }); Assert.IsFalse(collectionResponse.Headers.AllKeys().Contains(HttpConstants.HttpHeaders.CollectionLazyIndexingProgress), "No lazy indexer progress when reading none-index collection."); Assert.AreEqual(100, int.Parse(collectionResponse.Headers[HttpConstants.HttpHeaders.CollectionIndexTransformationProgress], CultureInfo.InvariantCulture), "Expect reindexer progress when reading none-index collection."); } } finally { await db.DeleteAsync(); } }
private static async Task ProcessLandingFacilityData(ContainerResponse container, string path) { var selector = new FixedLengthTypeMapperSelector(); selector.When(s => s.StartsWith("APT")).Use(LandingFacilityMapper.GetLandingFacilityDataMapper()); selector.When(s => s.StartsWith("ATT")) .Use(LandingFacilityMapper.GetFacilityAttendanceScheduleDataMapper()); selector.When(s => s.StartsWith("RWY")).Use(LandingFacilityMapper.GetFacilityRunwayDataMapper()); selector.When(s => s.StartsWith("ARS")) .Use(LandingFacilityMapper.GetRunwayArrestingSystemDataMapper()); selector.When(s => s.StartsWith("RMK")).Use(LandingFacilityMapper.GetFacilityRemarkDataMapper()); var mapit = new StringBuilder(); using var reader = new StreamReader(File.OpenRead($@"{path}\APT.txt")); var flr = selector.GetReader(reader); LandingFacilityData airport = null; while (flr.Read()) { switch (flr.Current) { case LandingFacilityData landingFacilityData: if (airport != null) { if (Coordinate.TryParse($"{airport.AirportReferencePointLongitudeFormatted} {airport.AirportReferencePointLatitudeFormatted}", out Coordinate c)) { airport.Location = new Point(c.Longitude.DecimalDegree, c.Latitude.DecimalDegree); } airport.id = $"{airport.LandingFacilitySiteNumber}|{airport.LandingFacilityType}"; try { var readResponse = await container.Container.ReadItemAsync <LandingFacilityData>(airport.id, new PartitionKey(airport.RecordTypeIndicator)); var replaceResponse = await container.Container.ReplaceItemAsync <LandingFacilityData>(airport, airport.id, new PartitionKey(airport.RecordTypeIndicator)); Console.WriteLine($"Replaced {airport.LocationIdentifier}"); } catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound) { // Save the record to the DB var airportResponse = await container.Container.CreateItemAsync <LandingFacilityData>(airport, new PartitionKey(airport.RecordTypeIndicator)); Console.WriteLine($"Created {airport.LocationIdentifier}"); } } airport = landingFacilityData; break; case FacilityAttendanceScheduleData fa: airport.AttendanceSchedule ??= new List <FacilityAttendanceScheduleData>(); airport.AttendanceSchedule.Add(fa); break; case FacilityRunwayData runway: airport.Runways ??= new List <FacilityRunwayData>(); airport.Runways.Add(runway); break; case RunwayArrestingSystemData ras: airport.ArrestingSystems ??= new List <RunwayArrestingSystemData>(); airport.ArrestingSystems.Add(ras); break; case FacilityRemarkData remark: airport.Remarks ??= new List <FacilityRemarkData>(); airport.Remarks.Add(remark); break; default: throw new Exception("Not found"); } } }
public async Task SpatialTest() { ContainerProperties geographyWithoutBoundingBox = new ContainerProperties() { Id = "geographyWithoutBoundingBox", PartitionKeyPath = "/pk", IndexingPolicy = new Cosmos.IndexingPolicy() { Automatic = true, IndexingMode = Cosmos.IndexingMode.Consistent, IncludedPaths = new Collection <Cosmos.IncludedPath>() { new Cosmos.IncludedPath() { Path = "/*", } }, ExcludedPaths = new Collection <Cosmos.ExcludedPath>(), SpatialIndexes = new Collection <Cosmos.SpatialPath>() { new Cosmos.SpatialPath() { Path = "/location/?", SpatialTypes = new Collection <Cosmos.SpatialType>() { Cosmos.SpatialType.LineString, Cosmos.SpatialType.MultiPolygon, Cosmos.SpatialType.Point, Cosmos.SpatialType.Polygon, } } } } }; ContainerProperties geometryWithoutBoundingBox = new ContainerProperties() { Id = "geometryWithoutBoundingBox", PartitionKeyPath = "/pk", IndexingPolicy = new Cosmos.IndexingPolicy() { Automatic = true, IndexingMode = Cosmos.IndexingMode.Consistent, IncludedPaths = new Collection <Cosmos.IncludedPath>() { new Cosmos.IncludedPath() { Path = "/*", } }, ExcludedPaths = new Collection <Cosmos.ExcludedPath>(), SpatialIndexes = new Collection <Cosmos.SpatialPath>() { new Cosmos.SpatialPath() { Path = "/location/?", SpatialTypes = new Collection <Cosmos.SpatialType>() { Cosmos.SpatialType.LineString, Cosmos.SpatialType.MultiPolygon, Cosmos.SpatialType.Point, Cosmos.SpatialType.Polygon, } } } }, GeospatialConfig = new Cosmos.GeospatialConfig() { GeospatialType = Cosmos.GeospatialType.Geometry } }; ContainerProperties geographyWithBoundingBox = new ContainerProperties() { Id = "geographyWithBoundingBox", PartitionKeyPath = "/pk", IndexingPolicy = new Cosmos.IndexingPolicy() { Automatic = true, IndexingMode = Cosmos.IndexingMode.Consistent, IncludedPaths = new Collection <Cosmos.IncludedPath>() { new Cosmos.IncludedPath() { Path = "/*", } }, ExcludedPaths = new Collection <Cosmos.ExcludedPath>(), SpatialIndexes = new Collection <Cosmos.SpatialPath>() { new Cosmos.SpatialPath() { Path = "/location/?", SpatialTypes = new Collection <Cosmos.SpatialType>() { Cosmos.SpatialType.LineString, Cosmos.SpatialType.MultiPolygon, Cosmos.SpatialType.Point, Cosmos.SpatialType.Polygon, }, BoundingBox = new Cosmos.BoundingBoxProperties() { Xmin = 0, Ymin = 0, Xmax = 10, Ymax = 10, } } } } }; ContainerProperties geometryWithBoundingBox = new ContainerProperties() { Id = "geometryWithBoundingBox", PartitionKeyPath = "/pk", IndexingPolicy = new Cosmos.IndexingPolicy() { Automatic = true, IndexingMode = Cosmos.IndexingMode.Consistent, IncludedPaths = new Collection <Cosmos.IncludedPath>() { new Cosmos.IncludedPath() { Path = "/*", } }, ExcludedPaths = new Collection <Cosmos.ExcludedPath>(), SpatialIndexes = new Collection <Cosmos.SpatialPath>() { new Cosmos.SpatialPath() { Path = "/location/?", SpatialTypes = new Collection <Cosmos.SpatialType>() { Cosmos.SpatialType.LineString, Cosmos.SpatialType.MultiPolygon, Cosmos.SpatialType.Point, Cosmos.SpatialType.Polygon, }, BoundingBox = new Cosmos.BoundingBoxProperties() { Xmin = 0, Ymin = 0, Xmax = 10, Ymax = 10, } } } }, GeospatialConfig = new Cosmos.GeospatialConfig() { GeospatialType = Cosmos.GeospatialType.Geometry } }; ContainerProperties geometryWithWrongBoundingBox = new ContainerProperties() { Id = "geometryWithWrongBoundingBox", PartitionKeyPath = "/pk", IndexingPolicy = new Cosmos.IndexingPolicy() { Automatic = true, IndexingMode = Cosmos.IndexingMode.Consistent, IncludedPaths = new Collection <Cosmos.IncludedPath>() { new Cosmos.IncludedPath() { Path = "/*", } }, ExcludedPaths = new Collection <Cosmos.ExcludedPath>(), SpatialIndexes = new Collection <Cosmos.SpatialPath>() { new Cosmos.SpatialPath() { Path = "/location/?", SpatialTypes = new Collection <Cosmos.SpatialType>() { Cosmos.SpatialType.LineString, Cosmos.SpatialType.MultiPolygon, Cosmos.SpatialType.Point, Cosmos.SpatialType.Polygon, }, BoundingBox = new Cosmos.BoundingBoxProperties() { Xmin = 0, Ymin = 0, Xmax = 0, Ymax = 0, } } } }, GeospatialConfig = new Cosmos.GeospatialConfig() { GeospatialType = Cosmos.GeospatialType.Geometry } }; //Test 1: try to create a geography collection, with no bounding box ContainerResponse containerResponse = await this.cosmosDatabase.CreateContainerAsync(geographyWithoutBoundingBox); // Test 2: try to create a geometry collection, with no bounding box try { containerResponse = await this.cosmosDatabase.CreateContainerAsync(geometryWithoutBoundingBox); Assert.Fail("Expected an exception"); } catch { //"Required parameter 'boundingBox' for 'Geometry' collection is missing in spatial path '\/location\/?'" } // Test 3: try to create a geography collection, with bounding box try { containerResponse = await this.cosmosDatabase.CreateContainerAsync(geographyWithBoundingBox); Assert.Fail("Expected an exception"); } catch { //"Incorrect parameter 'boundingBox' specified for 'Geography' collection" } // Test 4: try to create a geometry collection, with bounding box containerResponse = await this.cosmosDatabase.CreateContainerAsync(geometryWithBoundingBox); // Test 5: try to create a geometry collection, with wrong bounding box try { containerResponse = await this.cosmosDatabase.CreateContainerAsync(geometryWithWrongBoundingBox); } catch { //The value of parameter 'xmax' must be greater than the value of parameter 'xmin' in 'boundingBox' for spatial path '\/location\/?' } containerResponse = await containerResponse.Container.DeleteContainerAsync(); Assert.AreEqual(HttpStatusCode.NoContent, containerResponse.StatusCode); }
public async Task ValidateAzureKeyCredentialDirectModeUpdateAsync() { string authKey = ConfigurationManager.AppSettings["MasterKey"]; string endpoint = ConfigurationManager.AppSettings["GatewayEndpoint"]; AzureKeyCredential masterKeyCredential = new AzureKeyCredential(authKey); using (CosmosClient client = new CosmosClient( endpoint, masterKeyCredential)) { string databaseName = Guid.NewGuid().ToString(); try { Cosmos.Database database = client.GetDatabase(databaseName); ResponseMessage responseMessage = await database.ReadStreamAsync(); Assert.AreEqual(HttpStatusCode.NotFound, responseMessage.StatusCode); { // Random key: Next set of actions are expected to fail => 401 (UnAuthorized) masterKeyCredential.Update(Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()))); responseMessage = await database.ReadStreamAsync(); Assert.AreEqual(HttpStatusCode.Unauthorized, responseMessage.StatusCode); string diagnostics = responseMessage.Diagnostics.ToString(); Assert.IsTrue(diagnostics.Contains("AuthProvider LifeSpan InSec"), diagnostics.ToString()); } { // Resetting back to master key => 404 (NotFound) masterKeyCredential.Update(authKey); responseMessage = await database.ReadStreamAsync(); Assert.AreEqual(HttpStatusCode.NotFound, responseMessage.StatusCode); } // Test with resource token interchageability masterKeyCredential.Update(authKey); database = await client.CreateDatabaseAsync(databaseName); string containerId = Guid.NewGuid().ToString(); ContainerResponse containerResponse = await database.CreateContainerAsync(containerId, "/id"); Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode); { // Resource token with ALL permissoin's string userId = Guid.NewGuid().ToString(); UserResponse userResponse = await database.CreateUserAsync(userId); Cosmos.User user = userResponse.User; Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode); Assert.AreEqual(userId, user.Id); string permissionId = Guid.NewGuid().ToString(); PermissionProperties permissionProperties = new PermissionProperties(permissionId, Cosmos.PermissionMode.All, client.GetContainer(databaseName, containerId)); PermissionResponse permissionResponse = await database.GetUser(userId).CreatePermissionAsync(permissionProperties); Assert.AreEqual(HttpStatusCode.Created, permissionResponse.StatusCode); Assert.AreEqual(permissionId, permissionResponse.Resource.Id); Assert.AreEqual(Cosmos.PermissionMode.All, permissionResponse.Resource.PermissionMode); Assert.IsNotNull(permissionResponse.Resource.Token); SelflinkValidator.ValidatePermissionSelfLink(permissionResponse.Resource.SelfLink); // Valdiate ALL on contianer masterKeyCredential.Update(permissionResponse.Resource.Token); ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(); Cosmos.Container container = client.GetContainer(databaseName, containerId); responseMessage = await container.ReadContainerStreamAsync(); Assert.AreEqual(HttpStatusCode.OK, responseMessage.StatusCode); responseMessage = await container.CreateItemStreamAsync(TestCommon.SerializerCore.ToStream(item), new Cosmos.PartitionKey(item.id)); Assert.AreEqual(HttpStatusCode.Created, responseMessage.StatusCode); // Read Only resorce token } // Reset to master key for new permission creation masterKeyCredential.Update(authKey); { // Resource token with Read-ONLY permissoin's string userId = Guid.NewGuid().ToString(); UserResponse userResponse = await database.CreateUserAsync(userId); Cosmos.User user = userResponse.User; Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode); Assert.AreEqual(userId, user.Id); string permissionId = Guid.NewGuid().ToString(); PermissionProperties permissionProperties = new PermissionProperties(permissionId, Cosmos.PermissionMode.Read, client.GetContainer(databaseName, containerId)); PermissionResponse permissionResponse = await database.GetUser(userId).CreatePermissionAsync(permissionProperties); //Backend returns Created instead of OK Assert.AreEqual(HttpStatusCode.Created, permissionResponse.StatusCode); Assert.AreEqual(permissionId, permissionResponse.Resource.Id); Assert.AreEqual(Cosmos.PermissionMode.Read, permissionResponse.Resource.PermissionMode); // Valdiate read on contianer masterKeyCredential.Update(permissionResponse.Resource.Token); ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(); Cosmos.Container container = client.GetContainer(databaseName, containerId); responseMessage = await container.ReadContainerStreamAsync(); Assert.AreEqual(HttpStatusCode.OK, responseMessage.StatusCode); responseMessage = await container.CreateItemStreamAsync(TestCommon.SerializerCore.ToStream(item), new Cosmos.PartitionKey(item.id)); Assert.AreEqual(HttpStatusCode.Forbidden, responseMessage.StatusCode); // Read Only resorce token } { // Reset to master key for new permission creation masterKeyCredential.Update(Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()))); ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(); Cosmos.Container container = client.GetContainer(databaseName, containerId); responseMessage = await container.CreateItemStreamAsync(TestCommon.SerializerCore.ToStream(item), new Cosmos.PartitionKey(item.id)); Assert.AreEqual(HttpStatusCode.Unauthorized, responseMessage.StatusCode); // Read Only resorce token string diagnostics = responseMessage.Diagnostics.ToString(); Assert.IsTrue(diagnostics.Contains("AuthProvider LifeSpan InSec"), diagnostics.ToString()); } } finally { // Reset to master key for clean-up masterKeyCredential.Update(authKey); await TestCommon.DeleteDatabaseAsync(client, client.GetDatabase(databaseName)); } } }
public async Task ContainerContractTest() { ContainerResponse response = await this.cosmosDatabase.CreateContainerAsync(Guid.NewGuid().ToString(), "/id"); this.ValidateCreateContainerResponseContract(response); }
// </RunIndexDemo> /// <summary> /// The default index policy on a Container will AUTOMATICALLY index ALL documents added. /// There may be scenarios where you want to exclude a specific doc from the index even though all other /// documents are being indexed automatically. /// This method demonstrates how to use an index directive to control this /// </summary> private static async Task ExplicitlyExcludeFromIndex(CosmosClient client) { string containerId = $"{Program.containerId}-ExplicitlyExcludeFromIndex"; // Create a collection with default index policy(i.e.automatic = true) ContainerResponse response = await Program.database.CreateContainerAsync(containerId, Program.partitionKey); Console.WriteLine("Container {0} created with index policy \n{1}", containerId, JsonConvert.SerializeObject(response.Resource.IndexingPolicy)); Container container = (Container)response; try { // Create a document // Then query on it immediately // Will work as this Collection is set to automatically index everything ItemResponse <dynamic> created = await container.CreateItemAsync <dynamic>(new { id = "doc1", partitionKey = "doc1", orderId = "order1" }, new PartitionKey("doc1")); Console.WriteLine("\nItem created: \n{0}", JsonConvert.SerializeObject(created.Resource)); FeedIterator <dynamic> resultSetIterator = container.GetItemQueryIterator <dynamic>(new QueryDefinition("SELECT * FROM root r WHERE r.orderId='order1'"), requestOptions: new QueryRequestOptions { MaxItemCount = 1 }); bool found = false; while (resultSetIterator.HasMoreResults) { FeedResponse <dynamic> feedResponse = await resultSetIterator.ReadNextAsync(); found = feedResponse.Count > 0; } Console.WriteLine("Item found by query: {0}", found); // Now, create an item but this time explictly exclude it from the collection using IndexingDirective // Then query for that document // Shoud NOT find it, because we excluded it from the index // BUT, the document is there and doing a ReadItem by Id will prove it created = await container.CreateItemAsync <dynamic>(new { id = "doc2", partitionKey = "doc2", orderId = "order2" }, new PartitionKey("doc2"), new ItemRequestOptions { IndexingDirective = IndexingDirective.Exclude }); Console.WriteLine("\nItem created: \n{0}", JsonConvert.SerializeObject(created.Resource)); resultSetIterator = container.GetItemQueryIterator <dynamic>(new QueryDefinition("SELECT * FROM root r WHERE r.orderId='order2'"), requestOptions: new QueryRequestOptions { MaxItemCount = 1 }); found = false; while (resultSetIterator.HasMoreResults) { FeedResponse <dynamic> feedResponse = await resultSetIterator.ReadNextAsync(); found = feedResponse.Count > 0; } Console.WriteLine("Item found by query: {0}", found); ItemResponse <dynamic> document = await container.ReadItemAsync <dynamic>((string)created.Resource.id, new PartitionKey("doc2")); Console.WriteLine("Item read by id: {0}", document != null); } finally { // Cleanup await container.DeleteContainerAsync(); } }
public async Task NoPartitionedCreateFail() { string containerName = Guid.NewGuid().ToString(); try { new CosmosContainerSettings(id: containerName, partitionKeyPath: null); Assert.Fail("Create should throw null ref exception"); } catch (ArgumentNullException ae) { Assert.IsNotNull(ae); } try { new CosmosContainerSettings(id: containerName, partitionKeyDefinition: null); Assert.Fail("Create should throw null ref exception"); } catch (ArgumentNullException ae) { Assert.IsNotNull(ae); } CosmosContainerSettings settings = new CosmosContainerSettings() { Id = containerName }; try { ContainerResponse containerResponse = await this.cosmosDatabase.CreateContainerAsync(settings); Assert.Fail("Create should throw null ref exception"); } catch (ArgumentNullException ae) { Assert.IsNotNull(ae); } try { ContainerResponse containerResponse = await this.cosmosDatabase.CreateContainerIfNotExistsAsync(settings); Assert.Fail("Create should throw null ref exception"); } catch (ArgumentNullException ae) { Assert.IsNotNull(ae); } try { ContainerResponse containerResponse = await this.cosmosDatabase.CreateContainerAsync(id : containerName, partitionKeyPath : null); Assert.Fail("Create should throw null ref exception"); } catch (ArgumentNullException ae) { Assert.IsNotNull(ae); } try { ContainerResponse containerResponse = await this.cosmosDatabase.CreateContainerIfNotExistsAsync(id : containerName, partitionKeyPath : null); Assert.Fail("Create should throw null ref exception"); } catch (ArgumentNullException ae) { Assert.IsNotNull(ae); } }
/// <summary> /// The default behavior is for DocumentDB to index every attribute in every document automatically. /// There are times when a document contains large amounts of information, in deeply nested structures /// that you know you will never search on. In extreme cases like this, you can exclude paths from the /// index to save on storage cost, improve write performance and also improve read performance because the index is smaller /// /// This method demonstrates how to set IndexingPolicy.ExcludedPaths /// </summary> private static async Task ExcludePathsFromIndex() { string containerId = $"{Program.containerId}-ExcludePathsFromIndex"; Console.WriteLine("\n3. Exclude specified paths from document index"); ContainerProperties containerProperties = new ContainerProperties(containerId, Program.partitionKey); containerProperties.IndexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*" }); // Special manadatory path of "/*" required to denote include entire tree containerProperties.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/metaData/*" }); // exclude metaData node, and anything under it containerProperties.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/subDoc/nonSearchable/*" }); // exclude ONLY a part of subDoc containerProperties.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/\"excludedNode\"/*" }); // exclude excludedNode node, and anything under it // The effect of the above IndexingPolicy is that only id, foo, and the subDoc/searchable are indexed ContainerResponse response = await Program.database.CreateContainerAsync(containerProperties); Console.WriteLine("Container {0} created with index policy \n{1}", containerId, JsonConvert.SerializeObject(response.Resource.IndexingPolicy)); Container container = (Container)response; try { int numDocs = 250; Console.WriteLine("Creating {0} documents", numDocs); for (int docIndex = 0; docIndex < numDocs; docIndex++) { dynamic dyn = new { id = "doc" + docIndex, partitionKey = "doc" + docIndex, foo = "bar" + docIndex, metaData = "meta" + docIndex, subDoc = new { searchable = "searchable" + docIndex, nonSearchable = "value" + docIndex }, excludedNode = new { subExcluded = "something" + docIndex, subExcludedNode = new { someProperty = "value" + docIndex } } }; ItemResponse <dynamic> created = await container.CreateItemAsync <dynamic>(dyn, new PartitionKey("doc" + docIndex)); Console.WriteLine("Creating document with id {0}", created.Resource.id); } // Querying for a document on either metaData or /subDoc/subSubDoc/someProperty will be expensive since they do not utilize the index, // but instead are served from scan automatically. int queryDocId = numDocs / 2; QueryStats queryStats = await Program.GetQueryResult(container, string.Format(CultureInfo.InvariantCulture, "SELECT * FROM root r WHERE r.metaData='meta{0}'", queryDocId)); Console.WriteLine("Query on metaData returned {0} results", queryStats.Count); Console.WriteLine("Query on metaData consumed {0} RUs", queryStats.RequestCharge); queryStats = await Program.GetQueryResult(container, string.Format(CultureInfo.InvariantCulture, "SELECT * FROM root r WHERE r.subDoc.nonSearchable='value{0}'", queryDocId)); Console.WriteLine("Query on /subDoc/nonSearchable returned {0} results", queryStats.Count); Console.WriteLine("Query on /subDoc/nonSearchable consumed {0} RUs", queryStats.RequestCharge); queryStats = await Program.GetQueryResult(container, string.Format(CultureInfo.InvariantCulture, "SELECT * FROM root r WHERE r.excludedNode.subExcludedNode.someProperty='value{0}'", queryDocId)); Console.WriteLine("Query on /excludedNode/subExcludedNode/someProperty returned {0} results", queryStats.Count); Console.WriteLine("Query on /excludedNode/subExcludedNode/someProperty cost {0} RUs", queryStats.RequestCharge); // Querying for a document using foo, or even subDoc/searchable > consume less RUs because they were not excluded queryStats = await Program.GetQueryResult(container, string.Format(CultureInfo.InvariantCulture, "SELECT * FROM root r WHERE r.foo='bar{0}'", queryDocId)); Console.WriteLine("Query on /foo returned {0} results", queryStats.Count); Console.WriteLine("Query on /foo cost {0} RUs", queryStats.RequestCharge); queryStats = await Program.GetQueryResult(container, string.Format(CultureInfo.InvariantCulture, "SELECT * FROM root r WHERE r.subDoc.searchable='searchable{0}'", queryDocId)); Console.WriteLine("Query on /subDoc/searchable returned {0} results", queryStats.Count); Console.WriteLine("Query on /subDoc/searchable cost {0} RUs", queryStats.RequestCharge); } finally { // Cleanup await container.DeleteContainerAsync(); } }
// <Main> public static async Task Main(string[] args) { try { databaseId = "deviceInformation" + Guid.NewGuid().ToString(); containerId = "device-samples" + Guid.NewGuid().ToString(); IConfigurationRoot configuration = new ConfigurationBuilder() .AddJsonFile("appSettings.json") .Build(); string endpoint = configuration["EndPointUrl"]; if (string.IsNullOrEmpty(endpoint)) { throw new ArgumentNullException("Please specify a valid endpoint in the appSettings.json"); } string authKey = configuration["AuthorizationKey"]; if (string.IsNullOrEmpty(authKey) || string.Equals(authKey, "Super secret key")) { throw new ArgumentException("Please specify a valid AuthorizationKey in the appSettings.json"); } using (CosmosClient client = new CosmosClient(endpoint, authKey)) { Database database = await client.CreateDatabaseIfNotExistsAsync(databaseId); // Create the container using REST API without a partition key definition await Program.CreateNonPartitionedContainerAsync(endpoint, authKey); Container container = database.GetContainer(containerId); // Read back the same container and verify that partition key path is populated // Partition key is returned when read from V3 SDK. ContainerResponse containerResposne = await container.ReadContainerAsync(); if (containerResposne.Resource.PartitionKeyPath != null) { Console.WriteLine("Container Partition Key path {0}", containerResposne.Resource.PartitionKeyPath); } else { throw new Exception("Unexpected error : Partition Key is not populated in a migrated collection"); } Console.WriteLine("--Demo Item operations with no partition key--"); await Program.ItemOperationsWithNonePartitionKeyValue(container); Console.WriteLine("--Demo Item operations with valid partition key--"); await Program.ItemOperationsWithValidPartitionKeyValue(container); Console.WriteLine("--Demo migration of items inserted with no partition key to items with a partition key--"); await Program.MigratedItemsFromNonePartitionKeyToValidPartitionKeyValue(container); // Clean up the database -- for rerunning the sample await database.DeleteAsync(); } } catch (CosmosException cre) { Console.WriteLine(cre.ToString()); } 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(); } }
/// <summary> /// Run samples for Order By queries. /// </summary> /// <returns>a Task object.</returns> private async Task <RunSummary> ExecuteAsync(BenchmarkConfig config, string accountKey) { using (CosmosClient cosmosClient = config.CreateCosmosClient(accountKey)) { if (config.CleanupOnStart) { Microsoft.Azure.Cosmos.Database database = cosmosClient.GetDatabase(config.Database); await database.DeleteStreamAsync(); } ContainerResponse containerResponse = await Program.CreatePartitionedContainerAsync(config, cosmosClient); Container container = containerResponse; int?currentContainerThroughput = await container.ReadThroughputAsync(); Console.WriteLine($"Using container {config.Container} with {currentContainerThroughput} RU/s"); int taskCount = config.GetTaskCount(currentContainerThroughput.Value); Console.WriteLine("Starting Inserts with {0} tasks", taskCount); Console.WriteLine(); string partitionKeyPath = containerResponse.Resource.PartitionKeyPath; int opsPerTask = config.ItemCount / taskCount; // TBD: 2 clients SxS some overhead RunSummary runSummary; using (DocumentClient documentClient = config.CreateDocumentClient(accountKey)) { Func <IBenchmarkOperation> benchmarkOperationFactory = this.GetBenchmarkFactory( config, partitionKeyPath, cosmosClient, documentClient); if (config.DisableCoreSdkLogging) { // Do it after client initialization (HACK) Program.ClearCoreSdkListeners(); } IExecutionStrategy execution = IExecutionStrategy.StartNew(config, benchmarkOperationFactory); runSummary = await execution.ExecuteAsync(taskCount, opsPerTask, config.TraceFailures, 0.01); } if (config.CleanupOnFinish) { Console.WriteLine($"Deleting Database {config.Database}"); Microsoft.Azure.Cosmos.Database database = cosmosClient.GetDatabase(config.Database); await database.DeleteStreamAsync(); } runSummary.WorkloadType = config.WorkloadType; runSummary.id = $"{DateTime.UtcNow.ToString("yyyy-MM-dd:HH-mm")}-{config.CommitId}"; runSummary.Commit = config.CommitId; runSummary.CommitDate = config.CommitDate; runSummary.CommitTime = config.CommitTime; runSummary.Date = DateTime.UtcNow.ToString("yyyy-MM-dd"); runSummary.Time = DateTime.UtcNow.ToString("HH-mm"); runSummary.BranchName = config.BranchName; runSummary.TotalOps = config.ItemCount; runSummary.Concurrency = taskCount; runSummary.Database = config.Database; runSummary.Container = config.Container; runSummary.AccountName = config.EndPoint; runSummary.pk = config.ResultsPartitionKeyValue; string consistencyLevel = config.ConsistencyLevel; if (string.IsNullOrWhiteSpace(consistencyLevel)) { AccountProperties accountProperties = await cosmosClient.ReadAccountAsync(); consistencyLevel = accountProperties.Consistency.DefaultConsistencyLevel.ToString(); } runSummary.ConsistencyLevel = consistencyLevel; if (config.PublishResults) { Container resultsContainer = cosmosClient.GetContainer(config.Database, config.ResultsContainer); await resultsContainer.CreateItemAsync(runSummary, new PartitionKey(runSummary.pk)); } return(runSummary); } }
public async Task Setup_CosmosResources() { // Create resources for Scenario 0 - setup Console.BackgroundColor = ConsoleColor.Blue; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Running setup...\n"); Console.ResetColor(); // Create database for partitioning demo. We set the RU/s high to simulate having a large workload with many partitions, to demo the impact of cross-partition queries on larger datasets. If you do not wish to demo Scenario 2, replace the throughput with the minimum of 400 RU/s. var partitionDemoDatabaseName = "PartitioningDemo"; Microsoft.Azure.Cosmos.Database partitioningDemoDB = await cosmosClient.CreateDatabaseIfNotExistsAsync(id : partitionDemoDatabaseName, throughput : 40000); Console.WriteLine("\tCreated database {0}", partitioningDemoDB.Id); // Create 2 containers with different partition keys for Scenarios UserReviewsV1Container = await partitioningDemoDB.CreateContainerIfNotExistsAsync(id : "UserReviews_v1", partitionKeyPath : "/id"); Console.WriteLine("\tCreated container {0} with partition key 'id'", UserReviewsV1Container.Id); UserReviewsV2Container = await partitioningDemoDB.CreateContainerIfNotExistsAsync(id : "UserReviews_v2", partitionKeyPath : "/username"); Console.WriteLine("\tCreated container {0} with partition key 'username'", UserReviewsV2Container.Id); // Create database Indexing policy demo var indexingDemoDatabaseName = "IndexingDemo"; Microsoft.Azure.Cosmos.Database indexingDemoDB = await cosmosClient.CreateDatabaseIfNotExistsAsync(id : indexingDemoDatabaseName, throughput : 400); // Create containers for Indexing policy demo var defaultIndexingPolicyContatinerName = "DefaultIndexingPolicy"; var tunedIndexingPolicyContainerName = "TunedIndexingPolicy"; ContainerResponse defaultIndexingPolicyContainer = await indexingDemoDB.DefineContainer(name : defaultIndexingPolicyContatinerName, partitionKeyPath : "/partitionKey") .CreateIfNotExistsAsync(); Console.WriteLine("\tCreated container {0} with default indexing policy", defaultIndexingPolicyContainer.Resource.Id); ContainerResponse tunedIndexingPolicyContainer = await indexingDemoDB.DefineContainer(name : tunedIndexingPolicyContainerName, partitionKeyPath : "/partitionKey") // Define indexing policy with included and excluded paths .WithIndexingPolicy() .WithIncludedPaths() .Path("/username/*") .Path("/productName/*") .Path("/verifiedPurchase/*") .Attach() .WithExcludedPaths() .Path("/*") .Attach() .Attach() .CreateIfNotExistsAsync(); Console.WriteLine("\tCreated container {0} with tuned indexing policy", tunedIndexingPolicyContainer.Resource.Id); // Import data into the containers Console.BackgroundColor = ConsoleColor.Blue; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("\nStarting data import: this will take a few minutes..."); Console.ResetColor(); using (WebClient wc = new WebClient()) { var json = wc.DownloadString("https://cosmosnotebooksdata.blob.core.windows.net/notebookdata/ProductReviewsData.json"); List <dynamic> userProductReviews = JsonConvert.DeserializeObject <List <dynamic> >(json); Console.WriteLine("\tFinished downloading data from 'https://cosmosnotebooksdata.blob.core.windows.net/notebookdata/ProductReviewsData.json'"); Console.WriteLine("\tWriting data into Cosmos containers..."); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // Enable bulk execution mode for faster import cosmosClient.ClientOptions.AllowBulkExecution = true; List <Task <int> > tasks = new List <Task <int> >(); for (var i = 0; i < userProductReviews.Count; i++) { var documentType = userProductReviews[i].documentType; if (documentType == "user") { User user = JsonConvert.DeserializeObject <User>(userProductReviews[i].ToString()); tasks.Add(InsertUserItemsAsync(user)); } else if (documentType == "review") { ProductReview productReview = JsonConvert.DeserializeObject <ProductReview>(userProductReviews[i].ToString()); tasks.Add(InsertProductReviewItemsAsync(productReview)); } } await Task.WhenAll(tasks); stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; //Print results string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("\tImport of {0} items into {1} and {2} containers completed. Total time: {3}", tasks.Sum(task => task.Result) / 2, UserReviewsV1Container.Id, UserReviewsV2Container.Id, elapsedTime); } // Scale down the database to the minimum RU/s. var currentThroughput = await partitioningDemoDB.ReadThroughputAsync(requestOptions : new RequestOptions()); int minThroughput = (int)currentThroughput.MinThroughput; var throughputResponse = await partitioningDemoDB.ReplaceThroughputAsync(minThroughput); Console.WriteLine("\tScaled PartitioningDemo database down to minimum {0} RU/s", throughputResponse.Resource.Throughput); // Reset bulk execution mode to false for rest of demo cosmosClient.ClientOptions.AllowBulkExecution = false; }
public async Task ItemResourcePermissionTest() { //create user string userId = Guid.NewGuid().ToString(); UserResponse userResponse = await this.cosmosDatabase.CreateUserAsync(userId); Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode); Assert.AreEqual(userId, userResponse.Resource.Id); User user = userResponse.User; //create resource string containerId = Guid.NewGuid().ToString(); ContainerResponse containerResponse = await this.cosmosDatabase.CreateContainerAsync(containerId, "/id"); Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode); Container container = containerResponse.Container; string itemId = Guid.NewGuid().ToString(); PartitionKey partitionKey = new PartitionKey(itemId); ItemResponse <dynamic> itemRespnose = await container.CreateItemAsync <dynamic>(new { id = itemId }, partitionKey); Assert.AreEqual(HttpStatusCode.Created, itemRespnose.StatusCode); //create permission string permissionId = Guid.NewGuid().ToString(); PermissionProperties permissionProperties = new PermissionProperties(permissionId, PermissionMode.Read, container, partitionKey, itemId); PermissionResponse permissionResponse = await user.CreatePermissionAsync(permissionProperties); PermissionProperties permission = permissionResponse.Resource; Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode); Assert.AreEqual(permissionId, permission.Id); Assert.AreEqual(permissionProperties.PermissionMode, permission.PermissionMode); //delete resource with PermissionMode.Read using (CosmosClient tokenCosmosClient = TestCommon.CreateCosmosClient(clientOptions: null, resourceToken: permission.Token)) { try { ItemResponse <dynamic> response = await tokenCosmosClient .GetDatabase(this.cosmosDatabase.Id) .GetContainer(containerId) .DeleteItemAsync <dynamic>(itemId, partitionKey); Assert.Fail(); } catch (CosmosException ex) { Assert.AreEqual(HttpStatusCode.Forbidden, ex.StatusCode); } } //update permission to PermissionMode.All permissionProperties = new PermissionProperties(permissionId, PermissionMode.All, container); permissionResponse = await user.GetPermission(permissionId).ReplaceAsync(permissionProperties); permission = permissionResponse.Resource; //delete resource with PermissionMode.All using (CosmosClient tokenCosmosClient = TestCommon.CreateCosmosClient(clientOptions: null, resourceToken: permission.Token)) { ItemResponse <dynamic> response = await tokenCosmosClient .GetDatabase(this.cosmosDatabase.Id) .GetContainer(containerId) .DeleteItemAsync <dynamic>(itemId, partitionKey); Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode); } }
public async Task CRUDTest() { string containerId = Guid.NewGuid().ToString(); ContainerResponse containerResponse = await this.cosmosDatabase.CreateContainerAsync(containerId, "/id"); Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode); string userId = Guid.NewGuid().ToString(); UserResponse userResponse = await this.cosmosDatabase.CreateUserAsync(userId); User user = userResponse.User; Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode); Assert.AreEqual(userId, user.Id); string permissionId = Guid.NewGuid().ToString(); PermissionProperties permissionProperties = new PermissionProperties(permissionId, PermissionMode.Read, containerResponse.Container); PermissionResponse permissionResponse = await user.CreatePermissionAsync(permissionProperties); Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode); Assert.AreEqual(permissionId, permissionResponse.Resource.Id); Assert.AreEqual(permissionProperties.PermissionMode, permissionResponse.Resource.PermissionMode); Assert.IsNotNull(permissionResponse.Resource.Token); SelflinkValidator.ValidatePermissionSelfLink(permissionResponse.Resource.SelfLink); PermissionProperties newPermissionProperties = new PermissionProperties(permissionId, PermissionMode.All, containerResponse.Container); permissionResponse = await user.GetPermission(permissionId).ReplaceAsync(newPermissionProperties); //Backend returns Created instead of OK Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode); Assert.AreEqual(permissionId, permissionResponse.Resource.Id); Assert.AreEqual(newPermissionProperties.PermissionMode, permissionResponse.Resource.PermissionMode); SelflinkValidator.ValidatePermissionSelfLink(permissionResponse.Resource.SelfLink); permissionResponse = await user.GetPermission(permissionId).ReadAsync(); Assert.AreEqual(HttpStatusCode.OK, permissionResponse.StatusCode); Assert.AreEqual(permissionId, permissionResponse.Resource.Id); SelflinkValidator.ValidatePermissionSelfLink(permissionResponse.Resource.SelfLink); permissionResponse = await user.GetPermission(permissionId).DeleteAsync(); Assert.AreEqual(HttpStatusCode.NoContent, permissionResponse.StatusCode); try { permissionResponse = await user.GetPermission(permissionId).ReadAsync(); Assert.Fail(); } catch (CosmosException ex) { Assert.AreEqual(HttpStatusCode.NotFound, ex.StatusCode); } permissionId = Guid.NewGuid().ToString(); permissionProperties = new PermissionProperties(permissionId, PermissionMode.Read, containerResponse.Container); permissionResponse = await user.CreatePermissionAsync(permissionProperties); Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode); Assert.AreEqual(permissionId, permissionResponse.Resource.Id); Assert.AreEqual(permissionProperties.PermissionMode, permissionResponse.Resource.PermissionMode); Assert.IsNotNull(permissionResponse.Resource.Token); newPermissionProperties = new PermissionProperties(permissionId, PermissionMode.All, containerResponse.Container); permissionResponse = await user.UpsertPermissionAsync(newPermissionProperties); Assert.AreEqual(HttpStatusCode.OK, permissionResponse.StatusCode); Assert.AreEqual(permissionId, permissionResponse.Resource.Id); Assert.AreEqual(newPermissionProperties.PermissionMode, permissionResponse.Resource.PermissionMode); }
public async Task ItemResourcePermissionTest() { //create user string userId = Guid.NewGuid().ToString(); UserResponse userResponse = await this.cosmosDatabase.CreateUserAsync(userId); Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode); Assert.AreEqual(userId, userResponse.Resource.Id); User user = userResponse.User; //create resource string containerId = Guid.NewGuid().ToString(); ContainerResponse containerResponse = await this.cosmosDatabase.CreateContainerAsync(containerId, "/id"); Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode); Container container = containerResponse.Container; string itemId = Guid.NewGuid().ToString(); PartitionKey partitionKey = new PartitionKey(itemId); ItemResponse <dynamic> itemRespnose = await container.CreateItemAsync <dynamic>(new { id = itemId }, partitionKey); Assert.AreEqual(HttpStatusCode.Created, itemRespnose.StatusCode); //create permission string permissionId = Guid.NewGuid().ToString(); PermissionProperties permissionProperties = new PermissionProperties(permissionId, PermissionMode.Read, container, partitionKey, itemId); PermissionResponse permissionResponse = await user.CreatePermissionAsync(permissionProperties); PermissionProperties permission = permissionResponse.Resource; Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode); Assert.AreEqual(permissionId, permission.Id); Assert.AreEqual(permissionProperties.PermissionMode, permission.PermissionMode); //delete resource with PermissionMode.Read using (CosmosClient tokenCosmosClient = TestCommon.CreateCosmosClient(clientOptions: null, resourceToken: permission.Token)) { Container tokenContainer = tokenCosmosClient.GetContainer(this.cosmosDatabase.Id, containerId); ItemResponse <dynamic> readPermissionItem = await tokenContainer.ReadItemAsync <dynamic>(itemId, partitionKey); Assert.AreEqual(itemId, readPermissionItem.Resource.id.ToString()); try { ItemResponse <dynamic> response = await tokenContainer.DeleteItemAsync <dynamic>( itemId, partitionKey); Assert.Fail(); } catch (CosmosException ex) { Assert.AreEqual(HttpStatusCode.Forbidden, ex.StatusCode); } } //update permission to PermissionMode.All permissionProperties = new PermissionProperties(permissionId, PermissionMode.All, container); permissionResponse = await user.GetPermission(permissionId).ReplaceAsync(permissionProperties); permission = permissionResponse.Resource; //delete resource with PermissionMode.All using (CosmosClient tokenCosmosClient = TestCommon.CreateCosmosClient(clientOptions: null, resourceToken: permission.Token)) { FeedIterator <dynamic> feed = tokenCosmosClient .GetDatabase(this.cosmosDatabase.Id) .GetContainer(containerId) .GetItemQueryIterator <dynamic>(new QueryDefinition("select * from t")); while (feed.HasMoreResults) { FeedResponse <dynamic> response = await feed.ReadNextAsync(); Assert.IsNotNull(response); } } }