Skip to content

Commit

Permalink
Fix GetCollectionSize and use ENSURE to throw exception in release mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdavid committed Jan 9, 2021
1 parent af3181e commit 1c2c397
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 24 deletions.
4 changes: 2 additions & 2 deletions LiteDB.Tests/Internals/HeaderPage_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion LiteDB/Engine/Disk/DiskReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion LiteDB/Engine/Disk/MemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions LiteDB/Engine/Pages/BasePage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions LiteDB/Engine/Pages/HeaderPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
6 changes: 3 additions & 3 deletions LiteDB/Engine/Services/SnapShot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,11 +514,11 @@ private void DeletePage<T>(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();
Expand Down
8 changes: 4 additions & 4 deletions LiteDB/LiteDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

<PropertyGroup>
<TargetFrameworks>net45;netstandard1.3;netstandard2.0</TargetFrameworks>
<AssemblyVersion>5.0.9</AssemblyVersion>
<FileVersion>5.0.9</FileVersion>
<VersionPrefix>5.0.9</VersionPrefix>
<AssemblyVersion>5.0.10</AssemblyVersion>
<FileVersion>5.0.10</FileVersion>
<VersionPrefix>5.0.10</VersionPrefix>
<Authors>Maurício David</Authors>
<Product>LiteDB</Product>
<Description>LiteDB - A lightweight embedded .NET NoSQL document store in a single datafile</Description>
<Copyright>MIT</Copyright>
<NeutralLanguage>en-US</NeutralLanguage>
<Title>LiteDB</Title>
<PackageId>LiteDB</PackageId>
<PackageVersion>5.0.9</PackageVersion>
<PackageVersion>5.0.10</PackageVersion>
<PackageTags>database nosql embedded</PackageTags>
<PackageIcon>icon_64x64.png</PackageIcon>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
Expand Down
32 changes: 25 additions & 7 deletions LiteDB/Utils/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -118,10 +118,9 @@ public static void LOG(bool conditional, string message, string category)
}

/// <summary>
/// Ensure condition is true, otherwise stop execution (for Debug proposes only)
/// Ensure condition is true, otherwise throw exception (check contract)
/// </summary>
[DebuggerHidden]
[Conditional("DEBUG")]
public static void ENSURE(bool conditional, string message = null)
{
if (conditional == false)
Expand All @@ -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);
}
}
}

/// <summary>
/// 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)
/// </summary>
[DebuggerHidden]
[Conditional("DEBUG")]
public static void ENSURE(bool ifTest, bool conditional, string message = null)
{
if (ifTest && conditional == false)
Expand All @@ -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);
}
}
}

/// <summary>
/// Ensure condition is true, otherwise throw exception (check contract)
/// </summary>
[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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion LiteDB/Utils/Extensions/BufferSliceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public static void Write(this BufferSlice buffer, string value, int offset)
/// </summary>
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)
{
Expand Down

0 comments on commit 1c2c397

Please sign in to comment.