Пример #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);
                }
            }
        }
        /// <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.Id, numberOfCollections, collectionIdPrefix, collectionSpec), 
     128,
     hashGenerator)
 {
     this.DocumentCollectionSpec = collectionSpec;
 }
 /// <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);
 }
Пример #5
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> GetOrCreateCollectionAsync(
            DocumentClient client,
            string databaseId,
            string collectionId,
            DocumentCollectionSpec collectionSpec)
        {
            DocumentCollection collection = client.CreateDocumentCollectionQuery(UriFactory.CreateDatabaseUri(databaseId))
                                            .Where(c => c.Id == collectionId).ToArray().SingleOrDefault();

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

            return(collection);
        }
        /// <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>
        /// 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;
        }
Пример #8
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="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,
            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;
                }
            }

            // Return selflinks in ID order
            return collections.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value).ToList();
        }