Skip to content

Commit 1c2c397

Browse files
committed
Fix GetCollectionSize and use ENSURE to throw exception in release mode
1 parent af3181e commit 1c2c397

File tree

9 files changed

+42
-24
lines changed

9 files changed

+42
-24
lines changed

LiteDB.Tests/Internals/HeaderPage_Tests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void HeaderPage_Collections()
2525
header.GetCollections().Count().Should().Be(2);
2626
((int) header.GetCollectionPageID("my-col1")).Should().Be(1);
2727
((int) header.GetCollectionPageID("my-col2")).Should().Be(2);
28-
header.GetAvailableCollectionSpace().Should().Be(7981);
28+
header.GetAvailableCollectionSpace().Should().Be(7955);
2929

3030
header.UpdateBuffer();
3131

@@ -35,7 +35,7 @@ public void HeaderPage_Collections()
3535
h2.GetCollections().Count().Should().Be(2);
3636
((int) h2.GetCollectionPageID("my-col1")).Should().Be(1);
3737
((int) h2.GetCollectionPageID("my-col2")).Should().Be(2);
38-
h2.GetAvailableCollectionSpace().Should().Be(7981);
38+
h2.GetAvailableCollectionSpace().Should().Be(7955);
3939

4040
buffer.ShareCounter = 0;
4141
}

LiteDB/Engine/Disk/DiskReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void ReadStream(Stream stream, long position, BufferSlice buffer)
6363

6464
stream.Read(buffer.Array, buffer.Offset, buffer.Count);
6565

66-
ENSURE(buffer.All(0) == false, "check if are not reading out of file length");
66+
DEBUG(buffer.All(0) == false, "check if are not reading out of file length");
6767
}
6868

6969
/// <summary>

LiteDB/Engine/Disk/MemoryCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private PageBuffer NewPage(long position, FileOrigin origin)
162162
page.Clear();
163163
}
164164

165-
ENSURE(page.All(0), "new page must be full zero empty before return");
165+
DEBUG(page.All(0), "new page must be full zero empty before return");
166166

167167
page.Origin = origin;
168168
page.Timestamp = DateTime.UtcNow.Ticks;

LiteDB/Engine/Pages/BasePage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public BasePage(PageBuffer buffer, uint pageID, PageType pageType)
141141
{
142142
_buffer = buffer;
143143

144-
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");
144+
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");
145145

146146
// page information
147147
this.PageID = pageID;
@@ -426,8 +426,8 @@ public void Delete(byte index)
426426
if (this.ItemsCount == 0)
427427
{
428428
ENSURE(this.HighestIndex == byte.MaxValue, "if there is no items, HighestIndex must be clear");
429-
ENSURE(_buffer.Slice(PAGE_HEADER_SIZE, PAGE_SIZE - PAGE_HEADER_SIZE - 1).All(0), "all content area must be 0");
430429
ENSURE(this.UsedBytes == 0, "should be no bytes used in clean page");
430+
DEBUG(_buffer.Slice(PAGE_HEADER_SIZE, PAGE_SIZE - PAGE_HEADER_SIZE - 1).All(0), "all content area must be 0");
431431

432432
this.NextFreePosition = PAGE_HEADER_SIZE;
433433
this.FragmentedBytes = 0;

LiteDB/Engine/Pages/HeaderPage.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ public int GetAvailableCollectionSpace()
249249
{
250250
return COLLECTIONS_SIZE -
251251
_collections.GetBytesCount(true) -
252-
1 + // for int32 type (0x10)
253-
1 + // for new CString ('\0')
254-
4 + // for PageID (int32)
252+
1 - // for int32 type (0x10)
253+
1 - // for new CString ('\0')
254+
4 - // for PageID (int32)
255255
8; // reserved
256256
}
257257
}

LiteDB/Engine/Services/SnapShot.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,11 +514,11 @@ private void DeletePage<T>(T page)
514514
where T : BasePage
515515
{
516516
ENSURE(page.PrevPageID == uint.MaxValue && page.NextPageID == uint.MaxValue, "before delete a page, no linked list with any another page");
517-
ENSURE(page.Buffer.Slice(PAGE_HEADER_SIZE, PAGE_SIZE - PAGE_HEADER_SIZE - 1).All(0), "page content shloud be empty");
518517
ENSURE(page.ItemsCount == 0 && page.UsedBytes == 0 && page.HighestIndex == byte.MaxValue && page.FragmentedBytes == 0, "no items on page when delete this page");
519518
ENSURE(page.PageType == PageType.Data || page.PageType == PageType.Index, "only data/index page can be deleted");
520-
ENSURE(!_collectionPage.FreeDataPageList.Any(x => x == page.PageID), "this page cann't be deleted because free data list page is linked o this page");
521-
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");
519+
DEBUG(!_collectionPage.FreeDataPageList.Any(x => x == page.PageID), "this page cann't be deleted because free data list page is linked o this page");
520+
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");
521+
DEBUG(page.Buffer.Slice(PAGE_HEADER_SIZE, PAGE_SIZE - PAGE_HEADER_SIZE - 1).All(0), "page content shloud be empty");
522522

523523
// mark page as empty and dirty
524524
page.MarkAsEmtpy();

LiteDB/LiteDB.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>net45;netstandard1.3;netstandard2.0</TargetFrameworks>
5-
<AssemblyVersion>5.0.9</AssemblyVersion>
6-
<FileVersion>5.0.9</FileVersion>
7-
<VersionPrefix>5.0.9</VersionPrefix>
5+
<AssemblyVersion>5.0.10</AssemblyVersion>
6+
<FileVersion>5.0.10</FileVersion>
7+
<VersionPrefix>5.0.10</VersionPrefix>
88
<Authors>Maurício David</Authors>
99
<Product>LiteDB</Product>
1010
<Description>LiteDB - A lightweight embedded .NET NoSQL document store in a single datafile</Description>
1111
<Copyright>MIT</Copyright>
1212
<NeutralLanguage>en-US</NeutralLanguage>
1313
<Title>LiteDB</Title>
1414
<PackageId>LiteDB</PackageId>
15-
<PackageVersion>5.0.9</PackageVersion>
15+
<PackageVersion>5.0.10</PackageVersion>
1616
<PackageTags>database nosql embedded</PackageTags>
1717
<PackageIcon>icon_64x64.png</PackageIcon>
1818
<PackageLicenseFile>LICENSE</PackageLicenseFile>

LiteDB/Utils/Constants.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ internal class Constants
102102
[Conditional("DEBUG")]
103103
public static void LOG(string message, string category)
104104
{
105+
//Debug.WriteLine is too slow in multi-threads
105106
//var threadID = Environment.CurrentManagedThreadId;
106-
//
107107
//Debug.WriteLine(message, threadID + "|" + category);
108108
}
109109

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

120120
/// <summary>
121-
/// Ensure condition is true, otherwise stop execution (for Debug proposes only)
121+
/// Ensure condition is true, otherwise throw exception (check contract)
122122
/// </summary>
123123
[DebuggerHidden]
124-
[Conditional("DEBUG")]
125124
public static void ENSURE(bool conditional, string message = null)
126125
{
127126
if (conditional == false)
@@ -132,16 +131,15 @@ public static void ENSURE(bool conditional, string message = null)
132131
}
133132
else
134133
{
135-
throw new Exception("ENSURE: " + message);
134+
throw new Exception("LiteDB contract violation: " + message);
136135
}
137136
}
138137
}
139138

140139
/// <summary>
141-
/// If ifTest are true, ensure condition is true, otherwise stop execution (for Debug proposes only)
140+
/// If ifTest are true, ensure condition is true, otherwise throw ensure exception (check contract)
142141
/// </summary>
143142
[DebuggerHidden]
144-
[Conditional("DEBUG")]
145143
public static void ENSURE(bool ifTest, bool conditional, string message = null)
146144
{
147145
if (ifTest && conditional == false)
@@ -152,7 +150,27 @@ public static void ENSURE(bool ifTest, bool conditional, string message = null)
152150
}
153151
else
154152
{
155-
throw new Exception("ENSURE: " + message);
153+
throw new Exception("LiteDB contract violation: " + message);
154+
}
155+
}
156+
}
157+
158+
/// <summary>
159+
/// Ensure condition is true, otherwise throw exception (check contract)
160+
/// </summary>
161+
[DebuggerHidden]
162+
[Conditional("DEBUG")]
163+
public static void DEBUG(bool conditional, string message = null)
164+
{
165+
if (conditional == false)
166+
{
167+
if (Debugger.IsAttached)
168+
{
169+
Debug.Fail(message);
170+
}
171+
else
172+
{
173+
throw new Exception("LiteDB contract violation: " + message);
156174
}
157175
}
158176
}

LiteDB/Utils/Extensions/BufferSliceExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public static void Write(this BufferSlice buffer, string value, int offset)
238238
/// </summary>
239239
public static void WriteIndexKey(this BufferSlice buffer, BsonValue value, int offset)
240240
{
241-
ENSURE(IndexNode.GetKeyLength(value, true) <= MAX_INDEX_KEY_LENGTH, $"index key must have less than {MAX_INDEX_KEY_LENGTH} bytes");
241+
DEBUG(IndexNode.GetKeyLength(value, true) <= MAX_INDEX_KEY_LENGTH, $"index key must have less than {MAX_INDEX_KEY_LENGTH} bytes");
242242

243243
if (value.IsString)
244244
{

0 commit comments

Comments
 (0)