Skip to content

Commit

Permalink
Remove DiskWriterQueue and use same thread to write on disk
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdavid committed Jun 26, 2024
1 parent 2536079 commit 198f0a6
Show file tree
Hide file tree
Showing 18 changed files with 152 additions and 320 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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@

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

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

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

public ILiteCollection<Person> GetCollection() => _collection;

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

public void Dispose()
{
_db?.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();
}
}
}
25 changes: 10 additions & 15 deletions LiteDB.Tests/Query/OrderBy_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ public class OrderBy_Tests
[Fact]
public void Query_OrderBy_Using_Index()
{
using var db = new Person_Tests();
var collection = db.GetCollection();
var local = db.GetLocal();
using var db = new PersonQueryData();
var (collection, local) = db.GetData();

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

Expand All @@ -31,9 +30,8 @@ public void Query_OrderBy_Using_Index()
[Fact]
public void Query_OrderBy_Using_Index_Desc()
{
using var db = new Person_Tests();
var collection = db.GetCollection();
var local = db.GetLocal();
using var db = new PersonQueryData();
var (collection, local) = db.GetData();

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

Expand All @@ -53,9 +51,8 @@ public void Query_OrderBy_Using_Index_Desc()
[Fact]
public void Query_OrderBy_With_Func()
{
using var db = new Person_Tests();
var collection = db.GetCollection();
var local = db.GetLocal();
using var db = new PersonQueryData();
var (collection, local) = db.GetData();

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

Expand All @@ -75,9 +72,8 @@ public void Query_OrderBy_With_Func()
[Fact]
public void Query_OrderBy_With_Offset_Limit()
{
using var db = new Person_Tests();
var collection = db.GetCollection();
var local = db.GetLocal();
using var db = new PersonQueryData();
var (collection, local) = db.GetData();

// no index

Expand All @@ -101,15 +97,14 @@ public void Query_OrderBy_With_Offset_Limit()
[Fact]
public void Query_Asc_Desc()
{
using var db = new Person_Tests();
var collection = db.GetCollection();
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);

}
}
}
12 changes: 5 additions & 7 deletions LiteDB.Tests/Query/QueryApi_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@

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

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

Expand All @@ -24,9 +23,8 @@ public void Query_And()
[Fact]
public void Query_And_Same_Field()
{
using var db = new Person_Tests();
var collection = db.GetCollection();
var local = db.GetLocal();
using var db = new PersonQueryData();
var (collection, local) = db.GetData();

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

Expand Down
21 changes: 9 additions & 12 deletions LiteDB.Tests/Query/Select_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@

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 Person_Tests();
var collection = db.GetCollection();
var local = db.GetLocal();
using var db = new PersonQueryData();
var (collection, local) = db.GetData();

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

Expand All @@ -34,9 +33,8 @@ public void Query_Select_Key_Only()
[Fact]
public void Query_Select_New_Document()
{
using var db = new Person_Tests();
var collection = db.GetCollection();
var local = db.GetLocal();
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}})
Expand All @@ -57,8 +55,8 @@ public void Query_Select_New_Document()
[Fact]
public void Query_Or_With_Null()
{
using var db = new Person_Tests();
var collection = db.GetCollection();
using var db = new PersonQueryData();
var (collection, _) = db.GetData();

var r = collection.Find(Query.Or(
Query.GTE("Date", new DateTime(2001, 1, 1)),
Expand All @@ -69,9 +67,8 @@ public void Query_Or_With_Null()
[Fact]
public void Query_Find_All_Predicate()
{
using var db = new Person_Tests();
var collection = db.GetCollection();
var local = db.GetLocal();
using var db = new PersonQueryData();
var (collection, _) = db.GetData();

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

Expand Down
Loading

0 comments on commit 198f0a6

Please sign in to comment.