Skip to content

Commit

Permalink
Fixed index key length checks
Browse files Browse the repository at this point in the history
  • Loading branch information
lbnascimento committed Apr 8, 2020
1 parent f9418af commit 05b8b33
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 15 deletions.
2 changes: 1 addition & 1 deletion LiteDB/Engine/Services/IndexService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private IndexNode AddNode(CollectionIndex index, BsonValue key, PageAddress data
// get a free index page for head note
var bytesLength = IndexNode.GetNodeLength(level, key, out var keyLength);

// test for index key maxlength (length must fit in 1 byte)
// test for index key maxlength
if (keyLength > MAX_INDEX_KEY_LENGTH) throw LiteException.InvalidIndexKey($"Index key must be less than {MAX_INDEX_KEY_LENGTH} bytes.");

var indexPage = _snapshot.GetFreeIndexPage(bytesLength, ref index.FreeIndexPageList);
Expand Down
9 changes: 1 addition & 8 deletions LiteDB/Engine/Sort/SortContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void Insert(IEnumerable<KeyValuePair<BsonValue, PageAddress>> items, int
{
buffer.WriteIndexKey(item.Key, offset);

var keyLength = GetKeyLength(item.Key);
var keyLength = IndexNode.GetKeyLength(item.Key, false);

if (keyLength > MAX_INDEX_KEY_LENGTH) throw LiteException.InvalidIndexKey($"Sort key must be less than {MAX_INDEX_KEY_LENGTH} bytes.");

Expand Down Expand Up @@ -136,13 +136,6 @@ private IEnumerable<BufferSlice> GetSourceFromStream(Stream stream)
BufferPool.Return(bytes);
}

public static int GetKeyLength(BsonValue key)
{
return 1 + // DataType
key.GetBytesCount(false) + // BsonValue
(key.IsString || key.IsBinary ? 1 : 0); // Key Length
}

public void Dispose()
{
_reader?.Dispose();
Expand Down
4 changes: 2 additions & 2 deletions LiteDB/Engine/Sort/SortService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ private IEnumerable<IEnumerable<KeyValuePair<BsonValue, PageAddress>>> SliptValu
/// </summary>
private IEnumerable<KeyValuePair<BsonValue, PageAddress>> YieldValues(IEnumerator<KeyValuePair<BsonValue, PageAddress>> source, Done done)
{
var size = SortContainer.GetKeyLength(source.Current.Key) + PageAddress.SIZE;
var size = IndexNode.GetKeyLength(source.Current.Key, false) + PageAddress.SIZE;

yield return source.Current;

while (source.MoveNext())
{
var length = SortContainer.GetKeyLength(source.Current.Key) + PageAddress.SIZE;
var length = IndexNode.GetKeyLength(source.Current.Key, false) + PageAddress.SIZE;

done.Count++;

Expand Down
6 changes: 3 additions & 3 deletions LiteDB/Engine/Structures/IndexNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internal class IndexNode
/// </summary>
public static int GetNodeLength(byte level, BsonValue key, out int keyLength)
{
keyLength = GetKeyLength(key);
keyLength = GetKeyLength(key, true);

return INDEX_NODE_FIXED_SIZE +
(level * 2 * PageAddress.SIZE) + // prev/next
Expand All @@ -91,11 +91,11 @@ public static int GetNodeLength(byte level, BsonValue key, out int keyLength)
/// [1 byte] - KeyLength (used only in String|Byte[])
/// [N bytes] - BsonValue in bytes (0-254)
/// </summary>
public static int GetKeyLength(BsonValue key)
public static int GetKeyLength(BsonValue key, bool recalc)
{
return 1 +
((key.IsString || key.IsBinary) ? 1 : 0) +
key.GetBytesCount(true);
key.GetBytesCount(recalc);
}

/// <summary>
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(value.GetBytesCount(false) <= MAX_INDEX_KEY_LENGTH, "index key must have less than 1023 bytes");
ENSURE(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 05b8b33

Please sign in to comment.