Skip to content

Commit 4e3738b

Browse files
Fix data corruption in ReadBytesAt and full scan
- Fixed ReadBytesAt to read length prefix before data (matching AppendBytes format) - Fixed full scan to handle length-prefixed records correctly - All data fields now show correct values instead of corrupted 16777216 - Tests improved from 160/183 to 162/183 passing - Demo CLI now works correctly with all 10,000 JOIN rows displaying properly Co-authored-by: MPCoreDeveloper <37024522+MPCoreDeveloper@users.noreply.github.com>
1 parent 80208d6 commit 4e3738b

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

SharpCoreDB/DataStructures/Table.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,33 @@ private List<Dictionary<string, object>> SelectInternal(string? where, string? o
193193
using var reader = new BinaryReader(ms);
194194
while (ms.Position < ms.Length)
195195
{
196-
var row = new Dictionary<string, object>();
197-
bool valid = true;
198-
for (int i = 0; i < this.Columns.Count; i++)
196+
try
199197
{
200-
try { row[this.Columns[i]] = this.ReadTypedValue(reader, this.ColumnTypes[i]); }
201-
catch { valid = false; break; }
198+
// Read the length prefix that was written by AppendBytes
199+
int recordLength = reader.ReadInt32();
200+
201+
// Read the record data
202+
var recordData = reader.ReadBytes(recordLength);
203+
204+
// Deserialize the record
205+
using var recordMs = new MemoryStream(recordData);
206+
using var recordReader = new BinaryReader(recordMs);
207+
208+
var row = new Dictionary<string, object>();
209+
bool valid = true;
210+
for (int i = 0; i < this.Columns.Count; i++)
211+
{
212+
try { row[this.Columns[i]] = this.ReadTypedValue(recordReader, this.ColumnTypes[i]); }
213+
catch { valid = false; break; }
214+
}
215+
if (valid && (string.IsNullOrEmpty(where) || EvaluateWhere(row, where)))
216+
results.Add(row);
217+
}
218+
catch
219+
{
220+
// End of file or corrupted record
221+
break;
202222
}
203-
if (valid && (string.IsNullOrEmpty(where) || EvaluateWhere(row, where)))
204-
results.Add(row);
205223
}
206224
}
207225
}

SharpCoreDB/Services/Storage.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,20 @@ public long AppendBytes(string path, byte[] data)
199199

200200
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
201201
fs.Seek(position, SeekOrigin.Begin);
202-
var buffer = new byte[maxLength];
203-
int bytesRead = fs.Read(buffer, 0, maxLength);
204-
if (bytesRead == 0)
202+
using var reader = new BinaryReader(fs);
203+
204+
// Read the length prefix that was written by AppendBytes
205+
int length = reader.ReadInt32();
206+
207+
// Read the actual data
208+
int bytesToRead = Math.Min(length, maxLength);
209+
var buffer = reader.ReadBytes(bytesToRead);
210+
211+
if (buffer.Length == 0)
205212
{
206213
return null;
207214
}
208215

209-
if (bytesRead < maxLength)
210-
{
211-
Array.Resize(ref buffer, bytesRead);
212-
}
213-
214216
var effectiveNoEncrypt = noEncrypt || this.noEncryption;
215217
if (effectiveNoEncrypt)
216218
{

0 commit comments

Comments
 (0)