Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initialize a HashPartitionResolver.
        /// </summary>
        /// <param name="database">The database to run the samples on.</param>
        /// <returns>The created HashPartitionResolver.</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);
        }
Ejemplo n.º 4
0
        /// <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);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Run samples for Database create, read, update and delete.
        /// </summary>
        /// <returns>a Task object.</returns>
        private async Task RunAsync()
        {
            // Let's see how a to use a HashPartitionResolver.
            Console.WriteLine("Running samples with the default hash partition resolver ...");
            Database database = await DocumentClientHelper.GetNewDatabaseAsync(this.client, DatabaseId);

            HashPartitionResolver hashResolver = await this.InitializeHashResolver(database);

            await this.RunCrudAndQuerySample(database, hashResolver);

            // Let's see how to use a RangePartitionResolver.
            Console.WriteLine("Running samples with the default range partition resolver ...");
            database = await DocumentClientHelper.GetNewDatabaseAsync(this.client, DatabaseId);

            RangePartitionResolver <string> rangeResolver = await this.InitializeRangeResolver(database);

            await this.RunCrudAndQuerySample(database, rangeResolver);

            // Now let's take a look at an example of how to derive from one of the supported partition resolvers.
            // Here we implement a generic hash resolver that takes an arbirary lambda to extract partition keys.
            database = await DocumentClientHelper.GetNewDatabaseAsync(this.client, DatabaseId);

            HashPartitionResolver customHashResolver = await this.InitializeCustomHashResolver(database);

            await this.RunCrudAndQuerySample(database, customHashResolver);

            // Let's take a look at a example of a LookupPartitionResolver, i.e., use a simple lookup table.
            Console.WriteLine("Running samples with a lookup partition resolver ...");
            database = await DocumentClientHelper.GetNewDatabaseAsync(this.client, DatabaseId);

            LookupPartitionResolver <string> lookupResolver = await this.InitializeLookupPartitionResolver(database);

            await this.RunCrudAndQuerySample(database, lookupResolver);

            // Now, a "managed" HashPartitionResolver that creates collections, while cloning their attributes like
            // IndexingPolicy and OfferType.
            Console.WriteLine("Running samples with a custom hash partition resolver that automates creating collections ...");
            database = await DocumentClientHelper.GetNewDatabaseAsync(this.client, DatabaseId);

            ManagedHashPartitionResolver managedHashResolver = this.InitializeManagedHashResolver(database);

            await this.RunCrudAndQuerySample(database, managedHashResolver);

            // Now, a PartitionResolver that starts with one collection and spills over to new ones as the collections
            // get filled up.
            Console.WriteLine("Running samples with a custom \"spillover\" partition resolver");
            database = await DocumentClientHelper.GetNewDatabaseAsync(this.client, DatabaseId);

            SpilloverPartitionResolver spilloverResolver = new SpilloverPartitionResolver(this.client, database);

            this.client.PartitionResolvers[database.SelfLink] = spilloverResolver;
            await this.RunCrudAndQuerySample(database, spilloverResolver);

            // Now let's see how to persist the settings for a PartitionResolver, and how to bootstrap from those settings.
            this.RunSerializeDeserializeSample(hashResolver);

            // Now let's take a look at how to add and remove partitions to a HashPartitionResolver.
            database = await DocumentClientHelper.GetNewDatabaseAsync(this.client, DatabaseId);

            await this.RepartitionDataSample(database);
        }