2020-11-26 14:26:53 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2022-06-01 10:20:51 +00:00
|
|
|
"os"
|
2022-02-02 13:28:08 +00:00
|
|
|
"path/filepath"
|
2020-11-26 14:26:53 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util"
|
2020-11-26 14:26:53 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Open opens an internal database at the configured path with the configured permissions.
|
2020-11-26 14:26:53 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// If the database file does not exist, it will be created automatically.
|
2020-11-26 14:26:53 +00:00
|
|
|
func (b *Blobovnicza) Open() error {
|
|
|
|
b.log.Debug("creating directory for BoltDB",
|
|
|
|
zap.String("path", b.path),
|
2021-09-14 14:44:07 +00:00
|
|
|
zap.Bool("ro", b.boltOptions.ReadOnly),
|
2020-11-26 14:26:53 +00:00
|
|
|
)
|
|
|
|
|
2021-09-14 14:44:07 +00:00
|
|
|
var err error
|
2020-11-26 14:26:53 +00:00
|
|
|
|
2021-09-14 14:44:07 +00:00
|
|
|
if !b.boltOptions.ReadOnly {
|
2022-02-02 13:28:08 +00:00
|
|
|
err = util.MkdirAllX(filepath.Dir(b.path), b.perm)
|
2021-09-14 14:44:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 14:44:07 +00:00
|
|
|
b.log.Debug("opening BoltDB",
|
|
|
|
zap.String("path", b.path),
|
|
|
|
zap.Stringer("permissions", b.perm),
|
|
|
|
)
|
|
|
|
|
|
|
|
b.boltDB, err = bbolt.Open(b.path, b.perm, b.boltOptions)
|
|
|
|
|
2020-11-26 14:26:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes internal database structure.
|
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// If Blobovnicza is already initialized, no action is taken.
|
2021-09-14 14:44:07 +00:00
|
|
|
//
|
|
|
|
// Should not be called in read-only configuration.
|
2020-11-26 14:26:53 +00:00
|
|
|
func (b *Blobovnicza) Init() error {
|
|
|
|
b.log.Debug("initializing...",
|
|
|
|
zap.Uint64("object size limit", b.objSizeLimit),
|
|
|
|
zap.Uint64("storage size limit", b.fullSizeLimit),
|
|
|
|
)
|
|
|
|
|
2022-01-19 13:17:36 +00:00
|
|
|
if size := b.filled.Load(); size != 0 {
|
|
|
|
b.log.Debug("already initialized", zap.Uint64("size", size))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := b.boltDB.Update(func(tx *bbolt.Tx) error {
|
2020-11-26 14:26:53 +00:00
|
|
|
return b.iterateBucketKeys(func(lower, upper uint64, key []byte) (bool, error) {
|
|
|
|
// create size range bucket
|
|
|
|
|
2022-01-19 13:17:36 +00:00
|
|
|
rangeStr := stringifyBounds(lower, upper)
|
2020-11-26 14:26:53 +00:00
|
|
|
b.log.Debug("creating bucket for size range",
|
2022-01-19 13:17:36 +00:00
|
|
|
zap.String("range", rangeStr))
|
2020-11-26 14:26:53 +00:00
|
|
|
|
2022-06-01 10:20:51 +00:00
|
|
|
_, err := tx.CreateBucketIfNotExists(key)
|
2021-05-18 08:12:51 +00:00
|
|
|
if err != nil {
|
2022-01-19 13:17:36 +00:00
|
|
|
return false, fmt.Errorf("(%T) could not create bucket for bounds %s: %w",
|
|
|
|
b, rangeStr, err)
|
2021-05-18 08:12:51 +00:00
|
|
|
}
|
2022-01-19 13:17:36 +00:00
|
|
|
|
2021-05-18 08:12:51 +00:00
|
|
|
return false, nil
|
2020-11-26 14:26:53 +00:00
|
|
|
})
|
|
|
|
})
|
2022-06-01 10:20:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := os.Stat(b.path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't determine DB size: %w", err)
|
|
|
|
}
|
2022-01-19 13:17:36 +00:00
|
|
|
|
2022-06-01 10:20:51 +00:00
|
|
|
b.filled.Store(uint64(info.Size()))
|
2022-01-19 13:17:36 +00:00
|
|
|
return err
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close releases all internal database resources.
|
|
|
|
func (b *Blobovnicza) Close() error {
|
2020-12-11 07:59:28 +00:00
|
|
|
b.log.Debug("closing BoltDB",
|
|
|
|
zap.String("path", b.path),
|
|
|
|
)
|
2020-11-26 14:26:53 +00:00
|
|
|
|
|
|
|
return b.boltDB.Close()
|
|
|
|
}
|