Skip to content

Commit

Permalink
Improved unique values filter to handle large numbers of values
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyIvon committed Feb 11, 2024
1 parent 5ccc2fa commit c89ad8b
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/DatabaseBenchmark/DatabaseBenchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

<ItemGroup>
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.301.9" />
<PackageReference Include="BloomFilter.NetCore" Version="2.2.0" />
<PackageReference Include="Bogus" Version="35.4.0" />
<PackageReference Include="CsvHelper" Version="31.0.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.38.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,5 @@ public class FloatGeneratorOptions : GeneratorOptionsBase
public double Delta { get; set; } = 0;

public bool RandomizeDelta { get; set; } = false;

//TODO: implement logic
public bool Unique { get; set; } = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,5 @@ public class IntegerGeneratorOptions : GeneratorOptionsBase
public int Delta { get; set; } = 0;

public bool RandomizeDelta { get; set; } = false;

//TODO: implement logic
public bool Unique { get; set; } = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@ public class StringGeneratorOptions : GeneratorOptionsBase
public int MaxLength { get; set; } = 10;

public string AllowedCharacters { get; set; } = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

//TODO: implement logic
public bool Unique { get; set; } = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public class UniqueGeneratorOptions : GeneratorOptionsBase
public int AttemptCount { get; set; } = 100;

public IGeneratorOptions SourceGeneratorOptions { get; set; }

public int MaxValues { get; set; } = 1000000;
}
}
20 changes: 17 additions & 3 deletions src/DatabaseBenchmark/Generators/UniqueGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DatabaseBenchmark.Generators.Interfaces;
using BloomFilter;
using DatabaseBenchmark.Generators.Interfaces;
using DatabaseBenchmark.Generators.Options;

namespace DatabaseBenchmark.Generators
Expand All @@ -7,14 +8,15 @@ public class UniqueGenerator : IGenerator, IDisposable
{
private readonly UniqueGeneratorOptions _options;
private readonly IGenerator _sourceGenerator;
private readonly HashSet<object> _existingValues = [];
private readonly IBloomFilter _bloomFilter;

public object Current { get; private set; }

public UniqueGenerator(UniqueGeneratorOptions options, IGenerator sourceGenerator)
{
_options = options;
_sourceGenerator = sourceGenerator;
_bloomFilter = FilterBuilder.Build(options.MaxValues, 0.01);
}

public bool Next()
Expand All @@ -23,7 +25,7 @@ public bool Next()
{
_sourceGenerator.Next();

if (_existingValues.Add(_sourceGenerator.Current))
if (AppendFilter(_sourceGenerator.Current))
{
Current = _sourceGenerator.Current;
return true;
Expand All @@ -39,6 +41,18 @@ public void Dispose()
{
disposable.Dispose();
}

_bloomFilter.Dispose();
}

public bool AppendFilter(object value) =>
value switch
{
int intValue => _bloomFilter.Add(intValue),
long longValue => _bloomFilter.Add(longValue),
double doubleValue => _bloomFilter.Add(doubleValue),
DateTime dateTimeValue => _bloomFilter.Add(dateTimeValue),
_ => _bloomFilter.Add(value.ToString())
};
}
}

0 comments on commit c89ad8b

Please sign in to comment.