forked from Level/rocksdb
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
74 lines (65 loc) · 1.99 KB
/
util.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
'use strict'
exports.kRef = Symbol('ref')
exports.kUnref = Symbol('unref')
function handleMany (sizes, data, options) {
const { valueEncoding } = options ?? {}
data ??= Buffer.alloc(0)
sizes ??= Buffer.alloc(0)
const rows = []
let offset = 0
const sizes32 = new Int32Array(sizes.buffer, sizes.byteOffset, sizes.byteLength / 4)
for (let n = 0; n < sizes32.length; n++) {
const size = sizes32[n]
const encoding = valueEncoding
if (size < 0) {
rows.push(undefined)
} else {
if (!encoding || encoding === 'buffer') {
rows.push(data.subarray(offset, offset + size))
} else if (encoding === 'slice') {
rows.push({ buffer: data, byteOffset: offset, byteLength: size })
} else {
rows.push(data.toString(encoding, offset, offset + size))
}
offset += size
if (offset & 0x7) {
offset = (offset | 0x7) + 1
}
}
}
return rows
}
function handleNextv (err, sizes, buffer, finished, options, callback) {
const { keyEncoding, valueEncoding } = options ?? {}
if (err) {
callback(err)
} else {
buffer ??= Buffer.alloc(0)
sizes ??= Buffer.alloc(0)
const rows = []
let offset = 0
const sizes32 = new Int32Array(sizes.buffer, sizes.byteOffset, sizes.byteLength / 4)
for (let n = 0; n < sizes32.length; n++) {
const size = sizes32[n]
const encoding = n & 1 ? valueEncoding : keyEncoding
if (size < 0) {
rows.push(undefined)
} else {
if (!encoding || encoding === 'buffer') {
rows.push(buffer.subarray(offset, offset + size))
} else if (encoding === 'slice') {
rows.push({ buffer, byteOffset: offset, byteLength: size })
} else {
rows.push(buffer.toString(encoding, offset, offset + size))
}
offset += size
if (offset & 0x7) {
offset = (offset | 0x7) + 1
}
}
}
callback(null, rows, finished)
}
}
exports.handleMany = handleMany
exports.handleNextv = handleNextv