Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/RocksDb.Extensions/IRocksDbAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public interface IRocksDbAccessor<TKey, TValue>
IEnumerable<TValue> GetAll();
bool HasKey(TKey key);
void Clear();
int Count();
}

#pragma warning restore CS1591
13 changes: 13 additions & 0 deletions src/RocksDb.Extensions/IRocksDbStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,17 @@ public abstract class RocksDbStore<TKey, TValue>
/// can impact performance during execution. Use with caution in high-frequency workflows.
/// </summary>
public void Clear() => _rocksDbAccessor.Clear();

/// <summary>
/// Gets the number of key-value pairs currently stored.
/// </summary>
/// <remarks>
/// This method is <b>not</b> a constant-time operation. Internally, it iterates over all entries in the store
/// to compute the count. While the keys and values are not deserialized during iteration, this process may still
/// be expensive for large datasets.
///
/// Use this method with caution in performance-critical paths, especially if the store contains a high number of entries.
/// </remarks>
/// <returns>The total count of items in the store.</returns>
public int Count() => _rocksDbAccessor.Count();
}
14 changes: 14 additions & 0 deletions src/RocksDb.Extensions/RocksDbAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,20 @@ public IEnumerable<TValue> GetAll()
_ = iterator.Next();
}
}

public int Count()
{
using var iterator = _rocksDbContext.Db.NewIterator(_columnFamily.Handle);
_ = iterator.SeekToFirst();
var count = 0;
while (iterator.Valid())
{
count++;
_ = iterator.Next();
}

return count;
}

public bool HasKey(TKey key)
{
Expand Down
5 changes: 4 additions & 1 deletion test/RocksDb.Extensions.Tests/ClearStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace RocksDb.Extensions.Tests;
public class ClearStoreTests
{
[Test]
public void should_reset_store_range_data_to_store()
public void should_clear_and_repopulate_store_data()
{
// Setup RocksDbStore
using var testFixture = CreateTestFixture<int, string>();
Expand All @@ -27,6 +27,7 @@ public void should_reset_store_range_data_to_store()
store.TryGet(key, out var value).ShouldBeTrue();
value.ShouldBe(expectedValue);
}
Assert.That(store.Count(), Is.EqualTo(100));

// Clear the store
store.Clear();
Expand All @@ -37,6 +38,7 @@ public void should_reset_store_range_data_to_store()
store.HasKey(key).ShouldBeFalse();
store.TryGet(key, out _).ShouldBeFalse();
}
Assert.That(store.Count(), Is.EqualTo(0));

// Try to put the data again
store.PutRange(cacheKeys);
Expand All @@ -48,6 +50,7 @@ public void should_reset_store_range_data_to_store()
store.TryGet(key, out var value).ShouldBeTrue();
value.ShouldBe(expectedValue);
}
Assert.That(store.Count(), Is.EqualTo(100));
}

private static TestFixture CreateTestFixture<TKey, TValue>()
Expand Down