forked from TrueCloudLab/frostfs-node
90 lines
1.7 KiB
Go
90 lines
1.7 KiB
Go
package objectstore
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io/fs"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util"
|
|
"github.com/cockroachdb/pebble"
|
|
)
|
|
|
|
const permissions fs.FileMode = 0o644
|
|
|
|
func (s *ObjectStore) Open(ctx context.Context, m mode.Mode) error {
|
|
s.mtx.Lock()
|
|
defer s.mtx.Unlock()
|
|
|
|
if s.opened {
|
|
return nil
|
|
}
|
|
|
|
if err := util.MkdirAllX(s.cfg.blobPath, permissions); err != nil {
|
|
return fmt.Errorf("create blob dir: %w", err)
|
|
}
|
|
|
|
if err := util.MkdirAllX(s.cfg.walPath, permissions); err != nil {
|
|
return fmt.Errorf("create WAL dir: %w", err)
|
|
}
|
|
|
|
if err := util.MkdirAllX(s.cfg.metaPath, permissions); err != nil {
|
|
return fmt.Errorf("create meta dir: %w", err)
|
|
}
|
|
|
|
db, err := pebble.Open(s.cfg.metaPath, pebbleOptions(s.cfg.walPath, m.ReadOnly()))
|
|
if err != nil {
|
|
return fmt.Errorf("objectstore pebble db open: %w", err)
|
|
}
|
|
s.meta = db
|
|
s.m = m
|
|
s.opened = true
|
|
return nil
|
|
}
|
|
|
|
func (s *ObjectStore) Init(ctx context.Context) error {
|
|
s.mtx.Lock()
|
|
defer s.mtx.Unlock()
|
|
|
|
if !s.opened {
|
|
return ErrObjectStoreIsNotOpened
|
|
}
|
|
if s.initialized {
|
|
return nil
|
|
}
|
|
|
|
if err := s.validateVersion(); err != nil {
|
|
return err
|
|
}
|
|
|
|
s.initialized = true
|
|
return nil
|
|
}
|
|
|
|
func (s *ObjectStore) Close(ctx context.Context) error {
|
|
s.mtx.Lock()
|
|
defer s.mtx.Unlock()
|
|
|
|
if !s.opened {
|
|
return nil
|
|
}
|
|
if err := s.meta.Close(); err != nil {
|
|
return err
|
|
}
|
|
s.opened = false
|
|
s.initialized = false
|
|
return nil
|
|
}
|
|
|
|
func (s *ObjectStore) validateState(write bool) error {
|
|
if !s.opened {
|
|
return ErrObjectStoreIsNotOpened
|
|
}
|
|
if !s.initialized {
|
|
return ErrObjectStoreIsNotInitialized
|
|
}
|
|
if write && s.m.ReadOnly() {
|
|
return ErrObjectStoreIsReadOnly
|
|
}
|
|
return nil
|
|
}
|