Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zhijian-pro committed Nov 14, 2024
1 parent 04f7998 commit 5b5c9fe
Show file tree
Hide file tree
Showing 39 changed files with 163 additions and 266 deletions.
2 changes: 1 addition & 1 deletion cmd/objbench.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ func functionalTesting(blob object.ObjectStorage, result *[][]string, colorful b
if err := blob.Put(key, bytes.NewReader([]byte("1"))); err != nil {
return fmt.Errorf("put encode file failed: %s", err)
} else {
if resp, err := blob.List("", "测试编码文件", "", 1, true); err != nil && err != utils.ENOTSUP {
if resp, _, _, err := blob.List("", "测试编码文件", "", "", 1, true); err != nil && err != utils.ENOTSUP {
return fmt.Errorf("list encode file failed %s", err)
} else if len(resp) == 1 && resp[0].Key() != key {
return fmt.Errorf("list encode file failed: expect key %s, but got %s", key, resp[0].Key())
Expand Down
18 changes: 11 additions & 7 deletions cmd/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ func (j *juiceFS) Head(key string) (object.Object, error) {
return &jObj{key, fi}, nil
}

func (j *juiceFS) List(prefix, marker, delimiter string, limit int64, followLink bool) ([]object.Object, error) {
func (j *juiceFS) List(prefix, marker, token, delimiter string, limit int64, followLink bool) ([]object.Object, bool, string, error) {
if delimiter != "/" {
return nil, utils.ENOTSUP
return nil, false, "", utils.ENOTSUP
}
dir := j.path(prefix)
var objs []object.Object
Expand All @@ -229,18 +229,18 @@ func (j *juiceFS) List(prefix, marker, delimiter string, limit int64, followLink
obj, err := j.Head(prefix)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
return nil, false, "", nil
}
return nil, err
return nil, false, "", err
}
objs = append(objs, obj)
}
entries, err := j.readDirSorted(dir, followLink)
if err != 0 {
if err == syscall.ENOENT {
return nil, nil
return nil, false, "", nil
}
return nil, err
return nil, false, "", err
}
for _, e := range entries {
key := dir[1:] + e.name
Expand All @@ -253,7 +253,11 @@ func (j *juiceFS) List(prefix, marker, delimiter string, limit int64, followLink
break
}
}
return objs, nil
var nextMarker string
if len(objs) > 0 {
nextMarker = objs[len(objs)-1].Key()
}
return objs, nextMarker != "", nextMarker, nil
}

type mEntry struct {
Expand Down
7 changes: 1 addition & 6 deletions pkg/object/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (

type wasb struct {
DefaultObjectStorage
hasV2
container *container.Client
azblobCli *azblob.Client
sc string
Expand Down Expand Up @@ -136,11 +135,7 @@ func (b *wasb) Delete(key string, getters ...AttrGetter) error {
return err
}

func (b *wasb) List(prefix, marker, delimiter string, limit int64, followLink bool) ([]Object, error) {
return b.hasV2.List(b, prefix, marker, delimiter, limit, followLink)
}

func (b *wasb) ListV2(prefix, startAfter, token, delimiter string, limit int64, followLink bool) ([]Object, bool, string, error) {
func (b *wasb) List(prefix, startAfter, token, delimiter string, limit int64, followLink bool) ([]Object, bool, string, error) {
if delimiter != "" {
return nil, false, "", notSupported
}
Expand Down
7 changes: 1 addition & 6 deletions pkg/object/b2.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (

type b2client struct {
DefaultObjectStorage
hasV2
bucket *backblaze.Bucket
}

Expand Down Expand Up @@ -122,11 +121,7 @@ func (c *b2client) Delete(key string, getters ...AttrGetter) error {
return err
}

func (c *b2client) List(prefix, marker, delimiter string, limit int64, followLink bool) ([]Object, error) {
return c.hasV2.List(c, prefix, marker, delimiter, limit, followLink)
}

func (c *b2client) ListV2(prefix, startAfter, token, delimiter string, limit int64, followLink bool) ([]Object, bool, string, error) {
func (c *b2client) List(prefix, startAfter, token, delimiter string, limit int64, followLink bool) ([]Object, bool, string, error) {
if limit > 1000 {
limit = 1000
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/object/bos.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,7 @@ func (q *bosclient) Delete(key string, getters ...AttrGetter) error {
return err
}

func (q *bosclient) List(prefix, marker, delimiter string, limit int64, followLink bool) ([]Object, error) {
return retryListV2(q, prefix, marker, delimiter, limit, followLink)
}

func (q *bosclient) ListV2(prefix, start, token, delimiter string, limit int64, followLink bool) ([]Object, bool, string, error) {
func (q *bosclient) List(prefix, start, token, delimiter string, limit int64, followLink bool) ([]Object, bool, string, error) {
if limit > 1000 {
limit = 1000
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/object/bunny.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (b *bunnyClient) Delete(key string, getters ...AttrGetter) error {
return err
}

func (b *bunnyClient) List(prefix, marker, delimiter string, limit int64, followLink bool) ([]Object, error) {
func (b *bunnyClient) List(prefix, marker, token, delimiter string, limit int64, followLink bool) ([]Object, bool, string, error) {
if delimiter != "/" {
return nil, notSupported
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/object/cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,7 @@ func (c *COS) Delete(key string, getters ...AttrGetter) error {
return err
}

func (c *COS) List(prefix, marker, delimiter string, limit int64, followLink bool) ([]Object, error) {
return retryListV2(c, prefix, marker, delimiter, limit, followLink)
}

func (c *COS) ListV2(prefix, start, token, delimiter string, limit int64, followLink bool) ([]Object, bool, string, error) {
func (c *COS) List(prefix, start, token, delimiter string, limit int64, followLink bool) ([]Object, bool, string, error) {
param := cos.BucketGetOptions{
Prefix: prefix,
Marker: start,
Expand Down
17 changes: 9 additions & 8 deletions pkg/object/dragonfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (d *dragonfly) String() string {

// Create creates the object if it does not exist.
func (d *dragonfly) Create() error {
if _, err := d.List("", "", "", 1, false); err == nil {
if _, _, _, err := d.List("", "", "", "", 1, false); err == nil {
return nil
}

Expand Down Expand Up @@ -438,14 +438,14 @@ func (d *dragonfly) Delete(key string, getters ...AttrGetter) error {
}

// List lists the objects with the given prefix.
func (d *dragonfly) List(prefix, marker, delimiter string, limit int64, followLink bool) ([]Object, error) {
func (d *dragonfly) List(prefix, marker, token, delimiter string, limit int64, followLink bool) ([]Object, bool, string, error) {
if limit > MaxGetObjectMetadatasLimit {
limit = MaxGetObjectMetadatasLimit
}

u, err := url.Parse(d.endpoint)
if err != nil {
return nil, err
return nil, false, "", err
}

u.Path = path.Join("buckets", d.bucket, "metadatas")
Expand All @@ -469,23 +469,23 @@ func (d *dragonfly) List(prefix, marker, delimiter string, limit int64, followLi
u.RawQuery = query.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
return nil, false, "", err
}

// List object.
resp, err := d.client.Do(req)
if err != nil {
return nil, err
return nil, false, "", err
}
defer resp.Body.Close()

if resp.StatusCode/100 != 2 {
return nil, fmt.Errorf("bad response status %s", resp.Status)
return nil, false, "", fmt.Errorf("bad response status %s", resp.Status)
}

var objectMetadatas ObjectMetadatas
if err := json.NewDecoder(resp.Body).Decode(&objectMetadatas); err != nil {
return nil, err
return nil, false, "", err
}

objs := make([]Object, 0, len(objectMetadatas.Metadatas))
Expand All @@ -506,7 +506,8 @@ func (d *dragonfly) List(prefix, marker, delimiter string, limit int64, followLi
sort.Slice(objs, func(i, j int) bool { return objs[i].Key() < objs[j].Key() })
}

return objs, err
hasMore, nextMarker := generateListResult(objs)
return objs, hasMore, nextMarker, err
}

// newDragonfly creates a new dragonfly object storage.
Expand Down
6 changes: 1 addition & 5 deletions pkg/object/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,7 @@ func genNextKey(key string) string {
return string(next)
}

func (c *etcdClient) List(prefix, marker, delimiter string, limit int64, followLink bool) ([]Object, error) {
return retryListV2(c, prefix, marker, delimiter, limit, followLink)
}

func (c *etcdClient) ListV2(prefix, start, token, delimiter string, limit int64, followLink bool) ([]Object, bool, string, error) {
func (c *etcdClient) List(prefix, start, token, delimiter string, limit int64, followLink bool) ([]Object, bool, string, error) {
if delimiter != "" {
return nil, false, "", notSupported
}
Expand Down
17 changes: 9 additions & 8 deletions pkg/object/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ func readDirSorted(dir string, followLink bool) ([]*mEntry, error) {
return mEntries, err
}

func (d *filestore) List(prefix, marker, delimiter string, limit int64, followLink bool) ([]Object, error) {
func (d *filestore) List(prefix, marker, token, delimiter string, limit int64, followLink bool) ([]Object, bool, string, error) {
if delimiter != "/" {
return nil, notSupported
return nil, false, "", notSupported
}
var dir string = d.root + prefix
var objs []Object
Expand All @@ -283,22 +283,22 @@ func (d *filestore) List(prefix, marker, delimiter string, limit int64, followLi
obj, err := d.Head(prefix)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
return nil, false, "", nil
}
return nil, err
return nil, false, "", err
}
objs = append(objs, obj)
}
entries, err := readDirSorted(dir, followLink)
if err != nil {
if os.IsPermission(err) {
logger.Warnf("skip %s: %s", dir, err)
return nil, nil
return nil, false, "", nil
}
if os.IsNotExist(err) {
return nil, nil
return nil, false, "", nil
}
return nil, err
return nil, false, "", err
}
for _, e := range entries {
p := path.Join(dir, e.Name())
Expand All @@ -319,7 +319,8 @@ func (d *filestore) List(prefix, marker, delimiter string, limit int64, followLi
break
}
}
return objs, nil
hasMore, nextMarker := generateListResult(objs)
return objs, hasMore, nextMarker, err
}

func (d *filestore) Chmod(key string, mode os.FileMode) error {
Expand Down
17 changes: 9 additions & 8 deletions pkg/object/gluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ func (g *gluster) readDirSorted(dirname string, followLink bool) ([]*mEntry, err
return mEntries, err
}

func (g *gluster) List(prefix, marker, delimiter string, limit int64, followLink bool) ([]Object, error) {
func (g *gluster) List(prefix, marker, token, delimiter string, limit int64, followLink bool) ([]Object, bool, string, error) {
if delimiter != "/" {
return nil, notSupported
return nil, false, "", notSupported
}
var dir string = prefix
var objs []Object
Expand All @@ -213,22 +213,22 @@ func (g *gluster) List(prefix, marker, delimiter string, limit int64, followLink
obj, err := g.Head(prefix)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
return nil, false, "", nil
}
return nil, err
return nil, false, "", err
}
objs = append(objs, obj)
}
entries, err := g.readDirSorted(dir, followLink)
if err != nil {
if os.IsPermission(err) {
logger.Warnf("skip %s: %s", dir, err)
return nil, nil
return nil, false, "", nil
}
if os.IsNotExist(err) {
return nil, nil
return nil, false, "", nil
}
return nil, err
return nil, false, "", err
}
for _, e := range entries {
p := filepath.Join(dir, e.Name())
Expand All @@ -246,7 +246,8 @@ func (g *gluster) List(prefix, marker, delimiter string, limit int64, followLink
break
}
}
return objs, nil
hasMore, nextMarker := generateListResult(objs)
return objs, hasMore, nextMarker, nil
}

func (g *gluster) Chtimes(path string, mtime time.Time) error {
Expand Down
10 changes: 2 additions & 8 deletions pkg/object/gs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (

type gs struct {
DefaultObjectStorage
hasV2
clients []*storage.Client
index uint64
bucket string
Expand All @@ -62,10 +61,9 @@ func (g *gs) getClient() *storage.Client {

func (g *gs) Create() error {
// check if the bucket is already exists
if objs, err := g.List("", "", "", 1, true); err == nil && len(objs) > 0 {
if objs, _, _, err := g.List("", "", "", "", 1, true); err == nil && len(objs) > 0 {
return nil
}

projectID := os.Getenv("GOOGLE_CLOUD_PROJECT")
if projectID == "" {
projectID, _ = metadata.ProjectID()
Expand Down Expand Up @@ -166,11 +164,7 @@ func (g *gs) Delete(key string, getters ...AttrGetter) error {
return nil
}

func (g *gs) List(prefix, marker, delimiter string, limit int64, followLink bool) ([]Object, error) {
return g.hasV2.List(g, prefix, marker, delimiter, limit, followLink)
}

func (g *gs) ListV2(prefix, start, token, delimiter string, limit int64, followLink bool) ([]Object, bool, string, error) {
func (g *gs) List(prefix, start, token, delimiter string, limit int64, followLink bool) ([]Object, bool, string, error) {
objectIterator := g.getClient().Bucket(g.bucket).Objects(ctx, &storage.Query{Prefix: prefix, Delimiter: delimiter, StartOffset: start})
pager := iterator.NewPager(objectIterator, int(limit), token)
var entries []*storage.ObjectAttrs
Expand Down
17 changes: 9 additions & 8 deletions pkg/object/hdfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ func (h *hdfsclient) Delete(key string, getters ...AttrGetter) error {
return err
}

func (h *hdfsclient) List(prefix, marker, delimiter string, limit int64, followLink bool) ([]Object, error) {
func (h *hdfsclient) List(prefix, marker, token, delimiter string, limit int64, followLink bool) ([]Object, bool, string, error) {
if delimiter != "/" {
return nil, notSupported
return nil, false, "", notSupported
}
dir := h.path(prefix)
var objs []Object
Expand All @@ -201,9 +201,9 @@ func (h *hdfsclient) List(prefix, marker, delimiter string, limit int64, followL
obj, err := h.Head(prefix)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
return nil, false, "", nil
}
return nil, err
return nil, false, "", err
}
objs = append(objs, obj)
}
Expand All @@ -216,12 +216,12 @@ func (h *hdfsclient) List(prefix, marker, delimiter string, limit int64, followL
if err != nil {
if os.IsPermission(err) {
logger.Warnf("skip %s: %s", dir, err)
return nil, nil
return nil, false, "", nil
}
if os.IsNotExist(err) {
return nil, nil
return nil, false, "", nil
}
return nil, err
return nil, false, "", err
}

// make sure they are ordered in full path
Expand Down Expand Up @@ -252,7 +252,8 @@ func (h *hdfsclient) List(prefix, marker, delimiter string, limit int64, followL
break
}
}
return objs, nil
hasMore, nextMarker := generateListResult(objs)
return objs, hasMore, nextMarker, nil
}

func (h *hdfsclient) Chtimes(key string, mtime time.Time) error {
Expand Down
Loading

0 comments on commit 5b5c9fe

Please sign in to comment.