Пример #1
0
        static void Main(string[] args)
        {
            if (args.Count() < 4)
            {
                Console.WriteLine("You must specify the following parameters: Document DB endpoint URL, secret key, database name, and collection name.");
                Console.WriteLine("Program terminated. Press any key to exit");
                Console.ReadKey();
                return; // exit program

            }

            // setup DocumentDB client using passed parameters
            Uri endpoint = new Uri(args[0]);
            string authKey = args[1];
            string databaseId = args[2];
            string collectionId = args[3];

            var connectionPolicy = new ConnectionPolicy()
            {
                ConnectionProtocol = Protocol.Https,
                ConnectionMode = ConnectionMode.Gateway
            };
            client = new DocumentClient(endpoint, authKey, connectionPolicy);

            Database database = client.CreateDatabaseQuery().Where(db => db.Id == databaseId).ToArray().FirstOrDefault();
            DocumentCollection collection = client.CreateDocumentCollectionQuery(database.SelfLink).Where(c => c.Id == collectionId).ToArray().FirstOrDefault();

            // get list of SP files
            var currentDir = new DirectoryInfo(Directory.GetCurrentDirectory());
            foreach (FileInfo tmpFile in currentDir.GetFiles("SP_*.js"))
            {
                string storedProecureId = tmpFile.Name.Substring(3, (tmpFile.Name.Length - 6));
                Console.WriteLine("Found File: " + storedProecureId);

                var storedProc = new StoredProcedure()
                {
                    Id = storedProecureId,
                    Body = File.ReadAllText(tmpFile.FullName)
                };

                TryDeleteStoredProcedure(collection.SelfLink, storedProc.Id); 

                client.CreateStoredProcedureAsync(collection.SelfLink, storedProc, null).Wait();
            }

            Console.WriteLine("Stored Procedure Setup completed. Press any key to continue.");
            Console.ReadKey();
        }
Пример #2
0
        public void Initialize(string modelsDirectory)
        {
            Database databaseId = new Database {
                Id = _databaseId
            };
            ResourceResponse <Database> database = _database.CreateDatabaseIfNotExistsAsync(databaseId).Result;
            Uri databaseUri = UriFactory.CreateDatabaseUri(_databaseId);

            string settingKey      = Constant.AppSettingKey.DatabaseCollectionThroughputUnits;
            string throughputUnits = AppSetting.GetValue(settingKey);

            RequestOptions collectionOptions = new RequestOptions()
            {
                OfferThroughput = int.Parse(throughputUnits)
            };

            DocumentCollection documentCollection = new DocumentCollection()
            {
                Id = Constant.Database.Collection.ContentInsight
            };

            ResourceResponse <DocumentCollection> collection = _database.CreateDocumentCollectionIfNotExistsAsync(databaseUri, documentCollection, collectionOptions).Result;
            Uri collectionUri = UriFactory.CreateDocumentCollectionUri(_databaseId, documentCollection.Id);

            string collectionDirectory = string.Concat(modelsDirectory, @"\", documentCollection.Id);

            string jsFile = string.Concat(collectionDirectory, @"\Functions\isTimecodeFragment.js");
            UserDefinedFunction function = new UserDefinedFunction()
            {
                Id   = "isTimecodeFragment",
                Body = File.ReadAllText(jsFile)
            };

            _database.CreateUserDefinedFunctionAsync(collectionUri, function);

            jsFile = string.Concat(collectionDirectory, @"\Procedures\getTimecodeFragment.js");
            StoredProcedure procedure = new StoredProcedure()
            {
                Id   = "getTimecodeFragment",
                Body = File.ReadAllText(jsFile)
            };

            _database.CreateStoredProcedureAsync(collectionUri, procedure);

            documentCollection = new DocumentCollection()
            {
                Id = Constant.Database.Collection.ProcessorConfig
            };

            collection    = _database.CreateDocumentCollectionIfNotExistsAsync(databaseUri, documentCollection, collectionOptions).Result;
            collectionUri = UriFactory.CreateDocumentCollectionUri(_databaseId, documentCollection.Id);

            collectionDirectory = string.Concat(modelsDirectory, @"\", documentCollection.Id);

            jsFile    = string.Concat(collectionDirectory, @"\Procedures\getProcessorConfig.js");
            procedure = new StoredProcedure()
            {
                Id   = "getProcessorConfig",
                Body = File.ReadAllText(jsFile)
            };
            _database.CreateStoredProcedureAsync(collectionUri, procedure);

            string presetsDirectory = string.Concat(modelsDirectory, @"\ProcessorPresets");

            string[] mediaProcessors = Directory.GetDirectories(presetsDirectory);
            foreach (string mediaProcessor in mediaProcessors)
            {
                string[] processorPresets = Directory.GetFiles(mediaProcessor);
                foreach (string processorPreset in processorPresets)
                {
                    if (processorPreset.EndsWith(Constant.Media.FileExtension.Json, StringComparison.OrdinalIgnoreCase))
                    {
                        StreamReader   streamReader = new StreamReader(processorPreset);
                        JsonTextReader presetReader = new JsonTextReader(streamReader);
                        JObject        presetConfig = JObject.Load(presetReader);
                        UpsertDocument(documentCollection.Id, presetConfig);
                    }
                }
            }
        }
Пример #3
0
        private static async Task CreateSprocIfNotExists(string scriptFileName, string scriptId, string scriptName)
        {
            var client = new DocumentClient(new Uri(Endpoint), AuthKey);
            Uri collectionLink = UriFactory.CreateDocumentCollectionUri(DbName, CollectionName);

            var sproc = new StoredProcedure
            {
                Id = scriptId,
                Body = File.ReadAllText(scriptFileName)
            };

            bool needToCreate = false;
            Uri sprocUri = UriFactory.CreateStoredProcedureUri(DbName, CollectionName, scriptName);

            try
            {
                await client.ReadStoredProcedureAsync(sprocUri);
            }
            catch (DocumentClientException de)
            {
                if (de.StatusCode != HttpStatusCode.NotFound)
                {
                    throw;
                }
                else
                {
                    needToCreate = true;
                }
            }

            if (needToCreate)
            {
                await client.CreateStoredProcedureAsync(collectionLink, sproc);
            }
        }