From cb53709f47be43d3f20b1e4866ba6cccead38a2e Mon Sep 17 00:00:00 2001 From: mbdavid Date: Sun, 2 Jul 2017 19:08:52 -0300 Subject: [PATCH] Fix journal initial position --- LiteDB.Tests/Concurrency/ProcessTest.cs | 30 ++++++++++++------------- LiteDB/Engine/Disks/FileDiskService.cs | 2 +- LiteDB/Engine/Engine/Find.cs | 2 +- LiteDB/Engine/Services/LockService.cs | 8 +++---- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/LiteDB.Tests/Concurrency/ProcessTest.cs b/LiteDB.Tests/Concurrency/ProcessTest.cs index 1d7145c93..738a6d06f 100644 --- a/LiteDB.Tests/Concurrency/ProcessTest.cs +++ b/LiteDB.Tests/Concurrency/ProcessTest.cs @@ -24,24 +24,24 @@ public void Process_Insert_Test() dbA.EnsureIndex("col", "process", false); // insert 1000 x instance=1 - //var ta = Task.Factory.StartNew(() => - //{ - for (var i = 0; i < 1000; i++) - { - dbA.Insert("col", new BsonDocument { { "process", 1 } }); - } - //}); + var ta = Task.Factory.StartNew(() => + { + for (var i = 0; i < 1000; i++) + { + dbA.Insert("col", new BsonDocument { { "process", 1 } }); + } + }); // insert 700 x instance=2 - //var tb = Task.Factory.StartNew(() => - //{ - for (var i = 0; i < 700; i++) - { - dbB.Insert("col", new BsonDocument { { "process", 2 } }); - } - //}); + var tb = Task.Factory.StartNew(() => + { + for (var i = 0; i < 700; i++) + { + dbB.Insert("col", new BsonDocument { { "process", 2 } }); + } + }); - //Task.WaitAll(ta, tb); + Task.WaitAll(ta, tb); Assert.AreEqual(1000, dbA.Count("col", Query.EQ("process", 1))); Assert.AreEqual(700, dbA.Count("col", Query.EQ("process", 2))); diff --git a/LiteDB/Engine/Disks/FileDiskService.cs b/LiteDB/Engine/Disks/FileDiskService.cs index 112dd4646..a1afbe672 100644 --- a/LiteDB/Engine/Disks/FileDiskService.cs +++ b/LiteDB/Engine/Disks/FileDiskService.cs @@ -169,7 +169,7 @@ public void WriteJournal(ICollection pages, uint lastPageID) BasePage.GetSizeOfPages(pages.Count)); // go to initial file position (after lastPageID) - _stream.Seek(lastPageID + 1, SeekOrigin.Begin); + _stream.Seek(BasePage.GetSizeOfPages(lastPageID + 1), SeekOrigin.Begin); foreach(var buffer in pages) { diff --git a/LiteDB/Engine/Engine/Find.cs b/LiteDB/Engine/Engine/Find.cs index 213105e2a..6498ba6d3 100644 --- a/LiteDB/Engine/Engine/Find.cs +++ b/LiteDB/Engine/Engine/Find.cs @@ -9,7 +9,7 @@ public partial class LiteEngine /// /// Find for documents in a collection using Query definition /// - public IEnumerable Find(string collection, Query query, int skip = 0, int limit = int.MaxValue, int bufferSize = 10) + public IEnumerable Find(string collection, Query query, int skip = 0, int limit = int.MaxValue) { if (collection.IsNullOrWhiteSpace()) throw new ArgumentNullException("collection"); if (query == null) throw new ArgumentNullException("query"); diff --git a/LiteDB/Engine/Services/LockService.cs b/LiteDB/Engine/Services/LockService.cs index 9a40f83ac..0f6701dc1 100644 --- a/LiteDB/Engine/Services/LockService.cs +++ b/LiteDB/Engine/Services/LockService.cs @@ -55,7 +55,7 @@ public LockControl Shared() } // try enter in read mode - if (_thread.TryEnterReadLock(_timeout)) + if (!_thread.TryEnterReadLock(_timeout)) { throw LiteException.LockTimeout(_timeout); } @@ -91,15 +91,15 @@ public LockControl Exclusive() { lock (_disk) { - // if are locked in shared, throw invalid operation - if (_state == LockState.Shared) throw new InvalidOperationException("Call exit shared lock before call exclusive"); - // if already in exclusive, do nothing if (_state == LockState.Exclusive) { return new LockControl(() => { }); } + // if are locked in shared, exit read to enter in write + if (_state == LockState.Shared) throw new NotSupportedException("Lock are in exclusive mode"); + // try enter in write mode (thread) if (!_thread.TryEnterWriteLock(_timeout)) {