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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public IActionResult Post([FromBody] User user)
[HttpGet]
public IEnumerable<User> Get()
{
return _usersStore.GetAll();
return _usersStore.GetAllValues();
}

[HttpDelete]
Expand Down
3 changes: 2 additions & 1 deletion src/RocksDb.Extensions/IRocksDbAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public interface IRocksDbAccessor<TKey, TValue>
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();
IEnumerable<TValue> GetAllValues();
IEnumerable<TKey> GetAllKeys();
bool HasKey(TKey key);
void Clear();
int Count();
Expand Down
15 changes: 14 additions & 1 deletion src/RocksDb.Extensions/IRocksDbStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ public abstract class RocksDbStore<TKey, TValue>
/// Gets all the values in the store.
/// </summary>
/// <returns>An enumerable collection of all the values in the store.</returns>
public IEnumerable<TValue> GetAll() => _rocksDbAccessor.GetAll();
public IEnumerable<TValue> GetAllValues() => _rocksDbAccessor.GetAllValues();

/// <summary>
/// Gets all the values in the store. (Obsolete, use GetAllValues instead)
/// </summary>
/// <returns>An enumerable collection of all the values in the store.</returns>
[Obsolete("Use GetAllValues() instead.")]
public IEnumerable<TValue> GetAll() => GetAllValues();

/// <summary>
/// Determines whether the store contains a value for a specific key.
Expand Down Expand Up @@ -94,4 +101,10 @@ public abstract class RocksDbStore<TKey, TValue>
/// </remarks>
/// <returns>The total count of items in the store.</returns>
public int Count() => _rocksDbAccessor.Count();

/// <summary>
/// Gets all the keys in the store.
/// </summary>
/// <returns>An enumerable collection of all the keys in the store.</returns>
public IEnumerable<TKey> GetAllKeys() => _rocksDbAccessor.GetAllKeys();
}
15 changes: 13 additions & 2 deletions src/RocksDb.Extensions/RocksDbAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public bool TryGet(TKey key, [MaybeNullWhen(false)] out TValue value)
}
}

public TValue Deserialize(ReadOnlySpan<byte> buffer)
TValue ISpanDeserializer<TValue>.Deserialize(ReadOnlySpan<byte> buffer)
{
return _valueSerializer.Deserialize(buffer);
}
Expand Down Expand Up @@ -298,8 +298,19 @@ private void AddToBatch(TKey key, TValue value, WriteBatch batch)
}
}
}

public IEnumerable<TKey> GetAllKeys()
{
using var iterator = _rocksDbContext.Db.NewIterator(_columnFamily.Handle);
_ = iterator.SeekToFirst();
while (iterator.Valid())
{
yield return _keySerializer.Deserialize(iterator.Key());
_ = iterator.Next();
}
}

public IEnumerable<TValue> GetAll()
public IEnumerable<TValue> GetAllValues()
{
using var iterator = _rocksDbContext.Db.NewIterator(_columnFamily.Handle);
_ = iterator.SeekToFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,52 @@ public void should_put_and_retrieve_data_with_lists_from_store()
value.ShouldBeEquivalentTo(cacheValue);
}

[Test]
public void should_return_all_keys_from_store()
{
// Arrange
using var testFixture = CreateTestFixture<ProtoNetCacheKey, ProtoNetCacheValue>();
var store = testFixture.GetStore<RocksDbGenericStore<ProtoNetCacheKey, ProtoNetCacheValue>>();
var cacheKeys = Enumerable.Range(0, 10)
.Select(x => new ProtoNetCacheKey { Id = x })
.ToArray();
var cacheValues = cacheKeys.Select(k => new ProtoNetCacheValue { Id = k.Id, Value = $"value-{k.Id}" }).ToArray();
store.PutRange(cacheKeys, cacheValues);

// Act
var keysFromStore = store.GetAllKeys().ToList();

// Assert
keysFromStore.Count.ShouldBe(cacheKeys.Length);
foreach (var key in cacheKeys)
{
keysFromStore.ShouldContain(k => k.Id == key.Id);
}
}

[Test]
public void should_return_all_values_from_store()
{
// Arrange
using var testFixture = CreateTestFixture<ProtoNetCacheKey, ProtoNetCacheValue>();
var store = testFixture.GetStore<RocksDbGenericStore<ProtoNetCacheKey, ProtoNetCacheValue>>();
var cacheKeys = Enumerable.Range(0, 10)
.Select(x => new ProtoNetCacheKey { Id = x })
.ToArray();
var cacheValues = cacheKeys.Select(k => new ProtoNetCacheValue { Id = k.Id, Value = $"value-{k.Id}" }).ToArray();
store.PutRange(cacheKeys, cacheValues);

// Act
var valuesFromStore = store.GetAllValues().ToList();

// Assert
valuesFromStore.Count.ShouldBe(cacheValues.Length);
foreach (var value in cacheValues)
{
valuesFromStore.ShouldContain(v => v.Id == value.Id && v.Value == value.Value);
}
}

private static TestFixture CreateTestFixture<TKey, TValue>()
{
var testFixture = TestFixture.Create(rockDb =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void should_retrieve_all_elements_from_store()
store.PutRange(cacheKeys, cacheValues);

// Act
var values = store.GetAll().ToList();
var values = store.GetAllValues().ToList();

// Assert
values.Count.ShouldBe(cacheKeys.Length);
Expand Down