forked from Level/classic-level
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
190 lines (157 loc) · 5.49 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
182
183
184
185
186
187
188
189
190
'use strict'
const { AbstractLevel } = require('abstract-level')
const ModuleError = require('module-error')
const { fromCallback } = require('catering')
const fs = require('fs')
const binding = require('./binding')
const { ChainedBatch } = require('./chained-batch')
const { Iterator } = require('./iterator')
const kPromise = Symbol('promise')
const kContext = Symbol('context')
const kLocation = Symbol('location')
class ClassicLevel extends AbstractLevel {
constructor (location, options, _) {
// To help migrating to abstract-level
if (typeof options === 'function' || typeof _ === 'function') {
throw new ModuleError('The levelup-style callback argument has been removed', {
code: 'LEVEL_LEGACY'
})
}
if (typeof location !== 'string' || location === '') {
throw new TypeError("The first argument 'location' must be a non-empty string")
}
super({
encodings: {
buffer: true,
utf8: true,
view: true
},
seek: true,
createIfMissing: true,
errorIfExists: true,
additionalMethods: {
approximateSize: true,
compactRange: true
}
}, options)
this[kLocation] = location
this[kContext] = binding.db_init()
}
get location () {
return this[kLocation]
}
_open (options, callback) {
if (options.createIfMissing) {
fs.mkdir(this[kLocation], { recursive: true }, (err) => {
if (err) return callback(err)
binding.db_open(this[kContext], this[kLocation], options, callback)
})
} else {
binding.db_open(this[kContext], this[kLocation], options, callback)
}
}
_close (callback) {
binding.db_close(this[kContext], callback)
}
_put (key, value, options, callback) {
binding.db_put(this[kContext], key, value, options, callback)
}
_get (key, options, callback) {
binding.db_get(this[kContext], key, options, callback)
}
_getMany (keys, options, callback) {
binding.db_get_many(this[kContext], keys, options, callback)
}
_del (key, options, callback) {
binding.db_del(this[kContext], key, options, callback)
}
_clear (options, callback) {
binding.db_clear(this[kContext], options, callback)
}
_chainedBatch () {
return new ChainedBatch(this, this[kContext])
}
_batch (operations, options, callback) {
binding.batch_do(this[kContext], operations, options, callback)
}
approximateSize (start, end, options, callback) {
if (arguments.length < 2 || typeof start === 'function' || typeof end === 'function') {
throw new TypeError("The arguments 'start' and 'end' are required")
} else if (typeof options === 'function') {
callback = options
options = null
} else if (typeof options !== 'object') {
options = null
}
callback = fromCallback(callback, kPromise)
if (this.status === 'opening') {
this.defer(() => this.approximateSize(start, end, options, callback))
} else if (this.status !== 'open') {
this.nextTick(callback, new ModuleError('Database is not open: cannot call approximateSize()', {
code: 'LEVEL_DATABASE_NOT_OPEN'
}))
} else {
const keyEncoding = this.keyEncoding(options && options.keyEncoding)
start = keyEncoding.encode(start)
end = keyEncoding.encode(end)
binding.db_approximate_size(this[kContext], start, end, callback)
}
return callback[kPromise]
}
compactRange (start, end, options, callback) {
if (arguments.length < 2 || typeof start === 'function' || typeof end === 'function') {
throw new TypeError("The arguments 'start' and 'end' are required")
} else if (typeof options === 'function') {
callback = options
options = null
} else if (typeof options !== 'object') {
options = null
}
callback = fromCallback(callback, kPromise)
if (this.status === 'opening') {
this.defer(() => this.compactRange(start, end, options, callback))
} else if (this.status !== 'open') {
this.nextTick(callback, new ModuleError('Database is not open: cannot call compactRange()', {
code: 'LEVEL_DATABASE_NOT_OPEN'
}))
} else {
const keyEncoding = this.keyEncoding(options && options.keyEncoding)
start = keyEncoding.encode(start)
end = keyEncoding.encode(end)
binding.db_compact_range(this[kContext], start, end, callback)
}
return callback[kPromise]
}
getProperty (property) {
if (typeof property !== 'string') {
throw new TypeError("The first argument 'property' must be a string")
}
// Is synchronous, so can't be deferred
if (this.status !== 'open') {
throw new ModuleError('Database is not open', {
code: 'LEVEL_DATABASE_NOT_OPEN'
})
}
return binding.db_get_property(this[kContext], property)
}
_iterator (options) {
return new Iterator(this, this[kContext], options)
}
static destroy (location, callback) {
if (typeof location !== 'string' || location === '') {
throw new TypeError("The first argument 'location' must be a non-empty string")
}
callback = fromCallback(callback, kPromise)
binding.destroy_db(location, callback)
return callback[kPromise]
}
static repair (location, callback) {
if (typeof location !== 'string' || location === '') {
throw new TypeError("The first argument 'location' must be a non-empty string")
}
callback = fromCallback(callback, kPromise)
binding.repair_db(location, callback)
return callback[kPromise]
}
}
exports.ClassicLevel = ClassicLevel