Skip to content

Commit

Permalink
Add bool support
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret committed Jun 9, 2024
1 parent 42215f3 commit b40613f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
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

0 comments on commit b40613f

Please sign in to comment.