-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathds.js
186 lines (163 loc) · 3.61 KB
/
ds.js
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
'use strict'
/* understand/
* We keep all the data structures
* in one location in the hope that
* it will make understanding the
* program easier
*
* "Show me your flowcharts and
* conceal your tables, and I shall
* continue to be mystified. Show
* me your tables, and I won't
* usually need your flowcharts;
* they'll be obvious."
* - Fred Brooks
*/
/* understand/
* All kore data is stored in
* this mega-structure. This is the
* one DS to rule them all.
*/
function KD() {
return {
WHOAMI: null,
SAVETO: null,
CONNECT: null,
LISTEN: null,
AUTHREQ: null,
CHECKREQ: null,
ERR: null,
FLUSH_PERIOD: 0,
REQ_PERIOD: 0,
MIGRATEFN: null,
SYNCHED: false,
UNSYNCHED_LOGS: {},
SEND_SYNC_NOW: false,
LAST_SEND_AT: 0,
LOGS: {},
LOG_PROCESSORS: [],
PROCESSOR_CACHE: {},
SAVEINFO: {},
CONNECT_NODE_INFO: null,
SERVER_PUSH_CONNECTIONS: [],
}
}
/* outcome/
* Return a new log structure
*/
function log(logname) {
return {
name: logname,
shards: {},
}
}
/* outcome/
* Return a new shard structure
*/
function shard(logname, writer) {
return {
log: logname,
writer: writer,
records: [],
}
}
/* outcome/
* Return a new 'saveinfo' for the
* given log.
*/
function saveInfo(logname) {
return {
name: logname,
shardInfo: {},
}
}
/* outcome/
* Return a new 'shardInfo' for the
* given shard
*/
function shardInfo(shard) {
return {
writer: shard.writer,
savedUpto: 0,
lastFlushed: Date.now(),
flushTimer: null,
}
}
/* outcome/
* Set the `kore` record fields in
* the user's record.
*/
function setKoreFields(num, writer, rec) {
rec._korenum = num
rec._korewho = writer
}
/* understand/
* The records are ordered -
* building on top of each other in
* sequence (branching if multiple
* records come in from different
* writers). Each record therefore
* has a built in record 'number'.
*
* outcome/
* Return the latest record
* number - which is the latest
* record written across all writers
* (shards).
*/
function latestRecNum(log) {
let shards = log.shards
let latest = 0
for(let k in shards) {
let l = last_rec_num_1(shards[k])
if(l > latest) latest = l
}
return latest
function last_rec_num_1(shard) {
let len = shard.records.length
if(!len) return 0
return shard.records[len-1]._korenum
}
}
/* problem/
* A basic requirement of `kore`
* is that every record needs to
* be ordered.
* A record also must belong to
* a 'shard' - a set of records
* that belong to a single
* writer. Having shards ensures
* that a node, as it writes,
* does not step on another
* nodes toes. (This is also the
* reason for uniquely
* identifying the node using
* `whoami`).
* Finally, it is useful to have an
* identifier for every record in
* many cases. This is usually an
* auto-generated `id` in other
* databases.
*
* way/
* We will store the `_korenum`
* (record number) and
* the `_korewho` (record writer) in
* every record. We can easily
* combine these to return a record
* id in the `recID()` function:
* <recnum>-<whoami>
*/
function recID(rec) {
return `${rec._korenum}-${rec._korewho}`
}
module.exports = {
KD: KD,
log: log,
shard: shard,
saveInfo: saveInfo,
shardInfo: shardInfo,
setKoreFields: setKoreFields,
latestRecNum: latestRecNum,
recID: recID,
}