From 59f7cf9873211ca09005786569b2da119ae24b8a Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 25 Nov 2020 14:18:54 +0300 Subject: [PATCH] [#211] blobstor: Implement Exists method Signed-off-by: Leonard Lyubich --- .../blobstor/delete_big.go | 7 ++- pkg/local_object_storage/blobstor/exists.go | 52 ++++++++++++++++++- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/pkg/local_object_storage/blobstor/delete_big.go b/pkg/local_object_storage/blobstor/delete_big.go index 22599471e..2f51b1d23 100644 --- a/pkg/local_object_storage/blobstor/delete_big.go +++ b/pkg/local_object_storage/blobstor/delete_big.go @@ -34,10 +34,9 @@ func (b *BlobStor) DeleteBig(prm *DeleteBigPrm) (*DeleteBigRes, error) { } func (t *fsTree) delete(addr *object.Address) error { - p := t.treePath(addr) - - if _, err := os.Stat(p); os.IsNotExist(err) { - return errFileNotFound + p, err := t.exists(addr) + if err != nil { + return err } return os.Remove(p) diff --git a/pkg/local_object_storage/blobstor/exists.go b/pkg/local_object_storage/blobstor/exists.go index b790ac921..c20b21c1c 100644 --- a/pkg/local_object_storage/blobstor/exists.go +++ b/pkg/local_object_storage/blobstor/exists.go @@ -1,5 +1,13 @@ package blobstor +import ( + "os" + + "github.com/nspcc-dev/neofs-api-go/pkg/object" + objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" + "github.com/pkg/errors" +) + // ExistsPrm groups the parameters of Exists operation. type ExistsPrm struct { address @@ -20,5 +28,47 @@ func (r ExistsRes) Exists() bool { // Returns any error encountered that did not allow // to completely check object existence. func (b *BlobStor) Exists(prm *ExistsPrm) (*ExistsRes, error) { - panic("implement me") + // check presence in shallow dir first (cheaper) + exists, err := b.existsBig(prm.addr) + if !exists { + // TODO: do smth if err != nil + + // check presence in blobovnicza + exists, err = b.existsSmall(prm.addr) + } + + if err != nil { + return nil, err + } + + return &ExistsRes{ + exists: exists, + }, err +} + +// checks if object is presented in shallow dir. +func (b *BlobStor) existsBig(addr *object.Address) (bool, error) { + _, err := b.fsTree.exists(addr) + if errors.Is(err, errFileNotFound) { + return false, nil + } + + return err == nil, err +} + +// checks if object is presented in blobovnicza. +func (b *BlobStor) existsSmall(addr *object.Address) (bool, error) { + // TODO: implement + return false, nil +} + +func (t *fsTree) exists(addr *objectSDK.Address) (string, error) { + p := t.treePath(addr) + + _, err := os.Stat(p) + if os.IsNotExist(err) { + err = errFileNotFound + } + + return p, err }