/// <summary> /// Initialize a HashPartitionResolver that uses a custom function to extract the partition key. /// </summary> /// <param name="partitionKeyExtractor">The partition key extractor function.</param> /// <param name="client">The DocumentDB client instance to use.</param> /// <param name="database">The database to run the samples on.</param> /// <param name="collectionNames">The names of collections used.</param> /// <returns>The created HashPartitionResolver.</returns> public static async Task <HashPartitionResolver> InitializeCustomHashResolver(Func <object, string> partitionKeyExtractor, DocumentClient client, Database database, string[] collectionNames) { // Set local to input. string[] CollectionNames = collectionNames; int numCollectionNames = CollectionNames.Length; // Create array of DocumentCollections. DocumentCollection[] collections = new DocumentCollection[numCollectionNames]; // Create string array of Self Links to Collections. string[] selfLinks = new string[numCollectionNames]; //Create some collections to partition data. for (int i = 0; i < numCollectionNames; i++) { collections[i] = await DocumentClientHelper.GetCollectionAsync(client, database, CollectionNames[i]); selfLinks[i] = collections[i].SelfLink; } // Join Self Link Array into a comma separated string. string selfLinkString = String.Join(", ", selfLinks); var hashResolver = new HashPartitionResolver( partitionKeyExtractor, new[] { selfLinkString }); client.PartitionResolvers[database.SelfLink] = hashResolver; return(hashResolver); }
/// <summary> /// Initialize a HashPartitionResolver. /// </summary> /// <param name="partitionKeyPropertyName">The property name to be used as the partition Key.</param> /// <param name="client">The DocumentDB client instance to use.</param> /// <param name="database">The database to run the samples on.</param> /// <param name="collectionNames">The names of collections used.</param> /// <returns>The created HashPartitionResolver.</returns> public static async Task <HashPartitionResolver> InitializeHashResolver(string partitionKeyPropertyName, DocumentClient client, Database database, string[] collectionNames) { // Set local to input. string[] CollectionNames = collectionNames; int numCollectionNames = CollectionNames.Length; // Create array of DocumentCollections. DocumentCollection[] collections = new DocumentCollection[numCollectionNames]; // Create string array of Self Links to Collections. string[] selfLinks = new string[numCollectionNames]; //Create some collections to partition data. for (int i = 0; i < numCollectionNames; i++) { collections[i] = await DocumentClientHelper.GetCollectionAsync(client, database, CollectionNames[i]); selfLinks[i] = collections[i].SelfLink; } // Join Self Link Array into a comma separated string. string selfLinkString = String.Join(", ", selfLinks); //Initialize a partition resolver that users hashing, and register with DocumentClient. //Uses User Id for PartitionKeyPropertyName, could also be TenantId, or any other variable. HashPartitionResolver hashResolver = new HashPartitionResolver(partitionKeyPropertyName, new[] { selfLinkString }); client.PartitionResolvers[database.SelfLink] = hashResolver; return(hashResolver); }
/// <summary> /// Create a collection if the list is empty, or if the latest one is getting full. /// </summary> private void CreateCollectionIfRequired() { if (this.ShouldCreateCollection()) { string collectionId = string.Format("{0}{1}", this.CollectionIdPrefix, this.CollectionLinks.Count); var createdCollection = DocumentClientHelper.GetCollectionAsync(this.Client, this.Database, collectionId, this.CollectionTemplate).Result; this.CollectionLinks.Add(createdCollection.SelfLink); } }
private static async Task <HashPartitionResolver> InitializeHashResolver(Database database) { var collection1 = await DocumentClientHelper.GetCollectionAsync(client, database, "TweeterStatus.HashBucket0"); var collection2 = await DocumentClientHelper.GetCollectionAsync(client, database, "TweeterStatus.HashBucket1"); HashPartitionResolver hashResolver = new DisplayableHashPartitionResolver(new [] { collection1, collection2 }, PartitionKeyExtractor); client.PartitionResolvers[database.SelfLink] = hashResolver; return(hashResolver); }
/// <summary> /// Initialize a RangePartitionResolver. /// </summary> /// <param name="partitionKeyPropertyName">The property name to be used as the partition Key.</param> /// <param name="client">The DocumentDB client instance to use.</param> /// <param name="database">The database to run the samples on.</param> /// <param name="collectionNames">The names of collections used.</param> /// <returns>The created HashPartitionResolver.</returns> public static async Task <RangePartitionResolver <string> > InitializeRangeResolver(string partitionKeyPropertyName, DocumentClient client, Database database, string[] collectionNames) { // Set local to input. string[] CollectionNames = collectionNames; int numCollectionNames = CollectionNames.Length; // Create array of DocumentCollections. DocumentCollection[] collections = new DocumentCollection[numCollectionNames]; // Create string array of Self Links to Collections. string[] selfLinks = new string[numCollectionNames]; //Create some collections to partition data. for (int i = 0; i < numCollectionNames; i++) { collections[i] = await DocumentClientHelper.GetCollectionAsync(client, database, CollectionNames[i]); selfLinks[i] = collections[i].SelfLink; } // Join Self Link Array into a comma separated string. string selfLinkString = String.Join(", ", selfLinks); // If each collection represents a range, it will have both a start and end point in its range, thus there are 2 rangeNames for each collection. string[] rangeNames = new string[numCollectionNames * 2]; // Keeping track of where in the rangeNames array to add a new start/end of a range. int currentRangePosition = 0; for (int y = 0; y < numCollectionNames; y++) { string[] rangeTemp = collectionNames[y].Split('-'); for (int z = 0; z < rangeTemp.Length; z++) { rangeNames[currentRangePosition] = rangeTemp[z]; currentRangePosition++; } } // Dictionary needed to input into RangePartitionResolver with corresponding range and collection self link. Dictionary <Range <string>, string> rangeCollections = new Dictionary <Range <string>, string>(); // TO DO:: Iterate through the ranges and add then to rangeCollections with the appropriate start/end point, with the corresponding collection self link. //rangeCollections.Add() RangePartitionResolver <string> rangeResolver = new RangePartitionResolver <string>( partitionKeyPropertyName, rangeCollections); client.PartitionResolvers[database.SelfLink] = rangeResolver; return(rangeResolver); }
/// <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.GetCollectionAsync(this.client, database, "Collection.HashBucket0"); DocumentCollection collection2 = await DocumentClientHelper.GetCollectionAsync(this.client, database, "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.GetCollectionAsync(this.client, database, "Collection.HashBucket0"); DocumentCollection collection2 = await DocumentClientHelper.GetCollectionAsync(this.client, database, "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); }
/// <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, Database database, int numberOfCollections, string collectionIdPrefix, DocumentCollectionSpec spec) { var collections = new List <string>(); for (int i = 0; i < numberOfCollections; i++) { string collectionId = string.Format("{0}{1}", collectionIdPrefix, i); var collection = DocumentClientHelper.GetCollectionAsync(client, database, 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.GetCollectionAsync(this.client, database, "Collection.A-M"); DocumentCollection collection2 = await DocumentClientHelper.GetCollectionAsync(this.client, database, "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.GetCollectionAsync(this.client, database, "Collection.US"); DocumentCollection collectionEU = await DocumentClientHelper.GetCollectionAsync(this.client, database, "Collection.Europe"); DocumentCollection collectionOther = await DocumentClientHelper.GetCollectionAsync(this.client, database, "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); }
/// <summary> /// Initialize a LookupPartitionResolver. Default is for US East/West to go to first collection name, Europe to second, and AsiaPacific / Other to third. /// </summary> /// <param name="partitionKeyPropertyName">The property name to be used as the partition Key.</param> /// <param name="client">The DocumentDB client instance to use.</param> /// <param name="database">The database to run the samples on.</param> /// <param name="collectionNames">The names of collections used.</param> /// <returns>The created HashPartitionResolver.</returns> public static async Task <LookupPartitionResolver <string> > InitializeLookupPartitionResolver(string partitionKeyPropertyName, DocumentClient client, Database database, string[] collectionNames) { // Set local to input. string[] CollectionNames = collectionNames; int numCollectionNames = CollectionNames.Length; // Create array of DocumentCollections. DocumentCollection[] collections = new DocumentCollection[numCollectionNames]; // Create string array of Self Links to Collections. string[] selfLinks = new string[numCollectionNames]; //Create some collections to partition data. for (int i = 0; i < numCollectionNames; i++) { collections[i] = await DocumentClientHelper.GetCollectionAsync(client, database, CollectionNames[i]); selfLinks[i] = collections[i].SelfLink; } // 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>( partitionKeyPropertyName, new Dictionary <string, string>() { { Region.UnitedStatesEast.ToString(), selfLinks[0] }, { Region.UnitedStatesWest.ToString(), selfLinks[0] }, { Region.Europe.ToString(), selfLinks[1] }, { Region.AsiaPacific.ToString(), selfLinks[2] }, { Region.Other.ToString(), selfLinks[2] }, }); client.PartitionResolvers[database.SelfLink] = lookupResolver; return(lookupResolver); }