/// <summary> /// Opens a database in context of this transaction. /// </summary> /// <param name="name">Database name (optional). If null then the default name is used.</param> /// <param name="configuration">Database open options.</param> /// <returns>Created database wrapper.</returns> public LMDBDatabase OpenDatabase(string name = null, DatabaseConfiguration configuration = null) { configuration = configuration ?? new DatabaseConfiguration(); var db = new LMDBDatabase(name, this, configuration); return(db); }
/// <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(LMDBDatabase db) { MDBStat stat; Lmdb.mdb_stat(_handle, db.Handle(), out stat); return(stat.ms_entries.ToInt64()); }
public static void Put(this LMDBTransaction tx, LMDBDatabase db, string key, string value) { var enc = System.Text.Encoding.UTF8; byte[] bufferKey = enc.GetBytes(key); byte[] bufferValue = enc.GetBytes(value); tx.Put(db, bufferKey, bufferValue); }
/// <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(LMDBDatabase db, byte[] key, byte[] value) { if (db == null) { throw new ArgumentNullException(nameof(db)); } Lmdb.mdb_del(_handle, db.Handle(), key, value); }
/// <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(LMDBDatabase db, byte[] key, byte[] value, PutOptions options = PutOptions.None) { if (db == null) { throw new ArgumentNullException(nameof(db)); } Lmdb.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(LMDBDatabase db, byte[] key, out byte[] value) { if (db == null) { throw new ArgumentNullException(nameof(db)); } return(Lmdb.mdb_get(_handle, db.Handle(), key, out value) != Lmdb.MDB_NOTFOUND); }
/// <summary> /// Check whether data exists in database. /// </summary> /// <param name="db">Database.</param> /// <param name="key">Key.</param> /// <returns>True if key exists, false if not.</returns> public bool ContainsKey(LMDBDatabase db, byte[] key) { if (db == null) { throw new ArgumentNullException(nameof(db)); } byte[] value; return(TryGet(db, key, out value)); }
public static string Get(this LMDBTransaction tx, LMDBDatabase db, string key) { var enc = System.Text.Encoding.UTF8; byte[] bufferKey = enc.GetBytes(key); var result = tx.Get(db, bufferKey); var ret = enc.GetString(result); return(ret); }
public static bool TryGet(this LMDBTransaction tx, LMDBDatabase db, string key, out string value) { var enc = System.Text.Encoding.UTF8; byte[] result; var found = tx.TryGet(db, enc.GetBytes(key), out result); value = enc.GetString(result); return(found); }
public void GetData(string key, out string value) { LMDBTransaction tx = _env.BeginTransaction(); LMDBDatabase db = tx.OpenDatabase(_dbName, new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create }); value = tx.Get(db, key); tx.Commit(); db.Dispose(); }
internal IDisposable ConfigureDatabase(LMDBTransaction tx, LMDBDatabase db) { var pinnedComparer = new ComparerKeepAlive(); if (_comparer != null) { CompareFunction compare = Compare; pinnedComparer.AddComparer(compare); } if (_duplicatesComparer != null) { CompareFunction dupCompare = IsDuplicate; pinnedComparer.AddComparer(dupCompare); } return(pinnedComparer); }
/// <summary> /// Creates new instance of LMDBCursor /// </summary> /// <param name="db">Database</param> /// <param name="txn">Transaction</param> internal LMDBCursor(LMDBDatabase db, LMDBTransaction 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> /// Truncates all data from the database. /// </summary> public void TruncateDatabase(LMDBDatabase database) { database.Truncate(this); }
/// <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(LMDBDatabase db, byte[] key) { Lmdb.mdb_del(_handle, db.Handle(), key); }
public static bool ContainsKey(this LMDBTransaction tx, LMDBDatabase db, string key) { var enc = System.Text.Encoding.UTF8; return(tx.ContainsKey(db, enc.GetBytes(key))); }
public static void Delete(this LMDBTransaction tx, LMDBDatabase db, string key) { var enc = System.Text.Encoding.UTF8; tx.Delete(db, enc.GetBytes(key)); }
/// <summary> /// Create a cursor. /// Cursors are associated with a specific transaction and database and may not span threads. /// </summary> /// <param name="db">A database.</param> public LMDBCursor CreateCursor(LMDBDatabase db) { return(new LMDBCursor(db, this)); }
/// <summary> /// Get value from a database. /// </summary> /// <param name="db">Database </param> /// <param name="key">Key byte array.</param> /// <returns>Requested value's byte array if exists, or null if not.</returns> public byte[] Get(LMDBDatabase db, byte[] key) { byte[] value; TryGet(db, key, out value); return(value); }
/// <summary> /// Drops the database. /// </summary> public void DropDatabase(LMDBDatabase database) { database.Drop(this); }