forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInMemorySessionFsSqliteHandler.cs
More file actions
261 lines (226 loc) · 9.27 KB
/
Copy pathInMemorySessionFsSqliteHandler.cs
File metadata and controls
261 lines (226 loc) · 9.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using System.Collections.Concurrent;
using GitHub.Copilot;
using GitHub.Copilot.Rpc;
using Microsoft.Data.Sqlite;
namespace GitHub.Copilot.Test.E2E;
internal record SqliteCall(string SessionId, string QueryType, string Query);
/// <summary>
/// A SessionFsProvider that implements <see cref="ISessionFsSqliteProvider"/> with a real
/// in-memory SQLite database, and uses a simple <see cref="ConcurrentDictionary{TKey,TValue}"/>
/// for file operations instead of touching disk.
/// </summary>
internal sealed class InMemorySessionFsSqliteHandler(string sessionId, List<SqliteCall> sqliteCalls)
: SessionFsProvider, ISessionFsSqliteProvider, ISessionFsSqliteTransactionProvider
{
internal ConcurrentDictionary<string, string> Files { get; } = new();
private readonly ConcurrentDictionary<string, byte> _directories = new();
private SqliteConnection? _db;
private SqliteConnection GetOrCreateDb()
{
if (_db is not null)
{
return _db;
}
_db = new SqliteConnection("Data Source=:memory:");
_db.Open();
using var cmd = _db.CreateCommand();
cmd.CommandText = "PRAGMA busy_timeout = 5000";
cmd.ExecuteNonQuery();
return _db;
}
// ---- ISessionFsSqliteProvider ----
public Task<SessionFsSqliteResult?> QueryAsync(
SessionFsSqliteQueryType queryType,
string query,
IDictionary<string, object?>? bindParams,
CancellationToken cancellationToken)
{
return Task.FromResult(RunStatement(GetOrCreateDb(), null, queryType, query, bindParams));
}
public Task<IList<SessionFsSqliteResult>> TransactionAsync(
IList<SessionFsSqliteStatement> statements,
CancellationToken cancellationToken)
{
var db = GetOrCreateDb();
using var transaction = db.BeginTransaction();
try
{
IList<SessionFsSqliteResult> results = statements
.Select(statement => RunStatement(db, transaction, statement.QueryType, statement.Query, statement.Params)
?? new SessionFsSqliteResult())
.ToList();
try
{
transaction.Commit();
}
catch (Exception ex)
{
throw new SessionFsSqliteTransactionException(
ex.Message,
SessionFsSqliteTransactionErrorClass.PostCommitAmbiguous,
ex);
}
return Task.FromResult(results);
}
catch (SessionFsSqliteTransactionException)
{
throw;
}
catch (SqliteException ex)
{
transaction.Rollback();
var errorClass = ex.SqliteErrorCode is 5 or 6
? SessionFsSqliteTransactionErrorClass.BusyOrLocked
: SessionFsSqliteTransactionErrorClass.Fatal;
throw new SessionFsSqliteTransactionException(ex.Message, errorClass, ex);
}
catch (Exception ex)
{
transaction.Rollback();
throw new SessionFsSqliteTransactionException(ex.Message, SessionFsSqliteTransactionErrorClass.Fatal, ex);
}
}
private SessionFsSqliteResult? RunStatement(
SqliteConnection db,
SqliteTransaction? transaction,
SessionFsSqliteQueryType queryType,
string query,
IDictionary<string, object?>? bindParams)
{
sqliteCalls.Add(new SqliteCall(sessionId, queryType.Value, query));
var trimmed = query.Trim();
if (trimmed.Length == 0)
{
return null;
}
if (queryType == SessionFsSqliteQueryType.Exec)
{
using var cmd = db.CreateCommand();
cmd.Transaction = transaction;
cmd.CommandText = trimmed;
cmd.ExecuteNonQuery();
return null;
}
if (queryType == SessionFsSqliteQueryType.Query)
{
using var cmd = db.CreateCommand();
cmd.Transaction = transaction;
cmd.CommandText = trimmed;
AddParams(cmd, bindParams);
using var reader = cmd.ExecuteReader();
var columns = new List<string>();
for (var i = 0; i < reader.FieldCount; i++)
{
columns.Add(reader.GetName(i));
}
var rows = new List<IDictionary<string, object>>();
while (reader.Read())
{
var row = new Dictionary<string, object>(reader.FieldCount);
for (var i = 0; i < reader.FieldCount; i++)
{
row[columns[i]] = reader.IsDBNull(i) ? null! : reader.GetValue(i);
}
rows.Add(row);
}
return new SessionFsSqliteResult
{
Columns = columns,
Rows = rows,
RowsAffected = 0,
};
}
if (queryType == SessionFsSqliteQueryType.Run)
{
using var cmd = db.CreateCommand();
cmd.Transaction = transaction;
cmd.CommandText = trimmed;
AddParams(cmd, bindParams);
var rowsAffected = cmd.ExecuteNonQuery();
using var rowidCmd = db.CreateCommand();
rowidCmd.Transaction = transaction;
rowidCmd.CommandText = "SELECT last_insert_rowid()";
var lastRowid = rowidCmd.ExecuteScalar();
return new SessionFsSqliteResult
{
Columns = [],
Rows = [],
RowsAffected = rowsAffected,
LastInsertRowid = lastRowid is long l ? l : null,
};
}
throw new ArgumentException($"Unknown queryType: {queryType}");
}
public Task<bool> ExistsAsync(CancellationToken cancellationToken)
{
return Task.FromResult(_db is not null);
}
private static void AddParams(SqliteCommand cmd, IDictionary<string, object?>? bindParams)
{
if (bindParams is null) return;
foreach (var (key, value) in bindParams)
{
cmd.Parameters.AddWithValue(key.StartsWith(':') || key.StartsWith('$') || key.StartsWith('@') ? key : $":{key}", value ?? DBNull.Value);
}
}
// ---- File operations (in-memory) ----
private string Resolve(string path) => $"/{sessionId}{(path.StartsWith('/') ? path : "/" + path)}";
protected override Task<string> ReadFileAsync(string path, CancellationToken cancellationToken)
{
var key = Resolve(path);
if (!Files.TryGetValue(key, out var content))
throw new FileNotFoundException($"File not found: {path}");
return Task.FromResult(content);
}
protected override Task WriteFileAsync(string path, string content, int? mode, CancellationToken cancellationToken)
{
Files[Resolve(path)] = content;
return Task.CompletedTask;
}
protected override Task AppendFileAsync(string path, string content, int? mode, CancellationToken cancellationToken)
{
Files.AddOrUpdate(Resolve(path), content, (_, existing) => existing + content);
return Task.CompletedTask;
}
protected override Task<bool> ExistsAsync(string path, CancellationToken cancellationToken)
{
var key = Resolve(path);
return Task.FromResult(Files.ContainsKey(key) || _directories.ContainsKey(key));
}
protected override Task<SessionFsStatResult> StatAsync(string path, CancellationToken cancellationToken)
{
var key = Resolve(path);
if (Files.TryGetValue(key, out var fileContent))
return Task.FromResult(new SessionFsStatResult { IsFile = true, IsDirectory = false, Size = fileContent.Length });
if (_directories.ContainsKey(key))
return Task.FromResult(new SessionFsStatResult { IsFile = false, IsDirectory = true, Size = 0 });
throw new FileNotFoundException($"Path does not exist: {path}");
}
protected override Task MakeDirectoryAsync(string path, bool recursive, int? mode, CancellationToken cancellationToken)
{
_directories[Resolve(path)] = 0;
return Task.CompletedTask;
}
protected override Task<IList<string>> ReadDirectoryAsync(string path, CancellationToken cancellationToken)
=> Task.FromResult<IList<string>>([]);
protected override Task<IList<SessionFsReaddirWithTypesEntry>> ReadDirectoryWithTypesAsync(string path, CancellationToken cancellationToken)
=> Task.FromResult<IList<SessionFsReaddirWithTypesEntry>>([]);
protected override Task RemoveAsync(string path, bool recursive, bool force, CancellationToken cancellationToken)
{
var key = Resolve(path);
Files.TryRemove(key, out _);
_directories.TryRemove(key, out _);
return Task.CompletedTask;
}
protected override Task RenameAsync(string src, string dest, CancellationToken cancellationToken)
{
var srcKey = Resolve(src);
var destKey = Resolve(dest);
if (Files.TryRemove(srcKey, out var content))
Files[destKey] = content;
return Task.CompletedTask;
}
}