Skip to content

Commit 1604134

Browse files
authored
Merge pull request #177 from DecoyFish/master
Accessing database stats with explicit transaction
2 parents 37dedf4 + 9c97ef5 commit 1604134

File tree

4 files changed

+60
-16
lines changed

4 files changed

+60
-16
lines changed

src/LightningDB.Tests/DatabaseTests.cs

+17
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,21 @@ public void TruncatingTheDatabase()
164164

165165
Assert.Equal(MDBResultCode.NotFound, result.resultCode);
166166
}
167+
168+
[Fact]
169+
public void DatabaseCanGetStats()
170+
{
171+
_env.Open();
172+
using var txn = _env.BeginTransaction();
173+
using var db = txn.OpenDatabase();
174+
175+
txn.Put(db, "key", 1.ToString()).ThrowOnError();
176+
var stats = db.DatabaseStats;
177+
Assert.Equal(1, stats.Entries);
178+
Assert.Equal(0, stats.BranchPages);
179+
Assert.Equal(1, stats.LeafPages);
180+
Assert.Equal(0, stats.OverflowPages);
181+
Assert.Equal(_env.EnvironmentStats.PageSize, stats.PageSize);
182+
Assert.Equal(1, stats.BTreeDepth);
183+
}
167184
}

src/LightningDB.Tests/TransactionTests.cs

+23
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,29 @@ public void CanCountTransactionEntries()
135135
});
136136
}
137137

138+
[Fact]
139+
public void CanGetDatabaseStatistics()
140+
{
141+
_env.RunTransactionScenario((commitTx, db) =>
142+
{
143+
commitTx.Commit().ThrowOnError();
144+
Assert.Throws<LightningException>(() => db.DatabaseStats);
145+
146+
const int entriesCount = 5;
147+
using var tx = _env.BeginTransaction();
148+
for (var i = 0; i < entriesCount; i++)
149+
tx.Put(db, i.ToString(), i.ToString()).ThrowOnError();
150+
151+
var stats = tx.GetStats(db);
152+
Assert.Equal(entriesCount, stats.Entries);
153+
Assert.Equal(0, stats.BranchPages);
154+
Assert.Equal(1, stats.LeafPages);
155+
Assert.Equal(0, stats.OverflowPages);
156+
Assert.Equal(_env.EnvironmentStats.PageSize, stats.PageSize);
157+
Assert.Equal(1, stats.BTreeDepth);
158+
});
159+
}
160+
138161
[Fact]
139162
public void TransactionShouldSupportCustomComparer()
140163
{

src/LightningDB/LightningDatabase.cs

+1-16
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,7 @@ internal LightningDatabase(string name, LightningTransaction transaction, Databa
4848
/// </summary>
4949
public bool IsOpened { get; private set; }
5050

51-
public Stats DatabaseStats
52-
{
53-
get
54-
{
55-
mdb_stat(_transaction._handle, _handle, out var nativeStat).ThrowOnError();
56-
return new Stats
57-
{
58-
BranchPages = nativeStat.ms_branch_pages,
59-
BTreeDepth = nativeStat.ms_depth,
60-
Entries = nativeStat.ms_entries,
61-
LeafPages = nativeStat.ms_leaf_pages,
62-
OverflowPages = nativeStat.ms_overflow_pages,
63-
PageSize = nativeStat.ms_psize
64-
};
65-
}
66-
}
51+
public Stats DatabaseStats => _transaction.GetStats(this);
6752

6853
/// <summary>
6954
/// Database name.

src/LightningDB/LightningTransaction.cs

+19
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,25 @@ public long GetEntriesCount(LightningDatabase db)
311311
return stat.ms_entries;
312312
}
313313

314+
/// <summary>
315+
/// Retrieve the statistics for the specified database.
316+
/// </summary>
317+
/// <param name="db">The database we are interested in.</param>
318+
/// <returns>The retrieved statistics.</returns>
319+
public Stats GetStats(LightningDatabase db)
320+
{
321+
mdb_stat(_handle, db._handle, out var stat).ThrowOnError();
322+
return new Stats
323+
{
324+
BranchPages = stat.ms_branch_pages,
325+
BTreeDepth = stat.ms_depth,
326+
Entries = stat.ms_entries,
327+
LeafPages = stat.ms_leaf_pages,
328+
OverflowPages = stat.ms_overflow_pages,
329+
PageSize = stat.ms_psize
330+
};
331+
}
332+
314333
/// <summary>
315334
/// Environment in which the transaction was opened.
316335
/// </summary>

0 commit comments

Comments
 (0)