cursorGet method

Future<CursorEntry?> cursorGet(
  1. Pointer<MDB_cursor> cursor,
  2. List<int>? key,
  3. CursorOp operation
)

Positions cursor and retrieves data

Parameters:

  • cursor - Active cursor
  • key - Optional key for positioning operations
  • operation - Cursor operation to perform

Returns the entry at cursor position, or null if no data was found

Example:

// Get first entry
final firstEntry = await db.cursorGet(cursor, null, CursorOp.first);

// Find specific entry
final entry = await db.cursorGet(
  cursor,
  utf8.encode('searchKey'),
  CursorOp.setRange
);

// Iterate through entries
var entry = await db.cursorGet(cursor, null, CursorOp.first);
while (entry != null) {
  print('Found: ${entry.toString()}');
  entry = await db.cursorGet(cursor, null, CursorOp.next);
}

Implementation

Future<CursorEntry?> cursorGet(
  Pointer<MDB_cursor> cursor,
  List<int>? key,
  CursorOp operation,
) async {
  final keyVal = calloc<MDB_val>();
  final dataVal = calloc<MDB_val>();

  try {
    if (key != null) {
      final keyPtr = calloc<Uint8>(key.length);
      try {
        final keyList = keyPtr.asTypedList(key.length);
        keyList.setAll(0, key);

        keyVal.ref.mv_size = key.length;
        keyVal.ref.mv_data = keyPtr.cast();

        final result =
            _lib.mdb_cursor_get(cursor, keyVal, dataVal, operation.value);

        if (result == MDB_NOTFOUND) return null;
        if (result != 0) {
          throw LMDBException('Cursor operation failed', result);
        }

        return CursorEntry(
          key: keyVal.ref.mv_data
              .cast<Uint8>()
              .asTypedList(keyVal.ref.mv_size)
              .toList(),
          data: dataVal.ref.mv_data
              .cast<Uint8>()
              .asTypedList(dataVal.ref.mv_size)
              .toList(),
        );
      } finally {
        calloc.free(keyPtr);
      }
    } else {
      final result =
          _lib.mdb_cursor_get(cursor, keyVal, dataVal, operation.value);

      if (result == MDB_NOTFOUND) return null;
      if (result != 0) {
        throw LMDBException('Cursor operation failed', result);
      }

      return CursorEntry(
        key: keyVal.ref.mv_data
            .cast<Uint8>()
            .asTypedList(keyVal.ref.mv_size)
            .toList(),
        data: dataVal.ref.mv_data
            .cast<Uint8>()
            .asTypedList(dataVal.ref.mv_size)
            .toList(),
      );
    }
  } finally {
    calloc.free(keyVal);
    calloc.free(dataVal);
  }
}