-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add snapshot and checkpoint #216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
42b0f51
4512caf
c72bf07
aed54c4
9f5c1c5
1d11db0
9b11d8e
f8cd2d1
611b36c
2b23935
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package rdb | ||
|
|
||
| // #include <stdint.h> | ||
| // #include <stdlib.h> | ||
| // #include "rdbc.h" | ||
| import "C" | ||
| import ( | ||
| "errors" | ||
| "unsafe" | ||
| ) | ||
|
|
||
| // Checkpoint can be used to create openable snapshots. | ||
| type Checkpoint struct { | ||
| c *C.rdb_checkpoint_t | ||
| cDb *C.rdb_t | ||
| } | ||
|
|
||
| // Destroy removes the snapshot from the database's list of snapshots. | ||
| func (s *Checkpoint) Destroy() { | ||
| C.rdb_destroy_checkpoint(s.c) | ||
| s.c, s.cDb = nil, nil | ||
| } | ||
|
|
||
| // NewCheckpoint creates a new snapshot of the database. | ||
| func (db *DB) NewCheckpoint() (*Checkpoint, error) { | ||
| var cErr *C.char | ||
| cCheck := C.rdb_create_checkpoint(db.c, &cErr) | ||
| if cErr != nil { | ||
| defer C.free(unsafe.Pointer(cErr)) | ||
| return nil, errors.New(C.GoString(cErr)) | ||
| } | ||
| return &Checkpoint{ | ||
| c: cCheck, | ||
| cDb: db.c, | ||
| }, nil | ||
| } | ||
|
|
||
| // Save builds openable snapshot of RocksDB on disk. | ||
| // CAUTION: checkpointDir should not already exist. If so, nothing will happen. | ||
| func (s *Checkpoint) Save(checkpointDir string) error { | ||
| var ( | ||
| cErr *C.char | ||
| cDir = C.CString(checkpointDir) | ||
| ) | ||
| defer C.free(unsafe.Pointer(cDir)) | ||
| C.rdb_open_checkpoint(s.c, cDir, &cErr) | ||
| if cErr != nil { | ||
| defer C.free(unsafe.Pointer(cErr)) | ||
| return errors.New(C.GoString(cErr)) | ||
| } | ||
| return nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,3 +34,12 @@ func (opts *ReadOptions) Destroy() { | |
| func (opts *ReadOptions) SetFillCache(value bool) { | ||
| C.rdb_readoptions_set_fill_cache(opts.c, boolToChar(value)) | ||
| } | ||
|
|
||
| // SetSnapshot updates the default read options to use the given snapshot. | ||
| func (opts *ReadOptions) SetSnapshot(snapshot *Snapshot) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported method ReadOptions.SetSnapshot should have comment or be unexported |
||
| if snapshot == nil { | ||
| C.rdb_readoptions_set_snapshot(opts.c, nil) | ||
| return | ||
| } | ||
| C.rdb_readoptions_set_snapshot(opts.c, snapshot.c) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package rdb | ||
|
|
||
| // #include <stdint.h> | ||
| // #include <stdlib.h> | ||
| // #include "rdbc.h" | ||
| import "C" | ||
|
|
||
| // Snapshot provides a consistent view of read operations in a DB. | ||
| type Snapshot struct { | ||
| c *C.rdb_snapshot_t | ||
| cDb *C.rdb_t | ||
| } | ||
|
|
||
| // NewNativeSnapshot creates a Snapshot object. | ||
| func NewNativeSnapshot(c *C.rdb_snapshot_t, cDb *C.rdb_t) *Snapshot { | ||
| return &Snapshot{c, cDb} | ||
| } | ||
|
|
||
| // Release removes the snapshot from the database's list of snapshots. | ||
| func (s *Snapshot) Release() { | ||
| C.rdb_release_snapshot(s.cDb, s.c) | ||
| s.c, s.cDb = nil, nil | ||
| } | ||
|
|
||
| // NewSnapshot creates a new snapshot of the database. | ||
| func (db *DB) NewSnapshot() *Snapshot { | ||
| cSnap := C.rdb_create_snapshot(db.c) | ||
| return NewNativeSnapshot(cSnap, db.c) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,13 +91,11 @@ func (s *Store) Get(key []byte) ([]byte, error) { | |
| return valSlice.Data(), nil | ||
| } | ||
|
|
||
| func (s *Store) SetOne(k []byte, val []byte) error { | ||
| return s.db.Put(s.wopt, k, val) | ||
| } | ||
| // SetOne adds a key-value to data store. | ||
| func (s *Store) SetOne(k []byte, val []byte) error { return s.db.Put(s.wopt, k, val) } | ||
|
|
||
| func (s *Store) Delete(k []byte) error { | ||
| return s.db.Delete(s.wopt, k) | ||
| } | ||
| // Delete deletes a key from data store. | ||
| func (s *Store) Delete(k []byte) error { return s.db.Delete(s.wopt, k) } | ||
|
|
||
| // NewIterator initializes a new iterator and returns it. | ||
| func (s *Store) NewIterator() *rdb.Iterator { | ||
|
|
@@ -108,24 +106,23 @@ func (s *Store) NewIterator() *rdb.Iterator { | |
| return s.db.NewIterator(ro) | ||
| } | ||
|
|
||
| func (s *Store) Close() { | ||
| s.db.Close() | ||
| } | ||
| // Close closes our data store. | ||
| func (s *Store) Close() { s.db.Close() } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported method Store.Close should have comment or be unexported There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported method Store.Close should have comment or be unexported There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported method Store.Close should have comment or be unexported |
||
|
|
||
| // Memtable returns the memtable size. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment on exported method Store.MemtableSize should be of the form "MemtableSize ..." |
||
| func (s *Store) MemtableSize() uint64 { | ||
| memTableSize, _ := strconv.ParseUint(s.db.GetProperty("rocksdb.cur-size-all-mem-tables"), 10, 64) | ||
| return memTableSize | ||
| } | ||
|
|
||
| // IndexFilterblockSize returns the filter block size. | ||
| func (s *Store) IndexFilterblockSize() uint64 { | ||
| blockSize, _ := strconv.ParseUint(s.db.GetProperty("rocksdb.estimate-table-readers-mem"), 10, 64) | ||
| return blockSize | ||
| } | ||
|
|
||
| // NewWriteBatch creates a new WriteBatch object and returns a pointer to it. | ||
| func (s *Store) NewWriteBatch() *rdb.WriteBatch { | ||
| return rdb.NewWriteBatch() | ||
| } | ||
| func (s *Store) NewWriteBatch() *rdb.WriteBatch { return rdb.NewWriteBatch() } | ||
|
|
||
| // WriteBatch does a batch write to RocksDB from the data in WriteBatch object. | ||
| func (s *Store) WriteBatch(wb *rdb.WriteBatch) error { | ||
|
|
@@ -134,3 +131,12 @@ func (s *Store) WriteBatch(wb *rdb.WriteBatch) error { | |
| } | ||
| return nil | ||
| } | ||
|
|
||
| // NewCheckpoint creates new checkpoint from current store. | ||
| func (s *Store) NewCheckpoint() (*rdb.Checkpoint, error) { return s.db.NewCheckpoint() } | ||
|
|
||
| // NewSnapshot creates new snapshot from current store. | ||
| func (s *Store) NewSnapshot() *rdb.Snapshot { return s.db.NewSnapshot() } | ||
|
|
||
| // SetSnapshot updates default read options to use the given snapshot. | ||
| func (s *Store) SetSnapshot(snapshot *rdb.Snapshot) { s.ropt.SetSnapshot(snapshot) } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exported method ReadOptions.SetSnapshot should have comment or be unexported