-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
181 lines (161 loc) · 4.7 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
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
var stream = require('stream')
var util = require('util')
var leveldb = require('leveldb')
var JSONStream = require('JSONStream')
var async = require('async')
var microtime = require('microtime')
var crypto = require('crypto')
var uuid = require('node-uuid')
function PlumbDB(name, cb) {
var me = this
me.name = name
// make sure stamp prefix sorts later than doc prefix
me.docPrefix = "@"
me.stampPrefix = "\u9999"
leveldb.open(name + ".leveldb", { create_if_missing: true }, loaded)
function loaded(err, db) {
if (db) me.db = db
cb(err, db)
}
}
module.exports = function(name, cb) {
return new PlumbDB(name, cb)
}
module.exports.PlumbDB = PlumbDB
PlumbDB.prototype.destroy = function(cb) {
leveldb.destroy(this.name + '.leveldb', cb)
}
PlumbDB.prototype.get = function(id, cb) {
var me = this
me.db.get(me.docPrefix + id, function (err, stamp) {
if (err) return cb(err)
if (!stamp) return cb(false, null)
me.db.get(me.stampPrefix + stamp, function (err, data) {
if (err) return cb(err)
cb(false, JSON.parse(data))
})
})
}
PlumbDB.prototype.put = function(readStream, cb) {
var me = this
var data = []
readStream.on('data', function(chunk) { data.push(chunk) })
readStream.on('end', function() {
var json = JSON.parse(data.join(''))
me._store(json, cb)
})
readStream.on('error', function(err) { cb(err) })
}
// hack until node-leveldb gets streams
PlumbDB.prototype._getLast = function(cb) {
this.db.iterator(function(err, iterator) {
if (err) return cb(err)
iterator.last(function(err) {
if (err) return cb(err)
iterator.current(function(err, key, val) {
cb(err, key)
})
})
})
}
PlumbDB.prototype.bulk = function(readStream, cb) {
var me = this
var parser = JSONStream.parse(['docs', /./])
var results = []
var doneParsing = false
var error = false
var q = async.queue(function (doc, cb) {
me._store(doc, function(err, stored) {
if (err) return cb(err)
results.push(stored)
cb(false)
})
}, 1)
q.drain = function() { if (doneParsing && !error) cb(false, results) }
readStream.pipe(parser)
parser.on('data', q.push)
parser.on('error', function(err) {
error = true
return cb(err)
})
parser.on('end', function() {
doneParsing = true
if (q.length() === 0 && !error) cb(false, results)
})
}
PlumbDB.prototype._computeRev = function(json) {
json = this._cloneObj(json)
var rev = json._rev
var version = 0
var oldHash = false
if (json._rev) {
version = json._rev.split('-')[0]
oldHash = json._rev.split('-')[1]
}
delete json._rev
var newHash = this._hash(json)
if (newHash === oldHash) return rev
return ++version + '-' + this._hash(json)
}
PlumbDB.prototype._updateMetadata = function(json) {
if (!json._rev) json._stamp = microtime.now() + ""
json._rev = this._computeRev(json)
}
PlumbDB.prototype._hash = function(json) {
return crypto.createHash('md5').update(JSON.stringify(json)).digest("hex")
}
PlumbDB.prototype._dumpAll = function() {
this.db.iterator(function(err, iterator) {
iterator.forRange(function(err, key, val) {
console.log(err, key, val)
})
})
}
PlumbDB.prototype._cloneObj = function(json) {
return JSON.parse(JSON.stringify(json))
}
PlumbDB.prototype._advanceKeySeek = function(key, iterator, readStream) {
var me = this
iterator.current(function(err, k, v) {
if (!k.match(new RegExp('^' + key))) return readStream.emit('end')
readStream.emit('data', JSON.parse(v))
iterator.next(function(err) {
me._advanceKeySeek(key, iterator, readStream)
})
})
}
PlumbDB.prototype.keyStream = function(key) {
var me = this
var readStream = new stream.Stream()
this.db.iterator(function(err, iterator) {
if (err) return readStream.emit('error', err)
iterator.seek(key, function(err) {
if (err) return readStream.emit('error', err)
me._advanceKeySeek(key, iterator, readStream)
})
})
return readStream
}
PlumbDB.prototype._store = function(json, cb) {
var me = this
json = me._cloneObj(json)
if (!json._id) json._id = uuid.v4()
function save(beforePut) {
me._updateMetadata(json)
// todo break out into easy batch function
var batch = new leveldb.Batch
if (beforePut) beforePut(batch)
batch.put(me.stampPrefix + json._stamp, JSON.stringify(json))
batch.put(me.docPrefix + json._id, json._stamp)
me.db.write(batch, done)
}
function done(err) { cb(err, json) }
me.get(json._id, function(err, stored) {
// todo decide how to handle err
if (!stored) return save()
if (stored._rev !== json._rev) return done({conflict: true})
return save(function beforePut(batch) {
batch.del(me.stampPrefix + stored._stamp)
})
})
}