Skip to content

Commit

Permalink
Add support for default name for indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdavid committed Jul 26, 2018
1 parent c89001c commit 4403bc6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
38 changes: 38 additions & 0 deletions LiteDB.Tests/Engine/Index_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Text.RegularExpressions;
using LiteDB.Engine;
using System.Threading;
using System.Linq.Expressions;

namespace LiteDB.Tests.Engine
{
[TestClass]
public class Index_Tests
{
[TestMethod]
public void Index_With_No_Name()
{
using (var db = new LiteEngine())
{
db.Insert("users", new BsonDocument { ["name"] = new BsonDocument { ["first"] = "John", ["last"] = "Doe" } });
db.Insert("users", new BsonDocument { ["name"] = new BsonDocument { ["first"] = "Marco", ["last"] = "Pollo" } });

// no index name defined
db.EnsureIndex("users", "name.last");
db.EnsureIndex("users", "$.name.first", true);

// default name: remove all non-[a-z] chars
Assert.IsNotNull(db.Query("$indexes").Where("collection = 'users' AND name = 'namelast'").ExecuteScalar());
Assert.IsNotNull(db.Query("$indexes").Where("collection = 'users' AND name = 'namefirst'").ExecuteScalar());
}
}
}
}
19 changes: 11 additions & 8 deletions LiteDB/Client/Database/Collections/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public partial class LiteCollection<T>
/// </summary>
/// <param name="expression">Document field/expression</param>
/// <param name="unique">If is a unique index</param>
public bool EnsureIndex(string expression, bool unique = false)
public bool EnsureIndex(BsonExpression expression, bool unique = false)
{
if (string.IsNullOrEmpty(expression)) throw new ArgumentNullException(nameof(expression));

Expand All @@ -22,10 +22,10 @@ public bool EnsureIndex(string expression, bool unique = false)
/// <summary>
/// Create a new permanent index in all documents inside this collections if index not exists already. Returns true if index was created or false if already exits
/// </summary>
/// <param name="name">Index name</param>
/// <param name="name">Index name - unique name for this collection</param>
/// <param name="expression">Create a custom expression function to be indexed</param>
/// <param name="unique">If is a unique index</param>
public bool EnsureIndex(string name, string expression, bool unique = false)
public bool EnsureIndex(string name, BsonExpression expression, bool unique = false)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));
if (string.IsNullOrEmpty(expression)) throw new ArgumentNullException(nameof(expression));
Expand All @@ -36,21 +36,24 @@ public bool EnsureIndex(string name, string expression, bool unique = false)
/// <summary>
/// Create a new permanent index in all documents inside this collections if index not exists already.
/// </summary>
/// <param name="predicate">LinqExpression to be converted into BsonExpression to be indexed</param>
/// <param name="keySelector">LinqExpression to be converted into BsonExpression to be indexed</param>
/// <param name="unique">Create a unique keys index?</param>
public bool EnsureIndex<K>(Expression<Func<T, K>> predicate, bool unique = false)
public bool EnsureIndex<K>(Expression<Func<T, K>> keySelector, bool unique = false)
{
var expression = _mapper.GetExpression(predicate);
var expression = _mapper.GetExpression(keySelector);

return _engine.Value.EnsureIndex(_collection, expression.Source, unique);
}

/// <summary>
/// Create a new permanent index in all documents inside this collections if index not exists already.
/// </summary>
public bool EnsureIndex<K>(string name, Expression<Func<T, K>> predicate, bool unique = false)
/// <param name="name">Index name - unique name for this collection</param>
/// <param name="keySelector">LinqExpression to be converted into BsonExpression to be indexed</param>
/// <param name="unique">Create a unique keys index?</param>
public bool EnsureIndex<K>(string name, Expression<Func<T, K>> keySelector, bool unique = false)
{
return _engine.Value.EnsureIndex(_collection, name, _mapper.GetExpression(predicate), unique);
return _engine.Value.EnsureIndex(_collection, name, _mapper.GetExpression(keySelector), unique);
}

/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions LiteDB/Engine/Engine/Index.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using static LiteDB.Constants;

namespace LiteDB.Engine
Expand All @@ -12,8 +13,9 @@ public partial class LiteEngine
/// </summary>
public bool EnsureIndex(string collection, string expression, bool unique = false)
{
//TODO: implement a index name cleaning/hash
return this.EnsureIndex(collection, expression, expression, unique);
var name = Regex.Replace(expression, @"[^a-z]", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);

return this.EnsureIndex(collection, name, expression, unique);
}

/// <summary>
Expand Down

0 comments on commit 4403bc6

Please sign in to comment.