Skip to content

Commit

Permalink
Moved MongoStore into this project from separate nuget
Browse files Browse the repository at this point in the history
  • Loading branch information
gonace committed Jun 3, 2024
1 parent 4826e68 commit efde838
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 1 deletion.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,19 @@ RunStrategy=Throughput WarmupCount=10
| MD5_Hex | 886.5 ns | 2.87 ns | 8.47 ns | 864.0 ns | 909.2 ns | 886.9 ns |
| SHA1_Hex | 985.2 ns | 3.80 ns | 11.21 ns | 963.1 ns | 1,012.8 ns | 986.1 ns |
| SHA256_Hex | 1.167 us | 0.0053 us | 0.0157 us | 1.135 us | 1.206 us | 1.168 us |
| SHA512_Hex | 2.066 us | 0.0153 us | 0.0452 us | 2.006 us | 2.172 us | 2.054 us |
| SHA512_Hex | 2.066 us | 0.0153 us | 0.0452 us | 2.006 us | 2.172 us | 2.054 us |

## Stores
### Mongo
> This example is shown using Autofac since this is the go-to IoC for us.
```c#
public class FingerprintModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.Register<IFingerprintManager<Fingerprint, User>>(c =>
new FingerprintManager<Fingerprint, User>(new MongoStore<Fingerprint, User>(new MongoUrl(c.Resolve<IConfiguration>().GetConnectionString("MongoConnectionString"))))
).SingleInstance();
}
}
```
118 changes: 118 additions & 0 deletions laget.Fingerprint/Stores/MongoStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using laget.Fingerprint.Interfaces;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace laget.Fingerprint.Stores
{
public class MongoStore<TFingerprint, TEntity> : IStore<TFingerprint> where TFingerprint : IFingerprint
{
private readonly IMongoCollection<TFingerprint> _collection;
private readonly TimeSpan? _ttl;

public MongoStore(MongoUrl url)
: this(url, null)
{
}

public MongoStore(MongoUrl url, TimeSpan? ttl)
{
var client = new MongoClient(url);

var database = client.GetDatabase(url.DatabaseName, new MongoDatabaseSettings
{
ReadConcern = ReadConcern.Default,
ReadPreference = ReadPreference.SecondaryPreferred,
WriteConcern = WriteConcern.Acknowledged
});

_collection = database.GetCollection<TFingerprint>($"{typeof(TEntity).Name.ToLower()}.fingerprints");
_ttl = ttl;

EnsureIndexes();
}

public void Add(TFingerprint model)
{
var options = new ReplaceOptions { IsUpsert = true };
var builder = Builders<TFingerprint>.Filter;
var filter = builder.Eq(x => x.Hash, model.Hash);

_collection.ReplaceOne(filter, model, options);
}

public async Task AddAsync(TFingerprint model)
{
var options = new ReplaceOptions { IsUpsert = true };
var builder = Builders<TFingerprint>.Filter;
var filter = builder.Eq(x => x.Hash, model.Hash);

await _collection.ReplaceOneAsync(filter, model, options);
}

public TFingerprint Get(string hash)
{
var builder = Builders<TFingerprint>.Filter;
var filter = builder.Eq(x => x.Hash, hash);

return _collection.Find(filter).FirstOrDefault();
}

public async Task<TFingerprint> GetAsync(string hash)
{
var builder = Builders<TFingerprint>.Filter;
var filter = builder.Eq(x => x.Hash, hash);

return await _collection.Find(filter).FirstOrDefaultAsync();
}

public void Remove(string hash)
{
var filter = Builders<TFingerprint>.Filter.Eq(x => x.Hash, hash);

_collection.DeleteOne(filter);
}

public async Task RemoveAsync(string hash)
{
var filter = Builders<TFingerprint>.Filter.Eq(x => x.Hash, hash);

await _collection.DeleteOneAsync(filter);
}

public bool Exists(string hash)
{
var builder = Builders<TFingerprint>.Filter;
var filter = builder.Eq(x => x.Hash, hash);

return _collection.FindSync(filter).Any();
}

public async Task<bool> ExistsAsync(string hash)
{
var builder = Builders<TFingerprint>.Filter;
var filter = builder.Eq(x => x.Hash, hash);

return await _collection.Find(filter).AnyAsync();
}

public IEnumerable<TFingerprint> Items => _collection.Find(_ => true).ToList();

private void EnsureIndexes()
{
var builder = Builders<TFingerprint>.IndexKeys;
var indexes = new List<CreateIndexModel<TFingerprint>>
{
new CreateIndexModel<TFingerprint>(builder.Ascending(_ => _.Hash), new CreateIndexOptions { Background = true, Unique = true })
};

if (_ttl != null)
{
indexes.Add(new CreateIndexModel<TFingerprint>(builder.Ascending(_ => _.CreatedAt), new CreateIndexOptions { Background = true, ExpireAfter = _ttl }));
}

_collection.Indexes.CreateMany(indexes);
}
}
}
4 changes: 4 additions & 0 deletions laget.Fingerprint/laget.Fingerprint.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
<NuspecProperties>version=$(Version)</NuspecProperties>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.25.0" />
</ItemGroup>

</Project>

0 comments on commit efde838

Please sign in to comment.