git ssb

0+

Dominic / flumeview-hashtable



Tree: 65f914096658ba9cb91db127af9d60c8b7c2cd51

Files: 65f914096658ba9cb91db127af9d60c8b7c2cd51 / hashtable.js

1224 bytesRaw
1function NotFound (key) {
2 var err = new Error('Not Found:'+key)
3 err.notFound = true
4 return err
5}
6
7module.exports = function (hash, matches, get) {
8 return function (buffer, slots) {
9 var self
10 var size = 4
11 var count = 0
12 if(!buffer) {
13 buffer = new Buffer(size*slots)
14 buffer.fill(0)
15 }
16 else {
17 slots = buffer.length/size
18 count = buffer.readUInt32BE(0)
19 }
20 function _get (i) {
21 return buffer.readUInt32BE((i%slots)*size)
22 }
23 function _set (i, v) {
24 buffer.writeUInt32BE(v, (i%slots)*size)
25 }
26
27 return self = {
28 get: function (key, cb) {
29 ;(function next (i) {
30 var k = _get(i)
31 if(k === 0)
32 cb(NotFound(key))
33 else
34 get(k, function (err, data) {
35 if(err) cb(err)
36 else if(matches(data, key)) cb(null, data)
37 else next(i + 1)
38 })
39 })(hash(key))
40 },
41 _get: _get,
42 add: function (key, value) {
43 var i = hash(key)
44 while(_get(i) !== 0) i++
45 //buffer.writeUInt32BE(value, (i%slots)*size)
46 _set(i, value)
47 self.count = ++count
48 return i
49 },
50 buffer: buffer
51 }
52 }
53}
54

Built with git-ssb-web