-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathiterator.js
239 lines (194 loc) · 6.37 KB
/
iterator.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
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
231
232
233
234
235
236
237
238
239
'use strict'
const { AbstractIterator } = require('abstract-level')
const createKeyRange = require('./util/key-range')
const deserialize = require('./util/deserialize')
const kCache = Symbol('cache')
const kFinished = Symbol('finished')
const kOptions = Symbol('options')
const kCurrentOptions = Symbol('currentOptions')
const kPosition = Symbol('position')
const kLocation = Symbol('location')
const kFirst = Symbol('first')
const emptyOptions = {}
class Iterator extends AbstractIterator {
constructor (db, location, options) {
super(db, options)
this[kCache] = []
this[kFinished] = this.limit === 0
this[kOptions] = options
this[kCurrentOptions] = { ...options }
this[kPosition] = undefined
this[kLocation] = location
this[kFirst] = true
}
// Note: if called by _all() then size can be Infinity. This is an internal
// detail; by design AbstractIterator.nextv() does not support Infinity.
async _nextv (size, options) {
this[kFirst] = false
if (this[kFinished]) {
return []
}
if (this[kCache].length > 0) {
// TODO: mixing next and nextv is not covered by test suite
size = Math.min(size, this[kCache].length)
return this[kCache].splice(0, size)
}
// Adjust range by what we already visited
if (this[kPosition] !== undefined) {
if (this[kOptions].reverse) {
this[kCurrentOptions].lt = this[kPosition]
this[kCurrentOptions].lte = undefined
} else {
this[kCurrentOptions].gt = this[kPosition]
this[kCurrentOptions].gte = undefined
}
}
let keyRange
try {
keyRange = createKeyRange(this[kCurrentOptions])
} catch (_) {
// The lower key is greater than the upper key.
// IndexedDB throws an error, but we'll just return 0 results.
this[kFinished] = true
return []
}
const transaction = this.db.db.transaction([this[kLocation]], 'readonly')
const store = transaction.objectStore(this[kLocation])
const entries = []
const promise = new Promise(function (resolve, reject) {
// If an error occurs (on the request), the transaction will abort.
transaction.onabort = () => {
reject(transaction.error || new Error('aborted by user'))
}
transaction.oncomplete = () => {
resolve(entries)
}
})
if (!this[kOptions].reverse) {
let keys
let values
const complete = () => {
// Wait for both requests to complete
if (keys === undefined || values === undefined) return
const length = Math.max(keys.length, values.length)
if (length === 0 || size === Infinity) {
this[kFinished] = true
} else {
this[kPosition] = keys[length - 1]
}
// Resize
entries.length = length
// Merge keys and values
for (let i = 0; i < length; i++) {
const key = keys[i]
const value = values[i]
entries[i] = [
this[kOptions].keys ? deserialize(key) : undefined,
this[kOptions].values ? deserialize(value) : undefined
]
}
maybeCommit(transaction)
}
// If keys were not requested and size is Infinity, we don't have to keep
// track of position and can thus skip getting keys.
if (this[kOptions].keys || size < Infinity) {
store.getAllKeys(keyRange, size < Infinity ? size : undefined).onsuccess = (ev) => {
keys = ev.target.result
complete()
}
} else {
keys = []
complete()
}
if (this[kOptions].values) {
store.getAll(keyRange, size < Infinity ? size : undefined).onsuccess = (ev) => {
values = ev.target.result
complete()
}
} else {
values = []
complete()
}
} else {
// Can't use getAll() in reverse, so use a slower cursor that yields one item at a time
// TODO: test if all target browsers support openKeyCursor
const method = !this[kOptions].values && store.openKeyCursor ? 'openKeyCursor' : 'openCursor'
store[method](keyRange, 'prev').onsuccess = (ev) => {
const cursor = ev.target.result
if (cursor) {
const { key, value } = cursor
this[kPosition] = key
entries.push([
this[kOptions].keys && key !== undefined ? deserialize(key) : undefined,
this[kOptions].values && value !== undefined ? deserialize(value) : undefined
])
if (entries.length < size) {
cursor.continue()
} else {
maybeCommit(transaction)
}
} else {
this[kFinished] = true
}
}
}
return promise
}
async _next () {
if (this[kCache].length > 0) {
return this[kCache].shift()
}
if (!this[kFinished]) {
let size = Math.min(100, this.limit - this.count)
if (this[kFirst]) {
// It's common to only want one entry initially or after a seek()
this[kFirst] = false
size = 1
}
this[kCache] = await this._nextv(size, emptyOptions)
// Shift returns undefined if empty, which is what we want
return this[kCache].shift()
}
}
async _all (options) {
this[kFirst] = false
// TODO: mixing next and all is not covered by test suite
const cache = this[kCache].splice(0, this[kCache].length)
const size = this.limit - this.count - cache.length
if (size <= 0) {
return cache
}
let entries = await this._nextv(size, emptyOptions)
if (cache.length > 0) entries = cache.concat(entries)
return entries
}
_seek (target, options) {
this[kFirst] = true
this[kCache] = []
this[kFinished] = false
this[kPosition] = undefined
// TODO: not covered by test suite
this[kCurrentOptions] = { ...this[kOptions] }
let keyRange
try {
keyRange = createKeyRange(this[kOptions])
} catch (_) {
this[kFinished] = true
return
}
if (keyRange !== null && !keyRange.includes(target)) {
this[kFinished] = true
} else if (this[kOptions].reverse) {
this[kCurrentOptions].lte = target
} else {
this[kCurrentOptions].gte = target
}
}
}
exports.Iterator = Iterator
function maybeCommit (transaction) {
// Commit (meaning close) now instead of waiting for auto-commit
if (typeof transaction.commit === 'function') {
transaction.commit()
}
}