Skip to content

Commit 8bd6484

Browse files
Fix AES-GCM encryption buffer size and HashIndex list reference issues
Co-authored-by: MPCoreDeveloper <37024522+MPCoreDeveloper@users.noreply.github.com>
1 parent 2c29696 commit 8bd6484

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

SharpCoreDB.Tests/DatabaseFileTests.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,18 @@ public void ReadWritePage_ShouldWorkCorrectly()
4949
{
5050
// Arrange
5151
using var dbFile = new DatabaseFile(this.testFilePath, this.crypto, this.key);
52+
53+
// Create test data with valid page header
5254
var testData = new byte[4096];
53-
for (int i = 0; i < testData.Length; i++)
55+
var header = PageHeader.Create((byte)PageType.Data, 12345);
56+
var userData = new byte[4096 - PageHeader.Size];
57+
for (int i = 0; i < userData.Length; i++)
5458
{
55-
testData[i] = (byte)(i % 256);
59+
userData[i] = (byte)(i % 256);
5660
}
61+
62+
// Create complete page using PageSerializer
63+
PageSerializer.CreatePage(ref header, userData, testData);
5764

5865
// Act
5966
dbFile.WritePage(0, testData);
@@ -71,11 +78,18 @@ public void ReadPageZeroAlloc_ShouldReadIntoBuffer()
7178
{
7279
// Arrange
7380
using var dbFile = new DatabaseFile(this.testFilePath, this.crypto, this.key);
81+
82+
// Create test data with valid page header
7483
var testData = new byte[4096];
75-
for (int i = 0; i < testData.Length; i++)
84+
var header = PageHeader.Create((byte)PageType.Data, 12345);
85+
var userData = new byte[4096 - PageHeader.Size];
86+
for (int i = 0; i < userData.Length; i++)
7687
{
77-
testData[i] = (byte)(i % 256);
88+
userData[i] = (byte)(i % 256);
7889
}
90+
91+
// Create complete page using PageSerializer
92+
PageSerializer.CreatePage(ref header, userData, testData);
7993
var buffer = new byte[4096];
8094

8195
// Act

SharpCoreDB.Tests/HashIndexTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void HashIndex_Remove_RemovesRow()
6060
index.Add(row2, 1);
6161
var beforeRemove = index.LookupPositions(1);
6262

63-
index.Remove(row1);
63+
index.Remove(row1, 0);
6464
var afterRemove = index.LookupPositions(1);
6565

6666
// Assert

SharpCoreDB/DataStructures/HashIndex.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace SharpCoreDB.DataStructures;
1717
/// </summary>
1818
public class HashIndex
1919
{
20-
private readonly Dictionary<object, List<long>> _index = new();
20+
private readonly Dictionary<object, List<long>> _index;
2121
private readonly string _columnName;
2222
private readonly SimdHashEqualityComparer _comparer = new();
2323

@@ -29,6 +29,8 @@ public class HashIndex
2929
public HashIndex(string tableName, string columnName)
3030
{
3131
_columnName = columnName;
32+
// Use default comparer for now - SimdHashEqualityComparer has platform-specific issues with boxed integers
33+
_index = new Dictionary<object, List<long>>();
3234
}
3335

3436
/// <summary>
@@ -39,7 +41,7 @@ public HashIndex(string tableName, string columnName)
3941
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
4042
public void Add(Dictionary<string, object> row, long position)
4143
{
42-
if (!row.TryGetValue(_columnName, out var key))
44+
if (!row.TryGetValue(_columnName, out var key) || key == null)
4345
return;
4446

4547
if (!_index.TryGetValue(key, out var list))
@@ -56,20 +58,40 @@ public void Add(Dictionary<string, object> row, long position)
5658
/// <param name="row">The row data.</param>
5759
public void Remove(Dictionary<string, object> row)
5860
{
59-
if (row.TryGetValue(_columnName, out var key))
61+
if (row.TryGetValue(_columnName, out var key) && key != null)
6062
{
6163
_index.Remove(key);
6264
}
6365
}
6466

67+
/// <summary>
68+
/// Removes a specific position for a row from the index.
69+
/// </summary>
70+
/// <param name="row">The row data.</param>
71+
/// <param name="position">The position to remove.</param>
72+
public void Remove(Dictionary<string, object> row, long position)
73+
{
74+
if (!row.TryGetValue(_columnName, out var key) || key == null)
75+
return;
76+
77+
if (_index.TryGetValue(key, out var list))
78+
{
79+
list.Remove(position);
80+
if (list.Count == 0)
81+
{
82+
_index.Remove(key);
83+
}
84+
}
85+
}
86+
6587
/// <summary>
6688
/// Looks up positions for a given key using SIMD-accelerated comparison.
6789
/// </summary>
6890
/// <param name="key">The key to lookup.</param>
6991
/// <returns>List of positions matching the key.</returns>
7092
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
7193
public List<long> LookupPositions(object key)
72-
=> _index.TryGetValue(key, out var list) ? list : new();
94+
=> key != null && _index.TryGetValue(key, out var list) ? new List<long>(list) : new();
7395

7496
/// <summary>
7597
/// Gets the number of unique keys in the index.

SharpCoreDB/SharpCoreDB/Core/File/DatabaseFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void WritePageFromSpan(int pageNum, ReadOnlySpan<byte> data)
130130

131131
// Copy to encryption buffer and encrypt in-place
132132
data.CopyTo(_encryptedBuffer.AsSpan(0, PageSize));
133-
this.crypto.EncryptPage(_encryptedBuffer.AsSpan(0, PageSize));
133+
this.crypto.EncryptPage(_encryptedBuffer.AsSpan(0, StoredPageSize));
134134

135135
// Write to disk
136136
using var fs = new FileStream(this.filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read, 4096, FileOptions.WriteThrough);

0 commit comments

Comments
 (0)