Skip to content

Commit

Permalink
refactor: Modified Contains And ContainsAsync params changed to ReadO…
Browse files Browse the repository at this point in the history
…nlySpan<byte>
  • Loading branch information
vla committed May 9, 2023
1 parent 0df2caf commit 0036a2c
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/BloomFilter.CSRedis/FilterCSRedis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ public override async ValueTask<IList<bool>> AddAsync(IEnumerable<byte[]> elemen
return results;
}

public override bool Contains(byte[] element)
public override bool Contains(ReadOnlySpan<byte> element)
{
var positions = ComputeHash(element);
return GetBit(positions).All(a => a);
}

public override async ValueTask<bool> ContainsAsync(byte[] element)
public override async ValueTask<bool> ContainsAsync(ReadOnlyMemory<byte> element)
{
var positions = ComputeHash(element);
var positions = ComputeHash(element.Span);
var results = await GetBitAsync(positions);
return results.All(a => a);
}
Expand Down
6 changes: 3 additions & 3 deletions src/BloomFilter.EasyCaching/FilterEasyCachingRedis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ public override async ValueTask<IList<bool>> AddAsync(IEnumerable<byte[]> elemen
return results;
}

public override bool Contains(byte[] element)
public override bool Contains(ReadOnlySpan<byte> element)
{
var positions = ComputeHash(element);
return GetBit(positions).All(a => a);
}

public override async ValueTask<bool> ContainsAsync(byte[] element)
public override async ValueTask<bool> ContainsAsync(ReadOnlyMemory<byte> element)
{
var positions = ComputeHash(element);
var positions = ComputeHash(element.Span);
var results = await GetBitAsync(positions);
return results.All(a => a);
}
Expand Down
6 changes: 3 additions & 3 deletions src/BloomFilter.FreeRedis/FilterFreeRedis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ public override async ValueTask<IList<bool>> AddAsync(IEnumerable<byte[]> elemen
return results;
}

public override bool Contains(byte[] element)
public override bool Contains(ReadOnlySpan<byte> element)
{
var positions = ComputeHash(element);
return GetBit(positions).All(a => a);
}

public override async ValueTask<bool> ContainsAsync(byte[] element)
public override async ValueTask<bool> ContainsAsync(ReadOnlyMemory<byte> element)
{
var positions = ComputeHash(element);
var positions = ComputeHash(element.Span);
var results = await GetBitAsync(positions);
return results.All(a => a);
}
Expand Down
6 changes: 3 additions & 3 deletions src/BloomFilter.Redis/FilterRedis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public override async ValueTask<IList<bool>> AddAsync(IEnumerable<byte[]> elemen
/// </summary>
/// <param name="element"></param>
/// <returns></returns>
public override bool Contains(byte[] element)
public override bool Contains(ReadOnlySpan<byte> element)
{
var positions = ComputeHash(element);

Expand All @@ -135,9 +135,9 @@ public override bool Contains(byte[] element)
return results.All(a => a);
}

public override async ValueTask<bool> ContainsAsync(byte[] element)
public override async ValueTask<bool> ContainsAsync(ReadOnlyMemory<byte> element)
{
var positions = ComputeHash(element);
var positions = ComputeHash(element.Span);

var results = await _redisBitOperate.GetAsync(_redisKey, positions);

Expand Down
12 changes: 12 additions & 0 deletions src/BloomFilter/BloomFilterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ public static class BloomFilterExtensions

#endregion String

#region ReadOnlySpan<char>

public static bool Add(this IBloomFilter bloomFilter, ReadOnlySpan<char> data) => bloomFilter.Add(data.ToUtf8().Span);

public static ValueTask<bool> AddAsync(this IBloomFilter bloomFilter, ReadOnlyMemory<char> data) => bloomFilter.AddAsync(data.Span.ToUtf8());

public static bool Contains(this IBloomFilter bloomFilter, ReadOnlySpan<char> data) => bloomFilter.Contains(data.ToUtf8().Span);

public static ValueTask<bool> ContainsAsync(this IBloomFilter bloomFilter, ReadOnlyMemory<char> data) => bloomFilter.ContainsAsync(data.Span.ToUtf8());

#endregion String

#region Double

public static bool Add(this IBloomFilter bloomFilter, double data) => bloomFilter.Add(BitConverter.GetBytes(data));
Expand Down
4 changes: 2 additions & 2 deletions src/BloomFilter/Filter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ public Filter(string name, uint capacity, uint hashes, HashFunction hashFunction
/// </summary>
/// <param name="element"></param>
/// <returns></returns>
public abstract bool Contains(byte[] element);
public abstract bool Contains(ReadOnlySpan<byte> element);

/// <summary>
/// Async Tests whether an element is present in the filter
/// </summary>
/// <param name="element"></param>
/// <returns></returns>
public abstract ValueTask<bool> ContainsAsync(byte[] element);
public abstract ValueTask<bool> ContainsAsync(ReadOnlyMemory<byte> element);

/// <summary>
/// Tests whether an elements is present in the filter
Expand Down
6 changes: 3 additions & 3 deletions src/BloomFilter/FilterMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public override ValueTask<IList<bool>> AddAsync(IEnumerable<byte[]> elements)
/// </summary>
/// <param name="element"></param>
/// <returns></returns>
public override bool Contains(byte[] element)
public override bool Contains(ReadOnlySpan<byte> element)
{
var positions = ComputeHash(element);
lock (sync)
Expand All @@ -165,9 +165,9 @@ public override bool Contains(byte[] element)
return true;
}

public override ValueTask<bool> ContainsAsync(byte[] element)
public override ValueTask<bool> ContainsAsync(ReadOnlyMemory<byte> element)
{
return new ValueTask<bool>(Contains(element));
return new ValueTask<bool>(Contains(element.Span));
}

public override IList<bool> Contains(IEnumerable<byte[]> elements)
Expand Down
4 changes: 2 additions & 2 deletions src/BloomFilter/IBloomFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ public interface IBloomFilter : IDisposable
/// </summary>
/// <param name="element"></param>
/// <returns></returns>
bool Contains(byte[] element);
bool Contains(ReadOnlySpan<byte> element);

/// <summary>
/// Async Tests whether an element is present in the filter
/// </summary>
/// <param name="element"></param>
/// <returns></returns>
ValueTask<bool> ContainsAsync(byte[] element);
ValueTask<bool> ContainsAsync(ReadOnlyMemory<byte> element);

/// <summary>
/// Tests whether an elements is present in the filter
Expand Down
57 changes: 57 additions & 0 deletions src/BloomFilter/StringSpanExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Text;

namespace BloomFilter;

internal static class StringSpanExtensions
{
public static ReadOnlyMemory<byte> ToUtf8(this ReadOnlySpan<char> value)
{
#if NET6_0_OR_GREATER
Memory<byte> bytes = new byte[Encoding.UTF8.GetByteCount(value)];
var bytesWritten = Encoding.UTF8.GetBytes(value, bytes.Span);
return bytes.Slice(0, bytesWritten);

#else
var chars = value.ToArray();
var bytes = new byte[Encoding.UTF8.GetByteCount(chars)];
var bytesWritten = Encoding.UTF8.GetBytes(chars, 0, value.Length, bytes, 0);
return new ReadOnlyMemory<byte>(bytes, 0, bytesWritten);
#endif
}

public static ReadOnlyMemory<char> FromUtf8(this ReadOnlySpan<byte> source)
{
#if NET6_0_OR_GREATER
source = source.WithoutBom();
Memory<char> chars = new char[Encoding.UTF8.GetCharCount(source)];
var charsWritten = Encoding.UTF8.GetChars(source, chars.Span);
return chars.Slice(0, charsWritten);
#else
var bytes = source.WithoutBom().ToArray();
var chars = new char[Encoding.UTF8.GetCharCount(bytes)];
var charsWritten = Encoding.UTF8.GetChars(bytes, 0, bytes.Length, chars, 0);
return new ReadOnlyMemory<char>(chars, 0, charsWritten);

#endif
}

public static byte[] ToUtf8Bytes(this ReadOnlySpan<char> value)
{
return ToUtf8(value).ToArray();
}

public static ReadOnlySpan<char> WithoutBom(this ReadOnlySpan<char> value)
{
return value.Length > 0 && value[0] == 65279
? value.Slice(1)
: value;
}

public static ReadOnlySpan<byte> WithoutBom(this ReadOnlySpan<byte> value)
{
return value.Length > 3 && value[0] == 0xEF && value[1] == 0xBB && value[2] == 0xBF
? value.Slice(3)
: value;
}
}

0 comments on commit 0036a2c

Please sign in to comment.