Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing database stats with explicit transaction #177

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -192,7 +192,7 @@
fixed (byte* valuePtr = value)
{
var mdbKey = new MDBValue(key.Length, keyPtr);
if (value == null)

Check warning on line 195 in src/LightningDB/LightningTransaction.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Comparing a span to 'null' might be redundant, the 'null' literal will be implicitly converted to a 'Span<T>.Empty' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2265)

Check warning on line 195 in src/LightningDB/LightningTransaction.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Comparing a span to 'null' might be redundant, the 'null' literal will be implicitly converted to a 'Span<T>.Empty' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2265)

Check warning on line 195 in src/LightningDB/LightningTransaction.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Comparing a span to 'null' might be redundant, the 'null' literal will be implicitly converted to a 'Span<T>.Empty' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2265)

Check warning on line 195 in src/LightningDB/LightningTransaction.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Comparing a span to 'null' might be redundant, the 'null' literal will be implicitly converted to a 'Span<T>.Empty' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2265)

Check warning on line 195 in src/LightningDB/LightningTransaction.cs

View workflow job for this annotation

GitHub Actions / build (macOS-latest)

Comparing a span to 'null' might be redundant, the 'null' literal will be implicitly converted to a 'Span<T>.Empty' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2265)

Check warning on line 195 in src/LightningDB/LightningTransaction.cs

View workflow job for this annotation

GitHub Actions / build (macOS-latest)

Comparing a span to 'null' might be redundant, the 'null' literal will be implicitly converted to a 'Span<T>.Empty' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2265)
{
return mdb_del(_handle, db._handle, mdbKey);
}
Expand Down Expand Up @@ -311,6 +311,25 @@
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
Loading