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> /// The number of items in the database. /// </summary> /// <param name="db">The database we are counting items in.</param> /// <returns>The number of items.</returns> public long GetEntriesCount(LightningDatabase db) { MDBStat stat; mdb_stat(_handle, db.Handle(), out stat); return(stat.ms_entries.ToInt64()); }
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; }
/// <summary> /// Delete items from a database. /// This function removes key/data pairs from the database. /// If the database does not support sorted duplicate data items (MDB_DUPSORT) the data parameter is ignored. /// If the database supports sorted duplicates and the data parameter is NULL, all of the duplicate data items for the key will be deleted. /// Otherwise, if the data parameter is non-NULL only the matching data item will be deleted. /// This function will return MDB_NOTFOUND if the specified key/data pair is not in the database. /// </summary> /// <param name="db">A database handle returned by mdb_dbi_open()</param> /// <param name="key">The key to delete from the database</param> public unsafe MDBResultCode Delete(LightningDatabase db, ReadOnlySpan <byte> key) { fixed(byte *ptr = key) { var mdbKey = new MDBValue(key.Length, ptr); return(mdb_del(_handle, db.Handle(), mdbKey)); } }
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); }
/// <summary> /// Put data into a database. /// </summary> /// <param name="db">Database.</param> /// <param name="key">Key byte array.</param> /// <param name="value">Value byte array.</param> /// <param name="options">Operation options (optional).</param> public void Put(LightningDatabase db, byte[] key, byte[] value, PutOptions options = PutOptions.None) { if (db == null) { throw new ArgumentNullException(nameof(db)); } mdb_put(_handle, db.Handle(), key, value, options); }
/// <summary> /// Delete items from a database. /// This function removes key/data pairs from the database. /// If the database does not support sorted duplicate data items (MDB_DUPSORT) the data parameter is ignored. /// If the database supports sorted duplicates and the data parameter is NULL, all of the duplicate data items for the key will be deleted. /// Otherwise, if the data parameter is non-NULL only the matching data item will be deleted. /// This function will return MDB_NOTFOUND if the specified key/data pair is not in the database. /// </summary> /// <param name="db">A database handle returned by mdb_dbi_open()</param> /// <param name="key">The key to delete from the database</param> /// <param name="value">The data to delete (optional)</param> public void Delete(LightningDatabase db, byte[] key, byte[] value) { if (db == null) { throw new ArgumentNullException(nameof(db)); } mdb_del(_handle, db.Handle(), key, value); }
/// <summary> /// Tries to get a value by its key. /// </summary> /// <param name="db">Database.</param> /// <param name="key">Key byte array.</param> /// <param name="value">Value byte array if exists.</param> /// <returns>True if key exists, false if not.</returns> public bool TryGet(LightningDatabase db, byte[] key, out byte[] value) { if (db == null) { throw new ArgumentNullException(nameof(db)); } return(mdb_get(_handle, db.Handle(), key, out value) != MDB_NOTFOUND); }
/// <summary> /// Delete items from a database. /// This function removes key/data pairs from the database. /// If the database does not support sorted duplicate data items (MDB_DUPSORT) the data parameter is ignored. /// If the database supports sorted duplicates and the data parameter is NULL, all of the duplicate data items for the key will be deleted. /// Otherwise, if the data parameter is non-NULL only the matching data item will be deleted. /// This function will return MDB_NOTFOUND if the specified key/data pair is not in the database. /// </summary> /// <param name="db">A database handle returned by mdb_dbi_open()</param> /// <param name="key">The key to delete from the database</param> /// <param name="value">The data to delete (optional)</param> public unsafe MDBResultCode Delete(LightningDatabase db, ReadOnlySpan <byte> key, ReadOnlySpan <byte> value) { if (db == null) { throw new ArgumentNullException(nameof(db)); fixed(byte *keyPtr = key) fixed(byte *valuePtr = value) { var mdbKey = new MDBValue(key.Length, keyPtr); if (value == null) { return(mdb_del(_handle, db.Handle(), mdbKey)); } var mdbValue = new MDBValue(value.Length, valuePtr); return(mdb_del(_handle, db.Handle(), mdbKey, mdbValue)); } }
/// <summary> /// Get value from a database. /// </summary> /// <param name="db">The database to query.</param> /// <param name="key">A span containing the key to look up.</param> /// <returns>Requested value's byte array if exists, or null if not.</returns> public unsafe (MDBResultCode resultCode, MDBValue key, MDBValue value) Get(LightningDatabase db, ReadOnlySpan <byte> key) { if (db == null) { throw new ArgumentNullException(nameof(db)); fixed(byte *keyBuffer = key) { var mdbKey = new MDBValue(key.Length, keyBuffer); return(mdb_get(_handle, db.Handle(), ref mdbKey, out var mdbValue), mdbKey, mdbValue); } }
/// <summary> /// Put data into a database. /// </summary> /// <param name="db">Database.</param> /// <param name="key">Key byte array.</param> /// <param name="value">Value byte array.</param> /// <param name="options">Operation options (optional).</param> public unsafe MDBResultCode Put(LightningDatabase db, ReadOnlySpan <byte> key, ReadOnlySpan <byte> value, PutOptions options = PutOptions.None) { if (db == null) { throw new ArgumentNullException(nameof(db)); fixed(byte *keyPtr = key) fixed(byte *valuePtr = value) { var mdbKey = new MDBValue(key.Length, keyPtr); var mdbValue = new MDBValue(value.Length, valuePtr); return(mdb_put(_handle, db.Handle(), mdbKey, mdbValue, options)); } }
/// <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> /// Delete items from a database. /// This function removes key/data pairs from the database. /// If the database does not support sorted duplicate data items (MDB_DUPSORT) the data parameter is ignored. /// If the database supports sorted duplicates and the data parameter is NULL, all of the duplicate data items for the key will be deleted. /// Otherwise, if the data parameter is non-NULL only the matching data item will be deleted. /// This function will return MDB_NOTFOUND if the specified key/data pair is not in the database. /// </summary> /// <param name="db">A database handle returned by mdb_dbi_open()</param> /// <param name="key">The key to delete from the database</param> /// <param name="value">The data to delete (optional)</param> public void Delete(LightningDatabase db, byte[] key, byte[] value) { if (db == null) throw new ArgumentNullException(nameof(db)); mdb_del(_handle, db.Handle(), key, value); }
/// <summary> /// Delete items from a database. /// This function removes key/data pairs from the database. /// If the database does not support sorted duplicate data items (MDB_DUPSORT) the data parameter is ignored. /// If the database supports sorted duplicates and the data parameter is NULL, all of the duplicate data items for the key will be deleted. /// Otherwise, if the data parameter is non-NULL only the matching data item will be deleted. /// This function will return MDB_NOTFOUND if the specified key/data pair is not in the database. /// </summary> /// <param name="db">A database handle returned by mdb_dbi_open()</param> /// <param name="key">The key to delete from the database</param> public void Delete(LightningDatabase db, byte[] key) { mdb_del(_handle, db.Handle(), key); }
/// <summary> /// The number of items in the database. /// </summary> /// <param name="db">The database we are counting items in.</param> /// <returns>The number of items.</returns> public long GetEntriesCount(LightningDatabase db) { MDBStat stat; mdb_stat(_handle, db.Handle(), out stat); return stat.ms_entries.ToInt64(); }
public MDBStat GetStats(LightningDatabase db) { MDBStat stat; mdb_stat(_handle, db.Handle(), out stat); return stat; }
/// <summary> /// Put data into a database. /// </summary> /// <param name="db">Database.</param> /// <param name="key">Key byte array.</param> /// <param name="value">Value byte array.</param> /// <param name="options">Operation options (optional).</param> public void Put(LightningDatabase db, byte[] key, byte[] value, PutOptions options = PutOptions.None) { if (db == null) throw new ArgumentNullException(nameof(db)); mdb_put(_handle, db.Handle(), key, value, options); }
/// <summary> /// Tries to get a value by its key. /// </summary> /// <param name="db">Database.</param> /// <param name="key">Key byte array.</param> /// <param name="value">Value byte array if exists.</param> /// <returns>True if key exists, false if not.</returns> public bool TryGet(LightningDatabase db, byte[] key, out byte[] value) { if (db == null) throw new ArgumentNullException(nameof(db)); return mdb_get(_handle, db.Handle(), key, out value) != MDB_NOTFOUND; }
/// <summary> /// The number of items in the database. /// </summary> /// <param name="db">The database we are counting items in.</param> /// <returns>The number of items.</returns> public long GetEntriesCount(LightningDatabase db) { mdb_stat(_handle, db.Handle(), out var stat).ThrowOnError(); return(stat.ms_entries.ToInt64()); }