delete method
Deletes a value from the database.
Parameters:
txn- Active transactionkey- Key to deletedbName- Optional named database. If not provided, the default database will be used.flags- Optional operation flags
Example:
final txn = await db.txnStart();
try {
await db.delete(txn, 'key');
await db.txnCommit(txn);
} catch (e) {
await db.txnAbort(txn);
rethrow;
}
Note: Does not throw if key doesn't exist
Implementation
Future<void> delete(
Pointer<MDB_txn> txn,
String key, {
String? dbName,
LMDBFlagSet? flags,
}) async {
if (!isInitialized) throw StateError(_errDbNotInitialized);
final dbi = await _getDatabase(txn, name: dbName, flags: flags);
final keyPtr = key.toNativeUtf8();
try {
return _withAllocated<void, MDB_val>((keyVal) {
keyVal.ref.mv_size = keyPtr.length;
keyVal.ref.mv_data = keyPtr.cast();
final result = _lib.mdb_del(
txn,
dbi,
keyVal,
nullptr,
);
if (result != 0 && result != MDB_NOTFOUND) {
throw LMDBException('Failed to delete data', result);
}
}, calloc<MDB_val>());
} finally {
calloc.free(keyPtr);
}
}