Skip to content

Commit

Permalink
Merge pull request litedb-org#33 from mbdavid/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
mbdavid committed May 11, 2015
2 parents 229e1fa + a55e3bc commit 7a40bd3
Show file tree
Hide file tree
Showing 25 changed files with 257 additions and 305 deletions.
52 changes: 4 additions & 48 deletions LiteDB/Database/Collections/Find.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,16 @@ public IEnumerable<T> Find(Query query, int skip = 0, int limit = int.MaxValue)

var nodes = query.Run<T>(this);

// if query run on index, lets skip/take with linq-to-object
if (query.ExecuteMode == QueryExecuteMode.IndexSeek)
{
if (skip > 0) nodes = nodes.Skip(skip);
if (skip > 0) nodes = nodes.Skip(skip);

if (limit != int.MaxValue) nodes = nodes.Take(limit);
}
if (limit != int.MaxValue) nodes = nodes.Take(limit);

foreach (var node in nodes)
{
var dataBlock = this.Database.Data.Read(node.DataBlock, true);

var doc = BsonSerializer.Deserialize(dataBlock.Buffer).AsDocument;

// if need run in full scan, execute full scan and test return
if (query.ExecuteMode == QueryExecuteMode.FullScan)
{
// execute query condition here - if false, do not add on final results
if(query.ExecuteFullScan(doc, new IndexOptions()) == false) continue;

// implement skip/limit before on full search - no linq
if (--skip >= 0) continue;

if (--limit <= -1) yield break;
}

// get object from BsonDocument
var obj = this.Database.Mapper.ToObject<T>(doc);

Expand Down Expand Up @@ -129,22 +113,7 @@ public int Count(Query query)

var nodes = query.Run<T>(this);

// if query execute with index, just returns nodes
if (query.ExecuteMode == QueryExecuteMode.IndexSeek) return nodes.Count();

var count = 0;

// execute full scan
foreach (var node in nodes)
{
var dataBlock = this.Database.Data.Read(node.DataBlock, true);

var doc = BsonSerializer.Deserialize(dataBlock.Buffer).AsDocument;

if (query.ExecuteFullScan(doc, new IndexOptions())) count++;
}

return count;
return nodes.Count();
}

/// <summary>
Expand All @@ -164,20 +133,7 @@ public bool Exists(Query query)

var nodes = query.Run<T>(this);

// if query execute with index, just returns nodes
if (query.ExecuteMode == QueryExecuteMode.IndexSeek) return nodes.FirstOrDefault() != null;

// execute full scan
foreach (var node in nodes)
{
var dataBlock = this.Database.Data.Read(node.DataBlock, true);

var doc = BsonSerializer.Deserialize(dataBlock.Buffer).AsDocument;

if (query.ExecuteFullScan(doc, new IndexOptions())) return true;
}

return false;
return nodes.FirstOrDefault() != null;
}

/// <summary>
Expand Down
9 changes: 0 additions & 9 deletions LiteDB/Query/Impl/QueryAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,5 @@ internal override IEnumerable<IndexNode> ExecuteIndex(IndexService indexer, Coll
{
return indexer.FindAll(index, _order);
}

internal override void NormalizeValues(IndexOptions options)
{
}

internal override bool ExecuteFullScan(BsonDocument doc, IndexOptions options)
{
return true;
}
}
}
13 changes: 0 additions & 13 deletions LiteDB/Query/Impl/QueryAnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,11 @@ internal override IEnumerable<IndexNode> ExecuteIndex(IndexService indexer, Coll
throw new NotSupportedException();
}

internal override void NormalizeValues(IndexOptions options)
{
throw new NotSupportedException();
}

internal override bool ExecuteFullScan(BsonDocument doc, IndexOptions options)
{
return _left.ExecuteFullScan(doc, options) && _right.ExecuteFullScan(doc, options);
}

internal override IEnumerable<IndexNode> Run<T>(LiteCollection<T> collection)
{
var left = _left.Run(collection);
var right = _right.Run(collection);

// if any query (left/right) is FullScan, this query is full scan too
this.ExecuteMode = _left.ExecuteMode == QueryExecuteMode.FullScan || _right.ExecuteMode == QueryExecuteMode.FullScan ? QueryExecuteMode.FullScan : QueryExecuteMode.IndexSeek;

return left.Intersect(right, new IndexNodeComparer());
}
}
Expand Down
13 changes: 0 additions & 13 deletions LiteDB/Query/Impl/QueryBetween.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,5 @@ internal override IEnumerable<IndexNode> ExecuteIndex(IndexService indexer, Coll
node = indexer.GetNode(node.NextPrev(0, order));
}
}

internal override void NormalizeValues(IndexOptions options)
{
_start = _start.Normalize(options);
_end = _end.Normalize(options);
}

internal override bool ExecuteFullScan(BsonDocument doc, IndexOptions options)
{
var val = doc.Get(this.Field).Normalize(options);

return val.CompareTo(_start) >= 0 && val.CompareTo(_end) <= 0;
}
}
}
18 changes: 2 additions & 16 deletions LiteDB/Query/Impl/QueryContains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,9 @@ public QueryContains(string field, BsonValue value)

internal override IEnumerable<IndexNode> ExecuteIndex(IndexService indexer, CollectionIndex index)
{
this.ExecuteMode = QueryExecuteMode.FullScan;
var v = _value.Normalize(index.Options);

return indexer.FindAll(index, Query.Ascending);
}

internal override void NormalizeValues(IndexOptions options)
{
_value = _value.Normalize(options);
}

internal override bool ExecuteFullScan(BsonDocument doc, IndexOptions options)
{
var val = doc.Get(this.Field).Normalize(options);

if(!val.IsString) return false;

return val.AsString.Contains(_value.AsString);
return indexer.FindAll(index, Query.Ascending).Where(x => x.Key.AsString.Contains(v));
}
}
}
12 changes: 0 additions & 12 deletions LiteDB/Query/Impl/QueryEquals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,5 @@ internal override IEnumerable<IndexNode> ExecuteIndex(IndexService indexer, Coll
}
}
}

internal override void NormalizeValues(IndexOptions options)
{
_value = _value.Normalize(options);
}

internal override bool ExecuteFullScan(BsonDocument doc, IndexOptions options)
{
var val = doc.Get(this.Field).Normalize(options);

return val.CompareTo(_value) == 0;
}
}
}
12 changes: 0 additions & 12 deletions LiteDB/Query/Impl/QueryGreater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,5 @@ internal override IEnumerable<IndexNode> ExecuteIndex(IndexService indexer, Coll
node = indexer.GetNode(node.Next[0]);
}
}

internal override void NormalizeValues(IndexOptions options)
{
_value = _value.Normalize(options);
}

internal override bool ExecuteFullScan(BsonDocument doc, IndexOptions options)
{
var val = doc.Get(this.Field).Normalize(options);

return val.CompareTo(_value) >= (_equals ? 0 : 1);
}
}
}
26 changes: 0 additions & 26 deletions LiteDB/Query/Impl/QueryIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,5 @@ internal override IEnumerable<IndexNode> ExecuteIndex(IndexService indexer, Coll
}
}
}

internal override void NormalizeValues(IndexOptions options)
{
var values = new List<BsonValue>();

foreach (var value in _values.Distinct())
{
values.Add(value.Normalize(options));
}

_values = values;
}

internal override bool ExecuteFullScan(BsonDocument doc, IndexOptions options)
{
var val = doc.Get(this.Field).Normalize(options);

foreach (var value in _values.Distinct())
{
var diff = val.CompareTo(value);

if (diff == 0) return true;
}

return false;
}
}
}
12 changes: 0 additions & 12 deletions LiteDB/Query/Impl/QueryLess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,5 @@ internal override IEnumerable<IndexNode> ExecuteIndex(IndexService indexer, Coll
yield return node;
}
}

internal override void NormalizeValues(IndexOptions options)
{
_value = _value.Normalize(options);
}

internal override bool ExecuteFullScan(BsonDocument doc, IndexOptions options)
{
var val = doc.Get(this.Field).Normalize(options);

return val.CompareTo(_value) <= (_equals ? 0 : -1);
}
}
}
12 changes: 0 additions & 12 deletions LiteDB/Query/Impl/QueryNot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,5 @@ internal override IEnumerable<IndexNode> ExecuteIndex(IndexService indexer, Coll

return indexer.FindAll(index, Query.Ascending).Where(x => x.Key.CompareTo(value) != 0);
}

internal override void NormalizeValues(IndexOptions options)
{
_value = _value.Normalize(options);
}

internal override bool ExecuteFullScan(BsonDocument doc, IndexOptions options)
{
var val = doc.Get(this.Field).Normalize(options);

return val.CompareTo(_value) != 0;
}
}
}
13 changes: 0 additions & 13 deletions LiteDB/Query/Impl/QueryOr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,11 @@ internal override IEnumerable<IndexNode> ExecuteIndex(IndexService indexer, Coll
throw new NotSupportedException();
}

internal override void NormalizeValues(IndexOptions options)
{
throw new NotSupportedException();
}

internal override bool ExecuteFullScan(BsonDocument doc, IndexOptions options)
{
return _left.ExecuteFullScan(doc, options) || _right.ExecuteFullScan(doc, options);
}

internal override IEnumerable<IndexNode> Run<T>(LiteCollection<T> collection)
{
var left = _left.Run(collection);
var right = _right.Run(collection);

// if any query (left/right) is FullScan, this query is full scan too
this.ExecuteMode = _left.ExecuteMode == QueryExecuteMode.FullScan || _right.ExecuteMode == QueryExecuteMode.FullScan ? QueryExecuteMode.FullScan : QueryExecuteMode.IndexSeek;

return left.Union(right, new IndexNodeComparer());
}
}
Expand Down
14 changes: 0 additions & 14 deletions LiteDB/Query/Impl/QueryStartsWith.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,5 @@ internal override IEnumerable<IndexNode> ExecuteIndex(IndexService indexer, Coll
node = indexer.GetNode(node.Next[0]);
}
}

internal override void NormalizeValues(IndexOptions options)
{
_value = _value.Normalize(options);
}

internal override bool ExecuteFullScan(BsonDocument doc, IndexOptions options)
{
var val = doc.Get(this.Field).Normalize(options);

if(!val.IsString) return false;

return val.AsString.StartsWith(_value.AsString);
}
}
}
41 changes: 8 additions & 33 deletions LiteDB/Query/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@

namespace LiteDB
{
internal enum QueryExecuteMode { IndexSeek, FullScan }

/// <summary>
/// Class helper to create query using indexes in database. All methods are statics.
/// Queries can be executed in 3 ways: Index Seek (fast), Index Scan (good), Full Scan (slow)
/// Queries can be executed in 2 ways: Index Seek (fast), Index Scan (good)
/// </summary>
public abstract class Query
{
public string Field { get; private set; }

/// <summary>
/// Indicate that query need to run under full scan (there is not index)
/// </summary>
internal QueryExecuteMode ExecuteMode { get; set; }

internal Query(string field)
{
this.Field = field;
Expand Down Expand Up @@ -175,17 +168,6 @@ public static Query Or(Query left, Query right)
/// </summary>
internal abstract IEnumerable<IndexNode> ExecuteIndex(IndexService indexer, CollectionIndex index);

/// <summary>
/// Abstract method to normalize values before run full scan
/// </summary>
internal abstract void NormalizeValues(IndexOptions options);

/// <summary>
/// Abstract method that must implement full scan - will be called for each document and need
/// returns true if condition was satisfied
/// </summary>
internal abstract bool ExecuteFullScan(BsonDocument doc, IndexOptions options);

/// <summary>
/// Find witch index will be used and run Execute method - define ExecuteMode here
/// </summary>
Expand All @@ -206,32 +188,25 @@ internal virtual IEnumerable<IndexNode> Run<T>(LiteCollection<T> collection)
{
var options = collection.Database.Mapper.GetIndexFromAttribute<T>(this.Field);

// create a new index
// create a new index using BsonIndex options
if (options != null)
{
collection.EnsureIndex(this.Field, options);

index = col.GetIndex(this.Field);
}
}


// if no index, let's auto create an index with default index options
if (index == null)
{
this.ExecuteMode = QueryExecuteMode.FullScan;
collection.EnsureIndex(this.Field);

// normalize query values before run full scan
this.NormalizeValues(new IndexOptions());

// if there is no index, returns all index nodes - will be used Full Scan
return collection.Database.Indexer.FindAll(col.PK, Query.Ascending);
index = col.GetIndex(this.Field);
}
else
{
this.ExecuteMode = QueryExecuteMode.IndexSeek;

// execute query to get all IndexNodes
return this.ExecuteIndex(collection.Database.Indexer, index);
}
// execute query to get all IndexNodes
return this.ExecuteIndex(collection.Database.Indexer, index);
}

#endregion
Expand Down
Loading

0 comments on commit 7a40bd3

Please sign in to comment.