コード例 #1
0
        private async Task CreateBulkImportStoredProcedure(IDocumentClient client, bool dropExistingProc = false)
        {
            var currentAssembly = typeof(AzureDocumentDBSink).GetTypeInfo().Assembly;

            SelfLog.WriteLine("Getting required resource.");
            var resourceName =
                currentAssembly.GetManifestResourceNames().Where(w => w.EndsWith("bulkImport.js")).FirstOrDefault();

            if (string.IsNullOrEmpty(resourceName))
            {
                SelfLog.WriteLine("Unable to find required resource.");
                return;
            }

            SelfLog.WriteLine($"Found resource: {resourceName}");

            using (var resourceStream = currentAssembly.GetManifestResourceStream(resourceName))
            {
                if (resourceStream != null)
                {
                    var reader        = new StreamReader(resourceStream);
                    var bulkImportSrc = reader.ReadToEnd();
                    try
                    {
                        var sp = new StoredProcedure
                        {
                            Id   = BulkStoredProcedureId,
                            Body = bulkImportSrc
                        };

                        var sproc = GetStoredProcedure(_collectionLink, sp.Id);

                        if ((sproc != null) && dropExistingProc)
                        {
                            await client.DeleteStoredProcedureAsync(sproc.SelfLink);

                            sproc = null;
                        }

                        if (sproc == null)
                        {
                            sproc = await client.CreateStoredProcedureAsync(_collectionLink, sp);
                        }

                        _bulkStoredProcedureLink = sproc.SelfLink;
                    }
                    catch (Exception ex)
                    {
                        SelfLog.WriteLine(ex.Message);
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Gets the stored procedure, or creates a new one if one with the specified storedProcedure.id doesn't exist.
        /// </summary>
        /// <param name="dbId">The id of the Database to search for, or create.</param>
        /// <param name="collectionId">The id of the document db collection.</param>
        /// <param name="storedProcedure">The stored procedure to get or create.</param>
        /// <param name="deleteStoredProcedure">Indicator to delete the stored procedure before creating it.</param>
        /// <returns>The matched, or created, StoredProcedure object</returns>
        public async Task <StoredProcedure> GetOrCreateStoredProcedureAsync(
            string dbId,
            string collectionId,
            StoredProcedure storedProcedure,
            bool deleteStoredProcedure = false)
        {
            Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b14a /* tag_961fk */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(collectionId, nameof(collectionId), TaggingUtilities.ReserveTag(0x2381b14b /* tag_961fl */));
            Code.ExpectsArgument(storedProcedure, nameof(storedProcedure), TaggingUtilities.ReserveTag(0x2381b14c /* tag_961fm */));

            IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false);

            StoredProcedure sproc  = null;
            var             colUri = UriFactory.CreateDocumentCollectionUri(dbId, collectionId);

            return(await DocumentDbAdapter.ExecuteAndLogAsync(TaggingUtilities.ReserveTag(0x2381b14d /* tag_961fn */),
                                                              async() =>
            {
                try
                {
                    if (deleteStoredProcedure)
                    {
                        await DeleteStoredProcedureAsync(dbId, collectionId, storedProcedure.Id).ConfigureAwait(false);
                    }

                    sproc = client.CreateStoredProcedureQuery(colUri)
                            .Where(p => p.Id == storedProcedure.Id).AsEnumerable().FirstOrDefault();

                    if (sproc != null)
                    {
                        return sproc;
                    }

                    sproc = await client.CreateStoredProcedureAsync(colUri, storedProcedure).ConfigureAwait(false);
                    return sproc;
                }
                catch (DocumentClientException ex)
                {
                    if (ex.StatusCode == HttpStatusCode.Conflict)
                    {
                        sproc = client.CreateStoredProcedureQuery(colUri)
                                .Where(p => p.Id == storedProcedure.Id).AsEnumerable().FirstOrDefault();

                        return sproc;
                    }

                    throw;
                }
            }).ConfigureAwait(false));
        }
コード例 #3
0
 public async Task ExecuteAsync(IDocumentClient client, DocumentCollection collection, Uri relativeCollectionUri)
 {
     foreach (IStoredProcedure storedProc in GetStoredProcedures())
     {
         try
         {
             await client.ReadStoredProcedureAsync(storedProc.GetUri(relativeCollectionUri));
         }
         catch (DocumentClientException e) when(e.StatusCode == HttpStatusCode.NotFound)
         {
             await client.CreateStoredProcedureAsync(relativeCollectionUri, storedProc.AsStoredProcedure());
         }
     }
 }
コード例 #4
0
        private static async Task <StoredProcedure> CreateStoredProcedure(IDocumentClient client, string sprocId)
        {
            string storedProcedureFileName = $@"..\..\Server\{sprocId}.js";
            string sprocBody = File.ReadAllText(storedProcedureFileName);

            StoredProcedure sprocDefinition = new StoredProcedure
            {
                Id   = sprocId,
                Body = sprocBody
            };

            ResourceResponse <StoredProcedure> result = await client.CreateStoredProcedureAsync(MyStoreCollectionUri, sprocDefinition);

            StoredProcedure sproc = result.Resource;

            Console.WriteLine($"Created stored procedure {sproc.Id}; RID: {sproc.ResourceId}");

            return(result);
        }
コード例 #5
0
        private async Task SetupStoredProcedureAsync()
        {
            var collectionName        = typeof(NotificationRecord).Name;
            var documentCollectionUri = UriFactory.CreateDocumentCollectionUri(_configuration.DatabaseId, collectionName);
            var storedProcedure       = _client.CreateStoredProcedureQuery(documentCollectionUri)
                                        .Where(db => db.Id == _configuration.BulkDeleteNotificationStoredProcedureId)
                                        .AsEnumerable()
                                        .FirstOrDefault();

            if (storedProcedure == null)
            {
                storedProcedure = new StoredProcedure()
                {
                    Id   = _configuration.BulkDeleteNotificationStoredProcedureId,
                    Body = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Scripts/bulkDeleteNotifications.js"))
                };
                await _client.CreateStoredProcedureAsync(documentCollectionUri, storedProcedure);
            }
        }