Skip to content

Commit

Permalink
Min and Max aggregators didn't need field names anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
rejemy committed Feb 22, 2019
1 parent dc6e404 commit c3b20ae
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 42 deletions.
27 changes: 4 additions & 23 deletions LiteDB/Database/Collections/Aggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,41 +74,22 @@ public bool Exists(Query query)
/// <summary>
/// Returns the first/min value from a index field
/// </summary>
public BsonValue Min(string field)
{
if (string.IsNullOrEmpty(field)) throw new ArgumentNullException(nameof(field));

return _engine.Value.Min(_name, field);
}

/// <summary>
/// Returns the first/min _id field
/// </summary>
public BsonValue Min()
{
return this.Min("_id");
return _engine.Value.Min(_name);
}



/// <summary>
/// Returns the last/max value from a index field
/// </summary>
public BsonValue Max(string field)
{
if (string.IsNullOrEmpty(field)) throw new ArgumentNullException(nameof(field));

return _engine.Value.Max(_name, field);
}

/// <summary>
/// Returns the last/max _id field
/// </summary>
public BsonValue Max()
{
return this.Max("_id");
return _engine.Value.Max(_name);
}



#endregion
}
}
11 changes: 4 additions & 7 deletions LiteDB/Engine/Engine/Aggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ public partial class LiteEngine
/// <summary>
/// Returns first value from an index (first is min value)
/// </summary>
public BsonValue Min(string collection, string field)
public BsonValue Min(string collection)
{
if (collection.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(collection));
if (field.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(field));


var col = this.GetCollectionPage(collection, false);

if (col == null) return BsonValue.MinValue;

// get index (no index, no min)
var index = col.GetIndex(field);
var index = col.PK;

if (index == null) return BsonValue.MinValue;

Expand All @@ -35,18 +34,16 @@ public BsonValue Min(string collection, string field)
/// <summary>
/// Returns last value from an index (last is max value)
/// </summary>
public BsonValue Max(string collection, string field)
public BsonValue Max(string collection)
{
if (collection.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(collection));
if (field.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(field));


var col = this.GetCollectionPage(collection, false);

if (col == null) return BsonValue.MaxValue;

// get index (no index, no max)
var index = col.GetIndex(field);
var index = col.PK;

if (index == null) return BsonValue.MaxValue;

Expand Down
2 changes: 1 addition & 1 deletion LiteDB/Engine/Engine/Insert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private void InsertDocument(CollectionPage col, BsonDocument doc, BsonType autoI
// ** this code can be removed when datafile change from 7 (HeaderPage.FILE_VERSION) **
if (col.Sequence == 0 && col.DocumentCount > 0)
{
var max = this.Max(col.CollectionName, "_id");
var max = this.Max(col.CollectionName);

// if max value is a number, convert to Sequence last value
// if not, just set sequence as document count
Expand Down
12 changes: 1 addition & 11 deletions LiteDB/Engine/Pages/CollectionPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,11 @@ protected override void WriteContent(ByteWriter writer)
/// </summary>
public CollectionIndex GetFreeIndex()
{
for (byte i = 0; i < this.Indexes.Length; i++)
{
if (this.Indexes[i].IsEmpty) return this.Indexes[i];
}
if (this.Indexes[0].IsEmpty) return this.Indexes[0];

throw LiteException.IndexLimitExceeded(this.CollectionName);
}

/// <summary>
/// Get index from field name (index field name is case sensitive) - returns null if not found
/// </summary>
public CollectionIndex GetIndex(string field)
{
return this.Indexes.FirstOrDefault(x => x.Field == field);
}

/// <summary>
/// Get primary key index (_id index)
Expand Down

0 comments on commit c3b20ae

Please sign in to comment.