internal IDisposable ConfigureDatabase(LightningTransaction tx, LightningDatabase db) { var pinnedComparer = new ComparerKeepAlive(); if (_comparer != null) { CompareFunction compare = Compare; pinnedComparer.AddComparer(compare); Lmdb.mdb_set_compare(tx.Handle(), db.Handle(), compare); } if (_duplicatesComparer != null) { CompareFunction dupCompare = IsDuplicate; pinnedComparer.AddComparer(dupCompare); Lmdb.mdb_set_dupsort(tx.Handle(), db.Handle(), dupCompare); } return(pinnedComparer); }
//TODO: tests /// <summary> /// Renew a cursor handle. /// Cursors are associated with a specific transaction and database and may not span threads. /// Cursors that are only used in read-only transactions may be re-used, to avoid unnecessary malloc/free overhead. /// The cursor may be associated with a new read-only transaction, and referencing the same database handle as it was created with. /// </summary> /// <param name="txn">Transaction to renew in.</param> public void Renew(LightningTransaction txn) { if (txn == null) { throw new ArgumentNullException(nameof(txn)); } if (!txn.IsReadOnly) { throw new InvalidOperationException("Can't renew cursor on non-readonly transaction"); } Lmdb.mdb_cursor_renew(txn.Handle(), _handle); }
/// <summary> /// Creates new instance of LightningCursor /// </summary> /// <param name="db">Database</param> /// <param name="txn">Transaction</param> internal LightningCursor(LightningDatabase db, LightningTransaction txn) { if (db == null) { throw new ArgumentNullException(nameof(db)); } if (txn == null) { throw new ArgumentNullException(nameof(txn)); } Lmdb.mdb_cursor_open(txn.Handle(), db.Handle(), out _handle); Transaction = txn; Transaction.Disposing += Dispose; }
/// <summary> /// Creates a LightningDatabase instance. /// </summary> /// <param name="name">Database name.</param> /// <param name="transaction">Active transaction.</param> /// <param name="configuration">Options for the database, like encoding, option flags, and comparison logic.</param> internal LightningDatabase(string name, LightningTransaction transaction, DatabaseConfiguration configuration) { if (transaction == null) { throw new ArgumentNullException(nameof(transaction)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } Name = name; _configuration = configuration; Environment = transaction.Environment; Environment.Disposing += Dispose; Lmdb.mdb_dbi_open(transaction.Handle(), name, _configuration.Flags, out _handle); _pinnedConfig = _configuration.ConfigureDatabase(transaction, this); IsOpened = true; }
/// <summary> /// Created new instance of LightningTransaction /// </summary> /// <param name="environment">Environment.</param> /// <param name="parent">Parent transaction or null.</param> /// <param name="flags">Transaction open options.</param> internal LightningTransaction(LightningEnvironment environment, LightningTransaction parent, TransactionBeginFlags flags) { if (environment == null) { throw new ArgumentNullException(nameof(environment)); } Environment = environment; ParentTransaction = parent; IsReadOnly = (flags & TransactionBeginFlags.ReadOnly) == TransactionBeginFlags.ReadOnly; State = LightningTransactionState.Active; Environment.Disposing += Dispose; if (parent != null) { parent.Disposing += Dispose; parent.StateChanging += OnParentStateChanging; } var parentHandle = parent?.Handle() ?? IntPtr.Zero; Lmdb.mdb_txn_begin(environment.Handle(), parentHandle, flags, out _handle); _originalHandle = _handle; }
/// <summary> /// Truncates all data from the database. /// </summary> public void Truncate(LightningTransaction transaction) { Lmdb.mdb_drop(transaction.Handle(), _handle, false); }
/// <summary> /// Drops the database. /// </summary> public void Drop(LightningTransaction transaction) { Lmdb.mdb_drop(transaction.Handle(), _handle, true); IsOpened = false; _handle = default(uint); }