This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
154 lines (126 loc) · 3.72 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
'use strict'
var AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
var AbstractChainedBatch = require('abstract-leveldown').AbstractChainedBatch
var AbstractIterator = require('abstract-leveldown').AbstractIterator
var inherits = require('inherits')
var Codec = require('level-codec')
var EncodingError = require('level-errors').EncodingError
module.exports = DB.default = DB
function DB (db, opts) {
if (!(this instanceof DB)) return new DB(db, opts)
AbstractLevelDOWN.call(this, '')
opts = opts || {}
if (typeof opts.keyEncoding === 'undefined') opts.keyEncoding = 'utf8'
if (typeof opts.valueEncoding === 'undefined') opts.valueEncoding = 'utf8'
this.db = db
this.codec = new Codec(opts)
}
inherits(DB, AbstractLevelDOWN)
DB.prototype._serializeKey =
DB.prototype._serializeValue = function (datum) {
return datum
}
DB.prototype._open = function (opts, cb) {
this.db.open(opts, cb)
}
DB.prototype._close = function (cb) {
this.db.close(cb)
}
DB.prototype._put = function (key, value, opts, cb) {
key = this.codec.encodeKey(key, opts)
value = this.codec.encodeValue(value, opts)
this.db.put(key, value, opts, cb)
}
DB.prototype._get = function (key, opts, cb) {
var self = this
key = this.codec.encodeKey(key, opts)
opts.asBuffer = this.codec.valueAsBuffer(opts)
this.db.get(key, opts, function (err, value) {
if (err) return cb(err)
try {
value = self.codec.decodeValue(value, opts)
} catch (err) {
return cb(new EncodingError(err))
}
cb(null, value)
})
}
DB.prototype._del = function (key, opts, cb) {
key = this.codec.encodeKey(key, opts)
this.db.del(key, opts, cb)
}
DB.prototype._chainedBatch = function () {
return new Batch(this)
}
DB.prototype._batch = function (ops, opts, cb) {
ops = this.codec.encodeBatch(ops, opts)
this.db.batch(ops, opts, cb)
}
DB.prototype._iterator = function (opts) {
opts.keyAsBuffer = this.codec.keyAsBuffer(opts)
opts.valueAsBuffer = this.codec.valueAsBuffer(opts)
return new Iterator(this, opts)
}
DB.prototype.approximateSize = function (start, end, opts, cb) {
start = this.codec.encodeKey(start, opts)
end = this.codec.encodeKey(end, opts)
return this.db.approximateSize(start, end, opts, cb)
}
function Iterator (db, opts) {
AbstractIterator.call(this, db)
this.codec = db.codec
this.keys = opts.keys
this.values = opts.values
this.opts = this.codec.encodeLtgt(opts)
this.it = db.db.iterator(this.opts)
}
inherits(Iterator, AbstractIterator)
Iterator.prototype._next = function (cb) {
var self = this
this.it.next(function (err, key, value) {
if (err) return cb(err)
try {
if (self.keys && typeof key !== 'undefined') {
key = self.codec.decodeKey(key, self.opts)
} else {
key = undefined
}
if (self.values && typeof value !== 'undefined') {
value = self.codec.decodeValue(value, self.opts)
} else {
value = undefined
}
} catch (err) {
return cb(new EncodingError(err))
}
cb(null, key, value)
})
}
Iterator.prototype._seek = function (key) {
key = this.codec.encodeKey(key, this.opts)
this.it.seek(key)
}
Iterator.prototype._end = function (cb) {
this.it.end(cb)
}
function Batch (db, codec) {
AbstractChainedBatch.call(this, db)
this.codec = db.codec
this.batch = db.db.batch()
}
inherits(Batch, AbstractChainedBatch)
Batch.prototype._put = function (key, value) {
key = this.codec.encodeKey(key)
value = this.codec.encodeValue(value)
this.batch.put(key, value)
}
Batch.prototype._del = function (key) {
key = this.codec.encodeKey(key)
this.batch.del(key)
}
Batch.prototype._clear = function () {
this.batch.clear()
}
Batch.prototype._write = function (opts, cb) {
this.batch.write(opts, cb)
}