コード例 #1
0
        /// <summary>
        /// Run examples of how to perform create, read, update, delete and query documents against a partitioned database.
        /// </summary>
        /// <param name="database">The database to run the samples on.</param>
        /// <param name="partitionResolver">The PartitionResolver instance to use.</param>
        /// <returns>The Task for asynchronous execution of these samples.</returns>
        private async Task RunCrudAndQuerySample(Database database, IPartitionResolver partitionResolver)
        {
            // Create some documents. Note that creates use the database's self link instead of a specific collection's self link.
            // The hash resolver will compute the hash of UserId in order to route the create to either of the collections.
            Document johnDocument = await this.client.CreateDocumentAsync(database.SelfLink, new UserProfile("J1", "@John", Region.UnitedStatesEast));

            Document ryanDocument = await this.client.CreateDocumentAsync(database.SelfLink, new UserProfile("U4", "@Ryan", Region.AsiaPacific, UserStatus.AppearAway));

            // Delete document using self link (as usual).
            await this.client.DeleteDocumentAsync(ryanDocument.SelfLink);

            // Read, then update status using self link (as usual).
            Document latestJohnDocument = await this.client.ReadDocumentAsync(johnDocument.SelfLink);

            UserProfile johnProfile = (UserProfile)(dynamic)latestJohnDocument;

            johnProfile.Status = UserStatus.Busy;

            await this.client.ReplaceDocumentAsync(latestJohnDocument.SelfLink, johnProfile);

            // Query for John's document by ID. We can use the PartitionResolver to restrict the query to just the partition containing @John
            // Again the query uses the database self link, and relies on the hash resolver to route the appropriate collection.
            var query = this.client.CreateDocumentQuery <UserProfile>(database.SelfLink, null, partitionResolver.GetPartitionKey(johnProfile))
                        .Where(u => u.UserName == "@John");

            johnProfile = query.AsEnumerable().FirstOrDefault();

            // Query for all Busy users. Here since there is no partition key, the query is serially executed across each partition/collection.
            query = this.client.CreateDocumentQuery <UserProfile>(database.SelfLink).Where(u => u.Status == UserStatus.Busy);
            foreach (UserProfile busyUser in query)
            {
                Console.WriteLine(busyUser);
            }

            // Find the collections where a document exists in. It's uncommon to do this, but can be useful if for example to execute a
            // stored procedure against a specific set of partitions.
            IPartitionResolver resolver        = this.client.PartitionResolvers[database.SelfLink];
            List <string>      collectionLinks = resolver.ResolveForRead(resolver.GetPartitionKey(johnProfile)).ToList();

            // Find the collections where a document will be created in.
            string collectionLink = resolver.ResolveForCreate(resolver.GetPartitionKey(johnProfile));

            // Cleanup.
            await this.client.DeleteDatabaseAsync(database.SelfLink);
        }
コード例 #2
0
        public Task WriteAsync(IDataItem dataItem, CancellationToken cancellation)
        {
            if (partitionResolver == null)
            {
                throw Errors.SinkIsNotInitialized();
            }

            dataItem = Transformation.Transform(dataItem);
            var partitionKey = partitionResolver.GetPartitionKey(dataItem);

            return(Client.CreateDocumentAsync(
                       partitionResolver.ResolveForCreate(partitionKey),
                       new DataItemSurrogate(dataItem),
                       Configuration.DisableIdGeneration));
        }
コード例 #3
0
        public Task WriteAsync(IDataItem dataItem, CancellationToken cancellation)
        {
            if (partitionResolver == null)
            {
                throw Errors.SinkIsNotInitialized();
            }

            dataItem = Transformation.Transform(dataItem);
            var partitionKey = partitionResolver.GetPartitionKey(dataItem);

            var collectionLink    = partitionResolver.ResolveForCreate(partitionKey);
            var dataItemSurrogate = new DataItemSurrogate(dataItem);

            return(Configuration.UpdateExisting
                ? Client.UpsertDocumentAsync(collectionLink, dataItemSurrogate, Configuration.DisableIdGeneration)
                : Client.CreateDocumentAsync(collectionLink, dataItemSurrogate, Configuration.DisableIdGeneration));
        }
        public Task WriteAsync(IDataItem dataItem, CancellationToken cancellation)
        {
            if (partitionResolver == null)
            {
                throw Errors.SinkIsNotInitialized();
            }

            dataItem = Transformation.Transform(dataItem);
            var collectionName = partitionResolver.ResolveForCreate(partitionResolver.GetPartitionKey(dataItem));

            IDataSinkAdapter adapter;

            if (!dataAdapters.TryGetValue(collectionName, out adapter))
            {
                throw Errors.UnexpectedPartitionCollection(collectionName);
            }

            return(adapter.WriteAsync(dataItem, cancellation));
        }
コード例 #5
0
        /// <summary>
        /// Run examples of how to perform create, read, update, delete and query documents against a partitioned database. 
        /// </summary>
        /// <param name="database">The database to run the samples on.</param>
        /// <param name="partitionResolver">The PartitionResolver instance to use.</param>
        /// <returns>The Task for asynchronous execution of these samples.</returns>
        private async Task RunCrudAndQuerySample(Database database, IPartitionResolver partitionResolver)
        {
            // Create some documents. Note that creates use the database's self link instead of a specific collection's self link. 
            // The hash resolver will compute the hash of UserId in order to route the create to either of the collections.
            Document johnDocument = await this.client.CreateDocumentAsync(database.SelfLink, new UserProfile("J1", "@John", Region.UnitedStatesEast));
            Document ryanDocument = await this.client.CreateDocumentAsync(database.SelfLink, new UserProfile("U4", "@Ryan", Region.AsiaPacific, UserStatus.AppearAway));

            // Delete document using self link (as usual).
            await this.client.DeleteDocumentAsync(ryanDocument.SelfLink);

            // Read, then update status using self link (as usual).
            Document latestJohnDocument = await this.client.ReadDocumentAsync(johnDocument.SelfLink);
            UserProfile johnProfile = (UserProfile)(dynamic)latestJohnDocument;
            johnProfile.Status = UserStatus.Busy;

            await this.client.ReplaceDocumentAsync(latestJohnDocument.SelfLink, johnProfile);

            // Query for John's document by ID. We can use the PartitionResolver to restrict the query to just the partition containing @John
            // Again the query uses the database self link, and relies on the hash resolver to route the appropriate collection.
            var query = this.client.CreateDocumentQuery<UserProfile>(database.SelfLink, null, partitionResolver.GetPartitionKey(johnProfile))
                .Where(u => u.UserName == "@John");
            johnProfile = query.AsEnumerable().FirstOrDefault();

            // Query for all Busy users. Here since there is no partition key, the query is serially executed across each partition/collection. 
            query = this.client.CreateDocumentQuery<UserProfile>(database.SelfLink).Where(u => u.Status == UserStatus.Busy);
            foreach (UserProfile busyUser in query)
            {
                Console.WriteLine(busyUser);
            }
             
            // Find the collections where a document exists in. It's uncommon to do this, but can be useful if for example to execute a 
            // stored procedure against a specific set of partitions.
            IPartitionResolver resolver = this.client.PartitionResolvers[database.SelfLink];
            List<string> collectionLinks = resolver.ResolveForRead(resolver.GetPartitionKey(johnProfile)).ToList();

            // Find the collections where a document will be created in.
            string collectionLink = resolver.ResolveForCreate(resolver.GetPartitionKey(johnProfile));

            // Cleanup.
            await this.client.DeleteDatabaseAsync(database.SelfLink);
        }