-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Description
Happyness,
This issue, it is in the LiteDB since the 3.x series, and still not fixed in the newest version.
The indexed Min Max values are stohastic. After insert in the same session they are ok, but after reopening the database (program restart) they returning UTC. The filtering looks good, but i am not sure if index is used there.
Here is a test code demonstrating the problem.
using System;
using System.IO;
using System.Linq;
using LiteDB;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace LiteDBDateTimeIndexMinMax
{
[TestClass]
public class LiteDBDateTimeIndexMinMaxUnitTest
{
[TestMethod]
public void MinMaxStandardDateTime()
{
File.Delete("test.litedb");
LiteDatabase db1 = new LiteDatabase("test.litedb");
LiteCollection<DateTimeTest> coll1 = db1.GetCollection<DateTimeTest>();
coll1.EnsureIndex(x => x.dt);
coll1.Insert(new DateTimeTest() { DateTimeTestID = 1, dt = new DateTime(2018, 02, 22, 0, 0, 0) });
coll1.Insert(new DateTimeTest() { DateTimeTestID = 2, dt = new DateTime(2018, 02, 22, 23, 59, 59) });
MinMaxCommon(coll1);
db1.Dispose();
LiteDatabase db2 = new LiteDatabase("test.litedb");
LiteCollection<DateTimeTest> coll2 = db2.GetCollection<DateTimeTest>();
MinMaxCommon(coll2);
coll2.Insert(new DateTimeTest() { DateTimeTestID = 3, dt = new DateTime(2018, 02, 21, 23, 59, 59) });
coll2.Insert(new DateTimeTest() { DateTimeTestID = 4, dt = new DateTime(2018, 02, 23, 0, 0, 0) });
coll2.Insert(new DateTimeTest() { DateTimeTestID = 5, dt = new DateTime(2018, 02, 22, 0, 0, 1) });
coll2.Insert(new DateTimeTest() { DateTimeTestID = 6, dt = new DateTime(2018, 02, 22, 23, 59, 58) });
MinMaxCommon(coll2);
db2.Dispose();
LiteDatabase db3 = new LiteDatabase("test.litedb");
LiteCollection<DateTimeTest> coll3 = db3.GetCollection<DateTimeTest>();
MinMaxCommon(coll3);
}
private void MinMaxCommon(LiteCollection<DateTimeTest> coll)
{
var searchdatetime = new DateTime(2018, 02, 22, 0, 0, 10);
var min = coll.Min(x => x.dt).AsDateTime;
var max = coll.Max(x => x.dt).AsDateTime;
var smaller = coll.Find(x => x.dt < searchdatetime);
var greater = coll.Find(x => x.dt > searchdatetime);
var all = coll.FindAll().ToList();
var linqmin = all.Min(x => x.dt);
var linqmax = all.Max(x => x.dt);
Assert.AreEqual(min, linqmin);
Assert.AreEqual(max, linqmax);
}
public class DateTimeTest
{
public int DateTimeTestID { get; set; }
public DateTime? dt { get; set; }
public string mittomen { get; set; }
}
}
}Joy and Harmony!