-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapp.js
More file actions
423 lines (381 loc) · 13.1 KB
/
app.js
File metadata and controls
423 lines (381 loc) · 13.1 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
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
window.onload = function(){
// Mapping of DOM table id to data keys corresponding to table columns
var column_keys = {
'log': ['lsn', 'txn_id', 'type', 'page_id', 'prev_lsn'],
'dirty-page-table': ['page_id', 'rec_lsn'],
'transaction-table': ['txn_id', 'txn_status', 'last_lsn'],
'buffer-pool': ['page_id', 'page_lsn', 'value'],
'disk': ['page_id', 'page_lsn', 'value']
};
// Helper function to convert an object map to a list
function mapToList(map, id_name) {
var list = [];
for (var key of Object.keys(map)) {
var obj = {};
obj[id_name] = key;
for (var field of Object.keys(map[key])) {
obj[field] = map[key][field];
}
list.push(obj);
}
return list;
}
// Given a DOM table id and data list/object, join the new data to the
// DOM table using D3 joins. If prev_data is not undefined, then any
// changes between the previous and new data should be highlighted.
function updateTable(table_id, data, prev_data) {
var columns = column_keys[table_id];
var key = columns[0];
if (!Array.isArray(data)) {
data = mapToList(data, key);
if (prev_data != null) {
prev_data = mapToList(prev_data, key);
}
}
var updated_rows = d3.select('table#' + table_id)
.select('tbody')
.selectAll('tr')
.data(data, function(d) {
return d ? d[key] : this.id;
})
.attr('class', function(d, i) {
if (prev_data != null) {
for (var objs of prev_data) {
if (objs[key] == this.id) {
return '';
}
}
return 'new-row';
}
return '';
});
updated_rows.exit().remove();
var cells = updated_rows.selectAll('td')
.data(function(row) {
return columns.map(function(column) {
return row[column];
});
})
.attr('class', function(d, i) {
if (prev_data != null) {
for (var objs of prev_data) {
if (objs[key] == this.parentNode.id) {
if (objs[columns[i]] !== d) {
d3.select(this.parentNode)
.attr('class', 'updated-row');
return 'updated-cell';
}
return '';
}
}
}
return '';
})
.text(function(d) { return d; });
var new_rows = updated_rows.enter()
.append('tr')
.attr('id', function(d) {
return d[key]
})
.attr('class', function(d, i) {
if (prev_data != null) {
for (var objs of prev_data) {
if (objs[key] == this.id) {
return '';
}
}
return 'new-row';
}
return '';
});
new_rows.selectAll('td')
.data(function(row) {
return columns.map(function(column) {
return row[column];
});
})
.enter()
.append('td')
.attr('class', function(d, i) {
if (prev_data != null) {
for (var objs of prev_data) {
if (objs[key] == this.parentNode.id) {
if (objs[columns[i]] !== d) {
d3.select(this.parentNode)
.attr('class', 'updated-row');
return 'updated-cell';
}
return '';
}
}
}
return '';
})
.text(function(d) { return d; });
}
// Given a DOM span id and datum value, assign the new datum value to the
// DOM span using D3 data bindings
function updateValue(span_id, datum) {
d3.select('span#' + span_id)
.datum(datum)
.text(function(d) {
if (d != -1) {
return d;
}
});
}
// Given the number of operations processed and position of the log record
// being analyzed, renders a pointer on the commands list and log to
// indicate the current position on each respectively.
function updatePointers(num_ops_processed, log_position) {
if (num_ops_processed != -1) {
d3.select('#commands')
.selectAll("li")
.attr('class','');
d3.select('#commands')
.selectAll("li")
.filter(function(d, i) { return i == num_ops_processed; })
.attr('class','current-op');
var commandEle = document.getElementById('commands');
commandEle.scrollTop = commandEle.children[0].offsetHeight * (num_ops_processed -2);
}
if (log_position != -1) {
d3.select('#log')
.selectAll("tr")
.filter(function(d, i) { return i == log_position; })
.attr('class','current-row');
}
}
// Given a list of explanations, renders them in the explanation box
function updateExplanation(explanation) {
var update = d3.select('#explanation')
.selectAll('li')
.data(explanation);
update.text(function(d) { return d; });
update.enter()
.append('li')
.text(function(d) { return d; });
update.exit().remove();
}
// Given a new phase, update the style of the UI
function updateStyle(phase) {
d3.select('span#phase')
.attr('class', phase);
d3.select('body')
.attr('class', phase);
d3.selectAll('.panel')
.attr('class', 'panel ' + phase);
}
// Given a new state index, enable/disable the left and right arrows
function updateArrows(stateIdx) {
if (stateIdx == 0) {
d3.select('#left-arrow')
.attr('disabled', true);
} else if (stateIdx == states.length - 1) {
d3.select('#right-arrow')
.attr('disabled', true);
} else {
d3.select('#left-arrow')
.attr('disabled', null);
d3.select('#right-arrow')
.attr('disabled', null);
}
}
// Given a new state, update the UI to reflect the state
function onStateChanged(stateIdx) {
var state = states[stateIdx];
var prevState = {};
if (stateIdx > 0) {
prevState = states[stateIdx - 1];
}
updateArrows(stateIdx);
updateTable('log', state.log, prevState.log);
updateTable('dirty-page-table', state.dirty_page_table, prevState.dirty_page_table);
updateTable('transaction-table', state.txn_table, prevState.txn_table);
updateTable('buffer-pool', state.buffer_pool, prevState.buffer_pool);
updateTable('disk', state.disk, prevState.disk);
updateValue('lsn-flushed', state.num_flushed - 1);
updateValue('phase', state.phase);
updatePointers(state.num_ops_processed, state.log_position);
updateExplanation(state.explanation);
updateStyle(state.phase);
}
// Update the current UI mode
function updateMode(newMode) {
mode = newMode; // sets global 'mode' variable
// Toggle the correct mode button
d3.select('div#modes')
.selectAll('button')
.attr('class', null);
d3.select('div#modes')
.selectAll('button#' + mode)
.attr('class', 'hidden');
// Toggle the correct sub controls
d3.select('div#sub-controls')
.selectAll('div')
.attr('class', 'hidden');
d3.select('div#sub-controls')
.select('div#sub-controls-' + mode)
.attr('class', null);
// Enable/disable editing the command list
if (mode === 'run') {
command_list.option('disabled', true);
d3.select('ul#commands').attr('class', null);
resetAries();
} else {
command_list.option('disabled', false);
d3.select('ul#commands')
.attr('class', 'editable')
.selectAll("li")
.attr('class','');
}
}
// Append a new command to the current command list
function appendCommand(command) {
var el = document.createElement('li');
el.innerHTML = '<span>' + command + '</span>' +
'<i class="fa fa-times remove"></i>' +
'<i class="fa fa-pencil edit"></i>';
el.dataset.id = command;
command_list.el.appendChild(el);
command_list.save();
}
// Parse input commands and reset ARIES state
function resetAries() {
var input = command_list.toArray().join(',');
var ops_result = aries.Op.parse_ops(input);
if (!ops_result.status) {
console.assert(false, Parsimmon.formatError(input, ops_result));
}
var ops = ops_result.value;
// Start the ARIES simulator.
states = aries.simulate(ops);
currStateIdx = 0;
onStateChanged(currStateIdx);
}
// Renders previous state
function renderPrevState() {
if (currStateIdx > 0) {
onStateChanged(--currStateIdx);
}
}
// Renders next state
function renderNextState() {
if (currStateIdx < states.length - 1) {
onStateChanged(++currStateIdx);
}
}
/**
* Event listeners
*/
// Set UI to run mode
var run_button = document.getElementById('run');
run_button.addEventListener('click', function(event) {
updateMode("run");
}, false);
// Set UI to edit mode
var edit_button = document.getElementById('edit');
edit_button.addEventListener('click', function(event) {
updateMode("edit");
}, false);
// Transition between states when left or right arrow keys are pressed
document.addEventListener('keydown', function(event) {
if (mode === "run") {
if (event.keyCode === 37) { // left arrow
renderPrevState();
} else if (event.keyCode === 39) { // right arrow
renderNextState();
}
}
}, false);
// Transition to previous state when left arrow button is clicked
var left_arrow = document.getElementById('left-arrow');
left_arrow.addEventListener('click', function(event) {
if (mode === "run") {
renderPrevState();
}
}, false);
// Transition to next state when right arrow button is clicked
var right_arrow = document.getElementById('right-arrow');
right_arrow.addEventListener('click', function(event) {
if (mode === "run") {
renderNextState();
}
}, false);
// Open a new command dialog when new command button is clicked
var new_command = document.getElementById('new-command');
new_command.addEventListener('click', function(event) {
vex.dialog.open({
message: 'New command:',
input: '<input name="command" type="text" required>',
callback: function(data) {
if (data) {
appendCommand(data.command);
}
}
});
}, false);
// Sortable element containing list of commands
var commands_el = document.getElementById('commands');
var command_list = Sortable.create(commands_el, {
filter: '.edit, .remove',
onFilter: function (evt) {
if (Sortable.utils.is(evt.target, ".remove")) {
evt.item.parentNode.removeChild(evt.item);
command_list.save();
} else if (Sortable.utils.is(evt.target, ".edit")) {
vex.dialog.open({
message: 'Edit command:',
input: '<input name="command" type="text" value="' +
evt.item.dataset.id + '" required>',
callback: function(data) {
if (data) {
evt.item.firstChild.innerHTML = data.command;
evt.item.dataset.id = data.command;
command_list.save();
}
}
});
}
}
});
// About content modal
var about = document.getElementById('about');
about.addEventListener('click', function(event) {
event.preventDefault();
var modalContent = d3.select('#about-content').html();
vex.dialog.alert({ unsafeMessage: modalContent });
}, false);
/**
* Initializer
*/
// UI global variables
var states, // list of ARIES states
currStateIdx, // index of the current state
mode; // UI mode, either "run" or "edit"
// Initializes the UI
function init() {
var starter_commands = [
"W_1(A, one)",
"W_2(B, two)",
"Flush(B)",
"W_3(C, three)",
"Flush(C)",
"Checkpoint()",
"W_2(D, four)",
"W_1(A, five)",
"Commit_1()",
"W_3(C, six)",
"W_2(D, seven)",
"Flush(D)",
"W_2(B, eight)",
"W_3(A, nine)"
];
for (var command of starter_commands) {
appendCommand(command);
}
// set UI to run mode
updateMode("run");
}
init();
}