예제 #1
0
        public InMemoryRavenConfiguration CreateTenantConfiguration(string tenantId)
        {
            var document = GetTenantDatabaseDocument(tenantId);

            if (document == null)
            {
                return(null);
            }

            var config = new InMemoryRavenConfiguration
            {
                Settings = new NameValueCollection(SystemConfiguration.Settings),
            };

            SetupTenantDatabaseConfiguration(config);

            config.CustomizeValuesForTenant(tenantId);


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

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

            var dataDir = document.Settings["Raven/DataDir"];

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

            config.Initialize();
            config.CopyParentSettings(SystemConfiguration);
            return(config);
        }
예제 #2
0
        protected InMemoryRavenConfiguration CreateConfiguration(
            string tenantId,
            DatabaseDocument document,
            string folderPropName,
            InMemoryRavenConfiguration parentConfiguration)
        {
            var config = new InMemoryRavenConfiguration
            {
                Settings = new NameValueCollection(parentConfiguration.Settings),
            };

            SetupTenantConfiguration(config);

            config.CustomizeValuesForTenant(tenantId);

            config.Settings["Raven/StorageEngine"]      = parentConfiguration.DefaultStorageTypeName;
            config.Settings["Raven/FileSystem/Storage"] = parentConfiguration.FileSystem.DefaultStorageTypeName;

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

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

            config.Settings[folderPropName]               = config.Settings[folderPropName].ToFullPath(parentConfiguration.DataDirectory);
            config.Settings["Raven/Esent/LogsPath"]       = config.Settings["Raven/Esent/LogsPath"].ToFullPath(parentConfiguration.DataDirectory);
            config.Settings[Constants.RavenTxJournalPath] = config.Settings[Constants.RavenTxJournalPath].ToFullPath(parentConfiguration.DataDirectory);

            config.Settings["Raven/VirtualDir"] = config.Settings["Raven/VirtualDir"] + "/" + tenantId;

            config.DatabaseName     = tenantId;
            config.IsTenantDatabase = true;

            config.Initialize();
            config.CopyParentSettings(parentConfiguration);
            return(config);
        }
예제 #3
0
		public InMemoryRavenConfiguration CreateTenantConfiguration(string tenantId)
		{
			var document = GetTenantDatabaseDocument(tenantId);
			if (document == null)
				return null;

			var config = new InMemoryRavenConfiguration
			{
				Settings = new NameValueCollection(SystemConfiguration.Settings),
			};

			SetupTenantDatabaseConfiguration(config);

			config.CustomizeValuesForTenant(tenantId);


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

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

			var dataDir = document.Settings["Raven/DataDir"];
			if (dataDir.StartsWith("~/") || dataDir.StartsWith(@"~\"))
			{
				var baseDataPath = Path.GetDirectoryName(SystemDatabase.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.IsTenantDatabase = true;

			config.Initialize();
			config.CopyParentSettings(SystemConfiguration);
			return config;
		}
예제 #4
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) || 
				jsonDocument.Metadata.Value<bool>(Constants.RavenDeleteMarker))
				return false;

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

			database = ResourcesStoresCache.GetOrAddAtomically(tenantId, s =>
			{
				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;
			});
			return true;
		}
예제 #5
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) ||
                jsonDocument.Metadata.Value <bool>(Constants.RavenDeleteMarker))
            {
                return(false);
            }

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

            database = ResourcesStoresCache.GetOrAddAtomically(tenantId, s =>
            {
                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);
            });
            return(true);
        }
예제 #6
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);
        }