public async Task CreateHashV1Container() { string containerName = Guid.NewGuid().ToString(); string partitionKeyPath = "/users"; CosmosContainerSettings settings = new CosmosContainerSettings(containerName, partitionKeyPath); settings.PartitionKeyDefinitionVersion = Cosmos.PartitionKeyDefinitionVersion.V1; CosmosContainerResponse cosmosContainerResponse = await this.cosmosDatabase.Containers.CreateContainerAsync(settings); Assert.AreEqual(HttpStatusCode.Created, cosmosContainerResponse.StatusCode); Assert.AreEqual(Cosmos.PartitionKeyDefinitionVersion.V1, cosmosContainerResponse.Resource.PartitionKeyDefinitionVersion); }
internal static async Task <CosmosStoredProcedureSettings> GetOrCreateStoredProcedureAsync( DocumentClient client, CosmosContainerSettings collection, CosmosStoredProcedureSettings newStoredProcedure) { var result = (from proc in client.CreateStoredProcedureQuery(collection) where proc.Id == newStoredProcedure.Id select proc).AsEnumerable().FirstOrDefault(); if (result != null) { return(result); } return(await client.CreateStoredProcedureAsync(collection, newStoredProcedure)); }
/// <summary> /// Applies the current Fluent definition and creates a container configuration. /// </summary> /// <returns>Builds the current Fluent configuration into an instance of <see cref="CosmosContainerSettings"/>.</returns> public virtual new CosmosContainerSettings Build() { CosmosContainerSettings settings = base.Build(); if (this.uniqueKeyPolicy != null) { settings.UniqueKeyPolicy = this.uniqueKeyPolicy; } if (this.conflictResolutionPolicy != null) { settings.ConflictResolutionPolicy = this.conflictResolutionPolicy; } return(settings); }
public async Task OpenAsync( CosmosContainerSettings collection, IReadOnlyList <PartitionKeyRangeIdentity> partitionKeyRangeIdentities, CancellationToken cancellationToken) { List <Task <FeedResource <Address> > > tasks = new List <Task <FeedResource <Address> > >(); int batchSize = GatewayAddressCache.DefaultBatchSize; #if !(NETSTANDARD15 || NETSTANDARD16) int userSpecifiedBatchSize = 0; if (int.TryParse(System.Configuration.ConfigurationManager.AppSettings[GatewayAddressCache.AddressResolutionBatchSize], out userSpecifiedBatchSize)) { batchSize = userSpecifiedBatchSize; } #endif using (DocumentServiceRequest request = DocumentServiceRequest.CreateFromName( OperationType.Read, collection.AltLink, ResourceType.Collection, AuthorizationTokenType.PrimaryMasterKey)) { for (int i = 0; i < partitionKeyRangeIdentities.Count; i += batchSize) { tasks.Add(this.GetServerAddressesViaGatewayAsync( request, collection.ResourceId, partitionKeyRangeIdentities.Skip(i).Take(batchSize).Select(range => range.PartitionKeyRangeId), false)); } } foreach (FeedResource <Address> response in await Task.WhenAll(tasks)) { IEnumerable <Tuple <PartitionKeyRangeIdentity, PartitionAddressInformation> > addressInfos = response.Where(addressInfo => ProtocolFromString(addressInfo.Protocol) == this.protocol) .GroupBy(address => address.PartitionKeyRangeId, StringComparer.Ordinal) .Select(group => this.ToPartitionAddressAndRange(collection.ResourceId, @group.ToList())); foreach (Tuple <PartitionKeyRangeIdentity, PartitionAddressInformation> addressInfo in addressInfos) { this.serverPartitionAddressCache.Set( new PartitionKeyRangeIdentity(collection.ResourceId, addressInfo.Item1.PartitionKeyRangeId), addressInfo.Item2); } } }
private async Task <DocumentServiceResponse> ExecutePartitionKeyRangeReadChangeFeed(string collectionRid, INameValueCollection headers) { using (DocumentServiceRequest request = DocumentServiceRequest.Create( OperationType.ReadFeed, collectionRid, ResourceType.PartitionKeyRange, AuthorizationTokenType.PrimaryMasterKey, headers)) { string authorizationToken = null; try { authorizationToken = this.authorizationTokenProvider.GetUserAuthorizationToken( request.ResourceAddress, PathsHelper.GetResourcePath(request.ResourceType), HttpConstants.HttpMethods.Get, request.Headers, AuthorizationTokenType.PrimaryMasterKey); } catch (UnauthorizedException) { } if (authorizationToken == null) { // User doesn't have rid based resource token. Maybe he has name based. CosmosContainerSettings collection = await this.collectionCache.ResolveCollectionAsync(request, CancellationToken.None); authorizationToken = this.authorizationTokenProvider.GetUserAuthorizationToken( collection.AltLink, PathsHelper.GetResourcePath(request.ResourceType), HttpConstants.HttpMethods.Get, request.Headers, AuthorizationTokenType.PrimaryMasterKey); } request.Headers[HttpConstants.HttpHeaders.Authorization] = authorizationToken; using (new ActivityScope(Guid.NewGuid())) { return(await this.storeModel.ProcessMessageAsync(request)); } } }
public async Task TestInitialize() { await base.TestInit(); string PartitionKey = "/status"; this.containerSettings = new CosmosContainerSettings(id: Guid.NewGuid().ToString(), partitionKeyPath: PartitionKey); CosmosContainerResponse response = await this.database.Containers.CreateContainerAsync( this.containerSettings, cancellationToken : this.cancellationToken); Assert.IsNotNull(response); Assert.IsNotNull(response.Container); Assert.IsNotNull(response.Resource); this.Container = response; this.jsonSerializer = new CosmosDefaultJsonSerializer(); }
private static async Task CreateContainerWithTtlExpiration() { CosmosContainerSettings settings = new CosmosContainerSettings (id: "TtlExpiryContainer", partitionKeyPath: partitionKey); settings.DefaultTimeToLive = (int)TimeSpan.FromDays(1).TotalSeconds; //expire in 1 day ContainerResponse ttlEnabledContainerResponse = await database.CreateContainerIfNotExistsAsync( containerSettings : settings); CosmosContainerSettings returnedSettings = ttlEnabledContainerResponse; Console.WriteLine($"\n1.3. Created Container \n{returnedSettings.Id} with TTL expiration of {returnedSettings.DefaultTimeToLive}"); await ttlEnabledContainerResponse.Container.DeleteAsync(); }
internal static Func <bool, IQueryable <Data> > GenerateSimpleData( DocumentClient client, CosmosDatabaseSettings testDb, out CosmosContainerSettings testCollection) { const int DocumentCount = 10; testCollection = client.CreateDocumentCollectionAsync( testDb.GetLink(), new CosmosContainerSettings() { Id = Guid.NewGuid().ToString() }).Result; Random random = new Random(); List <Data> testData = new List <Data>(); for (int index = 0; index < DocumentCount; index++) { Data dataEntry = new Data() { Number = random.Next(-10000, 10000), Flag = index % 2 == 0 ? true : false, Multiples = new int[] { index, index * 2, index * 3, index * 4 } }; client.CreateDocumentAsync(testCollection.GetLink(), dataEntry).Wait(); testData.Add(dataEntry); } FeedOptions feedOptions = new FeedOptions() { EnableScanInQuery = true, EnableCrossPartitionQuery = true }; var query = client.CreateDocumentQuery <Data>(testCollection.GetLink()).AsQueryable(); // To cover both query against backend and queries on the original data using LINQ nicely, // the LINQ expression should be written once and they should be compiled and executed against the two sources. // That is done by using Func that take a boolean Func. The parameter of the Func indicate whether the Cosmos DB query // or the data list should be used. When a test is executed, the compiled LINQ expression would pass different values // to this getQuery method. Func <bool, IQueryable <Data> > getQuery = useQuery => useQuery ? query : testData.AsQueryable(); return(getQuery); }
private static async Task CreateContainerWithCustomIndexingPolicy() { // Create a container with custom index policy (lazy indexing) // We cover index policies in detail in IndexManagement sample project CosmosContainerSettings containerSettings = new CosmosContainerSettings( id: "SampleContainerWithCustomIndexPolicy", partitionKeyPath: partitionKey); containerSettings.IndexingPolicy.IndexingMode = IndexingMode.Lazy; CosmosContainer containerWithLazyIndexing = await database.CreateContainerIfNotExistsAsync( containerSettings, requestUnitsPerSecond : 400); Console.WriteLine($"1.2. Created Container {containerWithLazyIndexing.Id}, with custom index policy \n"); await containerWithLazyIndexing.DeleteAsync(); }
public CosmosProxyItemQueryExecutionContext( CosmosQueryContext queryContext, CosmosContainerSettings containerSettings) { if (queryContext == null) { throw new ArgumentNullException(nameof(queryContext)); } if (queryContext == null) { throw new ArgumentNullException(nameof(queryContext)); } this.innerExecutionContext = new CosmosGatewayQueryExecutionContext(queryContext); this.queryContext = queryContext; this.containerSettings = containerSettings; }
private static async Task RoundTripWithLocal(Cosmos.IndexingPolicy indexingPolicy) { PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition { Paths = new System.Collections.ObjectModel.Collection <string>(new[] { "/id" }), Kind = PartitionKind.Hash }; CosmosContainerSettings containerSetting = new CosmosContainerSettings() { Id = Guid.NewGuid().ToString(), IndexingPolicy = indexingPolicy, PartitionKey = partitionKeyDefinition }; CosmosDatabase cosmosDatabase = await cosmosClient.Databases.CreateDatabaseIfNotExistsAsync(IndexingPolicyTests.database.Id); CosmosContainerResponse cosmosContainerResponse = await cosmosDatabase.Containers.CreateContainerAsync(containerSetting); Assert.IsTrue(IndexingPolicyTests.indexingPolicyEqualityComparer.Equals(indexingPolicy, containerSetting.IndexingPolicy)); }
public async Task ContainerContractTest() { CosmosContainerResponse response = await this.cosmosDatabase.Containers.CreateContainerAsync(new Guid().ToString(), "/id"); Assert.IsNotNull(response); Assert.IsTrue(response.RequestCharge > 0); Assert.IsNotNull(response.Headers); Assert.IsNotNull(response.Headers.ActivityId); CosmosContainerSettings containerSettings = response.Resource; Assert.IsNotNull(containerSettings.Id); Assert.IsNotNull(containerSettings.ResourceId); Assert.IsNotNull(containerSettings.ETag); Assert.IsTrue(containerSettings.LastModified.HasValue); Assert.IsTrue(containerSettings.LastModified.Value > new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), containerSettings.LastModified.Value.ToString()); }
/// <summary> /// Gets the list of partition key ranges. /// 1. Check partition key range id /// 2. Check Partition key /// 3. Check the effective partition key /// 4. Get the range from the PartitionedQueryExecutionInfo /// </summary> internal static async Task <List <PartitionKeyRange> > GetTargetPartitionKeyRangesAsync( CosmosQueryClient queryClient, string resourceLink, PartitionedQueryExecutionInfo partitionedQueryExecutionInfo, CosmosContainerSettings collection, QueryRequestOptions queryRequestOptions) { List <PartitionKeyRange> targetRanges; if (queryRequestOptions.PartitionKey != null) { // Dis-ambiguate the NonePK if used PartitionKeyInternal partitionKeyInternal = null; if (Object.ReferenceEquals(queryRequestOptions.PartitionKey, CosmosContainerSettings.NonePartitionKeyValue)) { partitionKeyInternal = collection.GetNoneValue(); } else { partitionKeyInternal = new PartitionKey(queryRequestOptions.PartitionKey).InternalKey; } targetRanges = await queryClient.GetTargetPartitionKeyRangesByEpkStringAsync( resourceLink, collection.ResourceId, partitionKeyInternal.GetEffectivePartitionKeyString(collection.PartitionKey)); } else if (TryGetEpkProperty(queryRequestOptions, out string effectivePartitionKeyString)) { targetRanges = await queryClient.GetTargetPartitionKeyRangesByEpkStringAsync( resourceLink, collection.ResourceId, effectivePartitionKeyString); } else { targetRanges = await queryClient.GetTargetPartitionKeyRangesAsync( resourceLink, collection.ResourceId, partitionedQueryExecutionInfo.QueryRanges); } return(targetRanges); }
public void ContainerSettingsDefaults() { string id = Guid.NewGuid().ToString(); string pkPath = "/partitionKey"; SettingsContractTests.TypeAccessorGuard(typeof(CosmosContainerSettings), "Id", "UniqueKeyPolicy", "DefaultTimeToLive", "IndexingPolicy", "TimeToLivePropertyPath", "PartitionKeyPath", "PartitionKeyDefinitionVersion", "ConflictResolutionPolicy"); // Two equivalent definitions CosmosContainerSettings cosmosContainerSettings = new CosmosContainerSettings(id, pkPath); Assert.AreEqual(id, cosmosContainerSettings.Id); Assert.AreEqual(pkPath, cosmosContainerSettings.PartitionKeyPath); Assert.IsNull(cosmosContainerSettings.ResourceId); Assert.IsNull(cosmosContainerSettings.LastModified); Assert.IsNull(cosmosContainerSettings.ETag); Assert.IsNull(cosmosContainerSettings.DefaultTimeToLive); Assert.IsNotNull(cosmosContainerSettings.IndexingPolicy); Assert.IsNotNull(cosmosContainerSettings.ConflictResolutionPolicy); Assert.IsTrue(object.ReferenceEquals(cosmosContainerSettings.IndexingPolicy, cosmosContainerSettings.IndexingPolicy)); Assert.IsNotNull(cosmosContainerSettings.IndexingPolicy.IncludedPaths); Assert.IsTrue(object.ReferenceEquals(cosmosContainerSettings.IndexingPolicy.IncludedPaths, cosmosContainerSettings.IndexingPolicy.IncludedPaths)); Cosmos.IncludedPath ip = new Cosmos.IncludedPath(); Assert.IsNotNull(ip.Indexes); Assert.IsNotNull(cosmosContainerSettings.UniqueKeyPolicy); Assert.IsTrue(object.ReferenceEquals(cosmosContainerSettings.UniqueKeyPolicy, cosmosContainerSettings.UniqueKeyPolicy)); Assert.IsNotNull(cosmosContainerSettings.UniqueKeyPolicy.UniqueKeys); Assert.IsTrue(object.ReferenceEquals(cosmosContainerSettings.UniqueKeyPolicy.UniqueKeys, cosmosContainerSettings.UniqueKeyPolicy.UniqueKeys)); Cosmos.UniqueKey uk = new Cosmos.UniqueKey(); Assert.IsNotNull(uk.Paths); }
private async Task <CosmosContainerSettings> SetupSingleCollectionScenario() { DocumentClient client = TestCommon.CreateClient(true); TestCommon.DeleteAllDatabasesAsync(client).Wait(); CosmosDatabaseSettings database = (await client.CreateDatabaseAsync(new CosmosDatabaseSettings { Id = this.DatabaseName })).Resource; CosmosContainerSettings collection = (await client.CreateDocumentCollectionIfNotExistsAsync(database.SelfLink, new CosmosContainerSettings { Id = this.CollectionName }, new RequestOptions { OfferThroughput = 10000 })).Resource; // await Task.Delay(30000); return(collection); }
internal static async Task WaitForReIndexingToFinish( int maxWaitDurationInSeconds, CosmosContainerSettings collection) { await Task.Delay(TimeSpan.FromSeconds(5)); int currentWaitSeconds = 0; var lockedClients = ReplicationTests.GetClientsLocked(); for (int index = 0; index < lockedClients.Length; ++index) { Logger.LogLine("Client: " + index); while (true) { long reindexerProgress = (await TestCommon.AsyncRetryRateLimiting( () => lockedClients[index].ReadDocumentCollectionAsync(collection.SelfLink, new RequestOptions { PopulateQuotaInfo = true }))).IndexTransformationProgress; Logger.LogLine("Progress: " + reindexerProgress); if (reindexerProgress == -1) { throw new Exception("Failed to obtain the reindexer progress."); } else if (reindexerProgress == 100) { Logger.LogLine("ReIndexing finished after: " + currentWaitSeconds + " seconds"); break; } else { await Task.Delay(TimeSpan.FromSeconds(1)); currentWaitSeconds++; Logger.LogLine("ReIndexing still running after: " + currentWaitSeconds + " seconds"); if (currentWaitSeconds > maxWaitDurationInSeconds) { throw new Exception("ReIndexing did not complete after: " + maxWaitDurationInSeconds + " seconds"); } } } } }
public async Task PartitionedCRUDTest() { string containerName = Guid.NewGuid().ToString(); string partitionKeyPath = "/users"; ContainerResponse containerResponse = await this.cosmosDatabase.CreateContainerAsync(containerName, partitionKeyPath); Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode); Assert.AreEqual(containerName, containerResponse.Resource.Id); Assert.AreEqual(partitionKeyPath, containerResponse.Resource.PartitionKey.Paths.First()); CosmosContainerSettings settings = new CosmosContainerSettings(containerName, partitionKeyPath) { IndexingPolicy = new Cosmos.IndexingPolicy() { IndexingMode = Cosmos.IndexingMode.None, Automatic = false } }; CosmosContainer cosmosContainer = containerResponse; containerResponse = await cosmosContainer.ReplaceAsync(settings); Assert.AreEqual(HttpStatusCode.OK, containerResponse.StatusCode); Assert.AreEqual(containerName, containerResponse.Resource.Id); Assert.AreEqual(partitionKeyPath, containerResponse.Resource.PartitionKey.Paths.First()); Assert.AreEqual(Cosmos.IndexingMode.None, containerResponse.Resource.IndexingPolicy.IndexingMode); Assert.IsFalse(containerResponse.Resource.IndexingPolicy.Automatic); containerResponse = await cosmosContainer.ReadAsync(); Assert.AreEqual(HttpStatusCode.OK, containerResponse.StatusCode); Assert.AreEqual(containerName, containerResponse.Resource.Id); Assert.AreEqual(Cosmos.PartitionKeyDefinitionVersion.V2, containerResponse.Resource.PartitionKeyDefinitionVersion); Assert.AreEqual(partitionKeyPath, containerResponse.Resource.PartitionKey.Paths.First()); Assert.AreEqual(Cosmos.IndexingMode.None, containerResponse.Resource.IndexingPolicy.IndexingMode); Assert.IsFalse(containerResponse.Resource.IndexingPolicy.Automatic); containerResponse = await containerResponse.Container.DeleteAsync(); Assert.AreEqual(HttpStatusCode.NoContent, containerResponse.StatusCode); }
public async Task TestConflictResolutionPolicy() { string containerName = Guid.NewGuid().ToString(); string partitionKeyPath = "/users"; ContainerResponse containerResponse = await this.database.DefineContainer(containerName, partitionKeyPath) .WithConflictResolution() .WithLastWriterWinsResolution("/lww") .Attach() .CreateAsync(); Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode); Assert.AreEqual(containerName, containerResponse.Resource.Id); Assert.AreEqual(partitionKeyPath, containerResponse.Resource.PartitionKey.Paths.First()); CosmosContainerSettings cosmosContainerSettings = containerResponse.Resource; Assert.IsNotNull(cosmosContainerSettings.ConflictResolutionPolicy); Assert.AreEqual(ConflictResolutionMode.LastWriterWins, cosmosContainerSettings.ConflictResolutionPolicy.Mode); Assert.AreEqual("/lww", cosmosContainerSettings.ConflictResolutionPolicy.ConflictResolutionPath); Assert.IsTrue(string.IsNullOrEmpty(cosmosContainerSettings.ConflictResolutionPolicy.ConflictResolutionProcedure)); // Delete container await containerResponse.Container.DeleteAsync(); // Re-create with custom policy string sprocName = "customresolsproc"; containerResponse = await this.database.DefineContainer(containerName, partitionKeyPath) .WithConflictResolution() .WithCustomStoredProcedureResolution(sprocName) .Attach() .CreateAsync(); Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode); Assert.AreEqual(containerName, containerResponse.Resource.Id); Assert.AreEqual(partitionKeyPath, containerResponse.Resource.PartitionKey.Paths.First()); cosmosContainerSettings = containerResponse.Resource; Assert.IsNotNull(cosmosContainerSettings.ConflictResolutionPolicy); Assert.AreEqual(ConflictResolutionMode.Custom, cosmosContainerSettings.ConflictResolutionPolicy.Mode); Assert.AreEqual(sprocName, cosmosContainerSettings.ConflictResolutionPolicy.ConflictResolutionProcedure); Assert.IsTrue(string.IsNullOrEmpty(cosmosContainerSettings.ConflictResolutionPolicy.ConflictResolutionPath)); }
internal void TestEtagOnUpsertOperation(DocumentClient client) { CosmosDatabaseSettings db = client.CreateDatabaseAsync(new CosmosDatabaseSettings() { Id = Guid.NewGuid().ToString() }).Result.Resource; CosmosContainerSettings coll = TestCommon.CreateCollectionAsync(client, db, new CosmosContainerSettings() { Id = Guid.NewGuid().ToString() }).Result; LinqGeneralBaselineTests.Book myBook = new LinqGeneralBaselineTests.Book(); myBook.Id = Guid.NewGuid().ToString(); myBook.Title = "Azure DocumentDB 101"; Document doc = client.CreateDocumentAsync(coll, myBook).Result.Resource; myBook.Title = "Azure DocumentDB 201"; client.ReplaceDocumentAsync(doc.SelfLink, myBook).Wait(); AccessCondition condition = new AccessCondition(); condition.Type = AccessConditionType.IfMatch; condition.Condition = doc.ETag; RequestOptions requestOptions = new RequestOptions(); requestOptions.AccessCondition = condition; myBook.Title = "Azure DocumentDB 301"; try { client.UpsertDocumentAsync(coll.SelfLink, myBook, requestOptions).Wait(); Assert.Fail("Upsert Document should fail since the Etag is not matching."); } catch (Exception ex) { var innerException = ex.InnerException as DocumentClientException; Assert.IsTrue(innerException.StatusCode == HttpStatusCode.PreconditionFailed, "Invalid status code"); } }
internal static async Task WaitForLazyIndexingToCompleteAsync(CosmosContainerSettings collection) { TimeSpan maxWaitTime = TimeSpan.FromMinutes(10); TimeSpan sleepTimeBetweenReads = TimeSpan.FromSeconds(1); // First wait for replication to complete TestCommon.WaitForServerReplication(); var lockedClients = ReplicationTests.GetClientsLocked(); for (int index = 0; index < lockedClients.Length; ++index) { Logger.LogLine("Client: " + index); while (true) { long lazyIndexingProgress = (await lockedClients[index].ReadDocumentCollectionAsync(collection, new RequestOptions { PopulateQuotaInfo = true })).LazyIndexingProgress; if (lazyIndexingProgress == -1) { throw new Exception("Failed to obtain the lazy indexing progress."); } else if (lazyIndexingProgress == 100) { Logger.LogLine("Indexing completed at {0}", DateTime.Now.ToString("HH:mm:ss.f", CultureInfo.InvariantCulture)); break; } else { Logger.LogLine("Obtained the lazy indexing progress: {0}. Sleep for {1} seconds", lazyIndexingProgress, sleepTimeBetweenReads.TotalSeconds); await Task.Delay(sleepTimeBetweenReads); maxWaitTime -= sleepTimeBetweenReads; if (maxWaitTime.TotalMilliseconds <= 0) { throw new Exception("Indexing didn't complete within the allocated time"); } } } } }
public async Task PartitionedCreateWithPathDelete() { string containerName = Guid.NewGuid().ToString(); string partitionKeyPath = "/users"; PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition(); partitionKeyDefinition.Paths.Add(partitionKeyPath); CosmosContainerSettings settings = new CosmosContainerSettings(containerName, partitionKeyDefinition); CosmosContainerResponse containerResponse = await this.cosmosDatabase.Containers.CreateContainerAsync(settings); Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode); Assert.AreEqual(containerName, containerResponse.Resource.Id); Assert.AreEqual(partitionKeyPath, containerResponse.Resource.PartitionKey.Paths.First()); containerResponse = await containerResponse.Container.DeleteAsync(); Assert.AreEqual(HttpStatusCode.NoContent, containerResponse.StatusCode); }
public CosmosContainerSettings ToCosmosContainerSettings() { var settings = new CosmosContainerSettings(ContainerId, PartitionKey); if (UniqueKeys != null && UniqueKeys.Any()) { settings.UniqueKeyPolicy = new UniqueKeyPolicy { UniqueKeys = new Collection <UniqueKey> { new UniqueKey { Paths = UniqueKeys } } }; } return(settings); }
public async Task TimeToLiveTest() { string containerName = Guid.NewGuid().ToString(); string partitionKeyPath = "/users"; TimeSpan timeToLive = TimeSpan.FromSeconds(1); CosmosContainerSettings setting = new CosmosContainerSettings() { Id = containerName, PartitionKey = new PartitionKeyDefinition() { Paths = new Collection <string> { partitionKeyPath }, Kind = PartitionKind.Hash }, DefaultTimeToLive = timeToLive }; CosmosContainerResponse containerResponse = await this.cosmosDatabase.Containers.CreateContainerIfNotExistsAsync(setting); Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode); CosmosContainer cosmosContainer = containerResponse; CosmosContainerSettings responseSettings = containerResponse; Assert.AreEqual(timeToLive.TotalSeconds, responseSettings.DefaultTimeToLive.Value.TotalSeconds); CosmosContainerResponse readResponse = await cosmosContainer.ReadAsync(); Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode); Assert.AreEqual(timeToLive.TotalSeconds, readResponse.Resource.DefaultTimeToLive.Value.TotalSeconds); JObject itemTest = JObject.FromObject(new { id = Guid.NewGuid().ToString(), users = "testUser42" }); CosmosItemResponse <JObject> createResponse = await cosmosContainer.Items.CreateItemAsync <JObject>(partitionKey : itemTest["users"].ToString(), item : itemTest); JObject responseItem = createResponse; Assert.IsNull(responseItem["ttl"]); containerResponse = await cosmosContainer.DeleteAsync(); Assert.AreEqual(HttpStatusCode.NoContent, containerResponse.StatusCode); }
public async Task StreamPartitionedCreateWithPathDelete() { string containerName = Guid.NewGuid().ToString(); string partitionKeyPath = "/users"; PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition(); partitionKeyDefinition.Paths.Add(partitionKeyPath); CosmosContainerSettings settings = new CosmosContainerSettings(containerName, partitionKeyDefinition); using (CosmosResponseMessage containerResponse = await this.cosmosDatabase.Containers.CreateContainerStreamAsync(settings.GetResourceStream())) { Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode); } using (CosmosResponseMessage containerResponse = await this.cosmosDatabase.Containers[containerName].DeleteStreamAsync()) { Assert.AreEqual(HttpStatusCode.NoContent, containerResponse.StatusCode); } }
/// <summary> /// This method is only used in client SDK in retry policy as it doesn't have request handy. /// </summary> public void Refresh(string resourceAddress) { if (PathsHelper.IsNameBased(resourceAddress)) { string resourceFullName = PathsHelper.GetCollectionPath(resourceAddress); this.collectionInfoByNameCache.Refresh( resourceFullName, async() => { CosmosContainerSettings collection = await this.GetByNameAsync(resourceFullName, CancellationToken.None); if (collection != null) { this.collectionInfoByIdCache.Set(collection.ResourceId, collection); } return(collection); }, CancellationToken.None); } }
private static async Task Initialize(CosmosClient client) { database = await client.Databases.CreateDatabaseIfNotExistsAsync(databaseId); // Delete the existing container to prevent create item conflicts await database.Containers[containerId].DeleteAsync(); // We create a partitioned collection here which needs a partition key. Partitioned collections // can be created with very high values of provisioned throughput (up to Throughput = 250,000) // and used to store up to 250 GB of data. You can also skip specifying a partition key to create // single partition collections that store up to 10 GB of data. // For this demo, we create a collection to store SalesOrders. We set the partition key to the account // number so that we can retrieve all sales orders for an account efficiently from a single partition, // and perform transactions across multiple sales order for a single account number. CosmosContainerSettings containerSettings = new CosmosContainerSettings(containerId, partitionKeyPath: "/AccountNumber"); // Create with a throughput of 1000 RU/s container = await database.Containers.CreateContainerIfNotExistsAsync( containerSettings, throughput : 1000); }
public void TestInitialize() { // The test collection should have range index on string properties // for the orderby tests var newCol = new CosmosContainerSettings() { Id = Guid.NewGuid().ToString(), IndexingPolicy = new IndexingPolicy() { IncludedPaths = new System.Collections.ObjectModel.Collection <IncludedPath>() { new IncludedPath() { Path = "/*", Indexes = new System.Collections.ObjectModel.Collection <Index>() { Index.Range(DataType.Number, -1), Index.Range(DataType.String, -1) } } } } }; testCollection = client.CreateDocumentCollectionAsync(testDb, newCol).Result; const int Records = 100; const int MaxStringLength = 100; Func <Random, Datum> createDataFunc = random => { var obj = new Datum(); obj.JsonProperty = random.NextDouble() < 0.3 ? "Hello" : LinqTestsCommon.RandomString(random, random.Next(MaxStringLength)); obj.JsonPropertyAndDataMember = random.NextDouble() < 0.3 ? "Hello" : LinqTestsCommon.RandomString(random, random.Next(MaxStringLength)); obj.DataMember = random.NextDouble() < 0.3 ? "Hello" : LinqTestsCommon.RandomString(random, random.Next(MaxStringLength)); obj.Default = random.NextDouble() < 0.3 ? "Hello" : LinqTestsCommon.RandomString(random, random.Next(MaxStringLength)); return(obj); }; getQuery = LinqTestsCommon.GenerateTestData(createDataFunc, Records, client, testCollection); }
public void ContainerSettingsWithConflictResolution() { string id = Guid.NewGuid().ToString(); string pkPath = "/partitionKey"; // Two equivalent definitions CosmosContainerSettings cosmosContainerSettings = new CosmosContainerSettings(id, pkPath) { ConflictResolutionPolicy = new Cosmos.ConflictResolutionPolicy() { Mode = Cosmos.ConflictResolutionMode.Custom, ConflictResolutionPath = "/path", ConflictResolutionProcedure = "sp" } }; DocumentCollection collection = new DocumentCollection() { Id = id, ConflictResolutionPolicy = new ConflictResolutionPolicy() { Mode = ConflictResolutionMode.Custom, ConflictResolutionPath = "/path", ConflictResolutionProcedure = "sp" } }; string cosmosSerialized = SettingsContractTests.CosmosSerialize(cosmosContainerSettings); string directSerialized = SettingsContractTests.DirectSerialize(collection); // Swap de-serialize and validate CosmosContainerSettings containerDeserSettings = SettingsContractTests.CosmosDeserialize <CosmosContainerSettings>(directSerialized); DocumentCollection collectionDeser = SettingsContractTests.DirectDeSerialize <DocumentCollection>(cosmosSerialized); Assert.AreEqual(cosmosContainerSettings.Id, collectionDeser.Id); Assert.AreEqual((int)cosmosContainerSettings.ConflictResolutionPolicy.Mode, (int)collectionDeser.ConflictResolutionPolicy.Mode); Assert.AreEqual(cosmosContainerSettings.ConflictResolutionPolicy.ConflictResolutionPath, collectionDeser.ConflictResolutionPolicy.ConflictResolutionPath); Assert.AreEqual(cosmosContainerSettings.ConflictResolutionPolicy.ConflictResolutionProcedure, collectionDeser.ConflictResolutionPolicy.ConflictResolutionProcedure); }
/// <summary> /// Gets the list of partition key ranges. /// 1. Check partition key range id /// 2. Check Partition key /// 3. Check the effective partition key /// 4. Get the range from the PartitionedQueryExecutionInfo /// </summary> internal static async Task <List <PartitionKeyRange> > GetTargetPartitionKeyRanges( DefaultDocumentQueryExecutionContext queryExecutionContext, PartitionedQueryExecutionInfo partitionedQueryExecutionInfo, CosmosContainerSettings collection, FeedOptions feedOptions) { List <PartitionKeyRange> targetRanges = null; if (!string.IsNullOrEmpty(feedOptions.PartitionKeyRangeId)) { targetRanges = new List <PartitionKeyRange>() { await queryExecutionContext.GetTargetPartitionKeyRangeById( collection.ResourceId, feedOptions.PartitionKeyRangeId) }; } else if (feedOptions.PartitionKey != null) { targetRanges = await queryExecutionContext.GetTargetPartitionKeyRangesByEpkString( collection.ResourceId, feedOptions.PartitionKey.InternalKey.GetEffectivePartitionKeyString(collection.PartitionKey)); } else if (TryGetEpkProperty(feedOptions, out string effectivePartitionKeyString)) { targetRanges = await queryExecutionContext.GetTargetPartitionKeyRangesByEpkString( collection.ResourceId, effectivePartitionKeyString); } else { targetRanges = await queryExecutionContext.GetTargetPartitionKeyRanges( collection.ResourceId, partitionedQueryExecutionInfo.QueryRanges); } return(targetRanges); }
/// <summary> /// Applies the current Fluent definition and creates a container configuration. /// </summary> /// <returns>Builds the current Fluent configuration into an instance of <see cref="CosmosContainerSettings"/>.</returns> public virtual CosmosContainerSettings Build() { CosmosContainerSettings settings = new CosmosContainerSettings(id: this.containerName, partitionKeyPath: this.partitionKeyPath); if (this.indexingPolicy != null) { settings.IndexingPolicy = this.indexingPolicy; } if (this.defaultTimeToLive.HasValue) { settings.DefaultTimeToLive = this.defaultTimeToLive.Value; } if (this.timeToLivePropertyPath != null) { settings.TimeToLivePropertyPath = timeToLivePropertyPath; } settings.ValidateRequiredProperties(); return(settings); }