Skip to content

Commit

Permalink
Merge pull request #1590 from mbdavid/new_expr_params
Browse files Browse the repository at this point in the history
Rework in the parameters system of a BsonExpression
  • Loading branch information
lbnascimento authored Mar 25, 2020
2 parents 4fddc91 + 0f1638c commit 78c5a04
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 175 deletions.
4 changes: 1 addition & 3 deletions LiteDB.Tests/Database/Document_Size_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public void Very_Large_Single_Document_Support_With_Partial_Load_Memory_Usage()
{
var col = db.GetCollection("col");


// insert 10 mb document

col.Insert(new BsonDocument
Expand Down Expand Up @@ -48,9 +47,8 @@ public void Very_Large_Single_Document_Support_With_Partial_Load_Memory_Usage()
var memoryFullDocument = Process.GetCurrentProcess().WorkingSet64;

// memory after full document must be at least 10Mb more than with name only
// using 50% of array size because there are no precise value when using memory usage

memoryFullDocument.Should().BeGreaterOrEqualTo(memoryForNameOnly + (ARRAY_SIZE / 2));
//memoryFullDocument.Should().BeGreaterOrEqualTo(memoryForNameOnly + (ARRAY_SIZE / 2));
}
}
}
Expand Down
25 changes: 14 additions & 11 deletions LiteDB.Tests/Issue1585.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@

namespace LiteDB.Issue1585
{
public class Issue1585_Tests
public class PlayerDto
{
public class PlayerDto
{
[BsonId]
public Guid Id { get; }
public string Name { get; }
[BsonId]
public Guid Id { get; }
public string Name { get; }

public PlayerDto(Guid id, string name)
{
Id = id;
Name = name;
}
public PlayerDto(Guid id, string name)
{
Id = id;
Name = name;
}
}

public class Issue1585a_Tests
{
[Fact]
public void Dto_Read()
{
Expand Down Expand Up @@ -60,7 +60,10 @@ public void Dto_Read2()
Assert.NotNull(player);
}
}
}

public class Issue1585b_Tests
{
[Fact]
public void Dto_Read3()
{
Expand Down
2 changes: 1 addition & 1 deletion LiteDB/Client/SqlParser/Commands/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private BsonDataReader ParseCreate()
_tokenizer.ReadToken().Expect(TokenType.OpenParenthesis);

// read index expression
var expr = BsonExpression.Create(_tokenizer, null, BsonExpressionParserMode.Full);
var expr = BsonExpression.Create(_tokenizer, BsonExpressionParserMode.Full, new BsonDocument());

// read )
_tokenizer.ReadToken().Expect(TokenType.CloseParenthesis);
Expand Down
2 changes: 1 addition & 1 deletion LiteDB/Client/SqlParser/Commands/Delete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private BsonDataReader ParseDelete()
// read WHERE
_tokenizer.ReadToken();

where = BsonExpression.Create(_tokenizer, _parameters, BsonExpressionParserMode.Full);
where = BsonExpression.Create(_tokenizer, BsonExpressionParserMode.Full, _parameters);
}

_tokenizer.ReadToken().Expect(TokenType.EOF, TokenType.SemiColon);
Expand Down
2 changes: 1 addition & 1 deletion LiteDB/Client/SqlParser/Commands/ParseLists.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private IEnumerable<BsonExpression> ParseListOfExpressions()
{
while(true)
{
var expr = BsonExpression.Create(_tokenizer, _parameters, BsonExpressionParserMode.Full);
var expr = BsonExpression.Create(_tokenizer, BsonExpressionParserMode.Full, _parameters);

yield return expr;

Expand Down
10 changes: 5 additions & 5 deletions LiteDB/Client/SqlParser/Commands/Select.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private IBsonDataReader ParseSelect()
token.Expect("SELECT");

// read required SELECT <expr> and convert into single expression
query.Select = BsonExpression.Create(_tokenizer, _parameters, BsonExpressionParserMode.SelectDocument);
query.Select = BsonExpression.Create(_tokenizer, BsonExpressionParserMode.SelectDocument, _parameters);

// read FROM|INTO
var from = _tokenizer.ReadToken();
Expand Down Expand Up @@ -87,7 +87,7 @@ private IBsonDataReader ParseSelect()
// read WHERE keyword
_tokenizer.ReadToken();

var where = BsonExpression.Create(_tokenizer, _parameters, BsonExpressionParserMode.Full);
var where = BsonExpression.Create(_tokenizer, BsonExpressionParserMode.Full, _parameters);

query.Where.Add(where);
}
Expand All @@ -100,7 +100,7 @@ private IBsonDataReader ParseSelect()
_tokenizer.ReadToken();
_tokenizer.ReadToken().Expect("BY");

var groupBy = BsonExpression.Create(_tokenizer, _parameters, BsonExpressionParserMode.Full);
var groupBy = BsonExpression.Create(_tokenizer, BsonExpressionParserMode.Full, _parameters);

query.GroupBy = groupBy;

Expand All @@ -111,7 +111,7 @@ private IBsonDataReader ParseSelect()
// read HAVING keyword
_tokenizer.ReadToken();

var having = BsonExpression.Create(_tokenizer, _parameters, BsonExpressionParserMode.Full);
var having = BsonExpression.Create(_tokenizer, BsonExpressionParserMode.Full, _parameters);

query.Having = having;
}
Expand All @@ -125,7 +125,7 @@ private IBsonDataReader ParseSelect()
_tokenizer.ReadToken();
_tokenizer.ReadToken().Expect("BY");

var orderBy = BsonExpression.Create(_tokenizer, _parameters, BsonExpressionParserMode.Full);
var orderBy = BsonExpression.Create(_tokenizer, BsonExpressionParserMode.Full, _parameters);

var orderByOrder = Query.Ascending;
var orderByToken = _tokenizer.LookAhead();
Expand Down
4 changes: 2 additions & 2 deletions LiteDB/Client/SqlParser/Commands/Update.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private BsonDataReader ParseUpdate()
var collection = _tokenizer.ReadToken().Expect(TokenType.Word).Value;
_tokenizer.ReadToken().Expect("SET");

var transform = BsonExpression.Create(_tokenizer, _parameters, BsonExpressionParserMode.UpdateDocument);
var transform = BsonExpression.Create(_tokenizer, BsonExpressionParserMode.UpdateDocument, _parameters);

// optional where
BsonExpression where = null;
Expand All @@ -33,7 +33,7 @@ private BsonDataReader ParseUpdate()
// read WHERE
_tokenizer.ReadToken();

where = BsonExpression.Create(_tokenizer, _parameters, BsonExpressionParserMode.Full);
where = BsonExpression.Create(_tokenizer, BsonExpressionParserMode.Full, _parameters);
}

// read eof
Expand Down
115 changes: 46 additions & 69 deletions LiteDB/Document/Expression/BsonExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public sealed class BsonExpression
/// <summary>
/// Get/Set parameter values that will be used on expression execution
/// </summary>
public BsonDocument Parameters { get; private set; } = new BsonDocument();
public BsonDocument Parameters { get; internal set; }

/// <summary>
/// In predicate expressions, indicate Left side
Expand Down Expand Up @@ -117,7 +117,7 @@ public sealed class BsonExpression
/// <summary>
/// Compiled Expression into a function to be executed: func(source[], root, current, parameters)[]
/// </summary>
private BsonExpressionEnumerableDelegate _func;
private BsonExpressionEnumerableDelegate _funcEnumerable;

/// <summary>
/// Compiled Expression into a scalar function to be executed: func(source[], root, current, parameters)1
Expand Down Expand Up @@ -205,7 +205,7 @@ internal IEnumerable<BsonValue> Execute(IEnumerable<BsonDocument> source, BsonDo
}
else
{
var values = _func(source, root, current, collation ?? Collation.Binary, this.Parameters);
var values = _funcEnumerable(source, root, current, collation ?? Collation.Binary, this.Parameters);

foreach (var value in values)
{
Expand Down Expand Up @@ -279,7 +279,8 @@ internal BsonValue ExecuteScalar(IEnumerable<BsonDocument> source, BsonDocument

#region Static method

private static ConcurrentDictionary<string, BsonExpression> _cache = new ConcurrentDictionary<string, BsonExpression>();
private static ConcurrentDictionary<string, BsonExpressionEnumerableDelegate> _cacheEnumerable = new ConcurrentDictionary<string, BsonExpressionEnumerableDelegate>();
private static ConcurrentDictionary<string, BsonExpressionScalarDelegate> _cacheScalar = new ConcurrentDictionary<string, BsonExpressionScalarDelegate>();

/// <summary>
/// Parse string and create new instance of BsonExpression - can be cached
Expand Down Expand Up @@ -311,113 +312,89 @@ public static BsonExpression Create(string expression, BsonDocument parameters)
{
if (string.IsNullOrWhiteSpace(expression)) throw new ArgumentNullException(nameof(expression));

if (!_cache.TryGetValue(expression, out var expr))
{
var tokenizer = new Tokenizer(expression);

expr = Parse(tokenizer, BsonExpressionParserMode.Full, true);
var tokenizer = new Tokenizer(expression);

tokenizer.LookAhead().Expect(TokenType.EOF);
var expr = Create(tokenizer, BsonExpressionParserMode.Full, parameters);

// if passed string expression are different from formatted expression, try add in cache "unformatted" expression too
if (expression != expr.Source)
{
_cache.TryAdd(expression, expr);
}
}
tokenizer.LookAhead().Expect(TokenType.EOF);

// return a copy from cache WITHOUT parameters
return new BsonExpression
{
Expression = expr.Expression,
IsImmutable = expr.IsImmutable,
UseSource = expr.UseSource,
IsScalar = expr.IsScalar,
Parameters = parameters ?? new BsonDocument(),
Fields = expr.Fields,
Left = expr.Left,
Right = expr.Right,
Source = expr.Source,
Type = expr.Type,
_func = expr._func,
_funcScalar = expr._funcScalar
};
return expr;
}

/// <summary>
/// Parse tokenizer and create new instance of BsonExpression - for now, do not use cache
/// </summary>
internal static BsonExpression Create(Tokenizer tokenizer, BsonDocument parameters, BsonExpressionParserMode mode)
internal static BsonExpression Create(Tokenizer tokenizer, BsonExpressionParserMode mode, BsonDocument parameters)
{
if (tokenizer == null) throw new ArgumentNullException(nameof(tokenizer));

var expr = Parse(tokenizer, mode, true);

// return a copy from cache using new Parameters
return new BsonExpression
{
Expression = expr.Expression,
IsImmutable = expr.IsImmutable,
UseSource = expr.UseSource,
IsScalar = expr.IsScalar,
Parameters = parameters ?? new BsonDocument(),
Fields = expr.Fields,
Left = expr.Left,
Right = expr.Right,
Source = expr.Source,
Type = expr.Type,
_func = expr._func,
_funcScalar = expr._funcScalar
};
return ParseAndCompile(tokenizer, mode, parameters, true);
}

/// <summary>
/// Parse and compile string expression and return BsonExpression
/// </summary>
internal static BsonExpression Parse(Tokenizer tokenizer, BsonExpressionParserMode mode, bool isRoot)
internal static BsonExpression ParseAndCompile(Tokenizer tokenizer, BsonExpressionParserMode mode, BsonDocument parameters, bool isRoot)
{
if (tokenizer == null) throw new ArgumentNullException(nameof(tokenizer));

var context = new ExpressionContext();

var expr =
mode == BsonExpressionParserMode.Full ? BsonExpressionParser.ParseFullExpression(tokenizer, context, isRoot) :
mode == BsonExpressionParserMode.Single ? BsonExpressionParser.ParseSingleExpression(tokenizer, context, isRoot) :
mode == BsonExpressionParserMode.SelectDocument ? BsonExpressionParser.ParseSelectDocumentBuilder(tokenizer, context) :
BsonExpressionParser.ParseUpdateDocumentBuilder(tokenizer, context);
mode == BsonExpressionParserMode.Full ? BsonExpressionParser.ParseFullExpression(tokenizer, context, parameters, isRoot) :
mode == BsonExpressionParserMode.Single ? BsonExpressionParser.ParseSingleExpression(tokenizer, context, parameters, isRoot) :
mode == BsonExpressionParserMode.SelectDocument ? BsonExpressionParser.ParseSelectDocumentBuilder(tokenizer, context, parameters) :
BsonExpressionParser.ParseUpdateDocumentBuilder(tokenizer, context, parameters);

// before compile try find in cache if this source already has in cache (already compiled)
var cached = _cache.GetOrAdd(expr.Source, (s) =>
{
// compile linq expression (with left+right expressions)
Compile(expr, context);
return expr;
});
// compile linq expression (with left+right expressions)
Compile(expr, context);

return cached;
return expr;
}

internal static void Compile(BsonExpression expr, ExpressionContext context)
{
// compile linq expression according with return type (scalar or not)
// compile linq expression according with return type (scalar or enumerable)
// in both case, try use cached compiled version
if (expr.IsScalar)
{
var lambda = System.Linq.Expressions.Expression.Lambda<BsonExpressionScalarDelegate>(expr.Expression, context.Source, context.Root, context.Current, context.Collation, context.Parameters);
var cached = _cacheScalar.GetOrAdd(expr.Source, s =>
{
var lambda = System.Linq.Expressions.Expression.Lambda<BsonExpressionScalarDelegate>(expr.Expression, context.Source, context.Root, context.Current, context.Collation, context.Parameters);
expr._funcScalar = lambda.Compile();
return lambda.Compile();
});

expr._funcScalar = cached;
}
else
{
var lambda = System.Linq.Expressions.Expression.Lambda<BsonExpressionEnumerableDelegate>(expr.Expression, context.Source, context.Root, context.Current, context.Collation, context.Parameters);
var cached = _cacheEnumerable.GetOrAdd(expr.Source, s =>
{
var lambda = System.Linq.Expressions.Expression.Lambda<BsonExpressionEnumerableDelegate>(expr.Expression, context.Source, context.Root, context.Current, context.Collation, context.Parameters);
return lambda.Compile();
});

expr._func = lambda.Compile();
expr._funcEnumerable = cached;
}

// compile child expressions (left/right)
if (expr.Left != null) Compile(expr.Left, context);
if (expr.Right != null) Compile(expr.Right, context);
}

/// <summary>
/// Set same parameter referente to all expression child (left, right)
/// </summary>
internal static void SetParameters(BsonExpression expr, BsonDocument parameters)
{
expr.Parameters = parameters;

if (expr.Left != null) SetParameters(expr.Left, parameters);
if (expr.Right != null) SetParameters(expr.Right, parameters);
}

/// <summary>
/// Get root document $ expression
/// </summary>
Expand Down
9 changes: 0 additions & 9 deletions LiteDB/Document/Expression/Parser/BsonExpressionFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ internal class BsonExpressionFunctions
{
public static IEnumerable<BsonValue> MAP(BsonDocument root, Collation collation, BsonDocument parameters, IEnumerable<BsonValue> input, BsonExpression mapExpr)
{
// update parameters in expression
parameters.CopyTo(mapExpr.Parameters);

foreach (var item in input)
{
// execute for each child value and except a first bool value (returns if true)
Expand All @@ -29,9 +26,6 @@ public static IEnumerable<BsonValue> MAP(BsonDocument root, Collation collation,

public static IEnumerable<BsonValue> FILTER(BsonDocument root, Collation collation, BsonDocument parameters, IEnumerable<BsonValue> input, BsonExpression filterExpr)
{
// update parameters in expression
parameters.CopyTo(filterExpr.Parameters);

foreach (var item in input)
{
// execute for each child value and except a first bool value (returns if true)
Expand All @@ -46,9 +40,6 @@ public static IEnumerable<BsonValue> FILTER(BsonDocument root, Collation collati

public static IEnumerable<BsonValue> SORT(BsonDocument root, Collation collation, BsonDocument parameters, IEnumerable<BsonValue> input, BsonExpression sortExpr, BsonValue order)
{
// update parameters in expression
parameters.CopyTo(sortExpr.Parameters);

IEnumerable<Tuple<BsonValue, BsonValue>> source()
{
foreach (var item in input)
Expand Down
6 changes: 0 additions & 6 deletions LiteDB/Document/Expression/Parser/BsonExpressionOperators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,6 @@ public static BsonValue ARRAY_INDEX(BsonValue value, int index, BsonExpression e
// for expr.Type = parameter, just get value as index (fixed position)
if (expr.Type == BsonExpressionType.Parameter)
{
// update parameters in expression
parameters.CopyTo(expr.Parameters);

// get fixed position based on parameter value (must return int value)
var indexValue = expr.ExecuteScalar(root, collation);

Expand Down Expand Up @@ -320,9 +317,6 @@ public static IEnumerable<BsonValue> ARRAY_FILTER(BsonValue value, int index, Bs
// [<expr>] - index are an expression
else
{
// update parameters in expression
parameters.CopyTo(filterExpr.Parameters);

foreach (var item in arr)
{
// execute for each child value and except a first bool value (returns if true)
Expand Down
Loading

0 comments on commit 78c5a04

Please sign in to comment.