-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
177 lines (146 loc) · 4.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'use strict'
const { AbstractLevel } = require('abstract-level')
const ModuleError = require('module-error')
const fsp = require('fs/promises')
const binding = require('./binding')
const { ChainedBatch } = require('./chained-batch')
const { Iterator } = require('./iterator')
const kContext = Symbol('context')
const kLocation = Symbol('location')
class ClassicLevel extends AbstractLevel {
constructor (location, options) {
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
},
signals: {
iterators: true
}
}, options)
this[kLocation] = location
this[kContext] = binding.db_init()
}
get location () {
return this[kLocation]
}
async _open (options) {
if (options.createIfMissing) {
await fsp.mkdir(this[kLocation], { recursive: true })
}
return binding.db_open(this[kContext], this[kLocation], options)
}
async _close () {
return binding.db_close(this[kContext])
}
async _put (key, value, options) {
return binding.db_put(this[kContext], key, value, options)
}
async _get (key, options) {
return binding.db_get(
this[kContext],
key,
encodingEnum(options.valueEncoding),
options.fillCache
)
}
async _getMany (keys, options) {
return binding.db_get_many(this[kContext], keys, options)
}
async _del (key, options) {
return binding.db_del(this[kContext], key, options)
}
async _clear (options) {
return binding.db_clear(this[kContext], options)
}
_chainedBatch () {
return new ChainedBatch(this, this[kContext])
}
async _batch (operations, options) {
return binding.batch_do(this[kContext], operations, options)
}
async approximateSize (start, end, options) {
if (arguments.length < 2) {
throw new TypeError("The arguments 'start' and 'end' are required")
} else if (typeof options !== 'object') {
options = null
}
if (this.status === 'opening') {
return this.deferAsync(() => this.approximateSize(start, end, options))
} else if (this.status !== 'open') {
throw 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)
return binding.db_approximate_size(this[kContext], start, end)
}
}
async compactRange (start, end, options) {
if (arguments.length < 2) {
throw new TypeError("The arguments 'start' and 'end' are required")
} else if (typeof options !== 'object') {
options = null
}
if (this.status === 'opening') {
return this.deferAsync(() => this.compactRange(start, end, options))
} else if (this.status !== 'open') {
throw 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)
return binding.db_compact_range(this[kContext], start, end)
}
}
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 async destroy (location) {
if (typeof location !== 'string' || location === '') {
throw new TypeError("The first argument 'location' must be a non-empty string")
}
return binding.destroy_db(location)
}
static async repair (location) {
if (typeof location !== 'string' || location === '') {
throw new TypeError("The first argument 'location' must be a non-empty string")
}
return binding.repair_db(location)
}
}
exports.ClassicLevel = ClassicLevel
// It's faster to read options in JS than to pass options objects to C++.
const encodingEnum = function (encoding) {
if (encoding === 'buffer') return 0
if (encoding === 'utf8') return 1
/* istanbul ignore else: should not happen */
if (encoding === 'view') return 2
}