/// <summary> /// Set parameters and initialize the instance. /// </summary> /// <param name="database">The database that will be used by the instance.</param> /// <param name="instance">The instance to initialize.</param> private static void SetParametersAndInitializeInstance(string database, Instance instance) { SetDefaultInstanceParameters(instance); SetPathParameters(instance, database); InitGrbit grbit = EsentVersion.SupportsWindows7Features ? Windows7Grbits.ReplayIgnoreLostLogs : InitGrbit.None; instance.Init(grbit); }
/// <summary> /// Initialize the JET_INSTANCE. /// </summary> /// <param name="grbit"> /// Initialization options. /// </param> public void Init(InitGrbit grbit) { this.CheckObjectIsNotDisposed(); JET_INSTANCE instance = this.JetInstance; // Use a constrained region so that the handle is // always set after JetInit2 is called. RuntimeHelpers.PrepareConstrainedRegions(); try { // Remember that a failure in JetInit can zero the handle // and that JetTerm should not be called in that case. Api.JetInit2(ref instance, grbit); } finally { this.SetHandle(instance.Value); } }
private void InitInstance() { string directory = Path.GetDirectoryName(m_DatabaseFileName); Instance = new Instance(Guid.NewGuid().ToString()); Instance.Parameters.TempDirectory = Path.Combine(directory, "temp"); Instance.Parameters.SystemDirectory = Path.Combine(directory, "system"); Instance.Parameters.LogFileDirectory = Path.Combine(directory, "logs"); Instance.Parameters.AlternateDatabaseRecoveryDirectory = directory; Instance.Parameters.CreatePathIfNotExist = true; Instance.Parameters.EnableIndexChecking = false; Instance.Parameters.CircularLog = true; Instance.Parameters.CheckpointDepthMax = 64 * 1024 * 1024; Instance.Parameters.LogFileSize = 1024; // 1MB logs Instance.Parameters.LogBuffers = 1024; // buffers = 1/2 of logfile Instance.Parameters.MaxTemporaryTables = 0; Instance.Parameters.MaxVerPages = 1024; Instance.Parameters.NoInformationEvent = true; Instance.Parameters.WaypointLatency = 1; Instance.Parameters.MaxSessions = 256; Instance.Parameters.MaxOpenTables = 256; Instance.Parameters.EventSource = "NCrawler"; InitGrbit grbit = EsentVersion.SupportsWindows7Features ? Windows7Grbits.ReplayIgnoreLostLogs : InitGrbit.None; try { Instance.Init(grbit); } catch { Directory.Delete(directory, true); throw; } }
/// <summary> /// Initializes a new instance of the PersistentDictionary class. /// </summary> /// <param name="directory"> /// The directory to create the database in. /// </param> public PersistentDictionary(string directory) { if (null == directory) { throw new ArgumentNullException("directory"); } Globals.Init(); this.converters = new PersistentDictionaryConverters <TKey, TValue>(); this.config = new PersistentDictionaryConfig(); this.databaseDirectory = directory; this.databasePath = Path.Combine(directory, this.config.Database); this.updateLocks = new object[NumUpdateLocks]; for (int i = 0; i < this.updateLocks.Length; ++i) { this.updateLocks[i] = new object(); } this.instance = new Instance(Guid.NewGuid().ToString()); this.instance.Parameters.SystemDirectory = directory; this.instance.Parameters.LogFileDirectory = directory; this.instance.Parameters.TempDirectory = directory; // If the database has been moved while inconsistent recovery // won't be able to find the database (logfiles contain the // absolute path of the referenced database). Set this parameter // to indicate a directory which contains any databases that couldn't // be found by recovery. this.instance.Parameters.AlternateDatabaseRecoveryDirectory = directory; this.instance.Parameters.CreatePathIfNotExist = true; this.instance.Parameters.BaseName = this.config.BaseName; this.instance.Parameters.EnableIndexChecking = false; // TODO: fix unicode indexes this.instance.Parameters.CircularLog = true; this.instance.Parameters.CheckpointDepthMax = 64 * 1024 * 1024; this.instance.Parameters.LogFileSize = 1024; // 1MB logs this.instance.Parameters.LogBuffers = 1024; // buffers = 1/2 of logfile this.instance.Parameters.MaxTemporaryTables = 0; this.instance.Parameters.MaxVerPages = 1024; this.instance.Parameters.NoInformationEvent = true; this.instance.Parameters.WaypointLatency = 1; this.instance.Parameters.MaxSessions = 256; this.instance.Parameters.MaxOpenTables = 256; InitGrbit grbit = EsentVersion.SupportsWindows7Features ? Windows7Grbits.ReplayIgnoreLostLogs : InitGrbit.None; this.instance.Init(grbit); try { if (!File.Exists(this.databasePath)) { this.CreateDatabase(this.databasePath); } else { this.CheckDatabaseMetaData(this.databasePath); } this.cursors = new PersistentDictionaryCursorCache <TKey, TValue>( this.instance, this.databasePath, this.converters, this.config); } catch (Exception) { // We have failed to initialize for some reason. Terminate // the instance. this.instance.Term(); throw; } }
/// <summary> /// Initialize the ESENT database engine. /// </summary> /// <param name="instance"> /// The instance to initialize. If an instance hasn't been /// allocated then a new one is created and the engine /// will operate in single-instance mode. /// </param> /// <param name="recoveryOptions"> /// Additional recovery parameters for remapping databases during /// recovery, position where to stop recovery at, or recovery status. /// </param> /// <param name="grbit"> /// Initialization options. /// </param> /// <returns> /// A warning code. /// </returns> public static JET_wrn JetInit3(ref JET_INSTANCE instance, JET_RSTINFO recoveryOptions, InitGrbit grbit) { return(Api.Check(Api.Impl.JetInit3(ref instance, recoveryOptions, grbit))); }
/// <summary> /// Initialize the ESENT database engine. /// </summary> /// <param name="instance"> /// The instance to initialize. If an instance hasn't been /// allocated then a new one is created and the engine /// will operate in single-instance mode. /// </param> /// <param name="grbit"> /// Initialization options. /// </param> /// <returns> /// A warning code. /// </returns> public static JET_wrn JetInit2(ref JET_INSTANCE instance, InitGrbit grbit) { return Api.Check(Impl.JetInit2(ref instance, grbit)); }
/// <summary> /// Initialize the ESENT database engine. /// </summary> /// <param name="instance"> /// The instance to initialize. If an instance hasn't been /// allocated then a new one is created and the engine /// will operate in single-instance mode. /// </param> /// <param name="recoveryOptions"> /// Additional recovery parameters for remapping databases during /// recovery, position where to stop recovery at, or recovery status. /// </param> /// <param name="grbit"> /// Initialization options. /// </param> /// <returns> /// A warning code. /// </returns> public static JET_wrn JetInit3(ref JET_INSTANCE instance, JET_RSTINFO recoveryOptions, InitGrbit grbit) { return Api.Check(Api.Impl.JetInit3(ref instance, recoveryOptions, grbit)); }
public void Open(string connectionString) { if (_EseInstance == null) { if (string.IsNullOrWhiteSpace(connectionString)) { connectionString = ApplicationFolderHelper.ApplicationFolderName; } this._DatabasePath = Path.Combine(connectionString, Path.ChangeExtension(_DatabaseName, _StorageEngineBaseName)); _EseInstance = new Instance(_DatabaseName); _EseInstance.Parameters.CreatePathIfNotExist = true; _EseInstance.Parameters.CircularLog = true; _EseInstance.Parameters.Recovery = true; _EseInstance.Parameters.BaseName = _StorageEngineBaseName; _EseInstance.Parameters.MaxSessions = _ConnectionPoolSize * 2; _EseInstance.Parameters.NoInformationEvent = true; if (!string.IsNullOrEmpty(connectionString)) { _EseInstance.Parameters.SystemDirectory = connectionString; _EseInstance.Parameters.LogFileDirectory = connectionString; _EseInstance.Parameters.TempDirectory = connectionString; _EseInstance.Parameters.AlternateDatabaseRecoveryDirectory = connectionString; } InitGrbit grbit = default(InitGrbit); if (EsentVersion.SupportsWindows7Features) { grbit = Windows7Grbits.ReplayIgnoreLostLogs; } else { grbit = InitGrbit.None; } _EseInstance.Init(grbit); _ConnectionPool = new EseConnectionPool(_EseInstance, _DatabasePath, _ConnectionPoolSize); EseConnection connection = _ConnectionPool.OpenConnection(); try { using (connection.GetTable(_TableNameCallbacks, OpenTableGrbit.ReadOnly)) { } } catch (EsentErrorException eeex) { if (eeex.Error == JET_err.ObjectNotFound) { CreateTaskTable(connection); } else { throw; } } finally { _ConnectionPool.CloseConnection(connection); } } }
// todo: // Instead of "TConfig" and abstract methods // perhaps a set of "readonly Action<T> or Funct<T, V>" would be better // these can be set at the top of the inheriting class tree and would // be passed in as variables? public ManagedTable(TConfig config, Func <Instance, TConfig, TCursor> OpenCursor) { // To ensure all references are valid _config = config; // Prepare locks _updateLocks = new object[NumUpdateLocks]; for (int i = 0; i < _updateLocks.Length; ++i) { _updateLocks[i] = new object(); } // Get the configuration var defaultConfig = ManagedTableDefaultConfig.GetDefaultDatabaseConfig(); var databaseConfig = new DatabaseConfig(); var databaseDirectory = Environment.CurrentDirectory; databaseConfig.SystemPath = databaseDirectory; databaseConfig.LogFilePath = databaseDirectory; databaseConfig.TempPath = databaseDirectory; // Apply configuration databaseConfig.Merge(defaultConfig); databaseConfig.Merge(_config.GetDefaultDatabaseConfig(), MergeRules.Overwrite); databaseConfig.SetGlobalParams(); // Get the database instance _instance = new Instance( databaseConfig.Identifier, databaseConfig.DisplayName, databaseConfig.DatabaseStopFlags ); // Apply instance level config databaseConfig.SetInstanceParams(_instance.JetInstance); // Todo: look for fastest recovery flags InitGrbit grbit = databaseConfig.DatabaseRecoveryFlags | (EsentVersion.SupportsWindows7Features ? Windows7Grbits.ReplayIgnoreLostLogs : InitGrbit.None); _instance.Init(grbit); // Recalculate path var databasePath = Path.Combine(databaseDirectory, databaseConfig.DatabaseFilename); try { _config.Database = databaseConfig.DatabaseFilename; using (var session = new Session(_instance)) { JET_DBID dbid; Api.JetCreateDatabase2( session, databasePath, databaseConfig.DatabaseMaxPages, out dbid, databaseConfig.DatabaseCreationFlags | CreateDatabaseGrbit.OverwriteExisting | CreateDatabaseGrbit.RecoveryOff ); try { // Abstract function using (var transaction = new Transaction(session)) { CreateManagedTable(session, dbid); transaction.Commit(CommitTransactionGrbit.None); Api.JetCloseDatabase(session, dbid, CloseDatabaseGrbit.None); } } catch (Exception) { // Delete the partially constructed database Api.JetCloseDatabase(session, dbid, CloseDatabaseGrbit.None); Api.JetDetachDatabase(session, databasePath); File.Delete(databasePath); throw; } } // Get the cursor cache _cursors = new ManagedTableCursorCache <TConfig, TCursor>( _instance, _config, OpenCursor ); } catch (Exception) { // We have failed to initialize for some reason. Terminate // the instance. _instance.Term(); throw; } // Now attach the database to the instance and held variable _database = new Database(_instance.JetInstance, false, databaseConfig); }
/// <summary> /// Initialize the JET_INSTANCE. /// </summary> /// <param name="grbit"> /// Initialization options. /// </param> public void Init(InitGrbit grbit) { this.CheckObjectIsNotDisposed(); JET_INSTANCE instance = this.JetInstance; try { // Remember that a failure in JetInit can zero the handle // and that JetTerm should not be called in that case. Api.JetInit2(ref instance, grbit); } finally { this.SetHandle(instance.Value); } }