diff --git a/src/RocksDb.Extensions/PrimitiveTypesSerializerFactory.cs b/src/RocksDb.Extensions/PrimitiveTypesSerializerFactory.cs
index 48a7b2e..3e567e7 100644
--- a/src/RocksDb.Extensions/PrimitiveTypesSerializerFactory.cs
+++ b/src/RocksDb.Extensions/PrimitiveTypesSerializerFactory.cs
@@ -4,7 +4,7 @@
namespace RocksDb.Extensions;
///
-/// 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.
///
public class PrimitiveTypesSerializerFactory : ISerializerFactory
{
@@ -24,6 +24,10 @@ public bool CanCreateSerializer()
{
return true;
}
+ if (type == typeof(bool))
+ {
+ return true;
+ }
return false;
}
@@ -42,7 +46,11 @@ public ISerializer CreateSerializer()
}
if (type == typeof(string))
{
- return (ISerializer)Activator.CreateInstance(typeof(StringSerializer));
+ return (ISerializer) Activator.CreateInstance(typeof(StringSerializer));
+ }
+ if (type == typeof(bool))
+ {
+ return (ISerializer) new BoolSerializer();
}
throw new ArgumentException($"Type {type.FullName} is not supported.");
@@ -119,4 +127,28 @@ public string Deserialize(ReadOnlySpan buffer)
return Encoding.UTF8.GetString(buffer);
}
}
+
+ private class BoolSerializer : ISerializer
+ {
+ public bool TryCalculateSize(ref bool value, out int size)
+ {
+ size = sizeof(bool);
+ return true;
+ }
+
+ public void WriteTo(ref bool value, ref Span span)
+ {
+ BitConverter.TryWriteBytes(span, value);
+ }
+
+ public void WriteTo(ref bool value, IBufferWriter buffer)
+ {
+ throw new NotImplementedException();
+ }
+
+ public bool Deserialize(ReadOnlySpan buffer)
+ {
+ return BitConverter.ToBoolean(buffer);
+ }
+ }
}
diff --git a/test/RocksDb.Extensions.Tests/RocksDbStoreWithPrimitiveSerializerTests.cs b/test/RocksDb.Extensions.Tests/RocksDbStoreWithPrimitiveSerializerTests.cs
index 28c939b..8718300 100644
--- a/test/RocksDb.Extensions.Tests/RocksDbStoreWithPrimitiveSerializerTests.cs
+++ b/test/RocksDb.Extensions.Tests/RocksDbStoreWithPrimitiveSerializerTests.cs
@@ -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();
+ var store = testFixture.GetStore>();
+
+ // 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();
+ var store = testFixture.GetStore>();
+ 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();
+ var store = testFixture.GetStore>();
+
+ // 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()
{