Skip to content

Commit 38e095a

Browse files
Merge pull request #396 from MPCoreDeveloper/sonar/mechanical-cleanup
sonar: fix mechanical and new-code issues (batch 1)
2 parents 224f36c + cd7a8e4 commit 38e095a

5 files changed

Lines changed: 29 additions & 37 deletions

File tree

‎src/SharpCoreDB/DataStructures/OverflowArena.cs‎

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,34 +121,29 @@ public long Write(byte[] payload)
121121
private bool TryReuseFreeBlock(byte[] payload, out long offset)
122122
{
123123
offset = 0;
124-
if (!_freeByLength.TryGetValue(payload.Length, out var offsets))
124+
if (!_freeByLength.TryGetValue(payload.Length, out var offsets) || offsets.Count == 0)
125125
{
126126
return false;
127127
}
128128

129-
while (offsets.Count > 0)
130-
{
131-
offset = offsets[^1];
132-
offsets.RemoveAt(offsets.Count - 1);
129+
offset = offsets[^1];
130+
offsets.RemoveAt(offsets.Count - 1);
133131

134-
if (_storage.OverwriteRecordAt(_filePath, offset, payload))
132+
if (_storage.OverwriteRecordAt(_filePath, offset, payload))
133+
{
134+
if (offsets.Count == 0)
135135
{
136-
if (offsets.Count == 0)
137-
{
138-
_freeByLength.Remove(payload.Length);
139-
}
140-
141-
_cache[offset] = payload;
142-
_blockReuses++;
143-
return true;
136+
_freeByLength.Remove(payload.Length);
144137
}
145138

146-
// In-place overwrite refused (e.g. transaction active): keep the block free for a
147-
// later write and try the next candidate; if none succeeds we fall back to append.
148-
offsets.Add(offset);
149-
break;
139+
_cache[offset] = payload;
140+
_blockReuses++;
141+
return true;
150142
}
151143

144+
// In-place overwrite refused (e.g. transaction active): keep the block free for a later
145+
// write and fall back to appending.
146+
offsets.Add(offset);
152147
offset = 0;
153148
return false;
154149
}

‎src/SharpCoreDB/DataStructures/Table.CRUD.cs‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,8 +2272,8 @@ oldHashValues is not null &&
22722272
var newPkVal = row[this.Columns[this.PrimaryKeyIndex]]?.ToString() ?? string.Empty;
22732273
if (!string.Equals(newPkVal, oldPkValue?.ToString(), StringComparison.Ordinal))
22742274
{
2275-
if (!string.IsNullOrEmpty(oldPkValue?.ToString()))
2276-
this.Index.Delete(oldPkValue!.ToString()!);
2275+
if (oldPkValue?.ToString() is { Length: > 0 } oldPkString)
2276+
this.Index.Delete(oldPkString);
22772277
if (!string.IsNullOrEmpty(newPkVal))
22782278
this.Index.Insert(newPkVal, oldPosition);
22792279
}
@@ -2692,12 +2692,9 @@ private bool HasExplicitNamedIndex(string column)
26922692
// Transactional delete: buffer the physical offsets so the in-place marker is
26932693
// applied at COMMIT (rollback discards the buffer). Durable in O(delete) — the
26942694
// flush-time full-file rewrite is no longer needed for transactional deletes.
2695-
foreach (var position in positions)
2695+
foreach (var position in positions.Where(static position => position >= 0))
26962696
{
2697-
if (position >= 0)
2698-
{
2699-
this.storage.BufferTombstoneForCommit(DataFile, position);
2700-
}
2697+
this.storage.BufferTombstoneForCommit(DataFile, position);
27012698
}
27022699
}
27032700
else
@@ -2755,10 +2752,10 @@ private void TombstoneRemainingVersionsOfDeletedKeys(List<(long storagePosition,
27552752
List<long>? remainingPositions = null;
27562753
if (!this.storage.AreRecordsEncrypted(DataFile))
27572754
{
2758-
remainingPositions = ScanLegacyPlaintextRemainingKeys(DataFile, pkCol, deletedKeys);
2755+
remainingPositions = ScanLegacyPlaintextRemainingKeys(DataFile, deletedKeys);
27592756
}
27602757

2761-
remainingPositions ??= ScanLegacyRemainingKeysViaStorage(DataFile, pkCol, deletedKeys);
2758+
remainingPositions ??= ScanLegacyRemainingKeysViaStorage(DataFile, deletedKeys);
27622759
if (remainingPositions is { Count: > 0 })
27632760
{
27642761
TombstoneDeletedPositions(remainingPositions.ToArray());
@@ -2772,7 +2769,7 @@ private void TombstoneRemainingVersionsOfDeletedKeys(List<(long storagePosition,
27722769
/// <see langword="null"/> when the raw layout could not be parsed safely (the caller then falls
27732770
/// back to the storage-layer scan, which understands per-record encryption).
27742771
/// </summary>
2775-
private List<long>? ScanLegacyPlaintextRemainingKeys(string dataFile, string pkCol, HashSet<string> deletedKeys)
2772+
private List<long>? ScanLegacyPlaintextRemainingKeys(string dataFile, HashSet<string> deletedKeys)
27762773
{
27772774
var matches = new List<long>();
27782775
try
@@ -2822,7 +2819,7 @@ private void TombstoneRemainingVersionsOfDeletedKeys(List<(long storagePosition,
28222819
break;
28232820
}
28242821

2825-
if (TryReadPrimaryKeyFromLegacyRecord(recordData, pkCol, out var pkStr) && deletedKeys.Contains(pkStr))
2822+
if (TryReadPrimaryKeyFromLegacyRecord(recordData, out var pkStr) && deletedKeys.Contains(pkStr))
28262823
{
28272824
matches.Add(position);
28282825
}
@@ -2843,12 +2840,12 @@ private void TombstoneRemainingVersionsOfDeletedKeys(List<(long storagePosition,
28432840
/// plaintext raw scan is unavailable): iterates the records through
28442841
/// <c>storage.ReadAllRecords</c>, which decrypts payloads and already skips tombstone markers.
28452842
/// </summary>
2846-
private List<long> ScanLegacyRemainingKeysViaStorage(string dataFile, string pkCol, HashSet<string> deletedKeys)
2843+
private List<long> ScanLegacyRemainingKeysViaStorage(string dataFile, HashSet<string> deletedKeys)
28472844
{
28482845
var matches = new List<long>();
2849-
foreach (var (recordOffset, recordData) in this.storage!.ReadAllRecords(dataFile))
2846+
foreach (var (recordOffset, recordData) in this.storage.ReadAllRecords(dataFile))
28502847
{
2851-
if (TryReadPrimaryKeyFromLegacyRecord(recordData, pkCol, out var pkStr) && deletedKeys.Contains(pkStr))
2848+
if (TryReadPrimaryKeyFromLegacyRecord(recordData, out var pkStr) && deletedKeys.Contains(pkStr))
28522849
{
28532850
matches.Add(recordOffset);
28542851
}
@@ -2861,7 +2858,7 @@ private List<long> ScanLegacyRemainingKeysViaStorage(string dataFile, string pkC
28612858
/// Walks a legacy variable-length record and returns the value of the primary-key column
28622859
/// (the same layout walk used by the reopen index rebuild).
28632860
/// </summary>
2864-
private bool TryReadPrimaryKeyFromLegacyRecord(byte[] recordData, string pkCol, out string? pkValue)
2861+
private bool TryReadPrimaryKeyFromLegacyRecord(byte[] recordData, out string? pkValue)
28652862
{
28662863
pkValue = null;
28672864
try

‎src/SharpCoreDB/Database/Core/Database.Core.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public partial class Database : IDatabase, IDisposable, IAsyncDisposable
3535
// Diagnostics: number of DELETE statements routed through the canonical structured batch path
3636
// (proves the scanner/batch fast paths are engaged; 0 means everything fell back to regex).
3737
private static long _canonicalDeleteStatements;
38-
public long CanonicalDeleteStatementsParsed => Interlocked.Read(ref _canonicalDeleteStatements);
38+
public static long CanonicalDeleteStatementsParsed => Interlocked.Read(ref _canonicalDeleteStatements);
3939

4040
private readonly IServiceProvider _serviceProvider;
4141
private readonly IStorage storage;

‎tests/SharpCoreDB.Tests/FixedWidthBulkDeleteTests.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,12 @@ public void CanonicalBatchDelete_EngagesStructuredPath()
299299
InsertDocs(db, 1, 50);
300300
db.Flush();
301301

302-
var before = ((SharpCoreDB.Database)db).CanonicalDeleteStatementsParsed;
302+
var before = SharpCoreDB.Database.CanonicalDeleteStatementsParsed;
303303
db.ExecuteBatchSQL(BuildDeletes(1, 10));
304304
db.Flush();
305305

306306
Assert.True(
307-
((SharpCoreDB.Database)db).CanonicalDeleteStatementsParsed >= before + 10,
307+
SharpCoreDB.Database.CanonicalDeleteStatementsParsed >= before + 10,
308308
"canonical DELETE statements must flow through the structured batch path");
309309
Assert.Equal(40, db.ExecuteQuery("SELECT id FROM docs").Count);
310310
}

‎tests/SharpCoreDB.Tests/FixedWidthRecordLayoutTests.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void RoundTrip_AllColumnTypes_PointAndFullScan()
5858
Assert.Single(row);
5959
Assert.Equal("beta", row[0]["name"]);
6060
Assert.Equal(2.5, Convert.ToDouble(row[0]["score"]));
61-
Assert.Equal(false, Convert.ToBoolean(row[0]["flag"]));
61+
Assert.False(Convert.ToBoolean(row[0]["flag"]));
6262

6363
var all = db.ExecuteQuery("SELECT * FROM t ORDER BY id");
6464
Assert.Equal(2, all.Count);

0 commit comments

Comments
 (0)