-
Notifications
You must be signed in to change notification settings - Fork 4
/
medeadown.js
148 lines (112 loc) · 3.19 KB
/
medeadown.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
var fs = require('fs')
, AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
, keydir = require('keydir')
, medea = require('medea')
, open = require('leveldown-open')
, MedeaIterator = require('./medeaiterator')
, MedeaDOWN = function (location) {
if (!(this instanceof MedeaDOWN))
return new MedeaDOWN(location)
AbstractLevelDOWN.call(this, location)
this.db = medea()
this.keys = keydir()
}
, setImmediate = global.setImmediate || process.nextTick
require('util').inherits(MedeaDOWN, AbstractLevelDOWN)
MedeaDOWN.prototype._open = function (options, callback) {
var self = this
open(this.location, options, function (err) {
if (err)
return callback(err)
self.db.open(self.location, function (err) {
if (err)
return callback(err)
self.db.listKeys(function (err, keys) {
keys.forEach(function (key) {
self.keys.put(key)
})
callback()
})
})
})
}
MedeaDOWN.prototype._close = function (callback) {
this.db.close(callback)
}
MedeaDOWN.prototype._put = function (key, value, options, callback) {
var self = this
this.db.put(key, value, function (err) {
if (err)
return callback(err)
self.keys.put(key)
callback()
})
}
MedeaDOWN.prototype._del = function (key, options, callback) {
var self = this
if (!this.keys.has(key))
return setImmediate(callback)
this.db.remove(key, function (err) {
if (err)
return callback(err)
self.keys.del(key)
callback()
})
}
MedeaDOWN.prototype._createBatch = function (array) {
var self = this
, batch = this.db.createBatch()
array.forEach(function (operation, idx) {
for(var idx2 = idx + 1; idx2 < array.length; ++idx2)
if (array[idx2].key === operation.key)
return
var key = self._serializeKey(operation.key)
var value = self._serializeValue(operation.value)
if (operation.type === 'put')
batch.put(key, value)
else if (self.keys.has(operation.key))
batch.remove(key)
})
return batch
}
MedeaDOWN.prototype._batch = function (array, options, callback) {
if (array.length === 0)
return setImmediate(callback)
var self = this
, batch = this._createBatch(array)
this.db.write(batch, function (err) {
if (err)
return callback(err)
array.forEach(function (operation) {
if (operation.type === 'put')
self.keys.put(operation.key)
else
self.keys.del(operation.key)
})
callback()
})
}
MedeaDOWN.prototype._get = function (key, options, callback) {
var self = this
, asBuffer = options.asBuffer !== false
this.db.get(key, function (err, value) {
if (!self.keys.has(key)) {
callback(new Error('NotFound'))
return
}
if (!err && value === undefined) {
callback(err, asBuffer ? new Buffer('') : '')
return
}
if (value && !asBuffer)
value = value.toString()
callback(err, value)
})
}
MedeaDOWN.prototype._iterator = function (options) {
return new MedeaIterator(this, options)
}
MedeaDOWN.destroy = function (location, callback) {
medea.destroy(location, callback)
}
module.exports = MedeaDOWN