-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (115 loc) · 3.52 KB
/
index.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
var EventEmitter = require('events').EventEmitter
var fs = require('fs')
var u = require('./util')
var pullRange = require('pull-stream-range')
var BlockIterator = require('./block-iterator')
//TODO: use bloom filters to remember what keys are present
//to optimize gets.
var open = function (file, cb) {
fs.stat(file, function (err, stat) {
if(err) return cb(err)
fs.open(file, 'r', function (err, fd) {
if(err) return cb(err)
stat.length = Math.ceil(stat.size / stat.blksize)
stat.fd = fd
cb(null, stat)
})
})
}
var exports = module.exports = function (file) {
//if(!cb) cb = it, it = null
var fd, blockSize, blockCount
var emitter = new EventEmitter()
/*
Hmm, so the default case should be an iterator,
and get is just a special-case - it's just an iterator
where start = end. Finding the block that an given key starts in
is the same for an iterator as for a get.
*/
emitter.open = function (cb) {
open(file, function (err, stat) {
if(err) return cb(err)
emitter._stat = stat
emitter.opened = true
cb()
})
}
emitter.location = file
emitter.all = function (opts) {
return BlockIterator(emitter._stat, opts).pipe(u.json(opts.reverse))
}
emitter.iterator = function (opts) {
opts = opts || {}
if(!emitter.opened)
throw new Error('SST is not yet opened!')
var reverse = opts.reverse
var range = [opts.start, opts.end]
if(opts.start && opts.end) range.sort()
if(opts.reverse) range.reverse()
opts.start = range[0] || null
opts.end = range[1] || null
function createStream(i) {
var _opts = u.merge({offset: i}, opts)
if(isNaN(_opts.offset))
throw new Error('i must be a number')
return BlockIterator(emitter._stat, _opts).pipe(u.json(reverse))
}
function compare (a, b) {
return ( a.key < b.key ? -1
: a.key > b.key ? 1
: 0 ) * (reverse ? -1 : 1)
}
var start, end
if(opts.start) start = {key: opts.start}
if(opts.end) end = {key: opts.end}
return pullRange(createStream, compare, emitter._stat.length, start, end)
}
emitter.createReadStream = emitter.iterator
emitter.get = function (key, cb) {
var read = emitter.iterator({start: key, end: key})
read(null, function (err, data) {
//close stream if it's not already closing.
if(!err) read(true, function () {})
cb(err, data && data.value)
})
}
emitter.close = function (cb) {
if(!fd) return cb(new Error('not open'))
fs.close(fd, cb)
}
return emitter
}
var toPull = require('stream-to-pull-stream')
var pull = require('pull-stream')
exports.createStream = function (file, cb) {
var meta = {items: 0, length: 0, meta: true}
return pull(
function (read) {
var max
return function (abort, _cb) {
read(abort, function (end, data) {
if(end) return _cb(end, data)
if(max === undefined) max = data.key
//TODO - FIX toPull to handle errors, and then fix this.
else if(max >= data.key) return cb(new Error('out of order'))
max = data.key; _cb(end, data)
})
}
},
pull.map(function (e) {
var json = JSON.stringify(e) + '\n'
meta.items ++
meta.length += json.length
return json
}),
toPull.sink(
fs.createWriteStream(file)
.on('close', function () {
var sst = exports(file)
sst.open(function (err) {
cb(err, sst)
})
})
)
)
}