Skip to content

Commit 6fc9596

Browse files
committed
feat: Supports export and import for in memory #6
1 parent 8184e3b commit 6fc9596

6 files changed

Lines changed: 684 additions & 5 deletions

File tree

src/BloomFilter/Annotations.cs

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
2+
#if NETSTANDARD2_0 || NETFRAMEWORK
3+
4+
namespace System.Diagnostics.CodeAnalysis;
5+
6+
/// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary>
7+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
8+
internal sealed class AllowNullAttribute : Attribute { }
9+
10+
/// <summary>Specifies that null is disallowed as an input even if the corresponding type allows it.</summary>
11+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
12+
internal sealed class DisallowNullAttribute : Attribute { }
13+
14+
/// <summary>Specifies that an output may be null even if the corresponding type disallows it.</summary>
15+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
16+
internal sealed class MaybeNullAttribute : Attribute { }
17+
18+
/// <summary>Specifies that an output will not be null even if the corresponding type allows it.</summary>
19+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
20+
internal sealed class NotNullAttribute : Attribute { }
21+
22+
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding type disallows it.</summary>
23+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
24+
internal sealed class MaybeNullWhenAttribute : Attribute
25+
{
26+
/// <summary>Initializes the attribute with the specified return value condition.</summary>
27+
/// <param name="returnValue">
28+
/// The return value condition. If the method returns this value, the associated parameter may be null.
29+
/// </param>
30+
public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
31+
32+
/// <summary>Gets the return value condition.</summary>
33+
public bool ReturnValue { get; }
34+
}
35+
36+
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary>
37+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
38+
internal sealed class NotNullWhenAttribute : Attribute
39+
{
40+
/// <summary>Initializes the attribute with the specified return value condition.</summary>
41+
/// <param name="returnValue">
42+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
43+
/// </param>
44+
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
45+
46+
/// <summary>Gets the return value condition.</summary>
47+
public bool ReturnValue { get; }
48+
}
49+
50+
/// <summary>Specifies that the output will be non-null if the named parameter is non-null.</summary>
51+
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
52+
internal sealed class NotNullIfNotNullAttribute : Attribute
53+
{
54+
/// <summary>Initializes the attribute with the associated parameter name.</summary>
55+
/// <param name="parameterName">
56+
/// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.
57+
/// </param>
58+
public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName;
59+
60+
/// <summary>Gets the associated parameter name.</summary>
61+
public string ParameterName { get; }
62+
}
63+
64+
/// <summary>Applied to a method that will never return under any circumstance.</summary>
65+
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
66+
internal sealed class DoesNotReturnAttribute : Attribute { }
67+
68+
/// <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified value.</summary>
69+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
70+
internal sealed class DoesNotReturnIfAttribute : Attribute
71+
{
72+
/// <summary>Initializes the attribute with the specified parameter value.</summary>
73+
/// <param name="parameterValue">
74+
/// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to
75+
/// the associated parameter matches this value.
76+
/// </param>
77+
public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue;
78+
79+
/// <summary>Gets the condition parameter value.</summary>
80+
public bool ParameterValue { get; }
81+
}
82+
83+
84+
/// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values.</summary>
85+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
86+
internal sealed class MemberNotNullAttribute : Attribute
87+
{
88+
/// <summary>Initializes the attribute with a field or property member.</summary>
89+
/// <param name="member">
90+
/// The field or property member that is promised to be not-null.
91+
/// </param>
92+
public MemberNotNullAttribute(string member) => Members = new[] { member };
93+
94+
/// <summary>Initializes the attribute with the list of field and property members.</summary>
95+
/// <param name="members">
96+
/// The list of field and property members that are promised to be not-null.
97+
/// </param>
98+
public MemberNotNullAttribute(params string[] members) => Members = members;
99+
100+
/// <summary>Gets field or property member names.</summary>
101+
public string[] Members { get; }
102+
}
103+
104+
/// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition.</summary>
105+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
106+
internal sealed class MemberNotNullWhenAttribute : Attribute
107+
{
108+
/// <summary>Initializes the attribute with the specified return value condition and a field or property member.</summary>
109+
/// <param name="returnValue">
110+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
111+
/// </param>
112+
/// <param name="member">
113+
/// The field or property member that is promised to be not-null.
114+
/// </param>
115+
public MemberNotNullWhenAttribute(bool returnValue, string member)
116+
{
117+
ReturnValue = returnValue;
118+
Members = new[] { member };
119+
}
120+
121+
/// <summary>Initializes the attribute with the specified return value condition and list of field and property members.</summary>
122+
/// <param name="returnValue">
123+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
124+
/// </param>
125+
/// <param name="members">
126+
/// The list of field and property members that are promised to be not-null.
127+
/// </param>
128+
public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
129+
{
130+
ReturnValue = returnValue;
131+
Members = members;
132+
}
133+
134+
/// <summary>Gets the return value condition.</summary>
135+
public bool ReturnValue { get; }
136+
137+
/// <summary>Gets field or property member names.</summary>
138+
public string[] Members { get; }
139+
}
140+
#endif
141+
142+
#if NETSTANDARD2_1
143+
144+
namespace System.Diagnostics.CodeAnalysis;
145+
146+
/// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values.</summary>
147+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
148+
internal sealed class MemberNotNullAttribute : Attribute
149+
{
150+
/// <summary>Initializes the attribute with a field or property member.</summary>
151+
/// <param name="member">
152+
/// The field or property member that is promised to be not-null.
153+
/// </param>
154+
public MemberNotNullAttribute(string member) => Members = new[] { member };
155+
156+
/// <summary>Initializes the attribute with the list of field and property members.</summary>
157+
/// <param name="members">
158+
/// The list of field and property members that are promised to be not-null.
159+
/// </param>
160+
public MemberNotNullAttribute(params string[] members) => Members = members;
161+
162+
/// <summary>Gets field or property member names.</summary>
163+
public string[] Members { get; }
164+
}
165+
166+
/// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition.</summary>
167+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
168+
internal sealed class MemberNotNullWhenAttribute : Attribute
169+
{
170+
/// <summary>Initializes the attribute with the specified return value condition and a field or property member.</summary>
171+
/// <param name="returnValue">
172+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
173+
/// </param>
174+
/// <param name="member">
175+
/// The field or property member that is promised to be not-null.
176+
/// </param>
177+
public MemberNotNullWhenAttribute(bool returnValue, string member)
178+
{
179+
ReturnValue = returnValue;
180+
Members = new[] { member };
181+
}
182+
183+
/// <summary>Initializes the attribute with the specified return value condition and list of field and property members.</summary>
184+
/// <param name="returnValue">
185+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
186+
/// </param>
187+
/// <param name="members">
188+
/// The list of field and property members that are promised to be not-null.
189+
/// </param>
190+
public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
191+
{
192+
ReturnValue = returnValue;
193+
Members = members;
194+
}
195+
196+
/// <summary>Gets the return value condition.</summary>
197+
public bool ReturnValue { get; }
198+
199+
/// <summary>Gets field or property member names.</summary>
200+
public string[] Members { get; }
201+
}
202+
203+
#endif

src/BloomFilter/Configurations/FilterMemoryOptions.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace BloomFilter.Configurations;
1+
using System.Collections;
2+
3+
namespace BloomFilter.Configurations;
24

35
public class FilterMemoryOptions
46
{
@@ -21,4 +23,24 @@ public class FilterMemoryOptions
2123
/// The Hash Method
2224
/// </summary>
2325
public HashMethod Method { get; set; } = HashMethod.Murmur3;
26+
27+
/// <summary>
28+
/// Sets the bit value
29+
/// </summary>
30+
public BitArray Bits { get; set; } = default!;
31+
32+
/// <summary>
33+
/// Sets more the bit value
34+
/// </summary>
35+
public BitArray? BitsMore { get; set; }
36+
37+
/// <summary>
38+
/// Sets the bit value
39+
/// </summary>
40+
public byte[] Bytes { get; set; } = default!;
41+
42+
/// <summary>
43+
/// Sets more the bit value
44+
/// </summary>
45+
public byte[]? BytesMore { get; set; }
2446
}

src/BloomFilter/Configurations/FilterMemoryOptionsExtension.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@ public void AddServices(IServiceCollection services)
2121
services.TryAddSingleton<IBloomFilterFactory, DefaultBloomFilterFactory>();
2222
services.AddSingleton<IBloomFilter, FilterMemory>(x =>
2323
{
24+
if (_options.Bits is not null)
25+
{
26+
return new FilterMemory(
27+
_options.Bits,
28+
_options.BitsMore,
29+
_options.Name,
30+
_options.ExpectedElements,
31+
_options.ErrorRate,
32+
HashFunction.Functions[_options.Method]);
33+
}
34+
35+
if (_options.Bytes is not null)
36+
{
37+
return new FilterMemory(
38+
_options.Bytes,
39+
_options.BytesMore,
40+
_options.Name,
41+
_options.ExpectedElements,
42+
_options.ErrorRate,
43+
HashFunction.Functions[_options.Method]);
44+
}
45+
2446
return new FilterMemory(
2547
_options.Name,
2648
_options.ExpectedElements,

0 commit comments

Comments
 (0)