Skip to content

Commit

Permalink
upgrade driver to v3.0 & release beta build
Browse files Browse the repository at this point in the history
  • Loading branch information
dj-nitehawk committed Oct 18, 2024
1 parent 6d73cbd commit 65dddf0
Show file tree
Hide file tree
Showing 100 changed files with 1,435 additions and 1,226 deletions.
1 change: 0 additions & 1 deletion Benchmark/Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0"/>
<PackageReference Include="MongoDB.Driver.GridFS" Version="2.30.0"/>
</ItemGroup>

<ItemGroup>
Expand Down
23 changes: 12 additions & 11 deletions Benchmark/Benchmarks/Relationships.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using BenchmarkDotNet.Attributes;
using MongoDB.Driver;
using MongoDB.Entities;
using System;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using MongoDB.Entities;

namespace Benchmark;

Expand Down Expand Up @@ -58,14 +59,14 @@ public Relationships()
public async Task Lookup()
{
_ = (await DB
.Fluent<Author>()
.Match(a => a.FirstName == authorName)
.Lookup<Author, Book, AuthorWithBooksDTO>(
.Fluent<Author>()
.Match(a => a.FirstName == authorName)
.Lookup<Author, Book, AuthorWithBooksDTO>(
DB.Collection<Book>(),
a => a.ID,
b => b.Author.ID,
dto => dto.BookList)
.ToListAsync())[0];
.ToListAsync())[0];
}

[Benchmark]
Expand All @@ -78,15 +79,15 @@ public async Task Clientside_Join()
FirstName = author.FirstName,
LastName = author.LastName,
ID = author.ID,
BookList = await DB.Find<Book>().ManyAsync(b => Equals(b.Author.ID, author.ID))
BookList = await DB.Find<Book>().ManyAsync(b => Equals(b.Author.ID, author.ID))
};
}

[Benchmark]
public async Task Children_Fluent()
{
var author = await DB.Find<Author>().Match(a => a.FirstName == authorName).ExecuteSingleAsync();
_ = new AuthorWithBooksDTO
_ = new AuthorWithBooksDTO
{
Birthday = author!.Birthday,
FirstName = author.FirstName,
Expand All @@ -106,7 +107,7 @@ public async Task Children_Queryable()
FirstName = author.FirstName,
LastName = author.LastName,
ID = author.ID,
BookList = await author.Books.ChildrenQueryable().ToListAsync()
BookList = await author.Books.ChildrenQueryable().ToListAsync()
};
}

Expand Down
54 changes: 29 additions & 25 deletions MongoDB.Entities/Builders/Distinct.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using MongoDB.Driver;
using System;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;

namespace MongoDB.Entities;

Expand All @@ -14,17 +14,17 @@ namespace MongoDB.Entities;
/// <typeparam name="TProperty">The type of the property of the entity you'd like to get unique values for</typeparam>
public class Distinct<T, TProperty> where T : IEntity
{
FieldDefinition<T, TProperty>? field;
FilterDefinition<T> filter = Builders<T>.Filter.Empty;
readonly DistinctOptions options = new();
readonly IClientSessionHandle? session;
readonly Dictionary<Type, (object filterDef, bool prepend)>? globalFilters;
bool ignoreGlobalFilters;
FieldDefinition<T, TProperty>? _field;
FilterDefinition<T> _filter = Builders<T>.Filter.Empty;
readonly DistinctOptions _options = new();
readonly IClientSessionHandle? _session;
readonly Dictionary<Type, (object filterDef, bool prepend)>? _globalFilters;
bool _ignoreGlobalFilters;

internal Distinct(IClientSessionHandle? session, Dictionary<Type, (object filterDef, bool prepend)>? globalFilters)
{
this.session = session;
this.globalFilters = globalFilters;
_session = session;
_globalFilters = globalFilters;
}

/// <summary>
Expand All @@ -33,7 +33,7 @@ internal Distinct(IClientSessionHandle? session, Dictionary<Type, (object filter
/// <param name="property">ex: "Address.Street"</param>
public Distinct<T, TProperty> Property(string property)
{
field = property;
_field = property;

return this;
}
Expand All @@ -44,7 +44,7 @@ public Distinct<T, TProperty> Property(string property)
/// <param name="property">x => x.Address.Street</param>
public Distinct<T, TProperty> Property(Expression<Func<T, object?>> property)
{
field = property.FullPath();
_field = property.FullPath();

return this;
}
Expand All @@ -55,7 +55,7 @@ public Distinct<T, TProperty> Property(Expression<Func<T, object?>> property)
/// <param name="filter">f => f.Eq(x => x.Prop, Value) &amp; f.Gt(x => x.Prop, Value)</param>
public Distinct<T, TProperty> Match(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> filter)
{
this.filter &= filter(Builders<T>.Filter);
_filter &= filter(Builders<T>.Filter);

return this;
}
Expand All @@ -75,7 +75,7 @@ public Distinct<T, TProperty> Match(Expression<Func<T, bool>> expression)
/// <param name="template">A Template with a find query</param>
public Distinct<T, TProperty> Match(Template template)
{
filter &= template.RenderToString();
_filter &= template.RenderToString();

return this;
}
Expand All @@ -89,7 +89,11 @@ public Distinct<T, TProperty> Match(Template template)
/// <param name="caseSensitive">Case sensitivity of the search (optional)</param>
/// <param name="diacriticSensitive">Diacritic sensitivity of the search (optional)</param>
/// <param name="language">The language for the search (optional)</param>
public Distinct<T, TProperty> Match(Search searchType, string searchTerm, bool caseSensitive = false, bool diacriticSensitive = false, string? language = null)
public Distinct<T, TProperty> Match(Search searchType,
string searchTerm,
bool caseSensitive = false,
bool diacriticSensitive = false,
string? language = null)
{
if (searchType != Search.Fuzzy)
{
Expand Down Expand Up @@ -143,7 +147,7 @@ public Distinct<T, TProperty> Match(Expression<Func<T, object?>> coordinatesProp
/// <param name="jsonString">{ Title : 'The Power Of Now' }</param>
public Distinct<T, TProperty> MatchString(string jsonString)
{
filter &= jsonString;
_filter &= jsonString;

return this;
}
Expand All @@ -154,7 +158,7 @@ public Distinct<T, TProperty> MatchString(string jsonString)
/// <param name="expression">{ $gt: ['$Property1', '$Property2'] }</param>
public Distinct<T, TProperty> MatchExpression(string expression)
{
filter &= "{$expr:" + expression + "}";
_filter &= "{$expr:" + expression + "}";

return this;
}
Expand All @@ -165,7 +169,7 @@ public Distinct<T, TProperty> MatchExpression(string expression)
/// <param name="template">A Template object</param>
public Distinct<T, TProperty> MatchExpression(Template template)
{
filter &= "{$expr:" + template.RenderToString() + "}";
_filter &= "{$expr:" + template.RenderToString() + "}";

return this;
}
Expand All @@ -176,7 +180,7 @@ public Distinct<T, TProperty> MatchExpression(Template template)
/// <param name="option">x => x.OptionName = OptionValue</param>
public Distinct<T, TProperty> Option(Action<DistinctOptions> option)
{
option(options);
option(_options);

return this;
}
Expand All @@ -186,7 +190,7 @@ public Distinct<T, TProperty> Option(Action<DistinctOptions> option)
/// </summary>
public Distinct<T, TProperty> IgnoreGlobalFilters()
{
ignoreGlobalFilters = true;
_ignoreGlobalFilters = true;

return this;
}
Expand All @@ -197,14 +201,14 @@ public Distinct<T, TProperty> IgnoreGlobalFilters()
/// <param name="cancellation">An optional cancellation token</param>
public Task<IAsyncCursor<TProperty>> ExecuteCursorAsync(CancellationToken cancellation = default)
{
if (field == null)
if (_field == null)
throw new InvalidOperationException("Please use the .Property() method to specify the field to use for obtaining unique values for!");

var mergedFilter = Logic.MergeWithGlobalFilter(ignoreGlobalFilters, globalFilters, filter);
var mergedFilter = Logic.MergeWithGlobalFilter(_ignoreGlobalFilters, _globalFilters, _filter);

return session == null
? DB.Collection<T>().DistinctAsync(field, mergedFilter, options, cancellation)
: DB.Collection<T>().DistinctAsync(session, field, mergedFilter, options, cancellation);
return _session == null
? DB.Collection<T>().DistinctAsync(_field, mergedFilter, _options, cancellation)
: DB.Collection<T>().DistinctAsync(_session, _field, mergedFilter, _options, cancellation);
}

/// <summary>
Expand Down
Loading

0 comments on commit 65dddf0

Please sign in to comment.