Skip to content

Commit

Permalink
Accessing database stats with explicit transaction
Browse files Browse the repository at this point in the history
Adding method to `LightningTransaction` to load database stats.  This is useful when the database was opened via a committed transaction, and then subsequently re-used by later transactions (since the original transaction is no longer valid).

Also updating LightningDatabase.DatabaseStats to call this method with its original transaction to reduce duplicate code.
  • Loading branch information
DecoyFish committed Dec 10, 2024
1 parent 37dedf4 commit 9c97ef5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 16 deletions.
17 changes: 17 additions & 0 deletions src/LightningDB.Tests/DatabaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
23 changes: 23 additions & 0 deletions src/LightningDB.Tests/TransactionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,29 @@ public void CanCountTransactionEntries()
});
}

[Fact]
public void CanGetDatabaseStatistics()
{
_env.RunTransactionScenario((commitTx, db) =>
{
commitTx.Commit().ThrowOnError();
Assert.Throws<LightningException>(() => 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()
{
Expand Down
17 changes: 1 addition & 16 deletions src/LightningDB/LightningDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,7 @@ internal LightningDatabase(string name, LightningTransaction transaction, Databa
/// </summary>
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);

/// <summary>
/// Database name.
Expand Down
19 changes: 19 additions & 0 deletions src/LightningDB/LightningTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,25 @@ public long GetEntriesCount(LightningDatabase db)
return stat.ms_entries;
}

/// <summary>
/// Retrieve the statistics for the specified database.
/// </summary>
/// <param name="db">The database we are interested in.</param>
/// <returns>The retrieved statistics.</returns>
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
};
}

/// <summary>
/// Environment in which the transaction was opened.
/// </summary>
Expand Down

0 comments on commit 9c97ef5

Please sign in to comment.