get method
Retrieves a raw byte value from the database.
Parameters:
txn- Active transactionkey- Key to retrievedbName- Optional named database. If not provided, the default database will be used.flags- Optional operation flags
Returns the value as byte list, or null if not found.
Example:
final txn = await db.txnStart(flags: LMDBFlagSet()..add(MDB_RDONLY));
try {
final bytes = await db.get(txn, 'key');
if (bytes != null) {
print('Value: $bytes');
}
await db.txnCommit(txn);
} catch (e) {
await db.txnAbort(txn);
rethrow;
}
Implementation
Future<List<int>?> get(
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<List<int>?, MDB_val>((keyVal) {
return _withAllocated<List<int>?, MDB_val>((dataVal) {
keyVal.ref.mv_size = keyPtr.length;
keyVal.ref.mv_data = keyPtr.cast();
final result = _lib.mdb_get(
txn,
dbi,
keyVal,
dataVal,
);
if (result == 0) {
final data = dataVal.ref.mv_data.cast<Uint8>();
return data.asTypedList(dataVal.ref.mv_size).toList();
} else if (result == MDB_NOTFOUND) {
return null;
} else {
throw LMDBException('Failed to get data', result);
}
}, calloc<MDB_val>());
}, calloc<MDB_val>());
} finally {
calloc.free(keyPtr);
}
}