[#645] blobtree: Store path as storageID
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
parent
2f6d4a49dc
commit
4daef70774
10 changed files with 106 additions and 112 deletions
|
@ -48,27 +48,29 @@ func New(opts ...Option) *BlobTree {
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlobTree) getDirectoryPath(addr oid.Address) string {
|
func (b *BlobTree) getDir(addr oid.Address) string {
|
||||||
sAddr := addr.Object().EncodeToString() + "." + addr.Container().EncodeToString()
|
sAddr := addr.Object().EncodeToString() + "." + addr.Container().EncodeToString()
|
||||||
var sb strings.Builder
|
var sb strings.Builder
|
||||||
size := int(1+b.cfg.depth*(directoryLength+1)) + len(b.cfg.rootPath) // /path + slash + (character + slash for every level)
|
size := int(b.cfg.depth * (directoryLength + 1)) // (character + slash for every level)
|
||||||
sb.Grow(size)
|
sb.Grow(size)
|
||||||
sb.WriteString(b.cfg.rootPath)
|
|
||||||
|
|
||||||
for i := uint64(0); i < b.cfg.depth; i++ {
|
for i := uint64(0); i < b.cfg.depth; i++ {
|
||||||
sb.WriteRune(filepath.Separator)
|
if i > 0 {
|
||||||
|
sb.WriteRune(filepath.Separator)
|
||||||
|
}
|
||||||
sb.WriteString(sAddr[:directoryLength])
|
sb.WriteString(sAddr[:directoryLength])
|
||||||
sAddr = sAddr[directoryLength:]
|
sAddr = sAddr[directoryLength:]
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.WriteRune(filepath.Separator)
|
|
||||||
return sb.String()
|
return sb.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlobTree) createDir(dir string) error {
|
func (b *BlobTree) createDir(dir string, isSystemPath bool) error {
|
||||||
b.dirLock.Lock(dir)
|
b.dirLock.Lock(dir)
|
||||||
defer b.dirLock.Unlock(dir)
|
defer b.dirLock.Unlock(dir)
|
||||||
|
|
||||||
|
if !isSystemPath {
|
||||||
|
dir = b.getSystemPath(dir)
|
||||||
|
}
|
||||||
if err := util.MkdirAllX(dir, b.cfg.permissions); err != nil {
|
if err := util.MkdirAllX(dir, b.cfg.permissions); err != nil {
|
||||||
if errors.Is(err, syscall.ENOSPC) {
|
if errors.Is(err, syscall.ENOSPC) {
|
||||||
err = common.ErrNoSpace
|
err = common.ErrNoSpace
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
||||||
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
||||||
|
@ -41,7 +42,7 @@ type objectData struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlobTree) readFileContent(path string) ([]objectData, error) {
|
func (b *BlobTree) readFileContent(path string) ([]objectData, error) {
|
||||||
rawData, err := os.ReadFile(path)
|
rawData, err := os.ReadFile(b.getSystemPath(path))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return []objectData{}, nil
|
return []objectData{}, nil
|
||||||
|
@ -130,6 +131,7 @@ func (b *BlobTree) saveContentToFile(records []objectData, path string) (uint64,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlobTree) writeFile(p string, data []byte) error {
|
func (b *BlobTree) writeFile(p string, data []byte) error {
|
||||||
|
p = b.getSystemPath(p)
|
||||||
f, err := os.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_TRUNC|os.O_EXCL|os.O_SYNC, b.cfg.permissions)
|
f, err := os.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_TRUNC|os.O_EXCL|os.O_SYNC, b.cfg.permissions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -141,6 +143,10 @@ func (b *BlobTree) writeFile(p string, data []byte) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *BlobTree) getSystemPath(path string) string {
|
||||||
|
return filepath.Join(b.cfg.rootPath, path)
|
||||||
|
}
|
||||||
|
|
||||||
func (b *BlobTree) marshalSlice(records []objectData) ([]byte, error) {
|
func (b *BlobTree) marshalSlice(records []objectData) ([]byte, error) {
|
||||||
buf := make([]byte, b.estimateSize(records))
|
buf := make([]byte, b.estimateSize(records))
|
||||||
result := buf
|
result := buf
|
||||||
|
@ -191,3 +197,13 @@ func (b *BlobTree) estimateSize(records []objectData) uint64 {
|
||||||
func (b *BlobTree) getFilePath(dir string, idx uint64) string {
|
func (b *BlobTree) getFilePath(dir string, idx uint64) string {
|
||||||
return filepath.Join(dir, strconv.FormatUint(idx, 16)+dataExtension)
|
return filepath.Join(dir, strconv.FormatUint(idx, 16)+dataExtension)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *BlobTree) parsePath(path string) (string, uint64, error) {
|
||||||
|
dir := filepath.Dir(path)
|
||||||
|
fileName := strings.TrimSuffix(filepath.Base(path), dataExtension)
|
||||||
|
idx, err := strconv.ParseUint(fileName, 16, 64)
|
||||||
|
if err != nil {
|
||||||
|
return "", 0, fmt.Errorf("failed to parse blobtree path: %w", err)
|
||||||
|
}
|
||||||
|
return dir, idx, nil
|
||||||
|
}
|
||||||
|
|
|
@ -19,37 +19,35 @@ func (b *BlobTree) Open(readOnly bool) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlobTree) Init() error {
|
func (b *BlobTree) Init() error {
|
||||||
if err := b.createDir(b.cfg.rootPath); err != nil {
|
if err := b.createDir(b.cfg.rootPath, true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var eg errgroup.Group
|
var eg errgroup.Group
|
||||||
eg.SetLimit(b.cfg.initWorkersCount)
|
eg.SetLimit(b.cfg.initWorkersCount)
|
||||||
eg.Go(func() error {
|
eg.Go(func() error {
|
||||||
return b.initDir(&eg, b.cfg.rootPath, 0)
|
return b.initDir(&eg, "")
|
||||||
})
|
})
|
||||||
return eg.Wait()
|
return eg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlobTree) initDir(eg *errgroup.Group, dir string, depth uint64) error {
|
func (b *BlobTree) initDir(eg *errgroup.Group, dir string) error {
|
||||||
entities, err := os.ReadDir(dir)
|
entities, err := os.ReadDir(b.getSystemPath(dir))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, entity := range entities {
|
for _, entity := range entities {
|
||||||
if depth < b.cfg.depth && entity.IsDir() {
|
if entity.IsDir() {
|
||||||
eg.Go(func() error {
|
eg.Go(func() error {
|
||||||
return b.initDir(eg, filepath.Join(dir, entity.Name()), depth+1)
|
return b.initDir(eg, filepath.Join(dir, entity.Name()))
|
||||||
})
|
})
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if depth != b.cfg.depth {
|
path := filepath.Join(dir, entity.Name())
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if b.isTempFile(entity.Name()) {
|
if b.isTempFile(entity.Name()) {
|
||||||
if err = os.Remove(filepath.Join(dir, entity.Name())); err != nil {
|
if err = os.Remove(b.getSystemPath(path)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
|
@ -57,12 +55,12 @@ func (b *BlobTree) initDir(eg *errgroup.Group, dir string, depth uint64) error {
|
||||||
|
|
||||||
idx, err := b.parseIdx(entity.Name())
|
idx, err := b.parseIdx(entity.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
return err
|
||||||
}
|
}
|
||||||
b.dispatcher.Init(dir, idx)
|
b.dispatcher.Init(dir, idx)
|
||||||
b.cfg.metrics.IncFilesCount()
|
b.cfg.metrics.IncFilesCount()
|
||||||
|
|
||||||
stat, err := os.Stat(filepath.Join(dir, entity.Name()))
|
stat, err := os.Stat(b.getSystemPath(path))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package blobtree
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
||||||
|
@ -27,7 +28,7 @@ func (b *BlobTree) Delete(ctx context.Context, prm common.DeletePrm) (common.Del
|
||||||
trace.WithAttributes(
|
trace.WithAttributes(
|
||||||
attribute.String("path", b.cfg.rootPath),
|
attribute.String("path", b.cfg.rootPath),
|
||||||
attribute.String("address", prm.Address.EncodeToString()),
|
attribute.String("address", prm.Address.EncodeToString()),
|
||||||
attribute.String("storage_id", storageIDToIdxStringSafe(prm.StorageID)),
|
attribute.String("storage_id", string(prm.StorageID)),
|
||||||
))
|
))
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
|
@ -37,8 +38,8 @@ func (b *BlobTree) Delete(ctx context.Context, prm common.DeletePrm) (common.Del
|
||||||
|
|
||||||
var res common.DeleteRes
|
var res common.DeleteRes
|
||||||
var err error
|
var err error
|
||||||
if idx, ok := tryParseIdxFromStorageID(prm.StorageID); ok {
|
if path, ok := getPathFromStorageID(prm.StorageID); ok {
|
||||||
res, err = b.deleteFromIdx(prm.Address, idx)
|
res, err = b.deleteFromPath(prm.Address, path)
|
||||||
} else {
|
} else {
|
||||||
res, err = b.findAndDelete(prm.Address)
|
res, err = b.findAndDelete(prm.Address)
|
||||||
}
|
}
|
||||||
|
@ -46,13 +47,15 @@ func (b *BlobTree) Delete(ctx context.Context, prm common.DeletePrm) (common.Del
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlobTree) deleteFromIdx(addr oid.Address, idx uint64) (common.DeleteRes, error) {
|
func (b *BlobTree) deleteFromPath(addr oid.Address, path string) (common.DeleteRes, error) {
|
||||||
dir := b.getDirectoryPath(addr)
|
|
||||||
path := b.getFilePath(dir, idx)
|
|
||||||
|
|
||||||
b.fileLock.Lock(path)
|
b.fileLock.Lock(path)
|
||||||
defer b.fileLock.Unlock(path)
|
defer b.fileLock.Unlock(path)
|
||||||
|
|
||||||
|
dir, idx, err := b.parsePath(path)
|
||||||
|
if err != nil {
|
||||||
|
return common.DeleteRes{}, err
|
||||||
|
}
|
||||||
|
|
||||||
records, err := b.readFileContent(path)
|
records, err := b.readFileContent(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.DeleteRes{}, err
|
return common.DeleteRes{}, err
|
||||||
|
@ -71,7 +74,7 @@ func (b *BlobTree) deleteFromIdx(addr oid.Address, idx uint64) (common.DeleteRes
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(records) == 1 {
|
if len(records) == 1 {
|
||||||
err = os.Remove(path)
|
err = os.Remove(b.getSystemPath(path))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
b.dispatcher.ReturnIdx(dir, idx)
|
b.dispatcher.ReturnIdx(dir, idx)
|
||||||
b.cfg.metrics.DecFilesCount()
|
b.cfg.metrics.DecFilesCount()
|
||||||
|
@ -79,7 +82,7 @@ func (b *BlobTree) deleteFromIdx(addr oid.Address, idx uint64) (common.DeleteRes
|
||||||
return common.DeleteRes{}, err
|
return common.DeleteRes{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
records = append(records[:idx], records[idx+1:]...)
|
records = append(records[:deleteIdx], records[deleteIdx+1:]...)
|
||||||
size, err := b.writeToTmpAndRename(records, path)
|
size, err := b.writeToTmpAndRename(records, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.DeleteRes{}, err
|
return common.DeleteRes{}, err
|
||||||
|
@ -91,16 +94,16 @@ func (b *BlobTree) deleteFromIdx(addr oid.Address, idx uint64) (common.DeleteRes
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlobTree) findAndDelete(addr oid.Address) (common.DeleteRes, error) {
|
func (b *BlobTree) findAndDelete(addr oid.Address) (common.DeleteRes, error) {
|
||||||
dir := b.getDirectoryPath(addr)
|
dir := b.getDir(addr)
|
||||||
idx, err := b.findFileIdx(dir, addr)
|
idx, err := b.findFileIdx(dir, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.DeleteRes{}, err
|
return common.DeleteRes{}, err
|
||||||
}
|
}
|
||||||
return b.deleteFromIdx(addr, idx)
|
return b.deleteFromPath(addr, b.getFilePath(dir, idx))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlobTree) findFileIdx(dir string, addr oid.Address) (uint64, error) {
|
func (b *BlobTree) findFileIdx(dir string, addr oid.Address) (uint64, error) {
|
||||||
entities, err := os.ReadDir(dir)
|
entities, err := os.ReadDir(filepath.Join(b.cfg.rootPath, dir))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return 0, logicerr.Wrap(new(apistatus.ObjectNotFound))
|
return 0, logicerr.Wrap(new(apistatus.ObjectNotFound))
|
||||||
|
|
|
@ -26,25 +26,22 @@ func (b *BlobTree) Exists(ctx context.Context, prm common.ExistsPrm) (common.Exi
|
||||||
trace.WithAttributes(
|
trace.WithAttributes(
|
||||||
attribute.String("path", b.cfg.rootPath),
|
attribute.String("path", b.cfg.rootPath),
|
||||||
attribute.String("address", prm.Address.EncodeToString()),
|
attribute.String("address", prm.Address.EncodeToString()),
|
||||||
attribute.String("storage_id", storageIDToIdxStringSafe(prm.StorageID)),
|
attribute.String("storage_id", string(prm.StorageID)),
|
||||||
))
|
))
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
var res common.ExistsRes
|
var res common.ExistsRes
|
||||||
var err error
|
var err error
|
||||||
if idx, ok := tryParseIdxFromStorageID(prm.StorageID); ok {
|
if path, ok := getPathFromStorageID(prm.StorageID); ok {
|
||||||
res, err = b.existsFromIdx(prm.Address, idx)
|
res, err = b.existsFromPath(prm.Address, path)
|
||||||
} else {
|
} else {
|
||||||
res, err = b.findAndCheck(prm.Address)
|
res, err = b.findAndCheckExistance(prm.Address)
|
||||||
}
|
}
|
||||||
success = err == nil
|
success = err == nil
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlobTree) existsFromIdx(addr oid.Address, idx uint64) (common.ExistsRes, error) {
|
func (b *BlobTree) existsFromPath(addr oid.Address, path string) (common.ExistsRes, error) {
|
||||||
dir := b.getDirectoryPath(addr)
|
|
||||||
path := b.getFilePath(dir, idx)
|
|
||||||
|
|
||||||
b.fileLock.RLock(path)
|
b.fileLock.RLock(path)
|
||||||
defer b.fileLock.RUnlock(path)
|
defer b.fileLock.RUnlock(path)
|
||||||
|
|
||||||
|
@ -64,15 +61,16 @@ func (b *BlobTree) existsFromIdx(addr oid.Address, idx uint64) (common.ExistsRes
|
||||||
return common.ExistsRes{}, nil
|
return common.ExistsRes{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlobTree) findAndCheck(addr oid.Address) (common.ExistsRes, error) {
|
func (b *BlobTree) findAndCheckExistance(addr oid.Address) (common.ExistsRes, error) {
|
||||||
dir := b.getDirectoryPath(addr)
|
dir := b.getDir(addr)
|
||||||
_, err := b.findFileIdx(dir, addr)
|
_, err := b.findFileIdx(dir, addr)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
var notFound *apistatus.ObjectNotFound
|
return common.ExistsRes{Exists: true}, nil
|
||||||
if errors.As(err, ¬Found) {
|
|
||||||
return common.ExistsRes{}, nil
|
|
||||||
}
|
|
||||||
return common.ExistsRes{}, err
|
|
||||||
}
|
}
|
||||||
return common.ExistsRes{Exists: true}, nil
|
|
||||||
|
var notFound *apistatus.ObjectNotFound
|
||||||
|
if errors.As(err, ¬Found) {
|
||||||
|
return common.ExistsRes{}, nil
|
||||||
|
}
|
||||||
|
return common.ExistsRes{}, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package blobtree
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
||||||
|
@ -29,7 +30,7 @@ func (b *BlobTree) Get(ctx context.Context, prm common.GetPrm) (common.GetRes, e
|
||||||
trace.WithAttributes(
|
trace.WithAttributes(
|
||||||
attribute.String("path", b.cfg.rootPath),
|
attribute.String("path", b.cfg.rootPath),
|
||||||
attribute.String("address", prm.Address.EncodeToString()),
|
attribute.String("address", prm.Address.EncodeToString()),
|
||||||
attribute.String("storage_id", storageIDToIdxStringSafe(prm.StorageID)),
|
attribute.String("storage_id", string(prm.StorageID)),
|
||||||
attribute.Bool("raw", prm.Raw),
|
attribute.Bool("raw", prm.Raw),
|
||||||
))
|
))
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
@ -41,16 +42,13 @@ func (b *BlobTree) Get(ctx context.Context, prm common.GetPrm) (common.GetRes, e
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlobTree) get(prm common.GetPrm) (common.GetRes, error) {
|
func (b *BlobTree) get(prm common.GetPrm) (common.GetRes, error) {
|
||||||
if idx, ok := tryParseIdxFromStorageID(prm.StorageID); ok {
|
if path, ok := getPathFromStorageID(prm.StorageID); ok {
|
||||||
return b.getFromIdx(prm.Address, idx)
|
return b.getFromPath(prm.Address, path)
|
||||||
}
|
}
|
||||||
return b.findAndGet(prm.Address)
|
return b.findAndGet(prm.Address)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlobTree) getFromIdx(addr oid.Address, idx uint64) (common.GetRes, error) {
|
func (b *BlobTree) getFromPath(addr oid.Address, path string) (common.GetRes, error) {
|
||||||
dir := b.getDirectoryPath(addr)
|
|
||||||
path := b.getFilePath(dir, idx)
|
|
||||||
|
|
||||||
b.fileLock.RLock(path)
|
b.fileLock.RLock(path)
|
||||||
defer b.fileLock.RUnlock(path)
|
defer b.fileLock.RUnlock(path)
|
||||||
|
|
||||||
|
@ -82,8 +80,8 @@ func (b *BlobTree) unmarshalGetRes(record objectData) (common.GetRes, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlobTree) findAndGet(addr oid.Address) (common.GetRes, error) {
|
func (b *BlobTree) findAndGet(addr oid.Address) (common.GetRes, error) {
|
||||||
dir := b.getDirectoryPath(addr)
|
dir := b.getDir(addr)
|
||||||
entities, err := os.ReadDir(dir)
|
entities, err := os.ReadDir(filepath.Join(b.cfg.rootPath, dir))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return common.GetRes{}, logicerr.Wrap(new(apistatus.ObjectNotFound))
|
return common.GetRes{}, logicerr.Wrap(new(apistatus.ObjectNotFound))
|
||||||
|
@ -97,11 +95,7 @@ func (b *BlobTree) findAndGet(addr oid.Address) (common.GetRes, error) {
|
||||||
if b.isTempFile(entity.Name()) {
|
if b.isTempFile(entity.Name()) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
idx, err := b.parseIdx(entity.Name())
|
path := filepath.Join(dir, entity.Name())
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
path := b.getFilePath(dir, idx)
|
|
||||||
res, err := b.tryReadObject(path, addr)
|
res, err := b.tryReadObject(path, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.GetRes{}, err
|
return common.GetRes{}, err
|
||||||
|
|
|
@ -27,7 +27,7 @@ func (b *BlobTree) GetRange(ctx context.Context, prm common.GetRangePrm) (common
|
||||||
trace.WithAttributes(
|
trace.WithAttributes(
|
||||||
attribute.String("path", b.cfg.rootPath),
|
attribute.String("path", b.cfg.rootPath),
|
||||||
attribute.String("address", prm.Address.EncodeToString()),
|
attribute.String("address", prm.Address.EncodeToString()),
|
||||||
attribute.String("storage_id", storageIDToIdxStringSafe(prm.StorageID)),
|
attribute.String("storage_id", string(prm.StorageID)),
|
||||||
attribute.String("offset", strconv.FormatUint(prm.Range.GetOffset(), 10)),
|
attribute.String("offset", strconv.FormatUint(prm.Range.GetOffset(), 10)),
|
||||||
attribute.String("length", strconv.FormatUint(prm.Range.GetLength(), 10)),
|
attribute.String("length", strconv.FormatUint(prm.Range.GetLength(), 10)),
|
||||||
))
|
))
|
||||||
|
|
|
@ -28,12 +28,12 @@ func (b *BlobTree) Iterate(ctx context.Context, prm common.IteratePrm) (common.I
|
||||||
))
|
))
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
err = b.iterateDir(b.cfg.rootPath, 0, prm)
|
err = b.iterateDir("", prm)
|
||||||
return common.IterateRes{}, err
|
return common.IterateRes{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlobTree) iterateDir(dir string, depth uint64, prm common.IteratePrm) error {
|
func (b *BlobTree) iterateDir(dir string, prm common.IteratePrm) error {
|
||||||
entities, err := os.ReadDir(dir)
|
entities, err := os.ReadDir(filepath.Join(b.cfg.rootPath, dir))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if prm.IgnoreErrors {
|
if prm.IgnoreErrors {
|
||||||
return nil
|
return nil
|
||||||
|
@ -42,24 +42,20 @@ func (b *BlobTree) iterateDir(dir string, depth uint64, prm common.IteratePrm) e
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, entity := range entities {
|
for _, entity := range entities {
|
||||||
if depth < b.cfg.depth && entity.IsDir() {
|
if entity.IsDir() {
|
||||||
err := b.iterateDir(filepath.Join(dir, entity.Name()), depth+1, prm)
|
err := b.iterateDir(filepath.Join(dir, entity.Name()), prm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if depth != b.cfg.depth {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.isTempFile(entity.Name()) {
|
if b.isTempFile(entity.Name()) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
idx, err := b.parseIdx(entity.Name())
|
|
||||||
if err != nil {
|
path := filepath.Join(dir, entity.Name())
|
||||||
continue
|
err = b.iterateRecords(path, prm)
|
||||||
}
|
|
||||||
path := b.getFilePath(dir, idx)
|
|
||||||
err = b.iterateRecords(idx, path, prm)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -67,7 +63,7 @@ func (b *BlobTree) iterateDir(dir string, depth uint64, prm common.IteratePrm) e
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlobTree) iterateRecords(idx uint64, path string, prm common.IteratePrm) error {
|
func (b *BlobTree) iterateRecords(path string, prm common.IteratePrm) error {
|
||||||
b.fileLock.RLock(path)
|
b.fileLock.RLock(path)
|
||||||
defer b.fileLock.RUnlock(path)
|
defer b.fileLock.RUnlock(path)
|
||||||
|
|
||||||
|
@ -103,7 +99,7 @@ func (b *BlobTree) iterateRecords(idx uint64, path string, prm common.IteratePrm
|
||||||
err = prm.Handler(common.IterationElement{
|
err = prm.Handler(common.IterationElement{
|
||||||
Address: record.Address,
|
Address: record.Address,
|
||||||
ObjectData: record.Data,
|
ObjectData: record.Data,
|
||||||
StorageID: idxToStorageID(idx),
|
StorageID: pathToStorageID(path),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -37,9 +37,9 @@ func (b *BlobTree) Put(ctx context.Context, prm common.PutPrm) (common.PutRes, e
|
||||||
return common.PutRes{}, common.ErrReadOnly
|
return common.PutRes{}, common.ErrReadOnly
|
||||||
}
|
}
|
||||||
|
|
||||||
dir := b.getDirectoryPath(prm.Address)
|
dir := b.getDir(prm.Address)
|
||||||
|
|
||||||
if err := b.createDir(dir); err != nil {
|
if err := b.createDir(dir, false); err != nil {
|
||||||
return common.PutRes{}, err
|
return common.PutRes{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ func (b *BlobTree) Put(ctx context.Context, prm common.PutPrm) (common.PutRes, e
|
||||||
prm.RawData = b.compressor.Compress(prm.RawData)
|
prm.RawData = b.compressor.Compress(prm.RawData)
|
||||||
}
|
}
|
||||||
|
|
||||||
idx, err := b.saveToFile(prm, dir)
|
path, err := b.saveToLocalDir(prm, dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.PutRes{}, err
|
return common.PutRes{}, err
|
||||||
}
|
}
|
||||||
|
@ -55,10 +55,10 @@ func (b *BlobTree) Put(ctx context.Context, prm common.PutPrm) (common.PutRes, e
|
||||||
success = true
|
success = true
|
||||||
size = len(prm.RawData)
|
size = len(prm.RawData)
|
||||||
|
|
||||||
return common.PutRes{StorageID: idxToStorageID(idx)}, nil
|
return common.PutRes{StorageID: pathToStorageID(path)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlobTree) saveToFile(prm common.PutPrm, dir string) (uint64, error) {
|
func (b *BlobTree) saveToLocalDir(prm common.PutPrm, dir string) (string, error) {
|
||||||
returnIdx := true
|
returnIdx := true
|
||||||
idx := b.dispatcher.GetIdxForWrite(dir)
|
idx := b.dispatcher.GetIdxForWrite(dir)
|
||||||
path := b.getFilePath(dir, idx)
|
path := b.getFilePath(dir, idx)
|
||||||
|
@ -74,7 +74,7 @@ func (b *BlobTree) saveToFile(prm common.PutPrm, dir string) (uint64, error) {
|
||||||
|
|
||||||
currentContent, err := b.readFileContent(path)
|
currentContent, err := b.readFileContent(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return "", err
|
||||||
}
|
}
|
||||||
var newRecord objectData
|
var newRecord objectData
|
||||||
newRecord.Address = prm.Address
|
newRecord.Address = prm.Address
|
||||||
|
@ -82,24 +82,24 @@ func (b *BlobTree) saveToFile(prm common.PutPrm, dir string) (uint64, error) {
|
||||||
|
|
||||||
size, err := b.writeToTmpAndRename(append(currentContent, newRecord), path)
|
size, err := b.writeToTmpAndRename(append(currentContent, newRecord), path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return "", err
|
||||||
}
|
}
|
||||||
returnIdx = size < b.cfg.targetFileSizeBytes
|
returnIdx = size < b.cfg.targetFileSizeBytes
|
||||||
|
|
||||||
return idx, nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlobTree) writeToTmpAndRename(records []objectData, path string) (uint64, error) {
|
func (b *BlobTree) writeToTmpAndRename(records []objectData, path string) (uint64, error) {
|
||||||
tmpFile := path + tempFileSymbols + strconv.FormatUint(b.suffix.Add(1), 16)
|
tmpPath := path + tempFileSymbols + strconv.FormatUint(b.suffix.Add(1), 16)
|
||||||
|
|
||||||
size, err := b.saveContentToFile(records, tmpFile)
|
size, err := b.saveContentToFile(records, tmpPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = os.Remove(tmpFile)
|
_ = os.Remove(b.getSystemPath(tmpPath))
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
newFile := false
|
newFile := false
|
||||||
_, err = os.Stat(path)
|
_, err = os.Stat(b.getSystemPath(path))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
newFile = true
|
newFile = true
|
||||||
|
@ -108,8 +108,8 @@ func (b *BlobTree) writeToTmpAndRename(records []objectData, path string) (uint6
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.Rename(tmpFile, path); err != nil {
|
if err := os.Rename(b.getSystemPath(tmpPath), b.getSystemPath(path)); err != nil {
|
||||||
_ = os.Remove(tmpFile)
|
_ = os.Remove(b.getSystemPath(tmpPath))
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,28 +1,15 @@
|
||||||
package blobtree
|
package blobtree
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"strings"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const storageIDLength = 8
|
const storageIDPrefix = "blobtree:"
|
||||||
|
|
||||||
func tryParseIdxFromStorageID(storageID []byte) (uint64, bool) {
|
func getPathFromStorageID(storageID []byte) (string, bool) {
|
||||||
if len(storageID) == storageIDLength {
|
return strings.CutPrefix(string(storageID), storageIDPrefix)
|
||||||
return binary.LittleEndian.Uint64(storageID), true
|
|
||||||
}
|
|
||||||
return 0, false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func storageIDToIdxStringSafe(storageID []byte) string {
|
func pathToStorageID(path string) []byte {
|
||||||
if len(storageID) == storageIDLength {
|
return []byte(storageIDPrefix + path)
|
||||||
return strconv.FormatUint(binary.LittleEndian.Uint64(storageID), 10)
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func idxToStorageID(idx uint64) []byte {
|
|
||||||
storageID := make([]byte, storageIDLength)
|
|
||||||
binary.LittleEndian.PutUint64(storageID, idx)
|
|
||||||
return storageID
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue