connect creates a connection to the configured file refreshDb will delete the file before to create an empty DB if it's set to true
()
| 114 | // connect creates a connection to the configured file |
| 115 | // refreshDb will delete the file before to create an empty DB if it's set to true |
| 116 | func (b *Persistent) connect() error { |
| 117 | var err error |
| 118 | |
| 119 | err = os.MkdirAll(b.dataPath, os.ModePerm) |
| 120 | if err != nil { |
| 121 | return fmt.Errorf("failed to create a data directory %q: %w", b.dataPath, err) |
| 122 | } |
| 123 | b.db, err = bolt.Open(b.dbPath, 0644, &bolt.Options{Timeout: b.features.DbWaitTime}) |
| 124 | if err != nil { |
| 125 | return fmt.Errorf("failed to open a cache connection to %q: %w", b.dbPath, err) |
| 126 | } |
| 127 | if b.features.PurgeDb { |
| 128 | b.Purge() |
| 129 | } |
| 130 | _ = b.db.Update(func(tx *bolt.Tx) error { |
| 131 | _, _ = tx.CreateBucketIfNotExists([]byte(RootBucket)) |
| 132 | _, _ = tx.CreateBucketIfNotExists([]byte(RootTsBucket)) |
| 133 | _, _ = tx.CreateBucketIfNotExists([]byte(DataTsBucket)) |
| 134 | _, _ = tx.CreateBucketIfNotExists([]byte(tempBucket)) |
| 135 | |
| 136 | return nil |
| 137 | }) |
| 138 | |
| 139 | b.open = true |
| 140 | return nil |
| 141 | } |
| 142 | |
| 143 | // getBucket prepares and cleans a specific path of the form: /var/tmp and will iterate through each path component |
| 144 | // to get to the nested bucket of the final part (in this example: tmp) |