forked from random-access-storage/random-access-idb
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
327 lines (268 loc) · 8.65 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
const RandomAccessStorage = require('random-access-storage')
const isOptions = require('is-options')
const b4a = require('b4a')
const cenc = require('compact-encoding')
const SubEnc = require('sub-encoder')
const once = require('once')
const DEFAULT_PAGE_SIZE = 4 * 1024 // 4096
const BLOCK_SIZE = 512
const DEFAULT_DBNAME = 'random-access-idb'
// dbname: String -> { db: IndexedDB, refs: Number }
const DBS = new Map()
module.exports = class RandomAccessIDB extends RandomAccessStorage {
_indexedDB = window.indexedDB
_dbs = DBS
db = null
dbname = DEFAULT_DBNAME
version = 1
name = null
size = DEFAULT_PAGE_SIZE
length = 0
codecs = {}
static storage (dbname = DEFAULT_DBNAME, opts = {}) {
return function (name, _opts = {}) {
if (isOptions(name)) {
_opts = name
name = _opts.name
}
return new RandomAccessIDB(name, Object.assign({}, opts, _opts, { dbname }))
}
}
constructor (name, opts = {}) {
super()
if (isOptions(name)) {
opts = name
name = opts.name
}
if (opts.size) this.size = opts.size || this.size
if (opts.dbname) this.dbname = opts.dbname || this.dbname
if (opts.version) this.version = opts.version || this.version
if (!name) throw new Error('Must provide name for RandomAccessIDB instance!')
this.name = name
const root = new SubEnc(this.dbname, { keyEncoding: 'utf-8' })
const ns = root.sub(this.name)
const meta = ns.sub('meta')
const page = ns.sub('page', {
keyEncoding: {
encode (obj) { return cenc.encode(cenc.lexint, obj) },
decode (buf) { return cenc.decode(cenc.lexint, buf) }
}
})
this.codecs = { root, ns, meta, page }
}
_open (req) {
const cb = once(req.callback.bind(req))
if (this._dbs.has(this.dbname)) {
const memo = this._dbs.get(this.dbname)
memo.refs++
this.db = memo.db
const store = this._store('readonly')
return this._get(store, 'length', this.codecs.meta, onlen.bind(this))
}
const open = this._indexedDB.open(this.dbname, this.version)
open.onerror = onerror
open.onsuccess = onopen.bind(this)
open.onupgradeneeded = onupgradeneeded.bind(this)
function onerror (e) { return cb(e || open.error) }
function onopen () {
this.db = open.result
this._dbs.set(this.dbname, { db: this.db, refs: 1 })
const store = this._store('readonly')
this._get(store, 'length', this.codecs.meta, onlen.bind(this))
}
function onupgradeneeded (event) {
const db = event.target.result
if (!db.objectStoreNames.contains(this.dbname)) db.createObjectStore(this.dbname)
}
function onlen (err, len = 0) {
if (err) return cb(err)
this.length = len
return cb(null)
}
}
_close (req) {
const cb = req.callback.bind(req)
const memo = this._dbs.get(this.dbname)
if (--memo.refs) return cb(null) // someone in the process still working
// close db and cleanup
this.db.close()
this._dbs.delete(this.dbname)
memo.db = null
return cb(null)
}
_blocks (start, end) {
const { size } = this
const blocks = []
for (let n = Math.floor(start / size) * size; n < end; n += size) {
blocks.push({
page: Math.floor(n / size),
start: Math.max(n, start) % size,
end: Math.min(n + size, end) % size || size
})
}
return blocks
}
_stat (req) {
const cb = req.callback.bind(req)
const store = this._store('readonly')
const st = {
size: this.length,
blksize: this.size,
blocks: 0
}
if (!this.length) return cb(null, st)
const maxidx = Math.floor((this.length - 1) / this.size)
let idx = 0
this._page(store, idx, false, onpage.bind(this))
function onpage (err, page) {
if (err) return cb(err)
st.blocks += (page && page.byteLength) ? Math.ceil(1, Math.floor(page.byteLength / BLOCK_SIZE)) : 0
if (++idx < maxidx) return this._page(store, idx, false, onpage.bind(this))
else return cb(null, st)
}
}
_read (req) {
const cb = req.callback.bind(req)
const store = this._store('readonly')
if ((req.offset + req.size) > this.length) return cb(new Error('Could not satisfy length'))
const blocks = this._blocks(req.offset, req.offset + req.size)
const buffers = new Array(blocks.length)
const fstBlock = blocks.length > 0 ? blocks[0].page : 0
let pending = blocks.length + 1
const onblock = _onblock.bind(this)
for (let i = 0; i < blocks.length; i++) onblock(blocks[i])
return done()
function _onblock (block) {
const { page, start, end } = block
const len = end - start
this._page(store, page, false, (err, buf) => {
if (err) return done(err)
buffers[page - fstBlock] = (buf)
? (len === this.size)
? buf
: b4a.from(buf.subarray(start, end))
: b4a.alloc(len)
return done()
})
}
function done (err) {
if (err) return cb(err)
if (!--pending) return cb(null, b4a.concat(buffers))
}
}
_write (req) {
const cb = req.callback.bind(req)
const { codecs } = this
const length = Math.max(this.length, req.offset + req.size)
const store = this._store('readwrite')
const blocks = this._blocks(req.offset, req.offset + req.size)
const buffers = {}
const done = _done.bind(this)
const onblock = _onblock.bind(this)
let pending = 1
for (const i in blocks) onblock(blocks[i], i)
return done()
function _onblock (block, i) {
const { page, start, end } = block
if (end - start === this.size) return
pending++
this._page(store, page, true, (err, buf) => {
if (err) return done(err)
buffers[i] = buf
return done()
})
}
function _done (err) {
if (err) return cb(err)
if (--pending > 0) return
let j = 0
for (const i in blocks) {
const { page, start, end } = blocks[i]
const len = end - start
const key = codecs.page.encode(page)
if (len === this.size) {
store.put(req.data.slice(j, j += len), key)
} else {
b4a.copy(req.data, buffers[i], start, j, j += len)
store.put(buffers[i], key)
}
}
store.put(length, codecs.meta.encode('length'))
const txn = store.transaction
txn.onabort = () => cb(txn.error)
txn.oncomplete = () => {
this.length = length
cb(null)
}
if (txn.commit) txn.commit()
}
}
_del (req) {
const cb = req.callback.bind(req)
const { codecs } = this
const length = ((req.offset + req.size) >= this.length) ? req.offset : this.length
const store = this._store('readwrite')
if (req.offset >= this.length || req.size === 0) return cb(null)
if (req.size === Infinity) req.size = Math.max(0, this.length - req.offset)
const blocks = this._blocks(req.offset, req.offset + req.size)
const buffers = {}
const fstBlock = blocks.length > 0 ? blocks[0].page : 0
const onblock = _onblock.bind(this)
const done = _done.bind(this)
let pending = 1
for (let i = 0; i < blocks.length; i++) onblock(blocks[i], i)
return done()
function _onblock (block, i) {
const { page, start, end } = block
const len = end - start
if (len === this.size) return
pending++
this._page(store, page, false, (err, buf) => {
if (err || !buf) return done(err)
b4a.fill(buf, 0, start, end)
buffers[page - fstBlock] = buf
return done()
})
}
function _done (err) {
if (err) return cb(err)
if (--pending > 0) return
for (const i in blocks) {
const { page } = blocks[i]
const key = codecs.page.encode(page)
if (buffers[i]) store.put(buffers[i], key)
else store.delete(key)
}
store.put(length, codecs.meta.encode('length'))
const txn = store.transaction
txn.onabort = () => cb(txn.error)
txn.oncomplete = () => {
this.length = length
cb(null)
}
if (txn.commit) txn.commit()
}
}
_store (mode) {
const txn = this.db.transaction([this.dbname], mode)
return txn.objectStore(this.dbname)
}
_get (store, key, codec, cb) {
cb = once(cb)
try {
const req = store.get(codec.encode(key))
req.onerror = (err) => cb(err)
req.onsuccess = () => cb(null, req.result)
} catch (err) {
return cb(err)
}
}
_page (store, key, upsert, cb) {
this._get(store, key, this.codecs.page, (err, page) => {
if (err) return cb(err)
if (page || !upsert) return cb(null, page)
page = b4a.alloc(this.size)
return cb(null, page)
})
}
}