forked from staltz/leveldown-nodejs-mobile
-
Notifications
You must be signed in to change notification settings - Fork 1
/
leveldown.js
155 lines (123 loc) · 4.18 KB
/
leveldown.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
'use strict'
const util = require('util')
const AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
const binding = require('./binding')
const ChainedBatch = require('./chained-batch')
const Iterator = require('./iterator')
function LevelDOWN (location) {
if (!(this instanceof LevelDOWN)) {
return new LevelDOWN(location)
}
if (typeof location !== 'string') {
throw new Error('constructor requires a location string argument')
}
AbstractLevelDOWN.call(this, {
bufferKeys: true,
snapshots: true,
permanence: true,
seek: true,
clear: true,
createIfMissing: true,
errorIfExists: true,
additionalMethods: {
approximateSize: true,
compactRange: true
}
})
this.location = location
this.context = binding.db_init()
}
util.inherits(LevelDOWN, AbstractLevelDOWN)
LevelDOWN.prototype._open = function (options, callback) {
binding.db_open(this.context, this.location, options, callback)
}
LevelDOWN.prototype._close = function (callback) {
binding.db_close(this.context, callback)
}
LevelDOWN.prototype._serializeKey = function (key) {
return Buffer.isBuffer(key) ? key : String(key)
}
LevelDOWN.prototype._serializeValue = function (value) {
return Buffer.isBuffer(value) ? value : String(value)
}
LevelDOWN.prototype._put = function (key, value, options, callback) {
binding.db_put(this.context, key, value, options, callback)
}
LevelDOWN.prototype._get = function (key, options, callback) {
binding.db_get(this.context, key, options, callback)
}
LevelDOWN.prototype._del = function (key, options, callback) {
binding.db_del(this.context, key, options, callback)
}
LevelDOWN.prototype._chainedBatch = function () {
return new ChainedBatch(this)
}
LevelDOWN.prototype._batch = function (operations, options, callback) {
binding.batch_do(this.context, operations, options, callback)
}
LevelDOWN.prototype.approximateSize = function (start, end, callback) {
if (start == null ||
end == null ||
typeof start === 'function' ||
typeof end === 'function') {
throw new Error('approximateSize() requires valid `start` and `end` arguments')
}
if (typeof callback !== 'function') {
throw new Error('approximateSize() requires a callback argument')
}
start = this._serializeKey(start)
end = this._serializeKey(end)
binding.db_approximate_size(this.context, start, end, callback)
}
LevelDOWN.prototype.compactRange = function (start, end, callback) {
if (start == null ||
end == null ||
typeof start === 'function' ||
typeof end === 'function') {
throw new Error('compactRange() requires valid `start` and `end` arguments')
}
if (typeof callback !== 'function') {
throw new Error('compactRange() requires a callback argument')
}
start = this._serializeKey(start)
end = this._serializeKey(end)
binding.db_compact_range(this.context, start, end, callback)
}
LevelDOWN.prototype.getProperty = function (property) {
if (typeof property !== 'string') {
throw new Error('getProperty() requires a valid `property` argument')
}
return binding.db_get_property(this.context, property)
}
LevelDOWN.prototype._iterator = function (options) {
if (this.status !== 'open') {
// Prevent segfault
throw new Error('cannot call iterator() before open()')
}
return new Iterator(this, options)
}
LevelDOWN.destroy = function (location, callback) {
if (arguments.length < 2) {
throw new Error('destroy() requires `location` and `callback` arguments')
}
if (typeof location !== 'string') {
throw new Error('destroy() requires a location string argument')
}
if (typeof callback !== 'function') {
throw new Error('destroy() requires a callback function argument')
}
binding.destroy_db(location, callback)
}
LevelDOWN.repair = function (location, callback) {
if (arguments.length < 2) {
throw new Error('repair() requires `location` and `callback` arguments')
}
if (typeof location !== 'string') {
throw new Error('repair() requires a location string argument')
}
if (typeof callback !== 'function') {
throw new Error('repair() requires a callback function argument')
}
binding.repair_db(location, callback)
}
module.exports = LevelDOWN