forked from Level/rocksdb
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chained-batch.js
150 lines (114 loc) · 3.38 KB
/
chained-batch.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
'use strict'
const { AbstractChainedBatch } = require('abstract-level')
const binding = require('./binding')
const ModuleError = require('module-error')
const { fromCallback } = require('catering')
const assert = require('node:assert')
const kBatchContext = Symbol('batchContext')
const kDbContext = Symbol('dbContext')
const kPromise = Symbol('promise')
const EMPTY = {}
class ChainedBatch extends AbstractChainedBatch {
constructor (db, context) {
super(db)
this[kDbContext] = context
this[kBatchContext] = binding.batch_init()
}
_put (key, value, options) {
assert(this[kBatchContext])
if (key === null || key === undefined) {
throw new ModuleError('Key cannot be null or undefined', {
code: 'LEVEL_INVALID_KEY'
})
}
if (value === null || value === undefined) {
throw new ModuleError('value cannot be null or undefined', {
code: 'LEVEL_INVALID_VALUE'
})
}
key = typeof key === 'string' ? Buffer.from(key) : key
value = typeof value === 'string' ? Buffer.from(value) : value
binding.batch_put(this[kBatchContext], key, value, options ?? EMPTY)
}
_del (key, options) {
assert(this[kBatchContext])
if (key === null || key === undefined) {
throw new ModuleError('Key cannot be null or undefined', {
code: 'LEVEL_INVALID_KEY'
})
}
key = typeof key === 'string' ? Buffer.from(key) : key
binding.batch_del(this[kBatchContext], key, options ?? EMPTY)
}
_clear () {
assert(this[kBatchContext])
binding.batch_clear(this[kBatchContext])
}
_write (options, callback) {
assert(this[kBatchContext])
callback = fromCallback(callback, kPromise)
try {
this._writeSync(options)
process.nextTick(callback, null)
} catch (err) {
process.nextTick(callback, err)
}
return callback[kPromise]
}
_writeSync (options) {
assert(this[kBatchContext])
binding.batch_write(this[kDbContext], this[kBatchContext], options ?? EMPTY)
}
_close (callback) {
this._closeSync()
process.nextTick(callback)
}
_closeSync () {
if (this[kBatchContext]) {
binding.batch_clear(this[kBatchContext])
this[kBatchContext] = null
}
}
get length () {
assert(this[kBatchContext])
return binding.batch_count(this[kBatchContext])
}
_merge (key, value, options) {
assert(this[kBatchContext])
if (key === null || key === undefined) {
throw new ModuleError('Key cannot be null or undefined', {
code: 'LEVEL_INVALID_KEY'
})
}
if (value === null || value === undefined) {
throw new ModuleError('value cannot be null or undefined', {
code: 'LEVEL_INVALID_VALUE'
})
}
key = typeof key === 'string' ? Buffer.from(key) : key
value = typeof value === 'string' ? Buffer.from(value) : value
binding.batch_merge(this[kBatchContext], key, value, options ?? EMPTY)
}
* [Symbol.iterator] () {
const rows = this.toArray()
for (let n = 0; n < rows.length; n += 4) {
yield {
type: rows[n + 0],
key: rows[n + 1],
value: rows[n + 2]
}
}
}
toArray (options) {
if (!this[kBatchContext]) {
return []
}
return binding.batch_iterate(this[kDbContext], this[kBatchContext], {
keys: true,
values: true,
data: true,
...options
})
}
}
exports.ChainedBatch = ChainedBatch