The specification/template for creating a new DocumentCollection.
Exemple #1
0
        /// <summary>
        /// Registers the stored procedures, triggers and UDFs in the collection spec/template.
        /// </summary>
        /// <param name="client">The DocumentDB client.</param>
        /// <param name="collectionSpec">The collection spec/template.</param>
        /// <param name="collection">The collection.</param>
        /// <returns>The Task object for asynchronous execution.</returns>
        public static async Task RegisterScripts(DocumentClient client, DocumentCollectionSpec collectionSpec, DocumentCollection collection)
        {
            if (collectionSpec.StoredProcedures != null)
            {
                foreach (StoredProcedure sproc in collectionSpec.StoredProcedures)
                {
                    await client.CreateStoredProcedureAsync(collection.SelfLink, sproc);
                }
            }

            if (collectionSpec.Triggers != null)
            {
                foreach (Trigger trigger in collectionSpec.Triggers)
                {
                    await client.CreateTriggerAsync(collection.SelfLink, trigger);
                }
            }

            if (collectionSpec.UserDefinedFunctions != null)
            {
                foreach (UserDefinedFunction udf in collectionSpec.UserDefinedFunctions)
                {
                    await client.CreateUserDefinedFunctionAsync(collection.SelfLink, udf);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Creates a new collection.
        /// </summary>
        /// <param name="client">The DocumentDB client instance.</param>
        /// <param name="database">The Database where this DocumentCollection exists / will be created</param>
        /// <param name="collectionId">The id of the DocumentCollection to search for, or create.</param>
        /// <param name="collectionSpec">The spec/template to create collections from.</param>
        /// <returns>The created DocumentCollection object</returns>
        public static async Task <DocumentCollection> CreateNewCollection(
            DocumentClient client,
            Database database,
            string collectionId,
            DocumentCollectionSpec collectionSpec)
        {
            DocumentCollection collectionDefinition = new DocumentCollection {
                Id = collectionId
            };

            if (collectionSpec != null)
            {
                CopyIndexingPolicy(collectionSpec, collectionDefinition);
            }

            DocumentCollection collection = await CreateDocumentCollectionWithRetriesAsync(
                client,
                database,
                collectionDefinition,
                (collectionSpec != null)?collectionSpec.OfferType : null);

            if (collectionSpec != null)
            {
                await RegisterScripts(client, collectionSpec, collection);
            }

            return(collection);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ManagedHashPartitionResolver" /> class.
 /// </summary>
 /// <param name="partitionKeyExtractor">The partition key extractor function.</param>
 /// <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="hashGenerator">the hash generator.</param>
 /// <param name="collectionSpec">the specification/template to create collections from.</param>
 /// <param name="collectionIdPrefix">the prefix to use for collections.</param>
 public ManagedHashPartitionResolver(
     Func<object, string> partitionKeyExtractor,
     DocumentClient client,
     Database database,
     int numberOfCollections,
     IHashGenerator hashGenerator = null,
     DocumentCollectionSpec collectionSpec = null,
     string collectionIdPrefix = "ManagedHashCollection.")
     : base(partitionKeyExtractor,
     GetCollections(client, database, numberOfCollections, collectionIdPrefix, collectionSpec),
     128,
     hashGenerator)
 {
     this.DocumentCollectionSpec = collectionSpec;
 }
Exemple #4
0
        /// <summary>
        /// Get a DocumentCollection by id, or create a new one if one with the id provided doesn't exist.
        /// </summary>
        /// <param name="client">The DocumentDB client instance.</param>
        /// <param name="database">The Database where this DocumentCollection exists / will be created</param>
        /// <param name="collectionId">The id of the DocumentCollection to search for, or create.</param>
        /// <param name="collectionSpec">The spec/template to create collections from.</param>
        /// <returns>The matched, or created, DocumentCollection object</returns>
        public static async Task <DocumentCollection> GetCollectionAsync(
            DocumentClient client,
            Database database,
            string collectionId,
            DocumentCollectionSpec collectionSpec)
        {
            DocumentCollection collection = client.CreateDocumentCollectionQuery(database.SelfLink)
                                            .Where(c => c.Id == collectionId).ToArray().FirstOrDefault();

            if (collection == null)
            {
                collection = await CreateNewCollection(client, database, collectionId, collectionSpec);
            }

            return(collection);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SpilloverPartitionResolver" /> class.
 /// </summary>
 /// <param name="client">The DocumentDB client instance.</param>
 /// <param name="database">The database to use.</param>
 /// <param name="collectionSpec">The specification/template to create collections from.</param>
 /// <param name="collectionIdPrefix">The prefix to use for collections.</param>
 /// <param name="fillFactor">The fill factor for spilling over collections.</param>
 /// <param name="checkIntervalSeconds">The interval between collection size checks.</param>
 public SpilloverPartitionResolver(
     DocumentClient client,
     Database database,
     DocumentCollectionSpec collectionSpec = null,
     string collectionIdPrefix = "Collection.",
     double fillFactor = 0.90,
     double checkIntervalSeconds = 3600)
 {
     this.Client = client;
     this.Database = database;
     this.CollectionTemplate = collectionSpec;
     this.CollectionLinks = GetCollections(client, database, collectionIdPrefix, collectionSpec);
     this.CollectionIdPrefix = collectionIdPrefix;
     this.FillFactor = fillFactor;
     this.CheckInterval = TimeSpan.FromSeconds(checkIntervalSeconds);
 }
Exemple #6
0
        /// <summary>
        /// Copies the indexing policy from the collection spec.
        /// </summary>
        /// <param name="collectionSpec">The collection spec/template</param>
        /// <param name="collectionDefinition">The collection definition to create.</param>
        public static void CopyIndexingPolicy(DocumentCollectionSpec collectionSpec, DocumentCollection collectionDefinition)
        {
            if (collectionSpec.IndexingPolicy != null)
            {
                collectionDefinition.IndexingPolicy.Automatic    = collectionSpec.IndexingPolicy.Automatic;
                collectionDefinition.IndexingPolicy.IndexingMode = collectionSpec.IndexingPolicy.IndexingMode;

                if (collectionSpec.IndexingPolicy.IncludedPaths != null)
                {
                    foreach (IncludedPath path in collectionSpec.IndexingPolicy.IncludedPaths)
                    {
                        collectionDefinition.IndexingPolicy.IncludedPaths.Add(path);
                    }
                }

                if (collectionSpec.IndexingPolicy.ExcludedPaths != null)
                {
                    foreach (ExcludedPath path in collectionSpec.IndexingPolicy.ExcludedPaths)
                    {
                        collectionDefinition.IndexingPolicy.ExcludedPaths.Add(path);
                    }
                }
            }
        }
        /// <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>
 /// 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="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 List<string> GetCollections(
     DocumentClient client,
     Database database,
     string collectionIdPrefix,
     DocumentCollectionSpec spec)
 {
     var collections = new Dictionary<int, string>();
     foreach (DocumentCollection collection in client.ReadDocumentCollectionFeedAsync(database.SelfLink).Result)
     {
         if (collection.Id.StartsWith(collectionIdPrefix))
         {
             int collectionNumber = int.Parse(collection.Id.Replace(collectionIdPrefix, string.Empty));
             collections[collectionNumber] = collection.SelfLink;
         }
     }
     if (collections.Any())
     {
         NextCollectionNumber = collections.Keys.Max() + 1;
     }
     else
     {
         NextCollectionNumber = 0;
     }
     // Return selflinks in ID order
     return collections.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value).ToList();
 }
 /// <summary>
 /// Initialize a "managed" HashPartitionResolver that also takes care of creating collections, and cloning collection properties like
 /// stored procedures, offer type and indexing policy.
 /// </summary>
 /// <param name="partitionKeyExtractor">The partition key extractor function. (Ex: "u => ((UserProfile)u).UserId")</param>
 /// <param name="client">The DocumentDB client instance to use.</param>
 /// <param name="database">The database to run the samples on.</param>
 /// <param name="numCollections">The number of collections to be used.</param>
 /// <param name="collectionSpec">The DocumentCollectionSpec to be used for each collection created. If null returns Spec with defaultOfferType as set in config.</param>
 /// <returns>The created HashPartitionResolver.</returns>
 public static ManagedHashPartitionResolver InitializeManagedHashResolver(Func<object, string> partitionKeyExtractor, DocumentClient client, Database database, int numCollections, DocumentCollectionSpec collectionSpec)
 {
     // If no collectionSpec used, use a default spec with the offerType (ie: S1, S2, S3) that is specified in config.
     if (collectionSpec == null)
     {
         var hashResolver = new ManagedHashPartitionResolver(partitionKeyExtractor, client, database, numCollections, null, new DocumentCollectionSpec { OfferType = defaultOfferType });
         client.PartitionResolvers[database.SelfLink] = hashResolver;
         return hashResolver;
     }
     // If there is a collectionSpec passed as a parameter, use that instead of default.
     else
     {
         var hashResolver = new ManagedHashPartitionResolver(partitionKeyExtractor, client, database, numCollections, null, collectionSpec);
         client.PartitionResolvers[database.SelfLink] = hashResolver;
         return hashResolver;
     }
 }