Skip to content

Commit

Permalink
Add missing comments to public methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret committed Mar 2, 2023
1 parent 7e3ef63 commit 7a5150e
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
using System;
using ProtoBuf.Meta;

namespace RocksDb.Extensions.ProtoBufNet;

/// <summary>
/// A factory class for creating ProtoBuf-Net serializers.
/// </summary>
public class ProtoBufNetSerializerFactory : ISerializerFactory
{
/// <inheritdoc/>
public bool CanCreateSerializer<T>()
{
// Checks if ProtoBuf-Net can serialize the specified type.
return RuntimeTypeModel.Default.CanSerialize(typeof(T));
}

/// <inheritdoc/>
public ISerializer<T> CreateSerializer<T>()
{
// Creates an instance of the ProtoBufNetSerializer with the specified type.
var type = typeof(ProtoBufNetSerializer<>).MakeGenericType(typeof(T));
return (ISerializer<T>) Activator.CreateInstance(type);
}
}
}
1 change: 0 additions & 1 deletion src/RocksDb.Extensions.Protobuf/ProtobufSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Buffers;
using Google.Protobuf;

Expand Down
6 changes: 5 additions & 1 deletion src/RocksDb.Extensions.Protobuf/ProtobufSerializerFactory.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using System;
using Google.Protobuf;

namespace RocksDb.Extensions.Protobuf;

/// <summary>
/// A factory class for creating Google.Protobuf serializers.
/// </summary>
public class ProtobufSerializerFactory : ISerializerFactory
{
/// <inheritdoc/>
public bool CanCreateSerializer<T>()
{
var type = typeof(T);
return typeof(IMessage).IsAssignableFrom(type);
}

/// <inheritdoc/>
public ISerializer<T> CreateSerializer<T>()
{
var type = typeof(ProtobufSerializer<>).MakeGenericType(typeof(T));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Buffers;
using System.Text.Json;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
using System;
using System.Text.Json;

namespace RocksDb.Extensions.System.Text.Json;

/// <summary>
/// A serializer factory implementation that creates instances of SystemTextJsonSerializer for any type.
/// </summary>
public class SystemTextJsonSerializerFactory : ISerializerFactory
{
private readonly JsonSerializerOptions? _options;


/// <summary>
/// Initializes a new instance of the SystemTextJsonSerializerFactory class with optional JsonSerializerOptions.
/// </summary>
/// <param name="options">Optional JsonSerializerOptions to use for serialization.</param>
public SystemTextJsonSerializerFactory(JsonSerializerOptions? options = null)
{
_options = options;
}

/// <inheritdoc/>
public bool CanCreateSerializer<T>()
{
return true;
}

/// <inheritdoc/>
public ISerializer<T> CreateSerializer<T>()
{
var type = typeof(SystemTextJsonSerializer<>).MakeGenericType(typeof(T));
return (ISerializer<T>)Activator.CreateInstance(type, _options);
return (ISerializer<T>) Activator.CreateInstance(type, _options);
}


}
}
12 changes: 11 additions & 1 deletion src/RocksDb.Extensions/IRocksDbAccessor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace RocksDb.Extensions;

#pragma warning disable CS1591

/// <summary>
/// This interface is not intended to be used directly by the clients of the library,
/// as it is used by the <see cref="RocksDbStore{TKey, TValue}"/> to access RocksDB.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public interface IRocksDbAccessor<TKey, TValue>
{
void Remove(TKey key);
Expand All @@ -10,4 +18,6 @@ public interface IRocksDbAccessor<TKey, TValue>
void PutRange(ReadOnlySpan<TKey> keys, ReadOnlySpan<TValue> values);
void PutRange(ReadOnlySpan<TValue> values, Func<TValue, TKey> keySelector);
IEnumerable<TValue> GetAll();
}
}

#pragma warning restore CS1591
17 changes: 16 additions & 1 deletion src/RocksDb.Extensions/IRocksDbBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
namespace RocksDb.Extensions;

/// <summary>
/// A builder that can be used to create a RocksDB instance with one or more stores.
/// </summary>
public interface IRocksDbBuilder
{
/// <summary>
/// Adds a RocksDB store to the builder for the specified column family.
/// </summary>
/// <param name="columnFamily"></param>
/// <typeparam name="TKey">The type of the store's key.</typeparam>
/// <typeparam name="TValue">The type of the store's value.</typeparam>
/// <typeparam name="TStore">The type of the store to add.</typeparam>
/// <returns>The builder instance for method chaining</returns>
/// <remarks>
/// The <typeparamref name="TStore"/> type must be a concrete implementation of the abstract class
/// <see cref="RocksDbStore{TKey,TValue}"/>.
/// </remarks>
IRocksDbBuilder AddStore<TKey, TValue, TStore>(string columnFamily) where TStore : RocksDbStore<TKey, TValue>;
}
}
39 changes: 39 additions & 0 deletions src/RocksDb.Extensions/IRocksDbStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,59 @@

namespace RocksDb.Extensions;

/// <summary>
/// Base class for a RocksDB store that provides basic operations such as add, update, remove, get and get all.
/// </summary>
/// <typeparam name="TKey">The type of the store's keys.</typeparam>
/// <typeparam name="TValue">The type of the store's values.</typeparam>
public abstract class RocksDbStore<TKey, TValue>
{
private readonly IRocksDbAccessor<TKey, TValue> _rocksDbAccessor;

/// <summary>
/// Initializes a new instance of the <see cref="RocksDbStore{TKey, TValue}"/> class with the specified RocksDB accessor.
/// </summary>
/// <param name="rocksDbAccessor">The RocksDB accessor to use for database operations.</param>
protected RocksDbStore(IRocksDbAccessor<TKey, TValue> rocksDbAccessor) => _rocksDbAccessor = rocksDbAccessor;

/// <summary>
/// Removes the specified key and its associated value from the store.
/// </summary>
/// <param name="key">The key to remove.</param>
public void Remove(TKey key) => _rocksDbAccessor.Remove(key);

/// <summary>
/// Adds or updates the specified key-value pair in the store.
/// </summary>
/// <param name="key">The key to add or update.</param>
/// <param name="value">The value to add or update.</param>
public void Put(TKey key, TValue value) => _rocksDbAccessor.Put(key, value);

/// <summary>
/// Tries to get the value associated with the specified key in the store.
/// </summary>
/// <param name="key">The key of the value to get.</param>
/// <param name="value">The value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter.</param>
/// <returns><c>true</c> if the key is found; otherwise, <c>false</c>.</returns>
public bool TryGet(TKey key, [MaybeNullWhen(false)] out TValue value) => _rocksDbAccessor.TryGet(key, out value);

/// <summary>
/// Puts the specified keys and values in the store.
/// </summary>
/// <param name="keys">The keys to put in the store.</param>
/// <param name="values">The values to put in the store.</param>
public void PutRange(ReadOnlySpan<TKey> keys, ReadOnlySpan<TValue> values) => _rocksDbAccessor.PutRange(keys, values);

/// <summary>
/// Puts the specified values in the store using the specified key selector function to generate keys.
/// </summary>
/// <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>
/// 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();
}
34 changes: 34 additions & 0 deletions src/RocksDb.Extensions/ISerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,44 @@

namespace RocksDb.Extensions;

/// <summary>
/// Defines the contract for a serializer that can serialize and deserialize objects of type T to and from a byte buffer.
/// </summary>
/// <typeparam name="T">The type of object to be serialized.</typeparam>
public interface ISerializer<T>
{
/// <summary>
/// Attempts to calculate the size in bytes required to serialize the specified object.
/// </summary>
/// <param name="value">The object to be serialized.</param>
/// <param name="size">The size of the buffer required to serialize the object.</param>
/// <returns><c>true</c> if the size was calculated successfully; otherwise, <c>false</c>.</returns>
bool TryCalculateSize(ref T value, out int size);

/// <summary>
/// Serializes the specified object to a byte buffer.
/// </summary>
/// <param name="value">The object to serialize.</param>
/// <param name="span">The byte span to write the serialized object to.</param>
/// <remarks>
/// This method will be used when <see cref="TryCalculateSize"/> returns <c>true</c>.
/// </remarks>
void WriteTo(ref T value, ref Span<byte> span);

/// <summary>
/// Serializes the specified object to a byte buffer using the provided buffer writer.
/// </summary>
/// <param name="value">The object to serialize.</param>
/// <param name="buffer">The <see cref="IBufferWriter{T}"/> to write the serialized object to.</param>
/// <remarks>
/// This method will be used when <see cref="TryCalculateSize"/> returns <c>false</c>.
/// </remarks>
void WriteTo(ref T value, IBufferWriter<byte> buffer);

/// <summary>
/// Deserializes an object from the provided byte span.
/// </summary>
/// <param name="buffer">The byte span containing the serialized object.</param>
/// <returns>The deserialized object.</returns>
T Deserialize(ReadOnlySpan<byte> buffer);
}
14 changes: 14 additions & 0 deletions src/RocksDb.Extensions/ISerializerFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
namespace RocksDb.Extensions;

/// <summary>
/// Defines an interface for a serializer factory that can create serializers for a given type.
/// </summary>
public interface ISerializerFactory
{
/// <summary>
/// Determines whether this factory can create a serializer for the specified type.
/// </summary>
/// <typeparam name="T">The type to create a serializer for.</typeparam>
/// <returns><c>true</c> if this factory can create a serializer for the specified type; otherwise, <c>false</c>.</returns>
bool CanCreateSerializer<T>();

/// <summary>
/// Creates a serializer for the specified type.
/// </summary>
/// <typeparam name="T">The type to create a serializer for.</typeparam>
/// <returns>A serializer for the specified type.</returns>
ISerializer<T> CreateSerializer<T>();
}
5 changes: 5 additions & 0 deletions src/RocksDb.Extensions/PrimitiveTypesSerializerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

namespace RocksDb.Extensions;

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

/// <inheritdoc/>
public ISerializer<T> CreateSerializer<T>()
{
var type = typeof(T);
Expand Down
11 changes: 10 additions & 1 deletion src/RocksDb.Extensions/RocksDbExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@

namespace RocksDb.Extensions;

/// <summary>
/// Extension methods for setting up RocksDB related services in an <see cref="IServiceCollection" />.
/// </summary>
public static class RocksDbExtensions
{
/// <summary>
/// This method adds the necessary services to the dependency injection container required for RocksDB integration.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="optionsAction">A delegate that is used to configure the options for the RocksDB instance.</param>
/// <returns>An instance of <see cref="IRocksDbBuilder"/> that can be used to build and configure a RocksDB instance.</returns>
public static IRocksDbBuilder AddRocksDb(this IServiceCollection services, Action<RocksDbOptions> optionsAction)
{
services.AddOptions<RocksDbOptions>();
services.Configure(optionsAction);
services.TryAddSingleton<RocksDbContext>();
return new RocksDbBuilder(services);
}
}
}
22 changes: 19 additions & 3 deletions src/RocksDb.Extensions/RocksDbOptions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
namespace RocksDb.Extensions;

/// <summary>
/// Represents the configuration options for a RocksDB instance.
/// </summary>
public class RocksDbOptions
{
public string Path { get; set; }
internal List<string> ColumnFamilies { get; } = new();
/// <summary>
/// A string that represents the path to the RocksDB instance on disk.
/// </summary>
public string Path { get; set; } = null!;

/// <summary>
/// A list of <see cref="ISerializerFactory"/> instances that are used to serialize and deserialize data in the RocksDB instance.
/// </summary>
/// <remarks>
/// At least one serializer factory must be registered before using the RocksDB instance.
/// If no factory can create a serializer for any type defined by any of the registered stores,
/// an <see cref="InvalidOperationException"/> will be thrown.
/// </remarks>
public List<ISerializerFactory> SerializerFactories { get; } = new();
}

internal List<string> ColumnFamilies { get; } = new();
}
2 changes: 1 addition & 1 deletion test/RocksDb.Extensions.Tests/Protos/ProtoNetCacheValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public class ProtoNetCacheValue
public int Id { get; set; }

[ProtoMember(2)]
public string Value { get; set; }
public string Value { get; set; } = null!;
}

0 comments on commit 7a5150e

Please sign in to comment.