This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
rdb_batch.go
93 lines (83 loc) · 1.61 KB
/
rdb_batch.go
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
//go:build rocksdb
// +build rocksdb
package db
// #include <stdlib.h>
// #include "rocksdb/c.h"
import "C"
type rdbBatch struct {
db *RDB
b *C.rocksdb_writebatch_t
}
func newRDBBatch(db *RDB) *rdbBatch {
return &rdbBatch{
db: db,
b: C.rocksdb_writebatch_create(),
}
}
func (b *rdbBatch) Set(key, value []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
if value == nil {
return errValueNil
}
if b.b == nil {
return errBatchClosed
}
ck, cv := b2c(key), b2c(value)
C.rocksdb_writebatch_put(b.b, ck, C.size_t(len(key)), cv, C.size_t(len(value)))
return nil
}
func (b *rdbBatch) Delete(key []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
if b.b == nil {
return errBatchClosed
}
C.rocksdb_writebatch_delete(b.b, b2c(key), C.size_t(len(key)))
return nil
}
func (b *rdbBatch) Write() error {
if b.b == nil {
return errBatchClosed
}
var cerr *C.char
C.rocksdb_write(b.db.db, b.db.wopts, b.b, &cerr) // nolint: gocritic
b.Close()
if cerr != nil {
return cerror(cerr)
}
return nil
}
func (b *rdbBatch) WriteSync() error {
if b.b == nil {
return errBatchClosed
}
var cerr *C.char
C.rocksdb_write(b.db.db, b.db.wsopts, b.b, &cerr) // nolint: gocritic
b.Close()
if cerr != nil {
return cerror(cerr)
}
return nil
}
func (b *rdbBatch) WriteLowPri() error {
if b.b == nil {
return errBatchClosed
}
var cerr *C.char
C.rocksdb_write(b.db.db, b.db.wlpopts, b.b, &cerr) // nolint: gocritic
b.Close()
if cerr != nil {
return cerror(cerr)
}
return nil
}
func (b *rdbBatch) Close() error {
if b.b != nil {
C.rocksdb_writebatch_destroy(b.b)
b.b = nil
}
return nil
}