forked from Level/rocksdb
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chained-batch.js
117 lines (93 loc) · 2.75 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
'use strict'
const { AbstractChainedBatch } = require('abstract-level')
const binding = require('./binding')
const ModuleError = require('module-error')
const { fromCallback } = require('catering')
const kWrite = Symbol('write')
const kBatchContext = Symbol('batchContext')
const kDbContext = Symbol('dbContext')
const kPromise = Symbol('promise')
const EMPTY = {}
class ChainedBatch extends AbstractChainedBatch {
constructor (db, context, write) {
super(db)
this[kWrite] = write
this[kDbContext] = context
this[kBatchContext] = binding.batch_init()
}
_put (key, value, options) {
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'
})
}
binding.batch_put(this[kBatchContext], key, value, options ?? EMPTY)
}
_del (key, options) {
if (key === null || key === undefined) {
throw new ModuleError('Key cannot be null or undefined', {
code: 'LEVEL_INVALID_KEY'
})
}
binding.batch_del(this[kBatchContext], key, options ?? EMPTY)
}
_clear () {
binding.batch_clear(this[kBatchContext])
}
_write (options, callback) {
callback = fromCallback(callback, kPromise)
this[kWrite](this, this[kBatchContext], options ?? EMPTY, callback)
return callback[kPromise]
}
_close (callback) {
process.nextTick(callback)
}
get length () {
return binding.batch_count(this[kBatchContext])
}
_putLogData (value, options) {
if (value === null || value === undefined) {
throw new ModuleError('value cannot be null or undefined', {
code: 'LEVEL_INVALID_VALUE'
})
}
binding.batch_put_log_data(this[kBatchContext], value, options ?? EMPTY)
}
_merge (key, value, options) {
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'
})
}
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) {
return binding.batch_iterate(this[kDbContext], this[kBatchContext], {
keys: true,
values: true,
data: true,
...options
})
}
}
exports.ChainedBatch = ChainedBatch