Skip to content

Commit

Permalink
Improve error handling when creating fdb directories
Browse files Browse the repository at this point in the history
  • Loading branch information
sunesimonsen committed May 31, 2023
1 parent e5f13c6 commit a536011
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
24 changes: 24 additions & 0 deletions directories.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package blobs

import (
"fmt"
"strings"

"github.com/apple/foundationdb/bindings/go/src/fdb"
"github.com/apple/foundationdb/bindings/go/src/fdb/directory"
)

type pathSegments []string

func (p pathSegments) String() string {
return strings.Join(p, "/")
}

func createDirectory(db fdb.Database, parentDir directory.Directory, path ...string) (directory.DirectorySubspace, error) {
dir, err := parentDir.CreateOrOpen(db, path, nil)
if err != nil {
var newDirectoryPath pathSegments = append(parentDir.GetPath(), path...)
return nil, fmt.Errorf("%w: could not create directory %s", err, newDirectoryPath)
}
return dir, err
}
8 changes: 4 additions & 4 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ type Store struct {
// NewStore constructs a new blob store with the given FoundationDB instance, a
// namespace ns the blobs are stored under and a list of options.
func NewStore(db fdb.Database, ns string, opts ...Option) (*Store, error) {
dir, err := directory.CreateOrOpen(db, []string{"fdb-blobs", ns}, nil)
dir, err := createDirectory(db, directory.Root(), "fdb-blobs", ns)
if err != nil {
return nil, err
}
blobsDir, err := dir.CreateOrOpen(db, []string{"blobs"}, nil)
blobsDir, err := createDirectory(db, dir, "blobs")
if err != nil {
return nil, err
}
uploadsDir, err := dir.CreateOrOpen(db, []string{"uploads"}, nil)
uploadsDir, err := createDirectory(db, dir, "uploads")
if err != nil {
return nil, err
}
removedDir, err := dir.CreateOrOpen(db, []string{"removed"}, nil)
removedDir, err := createDirectory(db, dir, "removed")
if err != nil {
return nil, err
}
Expand Down

0 comments on commit a536011

Please sign in to comment.