Skip to content

Commit

Permalink
* Fixed index size length calculation on CollectionIndex.GetLength()
Browse files Browse the repository at this point in the history
* Fixed some typos on exception messages and comments
  • Loading branch information
kcsombrio committed Jan 19, 2022
1 parent 9010d77 commit 1e01447
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion LiteDB/Client/SqlParser/Commands/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace LiteDB
internal partial class SqlParser
{
/// <summary>
/// CREATE [ UNQIUE ] INDEX {indexName} ON {collection} ({indexExpr})
/// CREATE [ UNIQUE ] INDEX {indexName} ON {collection} ({indexExpr})
/// </summary>
private BsonDataReader ParseCreate()
{
Expand Down
2 changes: 1 addition & 1 deletion LiteDB/Engine/Engine/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public bool EnsureIndex(string collection, string name, BsonExpression expressio

if (name.Length > INDEX_NAME_MAX_LENGTH) throw LiteException.InvalidIndexName(name, collection, "MaxLength = " + INDEX_NAME_MAX_LENGTH);
if (!name.IsWord()) throw LiteException.InvalidIndexName(name, collection, "Use only [a-Z$_]");
if (name.StartsWith("$")) throw LiteException.InvalidIndexName(name, collection, "Index name can't starts with `$`");
if (name.StartsWith("$")) throw LiteException.InvalidIndexName(name, collection, "Index name can't start with `$`");
if (expression.IsScalar == false && unique) throw new LiteException(0, "Multikey index expression do not support unique option");

if (expression.Source == "$._id") return false; // always exists
Expand Down
8 changes: 5 additions & 3 deletions LiteDB/Engine/Pages/DataPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ public IEnumerable<PageAddress> GetBlocks()
};

/// <summary>
/// Get page index slot on FreeDataPageID
/// Returns the slot the page should be in, given the <paramref name="freeBytes"/> it has
/// </summary>
/// <returns>A slot number between 0 and 4</returns>
public static byte FreeIndexSlot(int freeBytes)
{
ENSURE(freeBytes >= 0, "freeBytes must be positive");
Expand All @@ -118,9 +119,10 @@ public static byte FreeIndexSlot(int freeBytes)
}

/// <summary>
/// Get minimum slot with space enough for your data content
/// Returns -1 if no space guaranteed (more than 90%)
/// Returns the slot where there is a page with enough space for <paramref name="length"/> bytes of data.
/// Returns -1 if no space guaranteed (more than 90% of a DataPage net size)
/// </summary>
/// <returns>A slot number between -1 and 3</returns>
public static int GetMinimumIndexSlot(int length)
{
return FreeIndexSlot(length) - 1;
Expand Down
4 changes: 2 additions & 2 deletions LiteDB/Engine/Services/IndexService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public IndexService(Snapshot snapshot, Collation collation)
/// </summary>
public CollectionIndex CreateIndex(string name, string expr, bool unique)
{
// get how many butes needed fore each head/tail (both has same size)
// get how many bytes needed for each head/tail (both has same size)
var bytesLength = IndexNode.GetNodeLength(MAX_LEVEL_LENGTH, BsonValue.MinValue, out var keyLength);

// get a new empty page (each index contains your own linked nodes)
// get a new empty page (each index contains its own linked nodes)
var indexPage = _snapshot.NewPage<IndexPage>();

// create index ref
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 @@ -252,7 +252,7 @@ public DataPage GetFreeDataPage(int bytesLength)
// get minimum slot to check for free page. Returns -1 if need NewPage
var startSlot = DataPage.GetMinimumIndexSlot(length);

// check for avaiable re-usable page
// check for available re-usable page
for (int currentSlot = startSlot; currentSlot >= 0; currentSlot--)
{
var freePageID = _collectionPage.FreeDataPageList[currentSlot];
Expand All @@ -263,15 +263,15 @@ public DataPage GetFreeDataPage(int bytesLength)
var page = this.GetPage<DataPage>(freePageID);

ENSURE(page.PageListSlot == currentSlot, "stored slot must be same as called");
ENSURE(page.FreeBytes >= length, "ensure selected page has space enougth for this content");
ENSURE(page.FreeBytes >= length, "ensure selected page has space enough for this content");

// mark page page as dirty
page.IsDirty = true;

return page;
}

// if not page free list page avaiable, create new page
// if there is no re-usable page, create a new one
return this.NewPage<DataPage>();
}

Expand Down
2 changes: 1 addition & 1 deletion LiteDB/Engine/Structures/CollectionIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static int GetLength(string name, string expr)
PageAddress.SIZE + // Head
PageAddress.SIZE + // Tail
1 + // MaxLevel
(PAGE_FREE_LIST_SLOTS * PageAddress.SIZE); // FreeListPage
4; // FreeListPage
}
}
}

3 comments on commit 1e01447

@tjmoore
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this fix a known issue?

@kcsombrio
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tjmoore, afaik no. It was found while debugging, investigating another issue. It was just looking for some extra space inside the collection page that isn't actually needed to store index information.

@tjmoore
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I'm just looking through changes in this version to see what it fixes. At a late stage in a project release and want to evaluate dependency updates.

Please sign in to comment.