-
Notifications
You must be signed in to change notification settings - Fork 693
/
swappable_db.go
166 lines (143 loc) · 4.38 KB
/
swappable_db.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
package db
import (
"fmt"
"io"
"os"
"sync"
command "github.com/rqlite/rqlite/v8/command/proto"
)
// SwappableDB is a wrapper around DB that allows the underlying database to be swapped out
// in a thread-safe manner.
type SwappableDB struct {
db *DB
dbMu sync.RWMutex
}
// OpenSwappable returns a new SwappableDB instance, which opens the database at the given path.
func OpenSwappable(dbPath string, fkEnabled, wal bool) (*SwappableDB, error) {
db, err := Open(dbPath, fkEnabled, wal)
if err != nil {
return nil, err
}
return &SwappableDB{db: db}, nil
}
// Swap swaps the underlying database with that at the given path. The Swap operation
// may fail on some platforms if the file at path is open by another process. It is
// the caller's responsibility to ensure the file at path is not in use.
func (s *SwappableDB) Swap(path string, fkConstraints, walEnabled bool) error {
if !IsValidSQLiteFile(path) {
return fmt.Errorf("invalid SQLite data")
}
s.dbMu.Lock()
defer s.dbMu.Unlock()
if err := s.db.Close(); err != nil {
return fmt.Errorf("failed to close: %s", err)
}
if err := RemoveFiles(s.db.Path()); err != nil {
return fmt.Errorf("failed to remove files: %s", err)
}
if err := os.Rename(path, s.db.Path()); err != nil {
return fmt.Errorf("failed to rename database: %s", err)
}
db, err := Open(s.db.Path(), fkConstraints, walEnabled)
if err != nil {
return fmt.Errorf("open SQLite file failed: %s", err)
}
s.db = db
return nil
}
// Close closes the underlying database.
func (s *SwappableDB) Close() error {
s.dbMu.RLock()
defer s.dbMu.RUnlock()
return s.db.Close()
}
// Stats returns the underlying database's stats.
func (s *SwappableDB) Stats() (map[string]interface{}, error) {
s.dbMu.RLock()
defer s.dbMu.RUnlock()
return s.db.Stats()
}
// Request calls Request on the underlying database.
func (s *SwappableDB) Request(req *command.Request, xTime bool) ([]*command.ExecuteQueryResponse, error) {
s.dbMu.RLock()
defer s.dbMu.RUnlock()
return s.db.Request(req, xTime)
}
// Execute calls Execute on the underlying database.
func (s *SwappableDB) Execute(ex *command.Request, xTime bool) ([]*command.ExecuteQueryResponse, error) {
s.dbMu.RLock()
defer s.dbMu.RUnlock()
return s.db.Execute(ex, xTime)
}
// Query calls Query on the underlying database.
func (s *SwappableDB) Query(q *command.Request, xTime bool) ([]*command.QueryRows, error) {
s.dbMu.RLock()
defer s.dbMu.RUnlock()
return s.db.Query(q, xTime)
}
// QueryStringStmt calls QueryStringStmt on the underlying database.
func (s *SwappableDB) QueryStringStmt(query string) ([]*command.QueryRows, error) {
s.dbMu.RLock()
defer s.dbMu.RUnlock()
return s.db.QueryStringStmt(query)
}
// VacuumInto calls VacuumInto on the underlying database.
func (s *SwappableDB) VacuumInto(path string) error {
s.dbMu.RLock()
defer s.dbMu.RUnlock()
return s.db.VacuumInto(path)
}
// Backup calls Backup on the underlying database.
func (s *SwappableDB) Backup(path string, vacuum bool) error {
s.dbMu.RLock()
defer s.dbMu.RUnlock()
return s.db.Backup(path, vacuum)
}
// Serialize calls Serialize on the underlying database.
func (s *SwappableDB) Serialize() ([]byte, error) {
s.dbMu.RLock()
defer s.dbMu.RUnlock()
return s.db.Serialize()
}
// StmtReadOnly calls StmtReadOnly on the underlying database.
func (s *SwappableDB) StmtReadOnly(sql string) (bool, error) {
s.dbMu.RLock()
defer s.dbMu.RUnlock()
return s.db.StmtReadOnly(sql)
}
// Checkpoint calls Checkpoint on the underlying database.
func (s *SwappableDB) Checkpoint(mode CheckpointMode) error {
s.dbMu.RLock()
defer s.dbMu.RUnlock()
return s.db.Checkpoint(mode)
}
// Path calls Path on the underlying database.
func (s *SwappableDB) Path() string {
s.dbMu.RLock()
defer s.dbMu.RUnlock()
return s.db.Path()
}
// Dump calls Dump on the underlying database.
func (s *SwappableDB) Dump(w io.Writer) error {
s.dbMu.RLock()
defer s.dbMu.RUnlock()
return s.db.Dump(w)
}
// FKEnabled calls FKEnabled on the underlying database.
func (s *SwappableDB) FKEnabled() bool {
s.dbMu.RLock()
defer s.dbMu.RUnlock()
return s.db.FKEnabled()
}
// WALEnabled calls WALEnabled on the underlying database.
func (s *SwappableDB) WALEnabled() bool {
s.dbMu.RLock()
defer s.dbMu.RUnlock()
return s.db.WALEnabled()
}
// FileSize calls FileSize on the underlying database.
func (s *SwappableDB) FileSize() (int64, error) {
s.dbMu.RLock()
defer s.dbMu.RUnlock()
return s.db.FileSize()
}