getStats method

Future<DatabaseStats> getStats({
  1. String? dbName,
})

Gets statistics for a database.

If dbName is null, returns statistics for the default database. If dbName is provided, returns statistics for the named database.

Throws LMDBException if:

  • The database cannot be opened
  • Statistics cannot be retrieved

Example:

// Get stats for default database
final defaultStats = await db.getStats();

// Get stats for named database
final userStats = await db.getStats(dbName: 'users');

Implementation

Future<DatabaseStats> getStats({String? dbName}) async {
  final currentEnv = env;
  late final Pointer<MDB_txn> txn;
  try {
    // start transaction (read-only)
    final txnPtr = calloc<Pointer<MDB_txn>>();
    try {
      final result = _lib.mdb_txn_begin(
        currentEnv,
        nullptr,
        MDB_RDONLY,
        txnPtr,
      );

      if (result != 0) {
        throw LMDBException('Failed to start transaction', result);
      }
      txn = txnPtr.value;
    } finally {
      calloc.free(txnPtr);
    }

    // open db read-only
    final dbiPtr = calloc<MDB_dbi>();
    late final int dbi;
    try {
      final result = _lib.mdb_dbi_open(
        txn,
        dbName?.toNativeUtf8().cast() ?? nullptr,
        0,
        dbiPtr,
      );

      if (result != 0) {
        throw LMDBException('Failed to open database', result);
      }
      dbi = dbiPtr.value;
    } finally {
      calloc.free(dbiPtr);
    }

    return _getStats(txn, dbi);
  } finally {
    _lib.mdb_txn_abort(txn);
  }
}