Skip to content

Commit

Permalink
Merge pull request #2515 from mbdavid/no-diskqueue
Browse files Browse the repository at this point in the history
Remove DiskWriterQueue to avoid adicional thread to write on disk
  • Loading branch information
mbdavid committed Jul 3, 2024
2 parents ca3f905 + 198f0a6 commit c54674e
Show file tree
Hide file tree
Showing 19 changed files with 194 additions and 295 deletions.
4 changes: 1 addition & 3 deletions LiteDB.Tests/Internals/CacheAsync_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@ void serialize(ManualResetEventSlim toBlock, ManualResetEventSlim toFree)
// test starts here!!!
var p0 = new HeaderPage(r.NewPage(), 0);
disk.WriteAsync(new PageBuffer[] {p0.UpdateBuffer()});
disk.WriteLogDisk(new PageBuffer[] { p0.UpdateBuffer() });
// (1 ->) jump to thread B
serialize(wa, wb);
// (2 <-) continue from thread B
disk.Queue.Value.Wait();
// (3 ->) jump to thread B
serialize(wa, wb);
});
Expand Down
5 changes: 1 addition & 4 deletions LiteDB.Tests/Internals/Disk_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ public void Disk_Read_Write()
}

// page will be saved in LOG file in PagePosition order (0-99)
disk.WriteAsync(pages);

// wait for async queue writes
disk.Queue.Value.Wait();
disk.WriteLogDisk(pages);

// after release, no page can be read/write
pages.Clear();
Expand Down
32 changes: 32 additions & 0 deletions LiteDB.Tests/Query/Data/PersonGroupByData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using FluentAssertions;
using System;
using System.IO;
using System.Linq;
using Xunit;

namespace LiteDB.Tests.QueryTest
{
public class PersonGroupByData : IDisposable
{
private readonly Person[] _local;
private readonly ILiteDatabase _db;
private readonly ILiteCollection<Person> _collection;

public PersonGroupByData()
{
_local = DataGen.Person(1, 1000).ToArray();
_db = new LiteDatabase(new MemoryStream());
_collection = _db.GetCollection<Person>();

_collection.Insert(_local);
_collection.EnsureIndex(x => x.Age);
}

public (ILiteCollection<Person>, Person[]) GetData() => (_collection, _local);

public void Dispose()
{
_db.Dispose();
}
}
}
28 changes: 28 additions & 0 deletions LiteDB.Tests/Query/Data/PersonQueryData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Linq;

namespace LiteDB.Tests.QueryTest
{
public class PersonQueryData : IDisposable
{
private readonly Person[] _local;
private readonly ILiteDatabase _db;
private readonly ILiteCollection<Person> _collection;

public PersonQueryData()
{
_local = DataGen.Person().ToArray();

_db = new LiteDatabase(":memory:");
_collection = _db.GetCollection<Person>("person");
_collection.Insert(this._local);
}

public (ILiteCollection<Person>, Person[]) GetData() => (_collection, _local);

public void Dispose()
{
_db.Dispose();
}
}
}
62 changes: 22 additions & 40 deletions LiteDB.Tests/Query/GroupBy_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,33 @@

namespace LiteDB.Tests.QueryTest
{
public class GroupBy_Tests : IDisposable
public class GroupBy_Tests
{
private readonly Person[] local;

private readonly ILiteDatabase db;
private readonly ILiteCollection<Person> collection;

public GroupBy_Tests()
{
local = DataGen.Person(1, 1000).ToArray();

db = new LiteDatabase(new MemoryStream());
collection = db.GetCollection<Person>();

collection.Insert(local);
collection.EnsureIndex(x => x.Age);
}

[Fact(Skip = "Commented out")]
[Fact(Skip = "Missing implement LINQ for GroupBy")]
public void Query_GroupBy_Age_With_Count()
{
//** var r0 = local
//** .GroupBy(x => x.Age)
//** .Select(x => new { Age = x.Key, Count = x.Count() })
//** .OrderBy(x => x.Age)
//** .ToArray();
//**using var db = new PersonGroupByData();
//**var (collection, local) = db.GetData();
//**
//** var r1 = collection.Query()
//** .GroupBy(x => x.Age)
//** .Select(x => new { Age = x.Key, Count = x.Count() })
//** .ToArray();
//**var r0 = local
//** .GroupBy(x => x.Age)
//** .Select(x => new { Age = x.Key, Count = x.Count() })
//** .OrderBy(x => x.Age)
//** .ToArray();
//**
//** foreach (var r in r0.Zip(r1, (l, r) => new { left = l, right = r }))
//** {
//** r.left.Age.Should().Be(r.right.Age);
//** r.left.Count.Should().Be(r.right.Count);
//** }
//**var r1 = collection.Query()
//** .GroupBy("$.Age")
//** .Select(x => new { Age = x.Key, Count = x.Count() })
//** .ToArray();
//**
//**foreach (var r in r0.Zip(r1, (l, r) => new { left = l, right = r }))
//**{
//** r.left.Age.Should().Be(r.right.Age);
//** r.left.Count.Should().Be(r.right.Count);
//**}
}

[Fact(Skip = "Commented out")]
[Fact(Skip = "Missing implement LINQ for GroupBy")]
public void Query_GroupBy_Year_With_Sum_Age()
{
//** var r0 = local
Expand All @@ -66,7 +53,7 @@ public void Query_GroupBy_Year_With_Sum_Age()
//** }
}

[Fact(Skip = "Commented out")]
[Fact(Skip = "Missing implement LINQ for GroupBy")]
public void Query_GroupBy_Func()
{
//** var r0 = local
Expand All @@ -87,7 +74,7 @@ public void Query_GroupBy_Func()
//** }
}

[Fact(Skip = "Commented out")]
[Fact(Skip = "Missing implement LINQ for GroupBy")]
public void Query_GroupBy_With_Array_Aggregation()
{
//** // quite complex group by query
Expand All @@ -113,10 +100,5 @@ public void Query_GroupBy_With_Array_Aggregation()
//** Assert.Equal("Dahlia Warren", r[0].Users[0].Name);
//** Assert.Equal(24, r[0].Users[0].Age);
}

public void Dispose()
{
db?.Dispose();
}
}
}
18 changes: 16 additions & 2 deletions LiteDB.Tests/Query/OrderBy_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

namespace LiteDB.Tests.QueryTest
{
public class OrderBy_Tests : Person_Tests
public class OrderBy_Tests
{
[Fact]
public void Query_OrderBy_Using_Index()
{
using var db = new PersonQueryData();
var (collection, local) = db.GetData();

collection.EnsureIndex(x => x.Name);

var r0 = local
Expand All @@ -27,6 +30,9 @@ public void Query_OrderBy_Using_Index()
[Fact]
public void Query_OrderBy_Using_Index_Desc()
{
using var db = new PersonQueryData();
var (collection, local) = db.GetData();

collection.EnsureIndex(x => x.Name);

var r0 = local
Expand All @@ -45,6 +51,9 @@ public void Query_OrderBy_Using_Index_Desc()
[Fact]
public void Query_OrderBy_With_Func()
{
using var db = new PersonQueryData();
var (collection, local) = db.GetData();

collection.EnsureIndex(x => x.Date.Day);

var r0 = local
Expand All @@ -63,6 +72,9 @@ public void Query_OrderBy_With_Func()
[Fact]
public void Query_OrderBy_With_Offset_Limit()
{
using var db = new PersonQueryData();
var (collection, local) = db.GetData();

// no index

var r0 = local
Expand All @@ -85,12 +97,14 @@ public void Query_OrderBy_With_Offset_Limit()
[Fact]
public void Query_Asc_Desc()
{
using var db = new PersonQueryData();
var (collection, _) = db.GetData();

var asc = collection.Find(Query.All(Query.Ascending)).ToArray();
var desc = collection.Find(Query.All(Query.Descending)).ToArray();

asc[0].Id.Should().Be(1);
desc[0].Id.Should().Be(1000);

}
}
}
27 changes: 0 additions & 27 deletions LiteDB.Tests/Query/Person_Tests.cs

This file was deleted.

8 changes: 7 additions & 1 deletion LiteDB.Tests/Query/QueryApi_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@

namespace LiteDB.Tests.QueryTest
{
public class QueryApi_Tests : Person_Tests
public class QueryApi_Tests : PersonQueryData
{
[Fact]
public void Query_And()
{
using var db = new PersonQueryData();
var (collection, local) = db.GetData();

var r0 = local.Where(x => x.Age == 22 && x.Active == true).ToArray();

var r1 = collection.Find(Query.And(Query.EQ("Age", 22), Query.EQ("Active", true))).ToArray();
Expand All @@ -20,6 +23,9 @@ public void Query_And()
[Fact]
public void Query_And_Same_Field()
{
using var db = new PersonQueryData();
var (collection, local) = db.GetData();

var r0 = local.Where(x => x.Age > 22 && x.Age < 25).ToArray();

var r1 = collection.Find(Query.And(Query.GT("Age", 22), Query.LT("Age", 25))).ToArray();
Expand Down
16 changes: 15 additions & 1 deletion LiteDB.Tests/Query/Select_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@

namespace LiteDB.Tests.QueryTest
{
public class Select_Tests : Person_Tests
public class Select_Tests : PersonQueryData
{
[Fact]
public void Query_Select_Key_Only()
{
using var db = new PersonQueryData();
var (collection, local) = db.GetData();

collection.EnsureIndex(x => x.Address.City);

// must orderBy mem data because index will be sorted
Expand All @@ -30,6 +33,9 @@ public void Query_Select_Key_Only()
[Fact]
public void Query_Select_New_Document()
{
using var db = new PersonQueryData();
var (collection, local) = db.GetData();

var r0 = local
.Select(x => new {city = x.Address.City.ToUpper(), phone0 = x.Phones[0], address = new Address {Street = x.Name}})
.ToArray();
Expand All @@ -49,6 +55,9 @@ public void Query_Select_New_Document()
[Fact]
public void Query_Or_With_Null()
{
using var db = new PersonQueryData();
var (collection, _) = db.GetData();

var r = collection.Find(Query.Or(
Query.GTE("Date", new DateTime(2001, 1, 1)),
Query.EQ("Date", null)
Expand All @@ -58,6 +67,9 @@ public void Query_Or_With_Null()
[Fact]
public void Query_Find_All_Predicate()
{
using var db = new PersonQueryData();
var (collection, _) = db.GetData();

var r = collection.Find(x => true).ToArray();

r.Should().HaveCount(1000);
Expand All @@ -66,6 +78,8 @@ public void Query_Find_All_Predicate()
[Fact]
public void Query_With_No_Collection()
{
using var db = new LiteDatabase(":memory:");

using (var r = db.Execute("SELECT DAY(NOW()) as DIA"))
{
while(r.Read())
Expand Down
Loading

0 comments on commit c54674e

Please sign in to comment.