forked from Level/rocksdb
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathiterator.js
More file actions
230 lines (181 loc) · 4.91 KB
/
iterator.js
File metadata and controls
230 lines (181 loc) · 4.91 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
'use strict'
const { fromCallback } = require('catering')
const { AbstractIterator } = require('abstract-level')
const assert = require('node:assert')
const { kRef, kUnref } = require('./util')
const binding = require('./binding')
const kPromise = Symbol('promise')
const kDB = Symbol('db')
const kContext = Symbol('context')
const kCache = Symbol('cache')
const kFinished = Symbol('finished')
const kFirst = Symbol('first')
const kPosition = Symbol('position')
const kBusy = Symbol('busy')
const kEmpty = Object.freeze([])
class Iterator extends AbstractIterator {
constructor (db, context, options) {
super(db, options)
this[kContext] = binding.iterator_init_sync(context, options)
this[kFirst] = true
this[kCache] = kEmpty
this[kFinished] = false
this[kPosition] = 0
this[kDB] = db
this[kBusy] = false
}
[Symbol.asyncDispose] () {
return this.close()
}
_seek (target) {
this._seekSync(target)
}
_close (callback) {
return this._closeAsync(callback)
}
_end (callback) {
this._close(callback)
}
// Undocumented, exposed for tests only
get cached () {
return (this[kCache].length - this[kPosition]) / 2
}
_next (callback) {
assert(this[kContext])
assert(!this[kBusy])
if (this[kPosition] < this[kCache].length) {
const key = this[kCache][this[kPosition]++]
const val = this[kCache][this[kPosition]++]
process.nextTick(callback, null, key, val)
} else if (this[kFinished]) {
process.nextTick(callback)
} else {
const size = this[kFirst] ? 1 : 1000
this[kFirst] = false
try {
const { rows, finished } = binding.iterator_nextv_sync(this[kContext], size, null)
this[kCache] = rows
this[kFinished] = finished
this[kPosition] = 0
setImmediate(() => this._next(callback))
} catch (err) {
process.nextTick(callback, err)
}
}
return this
}
_nextv (size, options, callback) {
assert(this[kContext])
assert(!this[kBusy])
callback = fromCallback(callback, kPromise)
this._nextvAsync(size, options, (err, val) => {
if (err) {
callback(err)
} else {
const { rows, finished } = val
const entries = []
for (let n = 0; n < rows.length; n += 2) {
entries.push([rows[n + 0], rows[n + 1]])
}
callback(null, entries, finished)
}
})
return callback[kPromise]
}
// nxt API
_seekSync (target) {
assert(this[kContext])
assert(!this[kBusy])
if (target.length === 0) {
throw new Error('cannot seek() to an empty target')
}
this[kFirst] = true
this[kCache] = kEmpty
this[kFinished] = false
this[kPosition] = 0
binding.iterator_seek_sync(this[kContext], target)
}
_seekAsync (target, callback) {
assert(this[kContext])
assert(!this[kBusy])
callback = fromCallback(callback, kPromise)
this[kFirst] = true
this[kCache] = kEmpty
this[kFinished] = false
this[kPosition] = 0
try {
this[kDB][kRef]()
this[kBusy] = true
binding.iterator_seek(this[kContext], target, (err) => {
this[kBusy] = false
this[kDB][kUnref]()
if (err) {
callback(err)
} else {
callback(null)
}
})
} catch (err) {
this[kBusy] = false
this[kDB][kUnref]()
process.nextTick(callback, err)
}
return callback[kPromise]
}
_nextvSync (size, options) {
assert(this[kContext])
assert(!this[kBusy])
if (this[kFinished]) {
return { rows: [], finished: true }
}
const result = binding.iterator_nextv_sync(this[kContext], size, options)
this[kFinished] = result.finished
return result
}
_nextvAsync (size, options, callback) {
assert(this[kContext])
assert(!this[kBusy])
callback = fromCallback(callback, kPromise)
try {
if (this[kFinished]) {
process.nextTick(callback, null, { rows: [], finished: true })
} else {
this[kDB][kRef]()
this[kBusy] = true
binding.iterator_nextv(this[kContext], size, options, (err, result) => {
this[kBusy] = false
this[kDB][kUnref]()
if (err) {
callback(err)
} else {
this[kFinished] = result.finished
callback(null, result)
}
})
}
} catch (err) {
this[kBusy] = false
this[kDB][kUnref]()
process.nextTick(callback, err)
}
return callback[kPromise]
}
_closeSync () {
this[kCache] = kEmpty
if (this[kContext]) {
binding.iterator_close_sync(this[kContext])
this[kContext] = null
}
}
_closeAsync (callback) {
callback = fromCallback(callback, kPromise)
try {
this._closeSync()
process.nextTick(callback)
} catch (err) {
process.nextTick(callback, err)
}
return callback[kPromise]
}
}
exports.Iterator = Iterator