diff --git a/src/LightningDB.Tests/DatabaseTests.cs b/src/LightningDB.Tests/DatabaseTests.cs index b546b5f..2b9b649 100644 --- a/src/LightningDB.Tests/DatabaseTests.cs +++ b/src/LightningDB.Tests/DatabaseTests.cs @@ -164,4 +164,21 @@ public void TruncatingTheDatabase() Assert.Equal(MDBResultCode.NotFound, result.resultCode); } + + [Fact] + public void DatabaseCanGetStats() + { + _env.Open(); + using var txn = _env.BeginTransaction(); + using var db = txn.OpenDatabase(); + + txn.Put(db, "key", 1.ToString()).ThrowOnError(); + var stats = db.DatabaseStats; + Assert.Equal(1, stats.Entries); + Assert.Equal(0, stats.BranchPages); + Assert.Equal(1, stats.LeafPages); + Assert.Equal(0, stats.OverflowPages); + Assert.Equal(_env.EnvironmentStats.PageSize, stats.PageSize); + Assert.Equal(1, stats.BTreeDepth); + } } \ No newline at end of file diff --git a/src/LightningDB.Tests/TransactionTests.cs b/src/LightningDB.Tests/TransactionTests.cs index 88d2875..d7c0839 100644 --- a/src/LightningDB.Tests/TransactionTests.cs +++ b/src/LightningDB.Tests/TransactionTests.cs @@ -135,6 +135,29 @@ public void CanCountTransactionEntries() }); } + [Fact] + public void CanGetDatabaseStatistics() + { + _env.RunTransactionScenario((commitTx, db) => + { + commitTx.Commit().ThrowOnError(); + Assert.Throws(() => db.DatabaseStats); + + const int entriesCount = 5; + using var tx = _env.BeginTransaction(); + for (var i = 0; i < entriesCount; i++) + tx.Put(db, i.ToString(), i.ToString()).ThrowOnError(); + + var stats = tx.GetStats(db); + Assert.Equal(entriesCount, stats.Entries); + Assert.Equal(0, stats.BranchPages); + Assert.Equal(1, stats.LeafPages); + Assert.Equal(0, stats.OverflowPages); + Assert.Equal(_env.EnvironmentStats.PageSize, stats.PageSize); + Assert.Equal(1, stats.BTreeDepth); + }); + } + [Fact] public void TransactionShouldSupportCustomComparer() { diff --git a/src/LightningDB/LightningDatabase.cs b/src/LightningDB/LightningDatabase.cs index 0ada479..637191b 100644 --- a/src/LightningDB/LightningDatabase.cs +++ b/src/LightningDB/LightningDatabase.cs @@ -48,22 +48,7 @@ internal LightningDatabase(string name, LightningTransaction transaction, Databa /// public bool IsOpened { get; private set; } - public Stats DatabaseStats - { - get - { - mdb_stat(_transaction._handle, _handle, out var nativeStat).ThrowOnError(); - return new Stats - { - BranchPages = nativeStat.ms_branch_pages, - BTreeDepth = nativeStat.ms_depth, - Entries = nativeStat.ms_entries, - LeafPages = nativeStat.ms_leaf_pages, - OverflowPages = nativeStat.ms_overflow_pages, - PageSize = nativeStat.ms_psize - }; - } - } + public Stats DatabaseStats => _transaction.GetStats(this); /// /// Database name. diff --git a/src/LightningDB/LightningTransaction.cs b/src/LightningDB/LightningTransaction.cs index 21bc0d3..b89401f 100644 --- a/src/LightningDB/LightningTransaction.cs +++ b/src/LightningDB/LightningTransaction.cs @@ -311,6 +311,25 @@ public long GetEntriesCount(LightningDatabase db) return stat.ms_entries; } + /// + /// Retrieve the statistics for the specified database. + /// + /// The database we are interested in. + /// The retrieved statistics. + public Stats GetStats(LightningDatabase db) + { + mdb_stat(_handle, db._handle, out var stat).ThrowOnError(); + return new Stats + { + BranchPages = stat.ms_branch_pages, + BTreeDepth = stat.ms_depth, + Entries = stat.ms_entries, + LeafPages = stat.ms_leaf_pages, + OverflowPages = stat.ms_overflow_pages, + PageSize = stat.ms_psize + }; + } + /// /// Environment in which the transaction was opened. ///