Exemplo n.º 1
0
        protected override bool TryGetOrCreateResourceStore(string tenantId, out IResourceStore database)
        {
            if (ResourcesStoresCache.TryGetValue(tenantId, out database))
            {
                return(true);
            }

            JsonDocument jsonDocument;

            using (DocumentRetriever.DisableReadTriggers())
                jsonDocument = DefaultDatabase.Get("Raven/Databases/" + tenantId, null);

            if (jsonDocument == null)
            {
                return(false);
            }

            var document = jsonDocument.DataAsJson.JsonDeserialization <DatabaseDocument>();

            database = ResourcesStoresCache.GetOrAdd(tenantId, s =>
            {
                var config = new InMemoryRavenConfiguration
                {
                    Settings = DefaultConfiguration.Settings,
                };
                foreach (var setting in document.Settings)
                {
                    config.Settings[setting.Key] = setting.Value;
                }
                var dataDir = config.Settings["Raven/DataDir"];
                if (dataDir == null)
                {
                    throw new InvalidOperationException("Could not find Raven/DataDir");
                }
                if (dataDir.StartsWith("~/") || dataDir.StartsWith(@"~\"))
                {
                    var baseDataPath = Path.GetDirectoryName(DefaultDatabase.Configuration.DataDirectory);
                    if (baseDataPath == null)
                    {
                        throw new InvalidOperationException("Could not find root data path");
                    }
                    config.Settings["Raven/DataDir"] = Path.Combine(baseDataPath, dataDir.Substring(2));
                }
                config.Settings["Raven/VirtualDir"] = config.Settings["Raven/VirtualDir"] + "/" + tenantId;

                config.Initialize();
                var documentDatabase = new DocumentDatabase(config);
                documentDatabase.SpinBackgroundWorkers();
                return(documentDatabase);
            });
            return(true);
        }