diff --git a/LiteDB.Tests/Internals/HeaderPage_Tests.cs b/LiteDB.Tests/Internals/HeaderPage_Tests.cs index c98dcba56..7cffceffe 100644 --- a/LiteDB.Tests/Internals/HeaderPage_Tests.cs +++ b/LiteDB.Tests/Internals/HeaderPage_Tests.cs @@ -25,7 +25,7 @@ public void HeaderPage_Collections() header.GetCollections().Count().Should().Be(2); ((int) header.GetCollectionPageID("my-col1")).Should().Be(1); ((int) header.GetCollectionPageID("my-col2")).Should().Be(2); - header.GetAvailableCollectionSpace().Should().Be(7981); + header.GetAvailableCollectionSpace().Should().Be(7955); header.UpdateBuffer(); @@ -35,7 +35,7 @@ public void HeaderPage_Collections() h2.GetCollections().Count().Should().Be(2); ((int) h2.GetCollectionPageID("my-col1")).Should().Be(1); ((int) h2.GetCollectionPageID("my-col2")).Should().Be(2); - h2.GetAvailableCollectionSpace().Should().Be(7981); + h2.GetAvailableCollectionSpace().Should().Be(7955); buffer.ShareCounter = 0; } diff --git a/LiteDB/Engine/Disk/DiskReader.cs b/LiteDB/Engine/Disk/DiskReader.cs index 255628478..fba173658 100644 --- a/LiteDB/Engine/Disk/DiskReader.cs +++ b/LiteDB/Engine/Disk/DiskReader.cs @@ -63,7 +63,7 @@ private void ReadStream(Stream stream, long position, BufferSlice buffer) stream.Read(buffer.Array, buffer.Offset, buffer.Count); - ENSURE(buffer.All(0) == false, "check if are not reading out of file length"); + DEBUG(buffer.All(0) == false, "check if are not reading out of file length"); } /// diff --git a/LiteDB/Engine/Disk/MemoryCache.cs b/LiteDB/Engine/Disk/MemoryCache.cs index 19e15d80d..944497f6c 100644 --- a/LiteDB/Engine/Disk/MemoryCache.cs +++ b/LiteDB/Engine/Disk/MemoryCache.cs @@ -162,7 +162,7 @@ private PageBuffer NewPage(long position, FileOrigin origin) page.Clear(); } - ENSURE(page.All(0), "new page must be full zero empty before return"); + DEBUG(page.All(0), "new page must be full zero empty before return"); page.Origin = origin; page.Timestamp = DateTime.UtcNow.Ticks; diff --git a/LiteDB/Engine/Pages/BasePage.cs b/LiteDB/Engine/Pages/BasePage.cs index bfabfe420..0609a1ea5 100644 --- a/LiteDB/Engine/Pages/BasePage.cs +++ b/LiteDB/Engine/Pages/BasePage.cs @@ -141,7 +141,7 @@ public BasePage(PageBuffer buffer, uint pageID, PageType pageType) { _buffer = buffer; - ENSURE(buffer.Slice(PAGE_HEADER_SIZE, PAGE_SIZE - PAGE_HEADER_SIZE - 1).All(0), "new page buffer must be empty before use in a new page"); + DEBUG(buffer.Slice(PAGE_HEADER_SIZE, PAGE_SIZE - PAGE_HEADER_SIZE - 1).All(0), "new page buffer must be empty before use in a new page"); // page information this.PageID = pageID; @@ -426,8 +426,8 @@ public void Delete(byte index) if (this.ItemsCount == 0) { ENSURE(this.HighestIndex == byte.MaxValue, "if there is no items, HighestIndex must be clear"); - ENSURE(_buffer.Slice(PAGE_HEADER_SIZE, PAGE_SIZE - PAGE_HEADER_SIZE - 1).All(0), "all content area must be 0"); ENSURE(this.UsedBytes == 0, "should be no bytes used in clean page"); + DEBUG(_buffer.Slice(PAGE_HEADER_SIZE, PAGE_SIZE - PAGE_HEADER_SIZE - 1).All(0), "all content area must be 0"); this.NextFreePosition = PAGE_HEADER_SIZE; this.FragmentedBytes = 0; diff --git a/LiteDB/Engine/Pages/HeaderPage.cs b/LiteDB/Engine/Pages/HeaderPage.cs index 2e8360def..5addbbc1e 100644 --- a/LiteDB/Engine/Pages/HeaderPage.cs +++ b/LiteDB/Engine/Pages/HeaderPage.cs @@ -249,9 +249,9 @@ public int GetAvailableCollectionSpace() { return COLLECTIONS_SIZE - _collections.GetBytesCount(true) - - 1 + // for int32 type (0x10) - 1 + // for new CString ('\0') - 4 + // for PageID (int32) + 1 - // for int32 type (0x10) + 1 - // for new CString ('\0') + 4 - // for PageID (int32) 8; // reserved } } diff --git a/LiteDB/Engine/Services/SnapShot.cs b/LiteDB/Engine/Services/SnapShot.cs index b05f05c7c..045ce927c 100644 --- a/LiteDB/Engine/Services/SnapShot.cs +++ b/LiteDB/Engine/Services/SnapShot.cs @@ -514,11 +514,11 @@ private void DeletePage(T page) where T : BasePage { ENSURE(page.PrevPageID == uint.MaxValue && page.NextPageID == uint.MaxValue, "before delete a page, no linked list with any another page"); - ENSURE(page.Buffer.Slice(PAGE_HEADER_SIZE, PAGE_SIZE - PAGE_HEADER_SIZE - 1).All(0), "page content shloud be empty"); ENSURE(page.ItemsCount == 0 && page.UsedBytes == 0 && page.HighestIndex == byte.MaxValue && page.FragmentedBytes == 0, "no items on page when delete this page"); ENSURE(page.PageType == PageType.Data || page.PageType == PageType.Index, "only data/index page can be deleted"); - ENSURE(!_collectionPage.FreeDataPageList.Any(x => x == page.PageID), "this page cann't be deleted because free data list page is linked o this page"); - ENSURE(!_collectionPage.GetCollectionIndexes().Any(x => x.FreeIndexPageList == page.PageID), "this page cann't be deleted because free index list page is linked o this page"); + DEBUG(!_collectionPage.FreeDataPageList.Any(x => x == page.PageID), "this page cann't be deleted because free data list page is linked o this page"); + DEBUG(!_collectionPage.GetCollectionIndexes().Any(x => x.FreeIndexPageList == page.PageID), "this page cann't be deleted because free index list page is linked o this page"); + DEBUG(page.Buffer.Slice(PAGE_HEADER_SIZE, PAGE_SIZE - PAGE_HEADER_SIZE - 1).All(0), "page content shloud be empty"); // mark page as empty and dirty page.MarkAsEmtpy(); diff --git a/LiteDB/LiteDB.csproj b/LiteDB/LiteDB.csproj index f16bf51b0..4d22493ae 100644 --- a/LiteDB/LiteDB.csproj +++ b/LiteDB/LiteDB.csproj @@ -2,9 +2,9 @@ net45;netstandard1.3;netstandard2.0 - 5.0.9 - 5.0.9 - 5.0.9 + 5.0.10 + 5.0.10 + 5.0.10 MaurĂ­cio David LiteDB LiteDB - A lightweight embedded .NET NoSQL document store in a single datafile @@ -12,7 +12,7 @@ en-US LiteDB LiteDB - 5.0.9 + 5.0.10 database nosql embedded icon_64x64.png LICENSE diff --git a/LiteDB/Utils/Constants.cs b/LiteDB/Utils/Constants.cs index a8d404245..ffd5f31a8 100644 --- a/LiteDB/Utils/Constants.cs +++ b/LiteDB/Utils/Constants.cs @@ -102,8 +102,8 @@ internal class Constants [Conditional("DEBUG")] public static void LOG(string message, string category) { + //Debug.WriteLine is too slow in multi-threads //var threadID = Environment.CurrentManagedThreadId; - // //Debug.WriteLine(message, threadID + "|" + category); } @@ -118,10 +118,9 @@ public static void LOG(bool conditional, string message, string category) } /// - /// Ensure condition is true, otherwise stop execution (for Debug proposes only) + /// Ensure condition is true, otherwise throw exception (check contract) /// [DebuggerHidden] - [Conditional("DEBUG")] public static void ENSURE(bool conditional, string message = null) { if (conditional == false) @@ -132,16 +131,15 @@ public static void ENSURE(bool conditional, string message = null) } else { - throw new Exception("ENSURE: " + message); + throw new Exception("LiteDB contract violation: " + message); } } } /// - /// If ifTest are true, ensure condition is true, otherwise stop execution (for Debug proposes only) + /// If ifTest are true, ensure condition is true, otherwise throw ensure exception (check contract) /// [DebuggerHidden] - [Conditional("DEBUG")] public static void ENSURE(bool ifTest, bool conditional, string message = null) { if (ifTest && conditional == false) @@ -152,7 +150,27 @@ public static void ENSURE(bool ifTest, bool conditional, string message = null) } else { - throw new Exception("ENSURE: " + message); + throw new Exception("LiteDB contract violation: " + message); + } + } + } + + /// + /// Ensure condition is true, otherwise throw exception (check contract) + /// + [DebuggerHidden] + [Conditional("DEBUG")] + public static void DEBUG(bool conditional, string message = null) + { + if (conditional == false) + { + if (Debugger.IsAttached) + { + Debug.Fail(message); + } + else + { + throw new Exception("LiteDB contract violation: " + message); } } } diff --git a/LiteDB/Utils/Extensions/BufferSliceExtensions.cs b/LiteDB/Utils/Extensions/BufferSliceExtensions.cs index 1df2f1527..bfab84f8d 100644 --- a/LiteDB/Utils/Extensions/BufferSliceExtensions.cs +++ b/LiteDB/Utils/Extensions/BufferSliceExtensions.cs @@ -238,7 +238,7 @@ public static void Write(this BufferSlice buffer, string value, int offset) /// public static void WriteIndexKey(this BufferSlice buffer, BsonValue value, int offset) { - ENSURE(IndexNode.GetKeyLength(value, true) <= MAX_INDEX_KEY_LENGTH, $"index key must have less than {MAX_INDEX_KEY_LENGTH} bytes"); + DEBUG(IndexNode.GetKeyLength(value, true) <= MAX_INDEX_KEY_LENGTH, $"index key must have less than {MAX_INDEX_KEY_LENGTH} bytes"); if (value.IsString) {