Beispiel #1
0
        protected bool TryGetOrCreateResourceStore(string tenantId, out DocumentDatabase database)
        {
            if (ResourcesStoresCache.TryGetValue(tenantId, out database))
            {
                return(true);
            }

            JsonDocument jsonDocument;

            using (DefaultResourceStore.DisableAllTriggersForCurrentThread())
                jsonDocument = DefaultResourceStore.Get("Raven/Databases/" + tenantId, null);

            if (jsonDocument == null ||
                jsonDocument.Metadata == null ||
                jsonDocument.Metadata.Value <bool>(Constants.RavenDocumentDoesNotExists))
            {
                return(false);
            }

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

            database = ResourcesStoresCache.GetOrAddAtomically(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(DefaultResourceStore.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.DatabaseName = tenantId;

                config.Initialize();
                var documentDatabase = new DocumentDatabase(config);
                documentDatabase.SpinBackgroundWorkers();
                return(documentDatabase);
            });
            return(true);
        }
Beispiel #2
0
        private DatabaseDocument GetTenantDatabaseDocument(string tenantId)
        {
            JsonDocument jsonDocument;

            using (DefaultResourceStore.DisableAllTriggersForCurrentThread())
                jsonDocument = DefaultResourceStore.Get("Raven/Databases/" + tenantId, null);

            if (jsonDocument == null ||
                jsonDocument.Metadata == null ||
                jsonDocument.Metadata.Value <bool>(Constants.RavenDocumentDoesNotExists) ||
                jsonDocument.Metadata.Value <bool>(Constants.RavenDeleteMarker))
            {
                return(null);
            }

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

            if (document.Settings["Raven/DataDir"] == null)
            {
                throw new InvalidOperationException("Could not find Raven/DataDir");
            }

            return(document);
        }
Beispiel #3
0
        protected bool TryGetOrCreateResourceStore(string tenantId, out Task <DocumentDatabase> database)
        {
            if (ResourcesStoresCache.TryGetValue(tenantId, out database))
            {
                if (database.IsFaulted || database.IsCanceled)
                {
                    Task <DocumentDatabase> ignored;
                    ResourcesStoresCache.TryRemove(tenantId, out ignored);
                    DateTime time;
                    databaseLastRecentlyUsed.TryRemove(tenantId, out time);
                    //now go and re-create it.
                }
                else
                {
                    return(true);
                }
            }

            JsonDocument jsonDocument;

            using (DefaultResourceStore.DisableAllTriggersForCurrentThread())
                jsonDocument = DefaultResourceStore.Get("Raven/Databases/" + tenantId, null);

            if (jsonDocument == null ||
                jsonDocument.Metadata == null ||
                jsonDocument.Metadata.Value <bool>(Constants.RavenDocumentDoesNotExists) ||
                jsonDocument.Metadata.Value <bool>(Constants.RavenDeleteMarker))
            {
                return(false);
            }

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

            database = ResourcesStoresCache.GetOrAddAtomically(tenantId, s => Task.Factory.StartNew(() =>
            {
                var config = new InMemoryRavenConfiguration
                {
                    Settings = new NameValueCollection(DefaultConfiguration.Settings),
                };

                SetupTenantDatabaseConfiguration(config);

                config.CustomizeValuesForTenant(tenantId);

                var dataDir = document.Settings["Raven/DataDir"];
                if (dataDir == null)
                {
                    throw new InvalidOperationException("Could not find Raven/DataDir");
                }

                foreach (var setting in document.Settings)
                {
                    config.Settings[setting.Key] = setting.Value;
                }

                if (dataDir.StartsWith("~/") || dataDir.StartsWith(@"~\"))
                {
                    var baseDataPath = Path.GetDirectoryName(DefaultResourceStore.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.DatabaseName = tenantId;

                config.Initialize();
                config.CopyParentSettings(DefaultConfiguration);
                var documentDatabase = new DocumentDatabase(config);
                documentDatabase.SpinBackgroundWorkers();
                return(documentDatabase);
            })
                                                               .ContinueWith(task =>
            {
                if (task.Status == TaskStatus.Faulted)                                 // this observes the task exception
                {
                    logger.WarnException("Failed to create database " + tenantId, task.Exception);
                }
                return(task);
            }).Unwrap());
            return(true);
        }