internal void ConfigureDatabase(LightningTransaction tx, LightningDatabase db) { if (_comparer != null) { mdb_set_compare(tx.Handle(), db.Handle(), Compare); } if (_duplicatesComparer != null) { mdb_set_dupsort(tx.Handle(), db.Handle(), IsDuplicate); } }
/// <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)); } _currentWithOptimizedPair = () => _currentPair; _currentDefault = () => { if (_currentKeyStructure.size == IntPtr.Zero) { return(default(KeyValuePair <byte[], byte[]>)); } return(new KeyValuePair <byte[], byte[]>(_currentKeyStructure.GetBytes(), _currentValueStructure.GetBytes())); }; _getCurrent = _currentDefault; mdb_cursor_open(txn.Handle(), db.Handle(), out _handle); Database = db; Transaction = txn; Transaction.Disposing += Dispose; }
/// <summary> /// Drops the database. /// </summary> public MDBResultCode Drop(LightningTransaction transaction) { var result = mdb_drop(transaction.Handle(), _handle, true); IsOpened = false; _handle = default; return(result); }
internal IDisposable ConfigureDatabase(LightningTransaction tx, LightningDatabase db) { var pinnedComparer = new ComparerKeepAlive(); if (_comparer != null) { CompareFunction compare = Compare; pinnedComparer.AddComparer(compare); mdb_set_compare(tx.Handle(), db.Handle(), compare); } if (_duplicatesComparer != null) { CompareFunction dupCompare = IsDuplicate; pinnedComparer.AddComparer(dupCompare); mdb_set_dupsort(tx.Handle(), db.Handle(), dupCompare); } return pinnedComparer; }
internal IDisposable ConfigureDatabase(LightningTransaction tx, LightningDatabase db) { var pinnedComparer = new ComparerKeepAlive(); if (_comparer != null) { CompareFunction compare = Compare; pinnedComparer.AddComparer(compare); mdb_set_compare(tx.Handle(), db.Handle(), compare); } if (_duplicatesComparer != null) { CompareFunction dupCompare = IsDuplicate; pinnedComparer.AddComparer(dupCompare); 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"); } mdb_cursor_renew(txn.Handle(), _handle); }
/// <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)); } Name = name; _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); Environment = transaction.Environment; _transaction = transaction; Environment.Disposing += Dispose; mdb_dbi_open(transaction.Handle(), name, _configuration.Flags, out _handle).ThrowOnError(); _pinnedConfig = _configuration.ConfigureDatabase(transaction, this); IsOpened = true; }
/// <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; mdb_dbi_open(transaction.Handle(), name, _configuration.Flags, out _handle); _configuration.ConfigureDatabase(transaction, this); IsOpened = true; }
/// <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)); } mdb_cursor_open(txn.Handle(), db.Handle(), out _handle).ThrowOnError(); Transaction = txn; Transaction.Disposing += Dispose; }
/// <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) { Environment = environment ?? throw new ArgumentNullException(nameof(environment)); ParentTransaction = parent; IsReadOnly = flags == TransactionBeginFlags.ReadOnly; State = LightningTransactionState.Active; Environment.Disposing += Dispose; if (parent != null) { parent.Disposing += Dispose; parent.StateChanging += OnParentStateChanging; } var parentHandle = parent?.Handle() ?? IntPtr.Zero; mdb_txn_begin(environment.Handle(), parentHandle, flags, out _handle).ThrowOnError(); _originalHandle = _handle; }
/// <summary> /// Truncates all data from the database. /// </summary> public MDBResultCode Truncate(LightningTransaction transaction) { return(mdb_drop(transaction.Handle(), _handle, false)); }
/// <summary> /// Truncates all data from the database. /// </summary> public void Truncate(LightningTransaction transaction) { mdb_drop(transaction.Handle(), _handle, false); }
/// <summary> /// Drops the database. /// </summary> public void Drop(LightningTransaction transaction) { mdb_drop(transaction.Handle(), _handle, true); IsOpened = false; _handle = default(uint); }