-
Notifications
You must be signed in to change notification settings - Fork 1
/
BitemporalGenerator.cs
445 lines (420 loc) · 16.9 KB
/
BitemporalGenerator.cs
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
public class Entity { public string? Name; public List<(string Name, string Type)> Fields = new(); }
[Generator]
public class BitemporalGenerator : ISourceGenerator
{
public void Initialize(GeneratorInitializationContext context) { }
public void Execute(GeneratorExecutionContext context)
{
var namespaceName = context.Compilation.AssemblyName!;
foreach (var file in context.AdditionalFiles)
{
if (file.Path.EndsWith(".bitemporal"))
{
var archiveName = Path.GetFileNameWithoutExtension(file.Path);
var text = file.GetText();
if (text is null) throw new Exception(file.Path);
var code = ArchiveText(namespaceName, archiveName, Parse(text));
//System.Diagnostics.Debugger.Launch();
context.AddSource(archiveName + ".cs", code);
}
}
}
List<Entity> Parse(SourceText text)
{
var entities = new List<Entity>();
int line = 0;
var lines = text.Lines;
while (line < lines.Count)
{
var l = text.GetSubText(lines[line++].Span).ToString();
if (!string.IsNullOrWhiteSpace(l))
{
var entity = new Entity { Name = l };
while (line < lines.Count)
{
var field = text.GetSubText(lines[line++].Span).ToString();
if (string.IsNullOrWhiteSpace(field)) break;
int i = field.IndexOf(' ');
var fieldName = field.Substring(0, i);
var fieldType = field.Substring(i + 1);
entity.Fields.Add((fieldName, fieldType));
}
entities.Add(entity);
}
}
return entities;
}
public static string ArchiveText(string namespaceName, string archiveName, List<Entity> entities)
{
var sb = new StringBuilder(@"using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using Bitemporal;
namespace ").A(namespaceName).A(@"
{
public class ").A(archiveName).A(@"
{
uint[] Counts;
readonly SetSlim<string> Text;");
for (int e = 0; e < entities.Count; e++)
{
var fieldCount = entities[e].Fields.Count;
for (int f = 0; f < fieldCount; f++)
sb.A(@"
DataSeries[] Entity").A(e).A(@"_Field").A(f).A(@";");
}
sb.A(@"
public ").A(archiveName).A(@"()
{
Counts = new uint[").A(entities.Count).A(@"];
Text = new();");
for (int e = 0; e < entities.Count; e++)
{
var fieldCount = entities[e].Fields.Count;
for (int f = 0; f < fieldCount; f++)
sb.A(@"
Entity").A(e).A(@"_Field").A(f).A(@" = Array.Empty<DataSeries>();");
}
sb.A(@"
}
public ").A(archiveName).A(@"(string filename)
{
using var s = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
Counts = Serialize.UIntArrayGet(s);
Text = Serialize.SetSlimStringGet(s);");
for (int e = 0; e < entities.Count; e++)
{
var fieldCount = entities[e].Fields.Count;
for (int f = 0; f < fieldCount; f++)
sb.A(@"
Entity").A(e).A(@"_Field").A(f).A(@" = Serialize.DataSeriesArrayGet(s, Counts[").A(e).A(@"]);");
}
sb.A(@"
}
public void Save(string filename)
{
using var s = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None);
Serialize.UIntArraySet(s, Counts);
Serialize.SetSlimStringSet(s, Text);");
for (int e = 0; e < entities.Count; e++)
{
var fieldCount = entities[e].Fields.Count;
for (int f = 0; f < fieldCount; f++)
sb.A(@"
Serialize.DataSeriesArraySet(s, Entity").A(e).A(@"_Field").A(f).A(@");");
}
sb.A(@"
}
public Snapshot SnapshotLatest => new(this, Counts, new(Counts[0]-1U));
public Snapshot SnapshotAt(TxId txId)
{
var newCounts = new uint[").A(entities.Count).A(@"];
Array.Copy(Counts, 0, newCounts, 0, ").A(entities.Count).A(@");
var i = txId.I;
newCounts[0] = i;");
for (int e = 1; e < entities.Count; e++)
{
sb.A(@"
i = newCounts[").A(e).A(@"];
while (Entity").A(e).A(@"_Field0[--i].After(txId)) { };
newCounts[").A(e).A(@"] = i;");
}
sb.A(@"
return new(this, newCounts, txId);
}
public class Snapshot
{
public readonly ").A(archiveName).A(@" Archive;
public readonly uint[] Counts;
public readonly TxId TxId;
public TxId NextTxId => new(TxId.I + 1U);
SetSlim<string>? Text;");
for (int e = 0; e < entities.Count; e++)
{
var fieldCount = entities[e].Fields.Count;
for (int f = 0; f < fieldCount; f++)
sb.A(@"
internal MapSlim<uint, DataSeries>? Entity").A(e).A(@"_Field").A(f).A(@";");
}
sb.A(@"
internal Snapshot(").A(archiveName).A(@" archive, uint[] counts, TxId txId)
{
Archive = archive;
Counts = counts;
TxId = txId;
}
public SnapshotDate this[Date date] => new(this, date);
internal Vint AddString(string s)
{
var i = Archive.Text.IndexOf(s);
if (i >= 0) return new(i);
return new((Text ??= new()).Add(s) + Archive.Text.Count);
}
internal string GetString(Vint v)
{
return v.I < Archive.Text.Count ? Archive.Text[(int)v.I] : Text![(int)v.I - Archive.Text.Count];
}
public void Commit(");
var fields0 = entities[0].Fields;
var fieldCount0 = fields0.Count;
sb.A(fields0[0].Type).A(@" ").A(fields0[0].Name.ToLower());
for (int f = 1; f < fieldCount0; f++)
if (!fields0[f].Type.EndsWith(" Set"))
sb.A(@", ").A(fields0[f].Type).A(@" ").A(fields0[f].Name.ToLower());
sb.A(@")
{
this[time.ToDate()].TxCollection.New(");
sb.A(fields0[0].Name.ToLower());
for (int f = 1; f < fieldCount0; f++)
if (!fields0[f].Type.EndsWith(" Set"))
sb.A(@", ").A(fields0[f].Name.ToLower());
sb.A(@");
var newCounts = new uint[").A(entities.Count).A(@"];
Array.Copy(Counts, 0, newCounts, 0, ").A(entities.Count).A(@");
if(Text is not null)
{
for(int i = 0; i < Text.Count; i++)
Archive.Text.Add(Text[i]);
}");
for (int e = 0; e < entities.Count; e++)
{
var fieldCount = entities[e].Fields.Count;
for (int f = 0; f < fieldCount; f++)
{
sb.A(@"
if (Entity").A(e).A(@"_Field").A(f).A(@" is not null)
{
var maxID = 0U;
for(int i = Entity").A(e).A(@"_Field").A(f).A(@".Count - 1; i >= 0; i--)
{
var k = Entity").A(e).A(@"_Field").A(f).A(@".Key(i);
if(k > maxID) maxID = k;
}
if (Archive.Entity").A(e).A(@"_Field").A(f).A(@".Length <= maxID)
{
var a = new DataSeries[maxID + 1];
Array.Copy(Archive.Entity").A(e).A(@"_Field").A(f).A(@", 0, a, 0, Archive.Entity").A(e).A(@"_Field").A(f).A(@".Length);
Archive.Entity").A(e).A(@"_Field").A(f).A(@" = a;
newCounts[").A(e).A(@"] = maxID + 1;
}
foreach (var kv in Entity").A(e).A(@"_Field").A(f).A(@")
{
Archive.Entity").A(e).A(@"_Field").A(f).A(@"[kv.Key] = kv.Value;
}
}");
}
}
sb.A(@"
Archive.Counts = newCounts;
}
}
public class SnapshotDate
{
public readonly Snapshot Snapshot;
public readonly Date Date;
internal SnapshotDate(Snapshot snapshot, Date date)
{
Snapshot = snapshot;
Date = date;
}");
for (int e = 0; e < entities.Count; e++)
{
sb.A(@"
public ").A(entities[e].Name).A(@"Collection ").A(entities[e].Name).A(@"Collection => new(this);");
}
sb.A(@"
}");
for (int e = 0; e < entities.Count; e++)
{
if(e != 0)
sb.A(@"
public struct ").A(entities[e].Name).A(@"Id
{
public readonly uint I;
public ").A(entities[e].Name).A(@"Id(uint id) => I = id;
}
");
sb.A(@"
public struct ").A(entities[e].Name).A(@"Collection : IReadOnlyCollection<").A(entities[e].Name).A(@">
{
public readonly SnapshotDate SD;
public ").A(entities[e].Name).A(@"Collection(SnapshotDate sd) => SD = sd;
public IEnumerator<").A(entities[e].Name).A(@"> GetEnumerator() => Entities().GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => Entities().GetEnumerator();
IEnumerable<").A(entities[e].Name).A(@"> Entities()
{
var i = SD.Snapshot.Counts[").A(e).A(@"];
while (i > 0) yield return new(SD, new(--i));
}
public ").A(entities[e].Name).A(@" this[").A(entities[e].Name).A(@"Id id] => new(SD, id);");
var fields = entities[e].Fields;
var fieldCount = fields.Count;
sb.A(@"
public int Count => (int)SD.Snapshot.Counts[").A(e).A(@"];
public ").A(entities[e].Name).A(@" New(");
sb.A(fields[0].Type).A(@" ").A(fields[0].Name.ToLower());
for (int f = 1; f < fieldCount; f++)
if (!fields[f].Type.EndsWith(" Set"))
sb.A(@", ").A(fields[f].Type).A(@" ").A(fields[f].Name.ToLower());
sb.A(@")
{
var sd = SD;
var id = sd.Snapshot.Counts[").A(e).A(@"];
if (sd.Snapshot.Entity").A(e).A(@"_Field0 is null)
{");
for (int f = 0; f < fieldCount; f++) sb.A(@"
sd.Snapshot.Entity").A(e).A(@"_Field").A(f).A(@" = new();");
sb.A(@"
}
else
{
var m = sd.Snapshot.Entity").A(e).A(@"_Field0.Key(sd.Snapshot.Entity").A(e).A(@"_Field0.Count - 1);
if (m >= id) id = m + 1;
}");
for (int f = 0; f < fieldCount; f++)
{
if (fields[f].Type.EndsWith(" Set"))
sb.A(@"
sd.Snapshot.Entity").A(e).A(@"_Field").A(f).A(@"![id] = default;");
else
sb.A(@"
sd.Snapshot.Entity").A(e).A(@"_Field").A(f).A(@"![id] = DataSeries.Single(sd.Date, sd.Snapshot.NextTxId, ").ToVint(entities, fields[f].Type, fields[f].Name.ToLower()).A(@");");
}
sb.A(@"
return new(sd, new(id));
}");
sb.A(@"
}
public struct ").A(entities[e].Name).A(@"
{
readonly SnapshotDate SD;
public readonly ").A(entities[e].Name).A(@"Id Id;
public ").A(entities[e].Name).A(@"(SnapshotDate query, ").A(entities[e].Name).A(@"Id id)
{
SD = query;
Id = id;
}");
for (int f = 0; f < fieldCount; f++)
{
var field = fields[f];
sb.A(@"
DataSeries ").A(field.Name).A(@"_DataSeries
{
get
{
var sd = SD;
int i;
return sd.Snapshot.Entity").A(e).A(@"_Field").A(f).A(@" is not null
&& (i = sd.Snapshot.Entity").A(e).A(@"_Field").A(f).A(@".IndexOf(Id.I)) != -1
? sd.Snapshot.Entity").A(e).A(@"_Field").A(f).A(@".Value(i)
: sd.Snapshot.Archive.Entity").A(e).A(@"_Field").A(f).A(@"[Id.I];
}
set
{
(SD.Snapshot.Entity").A(e).A(@"_Field").A(f).A(@" ??= new())[Id.I] = value;
}
}
");
if (field.Type.EndsWith(" Set"))
{
sb.A(@"
public struct PositionsSet : IEnumerable<Position>
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(""Style"", ""IDE0044: Add readonly modifier"")]
Asset E;
public PositionsSet(Asset entity) => E = entity;
IEnumerable<Position> Items()
{
var sd = E.SD;
return E.Positions_DataSeries.SetGet(sd.Date, sd.Snapshot.TxId).Select(i => new Position(sd, new((uint)i.I)));
}
public IEnumerator<Position> GetEnumerator() => Items().GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => Items().GetEnumerator();
public void Add(Position p)
{
var sd = E.SD;
E.Positions_DataSeries = E.Positions_DataSeries.SetAdd(sd.Date, sd.Snapshot.NextTxId, new(p.Id.I));
}
public void Remove(Position p)
{
var sd = E.SD;
E.Positions_DataSeries = E.Positions_DataSeries.SetRemove(sd.Date, sd.Snapshot.NextTxId, new(p.Id.I));
}
}
public PositionsSet Positions => new(this);");
}
else
{
sb.A(@"
public ").A(field.Type).A(@" ").A(field.Name).A(@"
{
get
{
var sd = SD;
var ds = ").A(field.Name).A(@"_DataSeries;
return ").ToType(entities, field.Type, "ds[sd.Date, sd.Snapshot.TxId].Item3").A(@";
}
set
{
var sd = SD;
").A(field.Name).A(@"_DataSeries = ").A(field.Name).A(@"_DataSeries.Add(sd.Date, sd.Snapshot.NextTxId, ").ToVint(entities, field.Type, "value").A(@");
}
}
public IEnumerable<(Date, TxId, ").A(field.Type).A(@")> ").A(field.Name).A(@"_Audit
{
get
{
var sd = SD;
var date = sd.Date;
var txId = sd.Snapshot.TxId;
return ").A(field.Name).A(@"_DataSeries
.Where(i => i.Item1 <= date && i.Item2 <= txId)
.Select(i => (i.Item1, i.Item2, ").ToType(entities, field.Type, "i.Item3").A(@"));
}
}
public (Date, TxId, ").A(field.Type).A(@") ").A(field.Name).A(@"_Detail
{
get
{
var sd = SD;
var row = ").A(field.Name).A(@"_DataSeries[sd.Date, sd.Snapshot.TxId];
return (row.Item1, row.Item2, ").ToType(entities, field.Type, "row.Item3").A(@");
}
}");
}
}
sb.A(@"
}");
}
sb.A(@"
}
}");
return sb.ToString();
}
}
public static class StringBuilderExtensions
{
public static StringBuilder A(this StringBuilder sb, string? s) => sb.Append(s);
public static StringBuilder A(this StringBuilder sb, int i) => sb.Append(i);
public static StringBuilder ToVint(this StringBuilder sb, List<Entity> entities, string type, string name)
{
return entities.Any(i => i.Name == type) ? sb.Append("new(").Append(name).Append(@".Id.I)")
: type == "string" ? sb.Append("sd.Snapshot.AddString(").Append(name).Append(@")")
: sb.Append(name).Append(@".ToVint()");
}
public static StringBuilder ToType(this StringBuilder sb, List<Entity> entities, string type, string name)
{
return entities.Any(i => i.Name == type) ? sb.Append("new ").Append(type).Append(@"(sd, new((uint)").Append(name).Append(@".I))")
: type == "string" ? sb.Append("sd.Snapshot.GetString(").Append(name).Append(@")")
: sb.Append(name).Append(@".To").Append(type).Append(@"()");
}
}