Skip to content

Commit abd72ce

Browse files
author
MPCoreDeveloper
committed
latest update refactoring
1 parent 99c2caa commit abd72ce

36 files changed

+6394
-1046
lines changed

README.md

Lines changed: 62 additions & 419 deletions
Large diffs are not rendered by default.

SharpCoreDB.Benchmarks/StorageEngineComparisonBenchmark.cs

Lines changed: 110 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,13 @@ private void PrePopulateForUpdateSelectBenchmarks()
292292
liteCollection.InsertBulk(records);
293293

294294
// Pre-populate ENCRYPTED databases for UPDATE/SELECT
295-
appendOnlyEncryptedDb!.ExecuteBatchSQL(appendInserts); // Reuse same inserts
296-
pageBasedEncryptedDb!.ExecuteBatchSQL(pageInserts); // Reuse same inserts
297-
columnarAnalyticsDb!.ExecuteBatchSQL(appendInserts); // ? Pre-populate for analytics benchmarks
295+
// Skip AppendOnly ENCRYPTED pre-population to avoid PK conflicts with insert benchmark
296+
// appendOnlyEncryptedDb!.ExecuteBatchSQL(appendInserts); // SKIPPED
297+
// Skip PageBased ENCRYPTED pre-population to avoid PK conflicts with insert benchmark
298+
// pageBasedEncryptedDb!.ExecuteBatchSQL(pageInserts); // SKIPPED
299+
columnarAnalyticsDb!.ExecuteBatchSQL(appendInserts);
298300

299-
// ? NEW: Pre-transpose columnar data for analytics benchmarks (do this ONCE in setup!)
301+
// Pre-transpose columnar data for analytics benchmarks (do this ONCE in setup!)
300302
Console.WriteLine("Pre-transposing columnar data for SIMD benchmarks...");
301303
var columnarRows = columnarAnalyticsDb.ExecuteQuery("SELECT * FROM bench_records");
302304
var columnarRecords = columnarRows.Select(r => new BenchmarkRecord
@@ -352,13 +354,22 @@ public void Cleanup()
352354
[BenchmarkCategory("Insert")]
353355
public void AppendOnly_Insert_100K()
354356
{
355-
try { appendOnlyDb!.ExecuteSQL("DELETE FROM bench_records WHERE id >= 10000"); } catch { }
357+
// Compute starting id to avoid PK conflicts with any pre-populated data
358+
int startId = 0;
359+
try
360+
{
361+
var maxRows = appendOnlyDb!.ExecuteQuery("SELECT MAX(id) FROM bench_records");
362+
if (maxRows.Count > 0 && maxRows[0].TryGetValue("max", out var mv) && mv is not null)
363+
{
364+
startId = Convert.ToInt32(Convert.ToDecimal(mv)) + 1;
365+
}
366+
}
367+
catch { /* fallback to 0 */ }
356368

357-
// Insert NEW records using BULK INSERT (optimized path)
358369
var rows = new List<Dictionary<string, object>>(RecordCount);
359370
for (int i = 0; i < RecordCount; i++)
360371
{
361-
int id = RecordCount + i;
372+
int id = startId + i;
362373
rows.Add(new Dictionary<string, object>
363374
{
364375
["id"] = id,
@@ -380,13 +391,22 @@ public void AppendOnly_Insert_100K()
380391
[BenchmarkCategory("Insert")]
381392
public void PageBased_Insert_100K()
382393
{
383-
try { pageBasedDb!.ExecuteSQL("DELETE FROM bench_records WHERE id >= 10000"); } catch { }
394+
// Compute starting id to avoid PK conflicts with any pre-populated data
395+
int startId = 0;
396+
try
397+
{
398+
var maxRows = pageBasedDb!.ExecuteQuery("SELECT MAX(id) FROM bench_records");
399+
if (maxRows.Count > 0 && maxRows[0].TryGetValue("max", out var mv) && mv is not null)
400+
{
401+
startId = Convert.ToInt32(Convert.ToDecimal(mv)) + 1;
402+
}
403+
}
404+
catch { /* fallback to 0 */ }
384405

385-
// Insert NEW records using BULK INSERT (optimized path)
386406
var rows = new List<Dictionary<string, object>>(RecordCount);
387407
for (int i = 0; i < RecordCount; i++)
388408
{
389-
int id = RecordCount + i;
409+
int id = startId + i;
390410
rows.Add(new Dictionary<string, object>
391411
{
392412
["id"] = id,
@@ -407,11 +427,18 @@ public void PageBased_Insert_100K()
407427
[BenchmarkCategory("Insert")]
408428
public void SQLite_Insert_100K()
409429
{
410-
// Clear previous iteration data
430+
// Clear previous iteration data - wrapped in try-catch to handle missing records
411431
using (var delCmd = sqliteConn!.CreateCommand())
412432
{
413433
delCmd.CommandText = "DELETE FROM bench_records WHERE id >= 10000";
414-
try { delCmd.ExecuteNonQuery(); } catch { }
434+
try
435+
{
436+
delCmd.ExecuteNonQuery();
437+
}
438+
catch
439+
{
440+
// Ignore errors if no records exist
441+
}
415442
}
416443

417444
// Insert NEW records
@@ -448,9 +475,16 @@ public void SQLite_Insert_100K()
448475
[BenchmarkCategory("Insert")]
449476
public void LiteDB_Insert_100K()
450477
{
451-
// Clear previous iteration data
478+
// Clear previous iteration data - wrapped in try-catch to handle missing records
452479
var collection = liteDb!.GetCollection<BenchmarkRecord>("bench_records");
453-
try { collection.DeleteMany(x => x.Id >= RecordCount); } catch { }
480+
try
481+
{
482+
collection.DeleteMany(x => x.Id >= RecordCount);
483+
}
484+
catch
485+
{
486+
// Ignore errors if no records exist
487+
}
454488

455489
// Insert NEW records
456490
var records = new List<BenchmarkRecord>(RecordCount);
@@ -631,23 +665,40 @@ private class BenchmarkRecord
631665
[BenchmarkCategory("Encrypted")]
632666
public void AppendOnly_Encrypted_Insert_10K()
633667
{
634-
try { appendOnlyEncryptedDb!.ExecuteSQL("DELETE FROM bench_records WHERE id >= 10000"); } catch { }
635-
636-
var rows = new List<Dictionary<string, object>>(RecordCount);
637-
for (int i = 0; i < RecordCount; i++)
668+
try
638669
{
639-
int id = RecordCount + i;
640-
rows.Add(new Dictionary<string, object>
670+
int startId = 0;
671+
try
641672
{
642-
["id"] = id,
643-
["name"] = $"NewUser{id}",
644-
["email"] = $"newuser{id}@test.com",
645-
["age"] = 20 + (i % 50),
646-
["salary"] = (decimal)(30000 + (i % 70000)),
647-
["created"] = DateTime.Parse("2025-01-01")
648-
});
673+
var maxRows = appendOnlyEncryptedDb!.ExecuteQuery("SELECT MAX(id) FROM bench_records");
674+
if (maxRows.Count > 0 && maxRows[0].TryGetValue("max", out var mv) && mv is not null)
675+
{
676+
startId = Convert.ToInt32(Convert.ToDecimal(mv)) + 1;
677+
}
678+
}
679+
catch { }
680+
681+
var rows = new List<Dictionary<string, object>>(RecordCount);
682+
for (int i = 0; i < RecordCount; i++)
683+
{
684+
int id = startId + i;
685+
rows.Add(new Dictionary<string, object>
686+
{
687+
["id"] = id,
688+
["name"] = $"NewUser{id}",
689+
["email"] = $"newuser{id}@test.com",
690+
["age"] = 20 + (i % 50),
691+
["salary"] = (decimal)(30000 + (i % 70000)),
692+
["created"] = DateTime.Parse("2025-01-01")
693+
});
694+
}
695+
appendOnlyEncryptedDb!.BulkInsertAsync("bench_records", rows).GetAwaiter().GetResult();
696+
}
697+
catch (Exception ex)
698+
{
699+
Console.WriteLine($"[Encrypted AppendOnly Insert] Error: {ex.Message}");
700+
throw;
649701
}
650-
appendOnlyEncryptedDb!.BulkInsertAsync("bench_records", rows).GetAwaiter().GetResult();
651702
}
652703

653704
/// <summary>
@@ -658,23 +709,40 @@ public void AppendOnly_Encrypted_Insert_10K()
658709
[BenchmarkCategory("Encrypted")]
659710
public void PageBased_Encrypted_Insert_10K()
660711
{
661-
try { pageBasedEncryptedDb!.ExecuteSQL("DELETE FROM bench_records WHERE id >= 10000"); } catch { }
662-
663-
var rows = new List<Dictionary<string, object>>(RecordCount);
664-
for (int i = 0; i < RecordCount; i++)
712+
try
665713
{
666-
int id = RecordCount + i;
667-
rows.Add(new Dictionary<string, object>
714+
int startId = 0;
715+
try
668716
{
669-
["id"] = id,
670-
["name"] = $"NewUser{id}",
671-
["email"] = $"newuser{id}@test.com",
672-
["age"] = 20 + (i % 50),
673-
["salary"] = (decimal)(30000 + (i % 70000)),
674-
["created"] = DateTime.Parse("2025-01-01")
675-
});
717+
var maxRows = pageBasedEncryptedDb!.ExecuteQuery("SELECT MAX(id) FROM bench_records");
718+
if (maxRows.Count > 0 && maxRows[0].TryGetValue("max", out var mv) && mv is not null)
719+
{
720+
startId = Convert.ToInt32(Convert.ToDecimal(mv)) + 1;
721+
}
722+
}
723+
catch { }
724+
725+
var rows = new List<Dictionary<string, object>>(RecordCount);
726+
for (int i = 0; i < RecordCount; i++)
727+
{
728+
int id = startId + i;
729+
rows.Add(new Dictionary<string, object>
730+
{
731+
["id"] = id,
732+
["name"] = $"NewUser{id}",
733+
["email"] = $"newuser{id}@test.com",
734+
["age"] = 20 + (i % 50),
735+
["salary"] = (decimal)(30000 + (i % 70000)),
736+
["created"] = DateTime.Parse("2025-01-01")
737+
});
738+
}
739+
pageBasedEncryptedDb!.BulkInsertAsync("bench_records", rows).GetAwaiter().GetResult();
740+
}
741+
catch (Exception ex)
742+
{
743+
Console.WriteLine($"[Encrypted PageBased Insert] Error: {ex.Message}");
744+
throw;
676745
}
677-
pageBasedEncryptedDb!.BulkInsertAsync("bench_records", rows).GetAwaiter().GetResult();
678746
}
679747

680748
/// <summary>

0 commit comments

Comments
 (0)