forked from tecbot/gorocksdb
/
backup.go
157 lines (131 loc) · 4.41 KB
/
backup.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
package gorocksdb
// #include <stdlib.h>
// #include "rocksdb/c.h"
import "C"
import (
"errors"
"unsafe"
)
// BackupEngineInfo represents the information about the backups
// in a backup engine instance. Use this get the state of the
// backup like number of backups and their ids and timestamps etc
type BackupEngineInfo struct {
c *C.rocksdb_backup_engine_info_t
}
// GetCount gets the number backsup available
func (self *BackupEngineInfo) GetCount() int {
return int(C.rocksdb_backup_engine_info_count(self.c))
}
// GetTimestamp gets the timestamp at which the backup @index was taken
func (self *BackupEngineInfo) GetTimestamp(index int) int64 {
return int64(C.rocksdb_backup_engine_info_timestamp(self.c, C.int(index)))
}
// GetBackupId gets an id that uniquely identifies a backup
// regardless of its position
func (self *BackupEngineInfo) GetBackupId(index int) int64 {
return int64(C.rocksdb_backup_engine_info_backup_id(self.c, C.int(index)))
}
// GetSize get the size of the backup in bytes
func (self *BackupEngineInfo) GetSize(index int) int64 {
return int64(C.rocksdb_backup_engine_info_size(self.c, C.int(index)))
}
// GetNumFiles gets the number of files in the backup @index
func (self *BackupEngineInfo) GetNumFiles(index int) int32 {
return int32(C.rocksdb_backup_engine_info_number_files(self.c, C.int(index)))
}
// Destroy destroys the backup engine info instance
func (self *BackupEngineInfo) Destroy() {
C.rocksdb_backup_engine_info_destroy(self.c)
self.c = nil
}
// RestoreOptions captures the options to be used during
// restoration of a backup
type RestoreOptions struct {
c *C.rocksdb_restore_options_t
}
// NewRestoreOptions creates a RestoreOptions instance
func NewRestoreOptions() *RestoreOptions {
return &RestoreOptions{
c: C.rocksdb_restore_options_create(),
}
}
// SetKeepLogFiles is used to set or unset the keep_log_files option
// If true, restore won't overwrite the existing log files in wal_dir. It will
// also move all log files from archive directory to wal_dir. By default, this
// is false
func (self *RestoreOptions) SetKeepLogFiles(v int) {
C.rocksdb_restore_options_set_keep_log_files(self.c, C.int(v))
}
// Destroy destroys this RestoreOptions instance
func (self *RestoreOptions) Destroy() {
C.rocksdb_restore_options_destroy(self.c)
}
// BackupEngine is a reusable handle to a RocksDB Backup, created by
// OpenBackupEngine
type BackupEngine struct {
c *C.rocksdb_backup_engine_t
path string
opts *Options
}
// OpenBackupEngine opens a backup engine with specified options
func OpenBackupEngine(opts *Options, path string) (*BackupEngine, error) {
var cErr *C.char
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
be := C.rocksdb_backup_engine_open(opts.c, cpath, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
return &BackupEngine{
c: be,
path: path,
opts: opts,
}, nil
}
// UnsafeGetBackupEngine returns the underlying c backup engine
func (self *BackupEngine) UnsafeGetBackupEngine() unsafe.Pointer {
return unsafe.Pointer(self.c)
}
// CreateNewBackup takes a new backup from @db
func (self *BackupEngine) CreateNewBackup(db *DB) error {
var cErr *C.char
C.rocksdb_backup_engine_create_new_backup(self.c, db.c, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// GetInfo gets an object that gives information about
// the backups that have already been taken
func (self *BackupEngine) GetInfo() *BackupEngineInfo {
return &BackupEngineInfo{
c: C.rocksdb_backup_engine_get_backup_info(self.c),
}
}
// RestoreDBFromLatestBackup restores the latest backup to @db_dir. @wal_dir
// is where the write ahead logs are restored to and usually the same as @db_dir.
func (self *BackupEngine) RestoreDBFromLatestBackup(db_dir string, wal_dir string,
opts *RestoreOptions) error {
var cErr *C.char
c_db_dir := C.CString(db_dir)
c_wal_dir := C.CString(wal_dir)
defer func() {
C.free(unsafe.Pointer(c_db_dir))
C.free(unsafe.Pointer(c_wal_dir))
}()
C.rocksdb_backup_engine_restore_db_from_latest_backup(self.c,
c_db_dir, c_wal_dir, opts.c, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// Close close the backup engine and cleans up state
// The backups already taken remain on storage.
func (self *BackupEngine) Close() {
C.rocksdb_backup_engine_close(self.c)
self.c = nil
}