@@ -44,6 +44,7 @@ const BusyTimeout = 1 * time.Second
44
44
type DB struct {
45
45
mu sync.RWMutex
46
46
path string // part to database
47
+ metaPath string // Path to the database metadata.
47
48
db * sql.DB // target database
48
49
f * os.File // long-running db file descriptor
49
50
rtx * sql.Tx // long running read transaction
@@ -101,9 +102,12 @@ type DB struct {
101
102
102
103
// NewDB returns a new instance of DB for a given path.
103
104
func NewDB (path string ) * DB {
105
+ dir , file := filepath .Split (path )
106
+
104
107
db := & DB {
105
- path : path ,
106
- notify : make (chan struct {}),
108
+ path : path ,
109
+ metaPath : filepath .Join (dir , "." + file + MetaDirSuffix ),
110
+ notify : make (chan struct {}),
107
111
108
112
MinCheckpointPageN : DefaultMinCheckpointPageN ,
109
113
MaxCheckpointPageN : DefaultMaxCheckpointPageN ,
@@ -147,20 +151,24 @@ func (db *DB) WALPath() string {
147
151
148
152
// MetaPath returns the path to the database metadata.
149
153
func (db * DB ) MetaPath () string {
150
- dir , file := filepath .Split (db .path )
151
- return filepath .Join (dir , "." + file + MetaDirSuffix )
154
+ return db .metaPath
155
+ }
156
+
157
+ // SetMetaPath sets the path to database metadata.
158
+ func (db * DB ) SetMetaPath (mp string ) {
159
+ db .metaPath = mp
152
160
}
153
161
154
162
// GenerationNamePath returns the path of the name of the current generation.
155
163
func (db * DB ) GenerationNamePath () string {
156
- return filepath .Join (db .MetaPath () , "generation" )
164
+ return filepath .Join (db .metaPath , "generation" )
157
165
}
158
166
159
167
// GenerationPath returns the path of a single generation.
160
168
// Panics if generation is blank.
161
169
func (db * DB ) GenerationPath (generation string ) string {
162
170
assert (generation != "" , "generation name required" )
163
- return filepath .Join (db .MetaPath () , "generations" , generation )
171
+ return filepath .Join (db .metaPath , "generations" , generation )
164
172
}
165
173
166
174
// ShadowWALDir returns the path of the shadow wal directory.
@@ -283,7 +291,7 @@ func (db *DB) Open() (err error) {
283
291
}
284
292
285
293
// Clear old temporary files that my have been left from a crash.
286
- if err := removeTmpFiles (db .MetaPath () ); err != nil {
294
+ if err := removeTmpFiles (db .metaPath ); err != nil {
287
295
return fmt .Errorf ("cannot remove tmp files: %w" , err )
288
296
}
289
297
@@ -446,7 +454,7 @@ func (db *DB) init() (err error) {
446
454
}
447
455
448
456
// Ensure meta directory structure exists.
449
- if err := internal .MkdirAll (db .MetaPath () , db .dirInfo ); err != nil {
457
+ if err := internal .MkdirAll (db .metaPath , db .dirInfo ); err != nil {
450
458
return err
451
459
}
452
460
@@ -522,7 +530,7 @@ func (db *DB) cleanGenerations() error {
522
530
return err
523
531
}
524
532
525
- dir := filepath .Join (db .MetaPath () , "generations" )
533
+ dir := filepath .Join (db .metaPath , "generations" )
526
534
fis , err := ioutil .ReadDir (dir )
527
535
if os .IsNotExist (err ) {
528
536
return nil
@@ -652,7 +660,7 @@ func (db *DB) createGeneration() (string, error) {
652
660
generation := hex .EncodeToString (buf )
653
661
654
662
// Generate new directory.
655
- dir := filepath .Join (db .MetaPath () , "generations" , generation )
663
+ dir := filepath .Join (db .metaPath , "generations" , generation )
656
664
if err := internal .MkdirAll (dir , db .dirInfo ); err != nil {
657
665
return "" , err
658
666
}
0 commit comments