Spaces:
Sleeping
Sleeping
Commit ·
e4f56e2
1
Parent(s): fc5a6ca
fix: optimize SQLite write performance and re-enable WAL mode to resolve database locking
Browse files
FlowAPI.Infrastructure/Repositories/GraphRepository.cs
CHANGED
|
@@ -72,17 +72,15 @@ namespace FlowAPI.Infrastructure.Repositories
|
|
| 72 |
|| deletedNodeIds.Contains(e.ToNodeId))
|
| 73 |
.ToList();
|
| 74 |
|
| 75 |
-
// 2.
|
| 76 |
if (edgesToDelete.Any())
|
| 77 |
{
|
| 78 |
_context.Edges.RemoveRange(edgesToDelete);
|
| 79 |
-
await _context.SaveChangesAsync();
|
| 80 |
}
|
| 81 |
|
| 82 |
if (nodesToDelete.Any())
|
| 83 |
{
|
| 84 |
_context.TaskNodes.RemoveRange(nodesToDelete);
|
| 85 |
-
await _context.SaveChangesAsync();
|
| 86 |
}
|
| 87 |
|
| 88 |
// 3. Update or Create Nodes
|
|
@@ -114,7 +112,7 @@ namespace FlowAPI.Infrastructure.Repositories
|
|
| 114 |
// 4. Update or Create Edges
|
| 115 |
foreach (var edge in incomingEdges)
|
| 116 |
{
|
| 117 |
-
// Only create edge if it wasn't deleted
|
| 118 |
if (deletedNodeIds.Contains(edge.FromNodeId) || deletedNodeIds.Contains(edge.ToNodeId))
|
| 119 |
{
|
| 120 |
continue;
|
|
@@ -133,6 +131,7 @@ namespace FlowAPI.Infrastructure.Repositories
|
|
| 133 |
}
|
| 134 |
}
|
| 135 |
|
|
|
|
| 136 |
await _context.SaveChangesAsync();
|
| 137 |
return true;
|
| 138 |
}
|
|
|
|
| 72 |
|| deletedNodeIds.Contains(e.ToNodeId))
|
| 73 |
.ToList();
|
| 74 |
|
| 75 |
+
// 2. Queue deletes (but don't save yet)
|
| 76 |
if (edgesToDelete.Any())
|
| 77 |
{
|
| 78 |
_context.Edges.RemoveRange(edgesToDelete);
|
|
|
|
| 79 |
}
|
| 80 |
|
| 81 |
if (nodesToDelete.Any())
|
| 82 |
{
|
| 83 |
_context.TaskNodes.RemoveRange(nodesToDelete);
|
|
|
|
| 84 |
}
|
| 85 |
|
| 86 |
// 3. Update or Create Nodes
|
|
|
|
| 112 |
// 4. Update or Create Edges
|
| 113 |
foreach (var edge in incomingEdges)
|
| 114 |
{
|
| 115 |
+
// Only create edge if it wasn't deleted (i.e. not connected to a deleted node)
|
| 116 |
if (deletedNodeIds.Contains(edge.FromNodeId) || deletedNodeIds.Contains(edge.ToNodeId))
|
| 117 |
{
|
| 118 |
continue;
|
|
|
|
| 131 |
}
|
| 132 |
}
|
| 133 |
|
| 134 |
+
// 5. Single atomic save to minimize lock duration
|
| 135 |
await _context.SaveChangesAsync();
|
| 136 |
return true;
|
| 137 |
}
|
FlowAPI/Program.cs
CHANGED
|
@@ -16,7 +16,7 @@ namespace FlowAPI
|
|
| 16 |
builder.Services.AddDbContext<AppDbContext>(options =>
|
| 17 |
{
|
| 18 |
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
|
| 19 |
-
options.UseSqlite(connectionString);
|
| 20 |
});
|
| 21 |
|
| 22 |
// --- Repositories (Infrastructure) ---
|
|
|
|
| 16 |
builder.Services.AddDbContext<AppDbContext>(options =>
|
| 17 |
{
|
| 18 |
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
|
| 19 |
+
options.UseSqlite(connectionString, o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
|
| 20 |
});
|
| 21 |
|
| 22 |
// --- Repositories (Infrastructure) ---
|