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

Add bool support #24

Merged
merged 1 commit into from
Jun 9, 2024
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
36 changes: 34 additions & 2 deletions src/RocksDb.Extensions/PrimitiveTypesSerializerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace RocksDb.Extensions;

/// <summary>
/// Factory for creating serializers for primitive types such as int, long, and string.
/// Factory for creating serializers for primitive types such as int, long, bool and string.
/// </summary>
public class PrimitiveTypesSerializerFactory : ISerializerFactory
{
Expand All @@ -24,6 +24,10 @@ public bool CanCreateSerializer<T>()
{
return true;
}
if (type == typeof(bool))
{
return true;
}

return false;
}
Expand All @@ -42,7 +46,11 @@ public ISerializer<T> CreateSerializer<T>()
}
if (type == typeof(string))
{
return (ISerializer<T>)Activator.CreateInstance(typeof(StringSerializer));
return (ISerializer<T>) Activator.CreateInstance(typeof(StringSerializer));
}
if (type == typeof(bool))
{
return (ISerializer<T>) new BoolSerializer();
}

throw new ArgumentException($"Type {type.FullName} is not supported.");
Expand Down Expand Up @@ -119,4 +127,28 @@ public string Deserialize(ReadOnlySpan<byte> buffer)
return Encoding.UTF8.GetString(buffer);
}
}

private class BoolSerializer : ISerializer<bool>
{
public bool TryCalculateSize(ref bool value, out int size)
{
size = sizeof(bool);
return true;
}

public void WriteTo(ref bool value, ref Span<byte> span)
{
BitConverter.TryWriteBytes(span, value);
}

public void WriteTo(ref bool value, IBufferWriter<byte> buffer)
{
throw new NotImplementedException();
}

public bool Deserialize(ReadOnlySpan<byte> buffer)
{
return BitConverter.ToBoolean(buffer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,61 @@ public void should_put_range_of_data_when_key_is_derived_from_value()
cacheValue.ShouldBeEquivalentTo(expectedCacheValue);
}
}

[Test]
public void should_put_and_retrieve_data_from_store_using_bool_type()
{
// Arrange
using var testFixture = CreateTestFixture<bool, bool>();
var store = testFixture.GetStore<RocksDbGenericStore<bool, bool>>();

// Act
store.Put(true, false);

// Assert
store.HasKey(true).ShouldBeTrue();
store.TryGet(true, out var value).ShouldBeTrue();
value.ShouldBe(false);
}

[Test]
public void should_put_and_remove_data_from_store_using_bool_type()
{
// Arrange
using var testFixture = CreateTestFixture<bool, bool>();
var store = testFixture.GetStore<RocksDbGenericStore<bool, bool>>();
store.Put(true, true);

// Act
store.Remove(true);

// Assert
store.HasKey(true).ShouldBeFalse();
store.TryGet(true, out _).ShouldBeFalse();
}

[Test]
public void should_put_range_of_data_to_store_using_bool_types()
{
// Arrange
using var testFixture = CreateTestFixture<bool, bool>();
var store = testFixture.GetStore<RocksDbGenericStore<bool, bool>>();

// Act
var cacheKeys = new[] { true, false };
var cacheValues = new[] { false, true };

store.PutRange(cacheKeys.AsSpan(), cacheValues.AsSpan());

// Assert
for (var index = 0; index < cacheKeys.Length; index++)
{
var cacheKey = cacheKeys[index];
store.HasKey(cacheKey).ShouldBeTrue();
store.TryGet(cacheKey, out var cacheValue).ShouldBeTrue();
cacheValue.ShouldBe(cacheValues[index]);
}
}

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