Skip to content

Commit

Permalink
✨ revise file.Open interface
Browse files Browse the repository at this point in the history
Signed-off-by: Rintaro Okamura <rintaro.okamura@gmail.com>
  • Loading branch information
rinx committed Jul 8, 2020
1 parent e1e4c91 commit daa7c7c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
2 changes: 2 additions & 0 deletions internal/errors/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ var (
ErrFileAlreadyExists = func(path string) error {
return Errorf("file already exists: %s", path)
}

ErrPathNotSpecified = New("the path is not specified")
)
24 changes: 14 additions & 10 deletions internal/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ package file
import (
"os"
"path/filepath"

"github.com/vdaas/vald/internal/errors"
)

// Open opens the file with the given path, flag and permission.
// If the folder does not exists, create the folder.
// If the file does not exist, create the file.
func Open(path string, flg int, perm os.FileMode) *os.File {
func Open(path string, flg int, perm os.FileMode) (*os.File, error) {
if path == "" {
return nil
return nil, errors.ErrPathNotSpecified
}

var err error
Expand All @@ -36,25 +38,27 @@ func Open(path string, flg int, perm os.FileMode) *os.File {
if _, err = os.Stat(filepath.Dir(path)); err != nil {
err = os.MkdirAll(filepath.Dir(path), perm)
if err != nil {
return nil
return nil, err
}
}

file, err = os.Create(path)
if err != nil {
return nil
return nil, err
}

err = file.Close()
if err != nil {
return nil
if file != nil {
err = file.Close()
if err != nil {
return nil, err
}
}
}

file, err = os.OpenFile(path, flg, perm)

if err != nil {
return nil
return nil, err
}

return file
return file, nil
}
2 changes: 1 addition & 1 deletion internal/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ type Metadata struct {
}

type NGT struct {
IndexCount int64 `json:"index_count" yaml:"index_count"`
IndexCount uint64 `json:"index_count" yaml:"index_count"`
}
23 changes: 20 additions & 3 deletions pkg/agent/core/ngt/service/ngt.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,14 @@ func New(cfg *config.NGT) (nn NGT, err error) {
if len(n.path) != 0 && !n.inMem {
m := make(map[string]uint32)
gob.Register(map[string]uint32{})
f := file.Open(n.path+"/"+kvsFileName, os.O_RDONLY|os.O_SYNC, os.ModePerm)
f, err := file.Open(
n.path+"/"+kvsFileName,
os.O_RDONLY|os.O_SYNC,
os.ModePerm,
)
if err != nil {
return err
}
defer f.Close()
err = gob.NewDecoder(f).Decode(&m)
if err != nil {
Expand Down Expand Up @@ -587,7 +594,14 @@ func (n *ngt) saveIndex(ctx context.Context) (err error) {
mu.Unlock()
return true
})
f := file.Open(n.path+"/"+kvsFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
f, err := file.Open(
n.path+"/"+kvsFileName,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
os.ModePerm,
)
if err != nil {
return err
}
defer f.Close()
gob.Register(map[string]uint32{})
return gob.NewEncoder(f).Encode(&m)
Expand All @@ -604,11 +618,14 @@ func (n *ngt) saveIndex(ctx context.Context) (err error) {
return err
}

f := file.Open(
f, err := file.Open(
n.path+"/"+metadata.AgentMetadataFileName,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
os.ModePerm,
)
if err != nil {
return err
}
defer f.Close()

return json.Encode(f, &metadata.Metadata{
Expand Down
3 changes: 2 additions & 1 deletion pkg/agent/sidecar/service/restorer/restorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/vdaas/vald/internal/backoff"
"github.com/vdaas/vald/internal/errgroup"
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/file"
ctxio "github.com/vdaas/vald/internal/io"
"github.com/vdaas/vald/internal/log"
"github.com/vdaas/vald/internal/observability/trace"
Expand Down Expand Up @@ -224,7 +225,7 @@ func (r *restorer) restore(ctx context.Context) (err error) {
return err
}

f, err := os.OpenFile(
f, err := file.Open(
target,
os.O_CREATE|os.O_RDWR,
os.FileMode(header.Mode),
Expand Down

0 comments on commit daa7c7c

Please sign in to comment.