-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmulti.js
62 lines (53 loc) · 1.37 KB
/
multi.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
//instead of one single hashtable,
//use a series of increasing size
//so that we do not need to rebuild the table.
module.exports = function (createHashtable, tables) {
var count = 0, slots = 0
if(Buffer.isBuffer(tables)) {
var buffer = tables
tables = []
var start = 0
while(start < buffer.length) {
var slots = buffer.readUInt32BE(start)
var end = start + 8 + slots * 4
tables.push(createHashtable(buffer.slice(start, end)))
start = end
}
}
for(var i = 0; i < tables.length; i++) {
count += tables[i].count
slots += tables[i].slots
}
var self
return self = {
count: count,
slots: slots,
get: function (key, cb) {
;(function next (i) {
tables[i].get(key, function (err, value, seq) {
if(value) cb(null, value, seq)
else if(i) next(i-1)
else cb(err)
})
})(tables.length-1)
},
add: function (key, index) {
var last = tables[tables.length-1]
if(last.load() >= 0.5) {
tables.push(last = createHashtable(last.slots*2))
self.slots = slots += last.slots
}
if(last.add(key, index)) {
self.count = ++ count
return true
}
return false
},
load: function () {
return count/slots
},
buffer: function () {
return tables.map(function (e) { return e.buffer })
}
}
}