This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
forked from linxGnu/grocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_test.go
131 lines (103 loc) · 2.85 KB
/
backup_test.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
package grocksdb
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestBackupEngine(t *testing.T) {
db := newTestDB(t, nil)
defer db.Close()
var (
givenKey = []byte("hello")
givenVal1 = []byte("")
givenVal2 = []byte("world1")
wo = NewDefaultWriteOptions()
ro = NewDefaultReadOptions()
)
defer wo.Destroy()
defer ro.Destroy()
// create
require.Nil(t, db.Put(wo, givenKey, givenVal1))
// retrieve
v1, err := db.Get(ro, givenKey)
defer v1.Free()
require.Nil(t, err)
require.EqualValues(t, v1.Data(), givenVal1)
// retrieve bytes
_v1, err := db.GetBytes(ro, givenKey)
require.Nil(t, err)
require.EqualValues(t, _v1, givenVal1)
// update
require.Nil(t, db.Put(wo, givenKey, givenVal2))
v2, err := db.Get(ro, givenKey)
defer v2.Free()
require.Nil(t, err)
require.EqualValues(t, v2.Data(), givenVal2)
// retrieve pinned
v3, err := db.GetPinned(ro, givenKey)
defer v3.Destroy()
require.Nil(t, err)
require.EqualValues(t, v3.Data(), givenVal2)
engine, err := CreateBackupEngine(db)
require.Nil(t, err)
defer func() {
engine.Close()
// re-open with opts
opts := NewBackupableDBOptions(db.name)
env := NewDefaultEnv()
_, err = OpenBackupEngineWithOpt(opts, env)
require.Nil(t, err)
env.Destroy()
opts.Destroy()
}()
t.Run("createBackupAndVerify", func(t *testing.T) {
infos := engine.GetInfo()
require.Empty(t, infos)
// create first backup
require.Nil(t, engine.CreateNewBackup())
// create second backup
require.Nil(t, engine.CreateNewBackupFlush(true))
infos = engine.GetInfo()
require.Equal(t, 2, len(infos))
for i := range infos {
require.Nil(t, engine.VerifyBackup(infos[i].ID))
require.True(t, infos[i].Size > 0)
require.True(t, infos[i].NumFiles > 0)
}
})
t.Run("purge", func(t *testing.T) {
require.Nil(t, engine.PurgeOldBackups(1))
infos := engine.GetInfo()
require.Equal(t, 1, len(infos))
})
t.Run("restoreFromLatest", func(t *testing.T) {
dir := t.TempDir()
ro := NewRestoreOptions()
defer ro.Destroy()
require.Nil(t, engine.RestoreDBFromLatestBackup(dir, dir, ro))
require.Nil(t, engine.RestoreDBFromLatestBackup(dir, dir, ro))
})
t.Run("restoreFromBackup", func(t *testing.T) {
infos := engine.GetInfo()
require.Equal(t, 1, len(infos))
dir := t.TempDir()
ro := NewRestoreOptions()
defer ro.Destroy()
require.Nil(t, engine.RestoreDBFromBackup(dir, dir, ro, infos[0].ID))
// try to reopen restored db
backupDB, err := OpenDb(db.opts, dir)
require.Nil(t, err)
defer backupDB.Close()
r := NewDefaultReadOptions()
defer r.Destroy()
for i := 0; i < 1000; i++ {
v3, err := backupDB.GetPinned(r, givenKey)
require.Nil(t, err)
require.EqualValues(t, v3.Data(), givenVal2)
v3.Destroy()
v4, err := backupDB.GetPinned(r, []byte("justFake"))
require.Nil(t, err)
require.False(t, v4.Exists())
v4.Destroy()
}
})
}