Skip to content

Commit

Permalink
Fixing OrderBy before Select
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdavid committed Sep 29, 2018
1 parent dd5dc26 commit deabb0b
Show file tree
Hide file tree
Showing 21 changed files with 166 additions and 205 deletions.
4 changes: 2 additions & 2 deletions LiteDB.Tests/Engine/Index_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public void Index_Order()
col.EnsureIndex("text");

var asc = string.Join("", col.Query()
.Select("text")
.OrderBy("text")
.Select("text")
.ToDocuments()
.Select(x => x["text"].AsString));

var desc = string.Join("", col.Query()
.Select("text")
.OrderByDescending("text")
.Select("text")
.ToDocuments()
.Select(x => x["text"].AsString));

Expand Down
4 changes: 2 additions & 2 deletions LiteDB/Client/Database/Collections/Aggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public BsonValue Min(BsonExpression keySelector)
if (string.IsNullOrEmpty(keySelector)) throw new ArgumentNullException(nameof(keySelector));

var doc = this.Query()
.Select(keySelector)
.OrderBy(keySelector)
.Select(keySelector)
.ToDocuments()
.First();

Expand Down Expand Up @@ -156,8 +156,8 @@ public BsonValue Max(BsonExpression keySelector)
if (string.IsNullOrEmpty(keySelector)) throw new ArgumentNullException(nameof(keySelector));

var doc = this.Query()
.Select(keySelector)
.OrderByDescending(keySelector)
.Select(keySelector)
.ToDocuments()
.First();

Expand Down
44 changes: 21 additions & 23 deletions LiteDB/Client/Database/ILiteQueryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,31 @@ public interface ILiteQueryableWithIncludes<T>
}

public interface ILiteQueryable<T> :
ILiteQueryableSelect<T>,
ILiteQueryableSelected<T>,
ILiteQueryableOrdered<T>,
ILiteQueryableResult<T>
ILiteQueryableGrouped<T>
{
ILiteQueryable<T> Where(BsonExpression predicate);
ILiteQueryable<T> Where(string predicate, BsonDocument parameters);
ILiteQueryable<T> Where(string predicate, params BsonValue[] args);
ILiteQueryable<T> Where(Expression<Func<T, bool>> predicate);

ILiteQueryableSelect<T> GroupBy(BsonExpression keySelector);
ILiteQueryableSelect<T> GroupBy<K>(Expression<Func<T, K>> keySelector);
ILiteQueryableGrouped<T> GroupBy(BsonExpression keySelector);
ILiteQueryableGrouped<T> GroupBy<K>(Expression<Func<T, K>> keySelector);

ILiteQueryableOrdered<T> OrderBy(BsonExpression keySelector, int order = 1);
ILiteQueryableOrdered<T> OrderBy<K>(Expression<Func<T, K>> keySelector, int order = 1);
ILiteQueryableOrdered<T> OrderByDescending(BsonExpression keySelector);
ILiteQueryableOrdered<T> OrderByDescending<K>(Expression<Func<T, K>> keySelector);
}

public interface ILiteQueryableGrouped<T> :
ILiteQueryableOrdered<T>
{
ILiteQueryableOrdered<T> Having(BsonExpression predicate);
ILiteQueryableOrdered<T> Having(Expression<Func<T, bool>> predicate);
}

public interface ILiteQueryableSelect<T> :
public interface ILiteQueryableOrdered<T> :
ILiteQueryableSelected<T>
{
ILiteQueryableSelected<BsonDocument> Select(BsonExpression selector);
Expand All @@ -36,25 +46,13 @@ public interface ILiteQueryableSelect<T> :
ILiteQueryableSelected<K> SelectAll<K>(Expression<Func<T, K>> selector);
}

public interface ILiteQueryableSelected<T> :
ILiteQueryableOrdered<T>
{
ILiteQueryableSelected<T> Having(BsonExpression predicate);
ILiteQueryableSelected<T> Having(Expression<Func<T, bool>> predicate);

ILiteQueryableOrdered<T> OrderBy(BsonExpression keySelector, int order = 1);
ILiteQueryableOrdered<T> OrderBy<K>(Expression<Func<T, K>> keySelector, int order = 1);
ILiteQueryableOrdered<T> OrderByDescending(BsonExpression keySelector);
ILiteQueryableOrdered<T> OrderByDescending<K>(Expression<Func<T, K>> keySelector);
}

public interface ILiteQueryableOrdered<T> :
public interface ILiteQueryableSelected<T> :
ILiteQueryableResult<T>
{
ILiteQueryableOrdered<T> Limit(int limit);
ILiteQueryableOrdered<T> Skip(int offset);
ILiteQueryableOrdered<T> Offset(int offset);
ILiteQueryableOrdered<T> ForUpdate();
ILiteQueryableSelected<T> Limit(int limit);
ILiteQueryableSelected<T> Skip(int offset);
ILiteQueryableSelected<T> Offset(int offset);
ILiteQueryableSelected<T> ForUpdate();
}

public interface ILiteQueryableResult<T>
Expand Down
112 changes: 56 additions & 56 deletions LiteDB/Client/Database/LiteQueryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public ILiteQueryable<T> Where(Expression<Func<T, bool>> predicate)
/// <summary>
/// Groups the documents of resultset according to a specified key selector expression (support only one GroupBy)
/// </summary>
public ILiteQueryableSelect<T> GroupBy(BsonExpression keySelector)
public ILiteQueryableGrouped<T> GroupBy(BsonExpression keySelector)
{
_query.GroupBy = keySelector;
return this;
Expand All @@ -114,68 +114,20 @@ public ILiteQueryableSelect<T> GroupBy(BsonExpression keySelector)
/// <summary>
/// Groups the documents of resultset according to a specified key selector expression (support only one GroupBy)
/// </summary>
public ILiteQueryableSelect<T> GroupBy<K>(Expression<Func<T, K>> keySelector)
public ILiteQueryableGrouped<T> GroupBy<K>(Expression<Func<T, K>> keySelector)
{
_query.GroupBy = _mapper.GetExpression(keySelector);
return this;
}

#endregion

#region Select

/// <summary>
/// Project each document of resultset into a new document/value based on selector expression
/// </summary>
public ILiteQueryableSelected<BsonDocument> Select(BsonExpression selector)
{
_query.Select = selector;

return new LiteQueryable<BsonDocument>(_engine, _mapper, _collection, _query);
}

/// <summary>
/// Project each document of resultset into a new document/value based on selector expression
/// </summary>
public ILiteQueryableSelected<K> Select<K>(Expression<Func<T, K>> selector)
{
_query.Select = _mapper.GetExpression(selector);

return new LiteQueryable<K>(_engine, _mapper, _collection, _query);
}

/// <summary>
/// Project each document of resultset into a new document/value based on selector expression
/// Apply expression function over all results and will output a single result
/// </summary>
public ILiteQueryableSelected<BsonDocument> SelectAll(BsonExpression selector)
{
_query.Select = selector;
_query.SelectAll = true;

return new LiteQueryable<BsonDocument>(_engine, _mapper, _collection, _query);
}

/// <summary>
/// Project each document of resultset into a new document/value based on selector expression
/// Apply expression function over all results and will output a single result
/// </summary>
public ILiteQueryableSelected<K> SelectAll<K>(Expression<Func<T, K>> selector)
{
_query.Select = _mapper.GetExpression(selector);
_query.SelectAll = true;

return new LiteQueryable<K>(_engine, _mapper, _collection, _query);
}

#endregion

#region Having

/// <summary>
/// Filter documents after group by pipe according to predicate expression (requires GroupBy and support only one Having)
/// </summary>
public ILiteQueryableSelected<T> Having(BsonExpression predicate)
public ILiteQueryableOrdered<T> Having(BsonExpression predicate)
{
_query.Having = predicate;
return this;
Expand All @@ -184,7 +136,7 @@ public ILiteQueryableSelected<T> Having(BsonExpression predicate)
/// <summary>
/// Filter documents after group by pipe according to predicate expression (requires GroupBy and support only one Having)
/// </summary>
public ILiteQueryableSelected<T> Having(Expression<Func<T, bool>> predicate)
public ILiteQueryableOrdered<T> Having(Expression<Func<T, bool>> predicate)
{
_query.Having = _mapper.GetExpression(predicate);
return this;
Expand Down Expand Up @@ -224,12 +176,60 @@ public ILiteQueryableOrdered<T> OrderBy<K>(Expression<Func<T, K>> keySelector, i

#endregion

#region Select

/// <summary>
/// Project each document of resultset into a new document/value based on selector expression
/// </summary>
public ILiteQueryableSelected<BsonDocument> Select(BsonExpression selector)
{
_query.Select = selector;

return new LiteQueryable<BsonDocument>(_engine, _mapper, _collection, _query);
}

/// <summary>
/// Project each document of resultset into a new document/value based on selector expression
/// </summary>
public ILiteQueryableSelected<K> Select<K>(Expression<Func<T, K>> selector)
{
_query.Select = _mapper.GetExpression(selector);

return new LiteQueryable<K>(_engine, _mapper, _collection, _query);
}

/// <summary>
/// Project each document of resultset into a new document/value based on selector expression
/// Apply expression function over all results and will output a single result
/// </summary>
public ILiteQueryableSelected<BsonDocument> SelectAll(BsonExpression selector)
{
_query.Select = selector;
_query.SelectAll = true;

return new LiteQueryable<BsonDocument>(_engine, _mapper, _collection, _query);
}

/// <summary>
/// Project each document of resultset into a new document/value based on selector expression
/// Apply expression function over all results and will output a single result
/// </summary>
public ILiteQueryableSelected<K> SelectAll<K>(Expression<Func<T, K>> selector)
{
_query.Select = _mapper.GetExpression(selector);
_query.SelectAll = true;

return new LiteQueryable<K>(_engine, _mapper, _collection, _query);
}

#endregion

#region Offset/Limit/ForUpdate

/// <summary>
/// Execute query locking collection in write mode. This is avoid any other thread change results after read document and before transaction ends
/// </summary>
public ILiteQueryableOrdered<T> ForUpdate()
public ILiteQueryableSelected<T> ForUpdate()
{
_query.ForUpdate = true;
return this;
Expand All @@ -238,7 +238,7 @@ public ILiteQueryableOrdered<T> ForUpdate()
/// <summary>
/// Bypasses a specified number of documents in resultset and retun the remaining documents (same as Skip)
/// </summary>
public ILiteQueryableOrdered<T> Offset(int offset)
public ILiteQueryableSelected<T> Offset(int offset)
{
_query.Offset = offset;
return this;
Expand All @@ -247,12 +247,12 @@ public ILiteQueryableOrdered<T> Offset(int offset)
/// <summary>
/// Bypasses a specified number of documents in resultset and retun the remaining documents (same as Offset)
/// </summary>
public ILiteQueryableOrdered<T> Skip(int offset) => this.Offset(offset);
public ILiteQueryableSelected<T> Skip(int offset) => this.Offset(offset);

/// <summary>
/// Return a specified number of contiguous documents from start of resultset
/// </summary>
public ILiteQueryableOrdered<T> Limit(int limit)
public ILiteQueryableSelected<T> Limit(int limit)
{
_query.Limit = limit;
return this;
Expand Down
10 changes: 0 additions & 10 deletions LiteDB/Client/Structures/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@ public class Query
/// </summary>
public const int Descending = -1;

/// <summary>
/// Returns all documents that value are equals to value (=)
/// </summary>
public static Index All(string name, int order = Query.Ascending)
{
if (name.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(name));

return Index.All(order);
}

/// <summary>
/// Returns all documents that value are equals to value (=)
/// </summary>
Expand Down
1 change: 0 additions & 1 deletion LiteDB/Engine/LiteEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ public LiteEngine(EngineSettings settings)
private void Shutdown()
{
// here all private instances are loaded

if (_shutdown) return;

// start shutdown operation
Expand Down
8 changes: 4 additions & 4 deletions LiteDB/Engine/Query/IndexQuery/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace LiteDB.Engine
/// <summary>
/// Class that implement higher level of index search operations (equals, greater, less, ...)
/// </summary>
public abstract class Index
internal abstract class Index
{
/// <summary>
/// Index name
Expand Down Expand Up @@ -174,17 +174,17 @@ public static Index In(string name, BsonArray values, int order = Query.Ascendin
/// <summary>
/// Calculate cost based on type/value/collection - From 1 (best) to Collection.KeyCount (worst)
/// </summary>
internal abstract uint GetCost(CollectionIndex index);
public abstract uint GetCost(CollectionIndex index);

/// <summary>
/// Abstract method that must be implement for index seek/scan - Returns IndexNodes that match with index
/// </summary>
internal abstract IEnumerable<IndexNode> Execute(IndexService indexer, CollectionIndex index);
public abstract IEnumerable<IndexNode> Execute(IndexService indexer, CollectionIndex index);

/// <summary>
/// Find witch index will be used and run Execute method
/// </summary>
internal virtual IEnumerable<IndexNode> Run(CollectionPage col, IndexService indexer)
public virtual IEnumerable<IndexNode> Run(CollectionPage col, IndexService indexer)
{
// get index for this query
var index = col.GetIndex(this.Name);
Expand Down
4 changes: 2 additions & 2 deletions LiteDB/Engine/Query/IndexQuery/IndexAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public IndexAll(string name, int order)
{
}

internal override uint GetCost(CollectionIndex index)
public override uint GetCost(CollectionIndex index)
{
// no analyzed index
if (index.KeyCount == 0) return uint.MaxValue;
Expand All @@ -23,7 +23,7 @@ internal override uint GetCost(CollectionIndex index)
return index.KeyCount;
}

internal override IEnumerable<IndexNode> Execute(IndexService indexer, CollectionIndex index)
public override IEnumerable<IndexNode> Execute(IndexService indexer, CollectionIndex index)
{
return indexer.FindAll(index, this.Order);
}
Expand Down
4 changes: 2 additions & 2 deletions LiteDB/Engine/Query/IndexQuery/IndexEquals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public IndexEquals(string name, BsonValue value)
_value = value;
}

internal override uint GetCost(CollectionIndex index)
public override uint GetCost(CollectionIndex index)
{
if (index.Unique)
{
Expand All @@ -37,7 +37,7 @@ internal override uint GetCost(CollectionIndex index)
}
}

internal override IEnumerable<IndexNode> Execute(IndexService indexer, CollectionIndex index)
public override IEnumerable<IndexNode> Execute(IndexService indexer, CollectionIndex index)
{
var node = indexer.Find(index, _value, false, Query.Ascending);

Expand Down
4 changes: 2 additions & 2 deletions LiteDB/Engine/Query/IndexQuery/IndexIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public IndexIn(string name, BsonArray values, int order)
_values = values;
}

internal override uint GetCost(CollectionIndex index)
public override uint GetCost(CollectionIndex index)
{
var count = (uint)_values.Count;

Expand All @@ -40,7 +40,7 @@ internal override uint GetCost(CollectionIndex index)
}
}

internal override IEnumerable<IndexNode> Execute(IndexService indexer, CollectionIndex index)
public override IEnumerable<IndexNode> Execute(IndexService indexer, CollectionIndex index)
{
foreach (var value in _values.Distinct())
{
Expand Down
Loading

0 comments on commit deabb0b

Please sign in to comment.