-
Notifications
You must be signed in to change notification settings - Fork 6
/
info.js
170 lines (170 loc) · 4.28 KB
/
info.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
const level = require('level')
const interlevel = require('interlevel')
const kv = level('./.state', {
valueEncoding: 'json'
})
var server
var GENES
const init = function(genes) {
console.log("Info.init")
GENES = genes
return new Promise(function(resolve, reject) {
console.log("# storing genes", GENES)
kv.put('genes', GENES, function(err) {
console.log("Returned")
if (err) {
console.log("Error", err)
reject()
} else {
console.log("Initializing Interlevel Server")
server = interlevel.server({ db: kv, port: 28337 })
console.log("Initialized", server)
resolve()
}
})
})
}
/**
*
* getclock:
* 1. returns the 'from' from the gene if it's first time
* 2. returns the last crawled clock point if it's not the first time
*
*/
const getclock = function(gene) {
let from = gene.from
let addr = gene.address
return new Promise(function(resolve, reject) {
kv.get(addr, function(err, value) {
if (err) {
if (err.notFound) {
// the first time, it's not found, so the clock is set to 'from-1'
// because clock represents the checkpoint to which the last crawl
// has completed
kv.get('tip', function(err, value) {
if (err) {
console.log('Tip not found. Returning the "from":', from-1)
resolve(from-1)
} else {
console.log('Tip found. Returning the "from":', value)
let cp = parseInt(value)
resolve(cp)
}
})
//console.log('Clock not found. Returning the "from":', from)
//resolve(from-1)
} else {
console.log('err', err)
reject(err)
}
} else {
// if already set from previous sessions, return the value
let clk = parseInt(value)
console.log('Clk found,', clk)
resolve(clk)
}
})
})
}
/**
*
* setclock:
* sets clock for the machine specified by the gene
*
*/
const setclock = function(gene, index) {
let addr = gene.address
return new Promise(function(resolve, reject) {
kv.put(addr, index, function(err) {
if (err) {
console.log(err)
reject(err)
} else {
kv.put('clock', index, function(err) {
if (err) console.log(err)
})
resolve()
}
})
})
}
/**
* Return the last synchronized checkpoint
*/
const checkpoint = function() {
return new Promise(async function(resolve, reject) {
kv.get('tip', function(err, value) {
if (err) {
if (err.notFound) {
// start from the min value
console.log('Checkpoint not found, checking for the smallest GENESIS point')
let min
for(let i=0; i<GENES.length; i++) {
let g = GENES[i]
if (i === 0) {
min = g.from
} else {
if (g.from < min) min = g.from
}
console.log(g.from)
}
console.log("Found minimum:", min)
kv.put('clock', min, function(err) {
if (err) console.log(err)
})
resolve(min)
} else {
console.log('err', err)
reject(err)
}
} else {
let cp = parseInt(value)
console.log('Checkpoint found,', cp)
kv.put('clock', cp, function(err) {
if (err) console.log(err)
})
resolve(cp)
}
})
})
}
const updateTip = function(index) {
return new Promise(function(resolve, reject) {
kv.put('tip', index, function(err) {
if (err) {
console.log(err)
reject()
} else {
console.log('Tip updated to', index)
kv.put('clock', index, function(err) {
if (err) console.log(err)
})
resolve()
}
})
})
}
const deleteTip = function() {
return new Promise(function(resolve, reject) {
kv.del('tip', function(err) {
if (err) {
console.log(err)
reject()
} else {
console.log('Tip deleted')
kv.put('clock', null, function(err) {
if (err) console.log(err)
})
resolve()
}
})
})
}
module.exports = {
init: init,
getclock: getclock,
setclock: setclock,
checkpoint: checkpoint,
updateTip: updateTip,
deleteTip: deleteTip
}