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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
package.cpath = package.cpath .. ";../../target/debug/?.so"
package.path = package.path .. ";../../lua-bindings/?.lua"
local dia = require("diaphragm")
-- TODO: add a way to have modified prototypes of these functions
-- e.g.: local my_blocks = Blocks:new({ default_color = my_color })
-- my_blocks:new({ elements = ... }):draw()
dia.util.eprint("--------------------")
-- TODO: Fanwood has a high baseline, which renders weirdly in the GroupCommit layer,
-- and "Goudy Bookletter 1911" doesn't work
-- local serif_family = "Fanwood"
-- local serif_family = "Latin Modern Math"
-- local serif_family = "Goudy Bookletter 1911 Normal"
local serif_family = "OFL Sorts Mill Goudy"
local mono_family = "Fira Mono"
local active_color = "#5959ff"
local light_gray = "#cccccc"
local dark_gray = "#7f7f7f"
local block_unit_width = 24
local block_height = 36
local block_stroke_width = 2
local normal_margin = 20
local small_margin = 10
local function blocks(params)
local result = dia.shape(params)
result.unit_width = result.unit_width or dia.float.new()
result.default_color = result.default_color or light_gray
result.default_stroke_width = result.default_stroke_width or block_stroke_width
result.blocks = dia.util.tbl_map(function(el)
return dia.rectangle.new({
fill_color = el.color or result.default_color,
stroke_width = result.default_stroke_width,
height = result.height,
width = result.unit_width * (el.grow or 1),
})
end, params.elements)
function result:draw()
local len = #self.elements
assert(len >= 1, "Blocks must have at least 1 element")
local total_grow = 0
for _, el in pairs(self.elements) do
total_grow = total_grow + (el.grow or 1)
end
dia.constrain(self.width:eq(self.unit_width * total_grow))
dia.layout
.hstack({
elements = self.blocks,
top_left = self.top_left,
bottom_right = self.bottom_right,
width = self.width,
})
:draw()
end
return result
end
local function active_txn(params)
return blocks(dia.util.tbl_extend({
elements = { {}, {}, {}, {}, {} },
height = block_height,
default_color = active_color,
}, params))
end
local function fixed_blocks(params)
local elements = {}
for _ = 1, params.count do
table.insert(elements, {})
end
return blocks({
elements = elements,
height = block_height,
unit_width = block_unit_width,
default_color = params.color or dark_gray,
})
end
local function block_list(params)
local result = dia.shape(params)
result.delimiter_height = result.delimiter_height or dia.float.new()
result.font = result.font or dia.text.font({ family = serif_family })
function result:draw()
local opening_delimiter = dia.text.new({
content = "[",
height = self.delimiter_height,
font = self.font,
})
local closing_delimiter = dia.text.new({
content = "]",
height = self.delimiter_height,
font = self.font,
})
local function comma()
return dia.text.new({
content = ", ",
font = self.font,
})
end
local elements = dia.util.list_intersperse_with(comma, self.elements)
elements = { opening_delimiter, table.unpack(elements) }
table.insert(elements, closing_delimiter)
-- TODO: not really elegant
dia.constrain(self.delimiter_height:eq(self.elements[1].height * 2.5))
dia.layout
.hstack({
elements = elements,
align = "center",
top_left = self.top_left,
bottom_right = self.bottom_right,
})
:draw()
end
return result
end
local function disk_log_legend(params)
local result = dia.shape(params)
local function lines(params)
local result = dia.shape(params)
result.ticks = {}
for _, tick in ipairs(params.ticks) do
table.insert(result.ticks,
dia.straight_path.new({
points = {
{ x = tick, y = result.top },
{ x = tick, y = result.bottom },
}
}))
end
result.baseline = dia.straight_path.new({
points = { result.middle_left, result.middle_right },
width = result.width,
})
function result:draw()
for _, tick in ipairs(self.ticks) do
tick:draw()
end
self.baseline:draw()
end
return result
end
result.lines = lines({
ticks = params.ticks,
top_left = result.top_left,
height = result.height / 2,
width = result.width,
})
result.font = dia.text.font({ family = serif_family })
result.labels = {}
for i = 1, #params.ticks - 1 do
local label = dia.text.new({
content = params.labels[i],
height = (result.height / 2) + result.height / 6,
top = result.lines.bottom - result.height / 6,
horiz_center = (params.ticks[i] + params.ticks[i + 1]) / 2,
font = result.font,
})
table.insert(result.labels, label)
end
function result:draw()
self.lines:draw()
for _, label in ipairs(self.labels) do
label:draw()
end
end
return result
end
local function disk_log(params)
local result = dia.shape(params)
local anchor_ids = {}
local last_group_id = 0
local labels = {}
local elements = {}
for _, global_spec in pairs(params.elements) do
for _, spec in pairs(global_spec.elements) do
local spec_params = spec.params or {}
for _ = 1, spec.count do
table.insert(elements, spec_params)
end
last_group_id = last_group_id + spec.count
end
table.insert(anchor_ids, last_group_id)
table.insert(labels, global_spec.label)
end
result.blocks = blocks({
elements = elements,
height = block_height,
top_left = result.top_left,
width = result.width,
})
result.anchors = { result.left }
for _, id in pairs(anchor_ids) do
table.insert(result.anchors, result.blocks.blocks[id].right)
end
result.legend = disk_log_legend({
ticks = result.anchors,
labels = labels,
height = normal_margin * 2,
width = result.width,
bottom_right = result.bottom_right,
})
dia.constraint.below(result.legend, result.blocks, normal_margin)
function result:draw()
result.blocks:draw()
result.legend:draw()
end
return result
end
local function layer_entry(params)
local result = dia.shape(params)
result.text = params.text or dia.text.new({
content = params.name .. ": ",
font = params.font,
})
result.text_vert_center = params.text_vert_center or params.content.vert_center
function result:draw()
dia.constraint.left_of(self.text, self.content)
dia.constrain(self.text.vert_center:eq(self.text_vert_center))
dia.constrain(self.left:eq(self.text.left))
dia.constrain(self.right:eq(self.content.right))
dia.constrain(self.top:eq(dia.float.min({ self.text.top, self.content.top })))
dia.constrain(self.bottom:eq(dia.float.max({ self.text.bottom, self.content.bottom })))
self.text:draw()
self.content:draw()
end
return result
end
local function layer(params)
local result = dia.shape(params)
result.name = result.name or "Layer"
result.title = dia.text.new({
content = result.name,
font = result.title_font,
top_left = result.top_left + { x = normal_margin, y = normal_margin },
})
result.content = result.content or dia.rectangle.new({
height = 100,
width = 100,
})
function result:draw()
dia.rectangle
.new({
top_left = self.top_left,
bottom_right = self.bottom_right,
fill_color = "#fff",
})
:draw()
dia.constrain(self.right:eq(self.content.right + normal_margin))
dia.constraint.below(self.content, self.title)
dia.constrain(self.left:lt(self.content.left - 150))
dia.constrain(self.bottom:eq(dia.float.max({ self.title.bottom, self.content.bottom }) + normal_margin))
self.title:draw()
self.content:draw()
end
return result
end
dia.draw({
content = {
draw = function()
local title_font = dia.text.font({ family = serif_family, size = 50000 })
local entry_font = dia.text.font({ family = mono_family, size = 20000 })
local action_font = dia.text.font({ family = mono_family, height = normal_margin })
local function action(name)
return dia.layout.margin_top({
margin_top = small_margin,
content = dia.text.new({
content = '<span foreground="' .. active_color .. '">' .. name .. "</span>",
font = action_font,
height = normal_margin,
}),
})
end
local layer_right = dia.float.new()
local log_layer_entry_left = dia.float.new()
local group_commit_entry_left = dia.float.new()
local disk_log_entry_left = dia.float.new()
local layers = {
layer({
name = "LogAPI",
title_font = title_font,
-- top_left = { x = normal_margin, y = normal_margin },
top_left = { x = normal_margin, y = normal_margin },
content = dia.layout.margin_right({
content = layer_entry({
name = "activeTxn",
font = entry_font,
content = active_txn({
unit_width = block_unit_width,
left = log_layer_entry_left,
}),
}),
}),
}),
action("commit"),
layer({
name = "GroupCommit",
title_font = title_font,
-- top_left = { x = 20, y = 100 + 40 },
-- right = layer_right,
content = dia.layout.margin_right({
-- HACK: because I know that "GroupCommit" + "commitedTxns:"
-- takes the most space
margin_right = 0,
content = layer_entry({
name = "commitedTxns",
font = entry_font,
content = block_list({
elements = {
fixed_blocks({ count = 2 }),
fixed_blocks({ count = 7 }),
fixed_blocks({ count = 4 }),
active_txn({ unit_width = block_unit_width }),
},
left = group_commit_entry_left,
}),
}),
}),
}),
action("flush"),
layer({
name = "DiskLog",
title_font = title_font,
-- top_left = { x = 20, y = 100 + 40 },
right = layer_right,
content = dia.layout.margin_right({
margin_right = 0,
content = layer_entry({
-- TODO: align to blocks
name = "disk log",
font = entry_font,
content = disk_log({
elements = {
{
-- TODO: seems to fail solving if using other font size within text, or line returns
label = "header",
elements = {
{ count = 1, params = { color = "#000", grow = 11 } },
},
},
{
label = "data",
elements = {
{ count = 2, params = { color = light_gray, grow = 13 + 5 } },
{ count = 13, params = { color = dark_gray } },
{ count = 5, params = { color = active_color } },
},
},
{
label = "available",
elements = {
{ count = 1, params = { color = "#fff", grow = 13 + 5 } },
},
},
},
left = disk_log_entry_left,
}),
}),
}),
}),
action("apply"),
layer({
name = "Applier",
title_font = title_font,
right = layer_right,
}),
}
dia.layout.vstack({ elements = layers, align = "right" }):draw()
local layer_entries_text_right = dia.float.new()
for _, entry_left in pairs({ log_layer_entry_left, group_commit_entry_left, disk_log_entry_left }) do
dia.constrain(layer_entries_text_right:eq(entry_left))
end
end,
},
output = {},
})
-- TODO: anyone can use metatable?
--[[
local Blocks = diaphragm.Drawable:new()
Blocks.elements = {}
Blocks.unit_width = diaphragm.float()
function Blocks:draw()
local len = #self.elements
assert(len >= 1, "Blocks must have at least 1 element")
local total_grow = 0
for el in self.elements do
total_grow = el.grow or 1
end
diaphragm.constrain(self.width == self.unit_width * total_grow)
local param = self.elements[0]
-- TODO: this would be written with layout.hstack
local block = diaphragm.shape.Rectangle:new()
block.fill(param.color)
diaphragm.constrain(block.left() == self.left())
diaphragm.constrain(block.top() == self.top())
diaphragm.constrain(block.bottom() == self.bottom())
diaphragm.constrain(block.width() == (param.grow or 1) * self.unit_width)
block.draw()
local previous_right = block.right()
for i = 2, len do
param = self.elements[i]
block = diaphragm.shape.Rectangle:new()
block.fill(param.color)
diaphragm.constrain(block.left() == previous_right)
diaphragm.constrain(block.top() == self.top())
diaphragm.constrain(block.bottom() == self.bottom())
diaphragm.constrain(block.width() == (param.grow or 1) * self.unit_width)
block.draw()
previous_right = block.right()
end
end
-- Library
----------
local function block(color)
local res = diaphragm.shape.Rectangle:new()
res.fill(color)
return res
end
local function blocks_by_len(count, color)
local res = {}
for i = 1, count do
res[i] = block(color)
end
-- TODO: rename cons? Misleading
diaphragm.cons.same_size(res)
return diaphragm.layout.hstack(res)
end
local function layer(config)
local name = config.name or ""
local entries = config.entries or {}
-- TODO: how to do it without box?
-- TODO: implement union or combine
-- probably combine because
-- union could remove some
-- strokes
return diaphragm.layout.box({})
end
-- Parameters
-------------
local blue = diaphragm.color.from_rgb(0.35, 0.35, 1.)
local active_txn_count = 5
-- Log API
----------
local log_api = layer({
name = "LogAPI",
entries = {
{
name = "activeTxn",
content = blocks_by_len(active_txn_count, blue),
},
},
})
-- Group Commit
---------------
-- local group_commit = nil
-- Disk Log
-----------
-- local disk_log = nil
-- Applier
----------
-- local applier = nil
diaphragm.draw(diaphragm.layout.vstack({
log_api,
-- group_commit,
-- disk_log,
-- applier,
})) ]]
|