/// <summary> /// Initialize a HashPartitionResolver that uses a custom function to extract the partition key. /// </summary> /// <param name="database">The database to run the samples on.</param> /// <returns>The created HashPartitionResolver.</returns> private async Task <HashPartitionResolver> InitializeCustomHashResolver(Database database) { DocumentCollection collection1 = await DocumentClientHelper.GetOrCreateCollectionAsync(this.client, database.Id, "Collection.HashBucket0"); DocumentCollection collection2 = await DocumentClientHelper.GetOrCreateCollectionAsync(this.client, database.Id, "Collection.HashBucket1"); var hashResolver = new HashPartitionResolver( u => ((UserProfile)u).UserId, new[] { collection1.SelfLink, collection2.SelfLink }); this.client.PartitionResolvers[database.SelfLink] = hashResolver; return(hashResolver); }
/// <summary> /// Initialize a HashPartitionResolver. /// </summary> /// <param name="database">The database to run the samples on.</param> /// <returns>The created HashPartitionResolver.</returns> private async Task <HashPartitionResolver> InitializeHashResolver(Database database) { // Create some collections to partition data. DocumentCollection collection1 = await DocumentClientHelper.GetOrCreateCollectionAsync(this.client, database.Id, "Collection.HashBucket0"); DocumentCollection collection2 = await DocumentClientHelper.GetOrCreateCollectionAsync(this.client, database.Id, "Collection.HashBucket1"); // Initialize a partition resolver that users hashing, and register with DocumentClient. HashPartitionResolver hashResolver = new HashPartitionResolver("UserId", new[] { collection1.SelfLink, collection2.SelfLink }); this.client.PartitionResolvers[database.SelfLink] = hashResolver; return(hashResolver); }
private void CreateCollectionIfRequired() { if (this.ShouldCreateCollection()) { try { string collectionId = string.Format("{0}{1}", this.CollectionIdPrefix, NextCollectionNumber); var createdCollection = DocumentClientHelper.GetOrCreateCollectionAsync(this.Client, this.Database.Id, collectionId, this.CollectionTemplate).Result; this.CollectionLinks.Add(createdCollection.SelfLink); } catch { this.CollectionLinks = GetCollections(this.Client, this.Database, this.CollectionIdPrefix, this.CollectionTemplate); } } }
/// <summary> /// Gets or creates the collections for the hash resolver. /// </summary> /// <param name="client">The DocumentDB client instance.</param> /// <param name="database">The database to use.</param> /// <param name="numberOfCollections">The number of collections.</param> /// <param name="collectionIdPrefix">The prefix to use while creating collections.</param> /// <param name="spec">The specification/template to use to create collections.</param> /// <returns>The list of collection self links.</returns> private static List <string> GetCollections( DocumentClient client, string databaseId, int numberOfCollections, string collectionIdPrefix, DocumentCollectionSpec spec) { var collections = new List <string>(); for (int i = 0; i < numberOfCollections; i++) { var collectionId = string.Format("{0}{1}", collectionIdPrefix, i); var collection = DocumentClientHelper.GetOrCreateCollectionAsync(client, databaseId, collectionId, spec).Result; collections.Add(collection.SelfLink); } return(collections); }
/// <summary> /// Initialize a RangePartitionResolver. /// </summary> /// <param name="database">The database to run the samples on.</param> /// <returns>The created RangePartitionResolver.</returns> private async Task <RangePartitionResolver <string> > InitializeRangeResolver(Database database) { // Create some collections to partition data. DocumentCollection collection1 = await DocumentClientHelper.GetOrCreateCollectionAsync(this.client, database.Id, "Collection.A-M"); DocumentCollection collection2 = await DocumentClientHelper.GetOrCreateCollectionAsync(this.client, database.Id, "Collection.N-Z"); // Initialize a partition resolver that assigns users (A-M) -> collection1, and (N-Z) -> collection2 // and register with DocumentClient. // Note: \uffff is the largest UTF8 value, so M\ufff includes all strings that start with M. RangePartitionResolver <string> rangeResolver = new RangePartitionResolver <string>( "UserId", new Dictionary <Range <string>, string>() { { new Range <string>("A", "M\uffff"), collection1.SelfLink }, { new Range <string>("N", "Z\uffff"), collection2.SelfLink }, }); this.client.PartitionResolvers[database.SelfLink] = rangeResolver; return(rangeResolver); }
/// <summary> /// Initialize a LookupPartitionResolver. /// </summary> /// <param name="database">The database to run the samples on.</param> /// <returns>The created HashPartitionResolver.</returns> private async Task <LookupPartitionResolver <string> > InitializeLookupPartitionResolver(Database database) { DocumentCollection collectionUS = await DocumentClientHelper.GetOrCreateCollectionAsync(this.client, database.Id, "Collection.US"); DocumentCollection collectionEU = await DocumentClientHelper.GetOrCreateCollectionAsync(this.client, database.Id, "Collection.Europe"); DocumentCollection collectionOther = await DocumentClientHelper.GetOrCreateCollectionAsync(this.client, database.Id, "Collection.Other"); // This implementation takes strings as input. If you'd like to implement a strongly typed LookupPartitionResolver, // take a look at EnumLookupPartitionResolver for an example. var lookupResolver = new LookupPartitionResolver <string>( "PrimaryRegion", new Dictionary <string, string>() { { Region.UnitedStatesEast.ToString(), collectionUS.SelfLink }, { Region.UnitedStatesWest.ToString(), collectionUS.SelfLink }, { Region.Europe.ToString(), collectionEU.SelfLink }, { Region.AsiaPacific.ToString(), collectionOther.SelfLink }, { Region.Other.ToString(), collectionOther.SelfLink }, }); this.client.PartitionResolvers[database.SelfLink] = lookupResolver; return(lookupResolver); }