-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
rocksdb.go
793 lines (687 loc) · 21.8 KB
/
rocksdb.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
// Copyright 2014 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License. See the AUTHORS file
// for names of contributors.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
// Author: Andrew Bonventre (andybons@gmail.com)
// Author: Tobias Schottdorf (tobias.schottdorf@gmail.com)
// Author: Jiang-Ming Yang (jiangming.yang@gmail.com)
package engine
import (
"bytes"
"errors"
"fmt"
"syscall"
"unsafe"
"github.com/cockroachdb/cockroach/storage/engine/rocksdb"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/util"
"github.com/cockroachdb/cockroach/util/log"
"github.com/cockroachdb/cockroach/util/stop"
"github.com/gogo/protobuf/proto"
)
// #cgo darwin LDFLAGS: -Wl,-undefined -Wl,dynamic_lookup
// #cgo !darwin LDFLAGS: -Wl,-unresolved-symbols=ignore-all
// #cgo linux LDFLAGS: -lrt
//
// #include <stdlib.h>
// #include "rocksdb/db.h"
import "C"
func init() {
rocksdb.Logger = log.Infof
}
// RocksDB is a wrapper around a RocksDB database instance.
type RocksDB struct {
rdb *C.DBEngine
attrs roachpb.Attributes // Attributes for this engine
dir string // The data directory
cacheSize int64 // Memory to use to cache values.
memtableBudget int64 // Memory to use for the memory table.
stopper *stop.Stopper
deallocated chan struct{} // Closed when the underlying handle is deallocated.
}
// NewRocksDB allocates and returns a new RocksDB object.
func NewRocksDB(attrs roachpb.Attributes, dir string, cacheSize, memtableBudget int64,
stopper *stop.Stopper) *RocksDB {
if dir == "" {
panic(util.Errorf("dir must be non-empty"))
}
return &RocksDB{
attrs: attrs,
dir: dir,
cacheSize: cacheSize,
memtableBudget: memtableBudget,
stopper: stopper,
deallocated: make(chan struct{}),
}
}
func newMemRocksDB(attrs roachpb.Attributes, cacheSize, memtableBudget int64,
stopper *stop.Stopper) *RocksDB {
return &RocksDB{
attrs: attrs,
// dir: empty dir == "mem" RocksDB instance.
cacheSize: cacheSize,
memtableBudget: memtableBudget,
stopper: stopper,
deallocated: make(chan struct{}),
}
}
// String formatter.
func (r *RocksDB) String() string {
return fmt.Sprintf("%s=%s", r.attrs.Attrs, r.dir)
}
// Open creates options and opens the database. If the database
// doesn't yet exist at the specified directory, one is initialized
// from scratch. The RocksDB Open and Close methods are reference
// counted such that subsequent Open calls to an already opened
// RocksDB instance only bump the reference count. The RocksDB is only
// closed when a sufficient number of Close calls are performed to
// bring the reference count down to 0.
func (r *RocksDB) Open() error {
if r.rdb != nil {
return nil
}
if len(r.dir) != 0 {
log.Infof("opening rocksdb instance at %q", r.dir)
}
status := C.DBOpen(&r.rdb, goToCSlice([]byte(r.dir)),
C.DBOptions{
cache_size: C.int64_t(r.cacheSize),
memtable_budget: C.int64_t(r.memtableBudget),
allow_os_buffer: C.bool(true),
logging_enabled: C.bool(log.V(3)),
})
err := statusToError(status)
if err != nil {
return util.Errorf("could not open rocksdb instance: %s", err)
}
// Start a gorountine that will finish when the underlying handle
// is deallocated. This is used to check a leak in tests.
go func() {
<-r.deallocated
}()
r.stopper.AddCloser(r)
return nil
}
// Close closes the database by deallocating the underlying handle.
func (r *RocksDB) Close() {
if r.rdb == nil {
log.Errorf("closing unopened rocksdb instance")
return
}
if len(r.dir) == 0 {
log.Infof("closing in-memory rocksdb instance")
} else {
log.Infof("closing rocksdb instance at %q", r.dir)
}
if r.rdb != nil {
C.DBClose(r.rdb)
r.rdb = nil
}
close(r.deallocated)
}
// Attrs returns the list of attributes describing this engine. This
// may include a specification of disk type (e.g. hdd, ssd, fio, etc.)
// and potentially other labels to identify important attributes of
// the engine.
func (r *RocksDB) Attrs() roachpb.Attributes {
return r.attrs
}
// Put sets the given key to the value provided.
//
// The key and value byte slices may be reused safely. put takes a copy of
// them before returning.
func (r *RocksDB) Put(key MVCCKey, value []byte) error {
return dbPut(r.rdb, key, value)
}
// Merge implements the RocksDB merge operator using the function goMergeInit
// to initialize missing values and goMerge to merge the old and the given
// value into a new value, which is then stored under key.
// Currently 64-bit counter logic is implemented. See the documentation of
// goMerge and goMergeInit for details.
//
// The key and value byte slices may be reused safely. merge takes a copy
// of them before returning.
func (r *RocksDB) Merge(key MVCCKey, value []byte) error {
return dbMerge(r.rdb, key, value)
}
// Get returns the value for the given key.
func (r *RocksDB) Get(key MVCCKey) ([]byte, error) {
return dbGet(r.rdb, key)
}
// GetProto fetches the value at the specified key and unmarshals it.
func (r *RocksDB) GetProto(key MVCCKey, msg proto.Message) (
ok bool, keyBytes, valBytes int64, err error) {
return dbGetProto(r.rdb, key, msg)
}
// Clear removes the item from the db with the given key.
func (r *RocksDB) Clear(key MVCCKey) error {
return dbClear(r.rdb, key)
}
// Iterate iterates from start to end keys, invoking f on each
// key/value pair. See engine.Iterate for details.
func (r *RocksDB) Iterate(start, end MVCCKey, f func(MVCCKeyValue) (bool, error)) error {
return dbIterate(r.rdb, start, end, f)
}
// Capacity queries the underlying file system for disk capacity information.
func (r *RocksDB) Capacity() (roachpb.StoreCapacity, error) {
var fs syscall.Statfs_t
var capacity roachpb.StoreCapacity
dir := r.dir
if dir == "" {
dir = "/tmp"
}
if err := syscall.Statfs(dir, &fs); err != nil {
return capacity, err
}
capacity.Capacity = int64(fs.Bsize) * int64(fs.Blocks)
capacity.Available = int64(fs.Bsize) * int64(fs.Bavail)
return capacity, nil
}
// SetGCTimeouts calls through to the DBEngine's SetGCTimeouts method.
func (r *RocksDB) SetGCTimeouts(minTxnTS int64) {
C.DBSetGCTimeouts(r.rdb, C.int64_t(minTxnTS))
}
// CompactRange compacts the specified key range. Specifying nil for
// the start key starts the compaction from the start of the database.
// Similarly, specifying nil for the end key will compact through the
// last key. Note that the use of the word "Range" here does not refer
// to Cockroach ranges, just to a generalized key range.
func (r *RocksDB) CompactRange(start, end MVCCKey) {
var (
s, e C.DBSlice
sPtr, ePtr *C.DBSlice
)
if start != nil {
sPtr = &s
s = goToCSlice(start)
}
if end != nil {
ePtr = &e
e = goToCSlice(end)
}
err := statusToError(C.DBCompactRange(r.rdb, sPtr, ePtr))
if err != nil {
log.Warningf("compact range: %s", err)
}
}
// Destroy destroys the underlying filesystem data associated with the database.
func (r *RocksDB) Destroy() error {
return statusToError(C.DBDestroy(goToCSlice([]byte(r.dir))))
}
// ApproximateSize returns the approximate number of bytes on disk that RocksDB
// is using to store data for the given range of keys.
func (r *RocksDB) ApproximateSize(start, end MVCCKey) (uint64, error) {
return uint64(C.DBApproximateSize(r.rdb, goToCSlice(start), goToCSlice(end))), nil
}
// Flush causes RocksDB to write all in-memory data to disk immediately.
func (r *RocksDB) Flush() error {
return statusToError(C.DBFlush(r.rdb))
}
// NewIterator returns an iterator over this rocksdb engine.
func (r *RocksDB) NewIterator(prefix bool) Iterator {
return newRocksDBIterator(r.rdb, prefix)
}
// NewSnapshot creates a snapshot handle from engine and returns a
// read-only rocksDBSnapshot engine.
func (r *RocksDB) NewSnapshot() Engine {
if r.rdb == nil {
panic("RocksDB is not initialized yet")
}
return &rocksDBSnapshot{
parent: r,
handle: C.DBNewSnapshot(r.rdb),
}
}
// NewBatch returns a new batch wrapping this rocksdb engine.
func (r *RocksDB) NewBatch() Engine {
return newRocksDBBatch(r)
}
// Commit is a noop for RocksDB engine.
func (r *RocksDB) Commit() error {
return nil
}
// Defer is not implemented for RocksDB engine.
func (r *RocksDB) Defer(func()) {
panic("only implemented for rocksDBBatch")
}
type rocksDBSnapshot struct {
parent *RocksDB
handle *C.DBEngine
}
// Open is a noop.
func (r *rocksDBSnapshot) Open() error {
return nil
}
// Close releases the snapshot handle.
func (r *rocksDBSnapshot) Close() {
C.DBClose(r.handle)
}
// Attrs returns the engine/store attributes.
func (r *rocksDBSnapshot) Attrs() roachpb.Attributes {
return r.parent.Attrs()
}
// Put is illegal for snapshot and returns an error.
func (r *rocksDBSnapshot) Put(key MVCCKey, value []byte) error {
return util.Errorf("cannot Put to a snapshot")
}
// Get returns the value for the given key, nil otherwise using
// the snapshot handle.
func (r *rocksDBSnapshot) Get(key MVCCKey) ([]byte, error) {
return dbGet(r.handle, key)
}
func (r *rocksDBSnapshot) GetProto(key MVCCKey, msg proto.Message) (
ok bool, keyBytes, valBytes int64, err error) {
return dbGetProto(r.handle, key, msg)
}
// Iterate iterates over the keys between start inclusive and end
// exclusive, invoking f() on each key/value pair using the snapshot
// handle.
func (r *rocksDBSnapshot) Iterate(start, end MVCCKey, f func(MVCCKeyValue) (bool, error)) error {
return dbIterate(r.handle, start, end, f)
}
// Clear is illegal for snapshot and returns an error.
func (r *rocksDBSnapshot) Clear(key MVCCKey) error {
return util.Errorf("cannot Clear from a snapshot")
}
// Merge is illegal for snapshot and returns an error.
func (r *rocksDBSnapshot) Merge(key MVCCKey, value []byte) error {
return util.Errorf("cannot Merge to a snapshot")
}
// Capacity returns capacity details for the engine's available storage.
func (r *rocksDBSnapshot) Capacity() (roachpb.StoreCapacity, error) {
return r.parent.Capacity()
}
// SetGCTimeouts is a noop for a snapshot.
func (r *rocksDBSnapshot) SetGCTimeouts(minTxnTS int64) {
}
// ApproximateSize returns the approximate number of bytes the engine is
// using to store data for the given range of keys.
func (r *rocksDBSnapshot) ApproximateSize(start, end MVCCKey) (uint64, error) {
return r.parent.ApproximateSize(start, end)
}
// Flush is a no-op for snapshots.
func (r *rocksDBSnapshot) Flush() error {
return nil
}
// NewIterator returns a new instance of an Iterator over the
// engine using the snapshot handle.
func (r *rocksDBSnapshot) NewIterator(prefix bool) Iterator {
return newRocksDBIterator(r.handle, prefix)
}
// NewSnapshot is illegal for snapshot.
func (r *rocksDBSnapshot) NewSnapshot() Engine {
panic("cannot create a NewSnapshot from a snapshot")
}
// NewBatch is illegal for snapshot.
func (r *rocksDBSnapshot) NewBatch() Engine {
panic("cannot create a NewBatch from a snapshot")
}
// Commit is illegal for snapshot and returns an error.
func (r *rocksDBSnapshot) Commit() error {
return util.Errorf("cannot Commit to a snapshot")
}
// Defer is not implemented for rocksDBSnapshot.
func (r *rocksDBSnapshot) Defer(func()) {
panic("only implemented for rocksDBBatch")
}
type rocksDBBatch struct {
parent *RocksDB
batch *C.DBEngine
defers []func()
}
func newRocksDBBatch(r *RocksDB) *rocksDBBatch {
return &rocksDBBatch{
parent: r,
batch: C.DBNewBatch(r.rdb),
}
}
func (r *rocksDBBatch) Open() error {
return util.Errorf("cannot open a batch")
}
func (r *rocksDBBatch) Close() {
if r.batch != nil {
C.DBClose(r.batch)
}
}
// Attrs returns the engine/store attributes.
func (r *rocksDBBatch) Attrs() roachpb.Attributes {
return r.parent.Attrs()
}
func (r *rocksDBBatch) Put(key MVCCKey, value []byte) error {
return dbPut(r.batch, key, value)
}
func (r *rocksDBBatch) Merge(key MVCCKey, value []byte) error {
return dbMerge(r.batch, key, value)
}
func (r *rocksDBBatch) Get(key MVCCKey) ([]byte, error) {
return dbGet(r.batch, key)
}
func (r *rocksDBBatch) GetProto(key MVCCKey, msg proto.Message) (
ok bool, keyBytes, valBytes int64, err error) {
return dbGetProto(r.batch, key, msg)
}
func (r *rocksDBBatch) Iterate(start, end MVCCKey, f func(MVCCKeyValue) (bool, error)) error {
return dbIterate(r.batch, start, end, f)
}
func (r *rocksDBBatch) Clear(key MVCCKey) error {
return dbClear(r.batch, key)
}
func (r *rocksDBBatch) Capacity() (roachpb.StoreCapacity, error) {
return r.parent.Capacity()
}
func (r *rocksDBBatch) SetGCTimeouts(minTxnTS int64) {
// no-op
}
func (r *rocksDBBatch) ApproximateSize(start, end MVCCKey) (uint64, error) {
return r.parent.ApproximateSize(start, end)
}
func (r *rocksDBBatch) Flush() error {
return util.Errorf("cannot flush a batch")
}
func (r *rocksDBBatch) NewIterator(prefix bool) Iterator {
return newRocksDBIterator(r.batch, prefix)
}
func (r *rocksDBBatch) NewSnapshot() Engine {
panic("cannot create a NewSnapshot from a batch")
}
func (r *rocksDBBatch) NewBatch() Engine {
return newRocksDBBatch(r.parent)
}
func (r *rocksDBBatch) Commit() error {
if r.batch == nil {
panic("this batch was already committed")
}
if err := statusToError(C.DBWriteBatch(r.batch)); err != nil {
return err
}
C.DBClose(r.batch)
r.batch = nil
// On success, run the deferred functions in reverse order.
for i := len(r.defers) - 1; i >= 0; i-- {
r.defers[i]()
}
r.defers = nil
return nil
}
func (r *rocksDBBatch) Defer(fn func()) {
r.defers = append(r.defers, fn)
}
type rocksDBIterator struct {
iter *C.DBIterator
valid bool
key C.DBSlice
value C.DBSlice
}
// newRocksDBIterator returns a new iterator over the supplied RocksDB
// instance. If snapshotHandle is not nil, uses the indicated snapshot.
// The caller must call rocksDBIterator.Close() when finished with the
// iterator to free up resources.
func newRocksDBIterator(rdb *C.DBEngine, prefix bool) *rocksDBIterator {
// In order to prevent content displacement, caching is disabled
// when performing scans. Any options set within the shared read
// options field that should be carried over needs to be set here
// as well.
return &rocksDBIterator{
iter: C.DBNewIter(rdb, C.bool(prefix)),
}
}
// The following methods implement the Iterator interface.
func (r *rocksDBIterator) Close() {
C.DBIterDestroy(r.iter)
}
func (r *rocksDBIterator) Seek(key []byte) {
if len(key) == 0 {
// start=Key("") needs special treatment since we need
// to access start[0] in an explicit seek.
r.setState(C.DBIterSeekToFirst(r.iter))
} else {
r.setState(C.DBIterSeek(r.iter, goToCSlice(key)))
}
}
func (r *rocksDBIterator) Valid() bool {
return r.valid
}
func (r *rocksDBIterator) Next() {
r.setState(C.DBIterNext(r.iter))
}
func (r *rocksDBIterator) SeekReverse(key []byte) {
if len(key) == 0 {
r.setState(C.DBIterSeekToLast(r.iter))
} else {
r.setState(C.DBIterSeek(r.iter, goToCSlice(key)))
// Maybe the key sorts after the last key in RocksDB.
if !r.Valid() {
r.setState(C.DBIterSeekToLast(r.iter))
}
if !r.Valid() {
return
}
// Make sure the current key is <= the provided key.
curKey := r.Key()
if MVCCKey(key).Less(curKey) {
r.Prev()
}
}
}
func (r *rocksDBIterator) Prev() {
r.setState(C.DBIterPrev(r.iter))
}
func (r *rocksDBIterator) Key() MVCCKey {
// The data returned by rocksdb_iter_{key,value} is not meant to be
// freed by the client. It is a direct reference to the data managed
// by the iterator, so it is copied instead of freed.
return cSliceToGoBytes(r.key)
}
func (r *rocksDBIterator) Value() []byte {
return cSliceToGoBytes(r.value)
}
func (r *rocksDBIterator) ValueProto(msg proto.Message) error {
if r.value.len <= 0 {
return nil
}
// Make a byte slice that is backed by result.data. This slice
// cannot live past the lifetime of this method, but we're only
// using it to unmarshal the roachpb.
data := cSliceToUnsafeGoBytes(r.value)
return proto.Unmarshal(data, msg)
}
func (r *rocksDBIterator) unsafeKey() MVCCKey {
return cSliceToUnsafeGoBytes(r.key)
}
func (r *rocksDBIterator) unsafeValue() []byte {
return cSliceToUnsafeGoBytes(r.value)
}
func (r *rocksDBIterator) Error() error {
return statusToError(C.DBIterError(r.iter))
}
func (r *rocksDBIterator) setState(state C.DBIterState) {
r.valid = bool(state.valid)
r.key = state.key
r.value = state.value
}
func (r *rocksDBIterator) ComputeStats(ms *MVCCStats, start, end []byte, nowNanos int64) error {
result := C.MVCCComputeStats(r.iter, goToCSlice(start), goToCSlice(end), C.int64_t(nowNanos))
if err := statusToError(result.status); err != nil {
return err
}
ms.LiveBytes += int64(result.live_bytes)
ms.KeyBytes += int64(result.key_bytes)
ms.ValBytes += int64(result.val_bytes)
ms.IntentBytes += int64(result.intent_bytes)
ms.LiveCount += int64(result.live_count)
ms.KeyCount += int64(result.key_count)
ms.ValCount += int64(result.val_count)
ms.IntentCount += int64(result.intent_count)
ms.IntentAge += int64(result.intent_age)
ms.GCBytesAge += int64(result.gc_bytes_age)
ms.SysBytes += int64(result.sys_bytes)
ms.SysCount += int64(result.sys_count)
ms.LastUpdateNanos = nowNanos
return nil
}
// goToCSlice converts a go byte slice to a DBSlice. Note that this is
// potentially dangerous as the DBSlice holds a reference to the go
// byte slice memory that the Go GC does not know about. This method
// is only intended for use in converting arguments to C
// functions. The C function must copy any data that it wishes to
// retain once the function returns.
func goToCSlice(b []byte) C.DBSlice {
if len(b) == 0 {
return C.DBSlice{data: nil, len: 0}
}
return C.DBSlice{
data: (*C.char)(unsafe.Pointer(&b[0])),
len: C.int(len(b)),
}
}
func cStringToGoString(s C.DBString) string {
if s.data == nil {
return ""
}
result := C.GoStringN(s.data, s.len)
C.free(unsafe.Pointer(s.data))
return result
}
func cStringToGoBytes(s C.DBString) []byte {
if s.data == nil {
return nil
}
result := C.GoBytes(unsafe.Pointer(s.data), s.len)
C.free(unsafe.Pointer(s.data))
return result
}
func cSliceToGoBytes(s C.DBSlice) []byte {
if s.data == nil {
return nil
}
return C.GoBytes(unsafe.Pointer(s.data), s.len)
}
func cSliceToUnsafeGoBytes(s C.DBSlice) []byte {
if s.data == nil {
return nil
}
// Go limits arrays to a length that will fit in a (signed) 32-bit
// integer. Fall back to using cSliceToGoBytes if our slice is
// larger.
const maxLen = 0x7fffffff
if s.len > maxLen {
return cSliceToGoBytes(s)
}
return (*[maxLen]byte)(unsafe.Pointer(s.data))[:s.len:s.len]
}
func statusToError(s C.DBStatus) error {
if s.data == nil {
return nil
}
return errors.New(cStringToGoString(s))
}
// goMerge takes existing and update byte slices that are expected to
// be marshalled roachpb.Values and merges the two values returning a
// marshalled roachpb.Value or an error.
func goMerge(existing, update []byte) ([]byte, error) {
var result C.DBString
status := C.DBMergeOne(goToCSlice(existing), goToCSlice(update), &result)
if status.data != nil {
return nil, util.Errorf("%s: existing=%q, update=%q",
cStringToGoString(status), existing, update)
}
return cStringToGoBytes(result), nil
}
func emptyKeyError() error {
return util.ErrorfSkipFrames(1, "attempted access to empty key")
}
func dbPut(rdb *C.DBEngine, key MVCCKey, value []byte) error {
if len(key) == 0 {
return emptyKeyError()
}
// *Put, *Get, and *Delete call memcpy() (by way of MemTable::Add)
// when called, so we do not need to worry about these byte slices
// being reclaimed by the GC.
return statusToError(C.DBPut(rdb, goToCSlice(key), goToCSlice(value)))
}
func dbMerge(rdb *C.DBEngine, key MVCCKey, value []byte) error {
if len(key) == 0 {
return emptyKeyError()
}
// DBMerge calls memcpy() (by way of MemTable::Add)
// when called, so we do not need to worry about these byte slices being
// reclaimed by the GC.
return statusToError(C.DBMerge(rdb, goToCSlice(key), goToCSlice(value)))
}
// dbGet returns the value for the given key.
func dbGet(rdb *C.DBEngine, key MVCCKey) ([]byte, error) {
if len(key) == 0 {
return nil, emptyKeyError()
}
var result C.DBString
err := statusToError(C.DBGet(rdb, goToCSlice(key), &result))
if err != nil {
return nil, err
}
return cStringToGoBytes(result), nil
}
func dbGetProto(rdb *C.DBEngine, key MVCCKey,
msg proto.Message) (ok bool, keyBytes, valBytes int64, err error) {
if len(key) == 0 {
err = emptyKeyError()
return
}
var result C.DBString
if err = statusToError(C.DBGet(rdb, goToCSlice(key), &result)); err != nil {
return
}
if result.len <= 0 {
msg.Reset()
return
}
ok = true
if msg != nil {
// Make a byte slice that is backed by result.data. This slice
// cannot live past the lifetime of this method, but we're only
// using it to unmarshal the roachpb.
data := cSliceToUnsafeGoBytes(C.DBSlice(result))
err = proto.Unmarshal(data, msg)
}
C.free(unsafe.Pointer(result.data))
keyBytes = int64(len(key))
valBytes = int64(result.len)
return
}
func dbClear(rdb *C.DBEngine, key MVCCKey) error {
if len(key) == 0 {
return emptyKeyError()
}
return statusToError(C.DBDelete(rdb, goToCSlice(key)))
}
func dbIterate(rdb *C.DBEngine, start, end MVCCKey,
f func(MVCCKeyValue) (bool, error)) error {
if bytes.Compare(start, end) >= 0 {
return nil
}
it := newRocksDBIterator(rdb, false)
defer it.Close()
it.Seek(start)
for ; it.Valid(); it.Next() {
k := it.Key()
if !it.Key().Less(end) {
break
}
if done, err := f(MVCCKeyValue{Key: k, Value: it.Value()}); done || err != nil {
return err
}
}
// Check for any errors during iteration.
return it.Error()
}