Skip to content

Commit

Permalink
Add overload for AddRange accepting a list of tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret committed Jan 8, 2025
1 parent cdf0459 commit 4678c30
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/RocksDb.Extensions/IRocksDbAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public interface IRocksDbAccessor<TKey, TValue>
bool TryGet(TKey key, [MaybeNullWhen(false)] out TValue value);
void PutRange(ReadOnlySpan<TKey> keys, ReadOnlySpan<TValue> values);
void PutRange(ReadOnlySpan<TValue> values, Func<TValue, TKey> keySelector);
void PutRange(IReadOnlyList<(TKey key, TValue value)> items);
IEnumerable<TValue> GetAll();
bool HasKey(TKey key);
}
Expand Down
6 changes: 6 additions & 0 deletions src/RocksDb.Extensions/IRocksDbStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public abstract class RocksDbStore<TKey, TValue>
/// <param name="values">The values to put in the store.</param>
/// <param name="keySelector">The function to use to generate keys for the values.</param>
public void PutRange(ReadOnlySpan<TValue> values, Func<TValue, TKey> keySelector) => _rocksDbAccessor.PutRange(values, keySelector);

/// <summary>
/// Adds or updates a collection of key-value pairs in the store.
/// </summary>
/// <param name="items">The collection of key-value pairs to add or update.</param>
public void PutRange(IReadOnlyList<(TKey key, TValue value)> items) => _rocksDbAccessor.PutRange(items);

/// <summary>
/// Gets all the values in the store.
Expand Down
12 changes: 12 additions & 0 deletions src/RocksDb.Extensions/RocksDbAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,18 @@ public void PutRange(ReadOnlySpan<TValue> values, Func<TValue, TKey> keySelector
_rocksDb.Write(batch);
}

public void PutRange(IReadOnlyList<(TKey key, TValue value)> items)
{
using var batch = new WriteBatch();
for (var index = 0; index < items.Count; index++)
{
var (key, value) = items[index];
AddToBatch(key, value, batch);
}

_rocksDb.Write(batch);
}

private void AddToBatch(TKey key, TValue value, WriteBatch batch)
{
byte[]? rentedKeyBuffer = null;
Expand Down
40 changes: 40 additions & 0 deletions test/RocksDb.Extensions.Tests/PutRangeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using NUnit.Framework;
using RocksDb.Extensions.Tests.Utils;
using Shouldly;

namespace RocksDb.Extensions.Tests;

public class PutRangeTests
{
[Test]
public void should_put_range_data_to_store()
{
// Arrange
using var testFixture = CreateTestFixture<int, string>();

var store = testFixture.GetStore<RocksDbGenericStore<int, string>>();
var cacheKeys = Enumerable.Range(0, 100)
.Select(x => (key: x, value: x.ToString()))
.ToList();

// Act
store.PutRange(cacheKeys);

// Assert
foreach (var (key, expectedValue) in cacheKeys)
{
store.HasKey(key).ShouldBeTrue();
store.TryGet(key, out var value).ShouldBeTrue();
value.ShouldBe(expectedValue);
}
}

private static TestFixture CreateTestFixture<TKey, TValue>()
{
var testFixture = TestFixture.Create(rockDb =>
{
_ = rockDb.AddStore<TKey, TValue, RocksDbGenericStore<TKey, TValue>>("my-store");
});
return testFixture;
}
}

0 comments on commit 4678c30

Please sign in to comment.