From c34feca59c6ebfdf7ffd8b24b1c827a2f18f263b Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 25 May 2018 23:27:06 -0700 Subject: [PATCH] refactoring --- weed/filer2/entry.go | 42 ++++++++++++++++++ weed/filer2/entry_codec.go | 43 ++++++++++++++++++ weed/filer2/filer_structure.go | 79 ---------------------------------- weed/filer2/filerstore.go | 13 ++++++ weed/filer2/fullpath.go | 31 +++++++++++++ 5 files changed, 129 insertions(+), 79 deletions(-) create mode 100644 weed/filer2/entry.go create mode 100644 weed/filer2/entry_codec.go delete mode 100644 weed/filer2/filer_structure.go create mode 100644 weed/filer2/filerstore.go create mode 100644 weed/filer2/fullpath.go diff --git a/weed/filer2/entry.go b/weed/filer2/entry.go new file mode 100644 index 000000000..53d069ed1 --- /dev/null +++ b/weed/filer2/entry.go @@ -0,0 +1,42 @@ +package filer2 + +import ( + "os" + "time" + + "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" +) + +type Attr struct { + Mtime time.Time // time of last modification + Crtime time.Time // time of creation (OS X only) + Mode os.FileMode // file mode + Uid uint32 // owner uid + Gid uint32 // group gid +} + +func (attr Attr) IsDirectory() (bool) { + return attr.Mode&os.ModeDir > 0 +} + +type Entry struct { + FullPath + + Attr + + // the following is for files + Chunks []*filer_pb.FileChunk `json:"chunks,omitempty"` +} + +func (entry Entry) Size() uint64 { + return TotalSize(entry.Chunks) +} + +func (entry Entry) Timestamp() time.Time { + if entry.IsDirectory() { + return entry.Crtime + } else { + return entry.Mtime + } +} + diff --git a/weed/filer2/entry_codec.go b/weed/filer2/entry_codec.go new file mode 100644 index 000000000..7d2b2da37 --- /dev/null +++ b/weed/filer2/entry_codec.go @@ -0,0 +1,43 @@ +package filer2 + +import ( + "os" + "time" + + "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" + "github.com/gogo/protobuf/proto" + "fmt" +) + +func (entry Entry) EncodeAttributesAndChunks() ([]byte, error) { + message := &filer_pb.Entry{ + Attributes: &filer_pb.FuseAttributes{ + Crtime: entry.Attr.Crtime.Unix(), + Mtime: entry.Attr.Mtime.Unix(), + FileMode: uint32(entry.Attr.Mode), + Uid: entry.Uid, + Gid: entry.Gid, + }, + Chunks: entry.Chunks, + } + return proto.Marshal(message) +} + +func (entry Entry) DecodeAttributesAndChunks(blob []byte) (error) { + + message := &filer_pb.Entry{} + + if err := proto.UnmarshalMerge(blob, message); err != nil { + return fmt.Errorf("decoding value blob for %s: %v", entry.FullPath, err) + } + + entry.Attr.Crtime = time.Unix(message.Attributes.Crtime, 0) + entry.Attr.Mtime = time.Unix(message.Attributes.Mtime, 0) + entry.Attr.Mode = os.FileMode(message.Attributes.FileMode) + entry.Attr.Uid = message.Attributes.Uid + entry.Attr.Gid = message.Attributes.Gid + + entry.Chunks = message.Chunks + + return nil +} diff --git a/weed/filer2/filer_structure.go b/weed/filer2/filer_structure.go deleted file mode 100644 index 7a5dc3d8d..000000000 --- a/weed/filer2/filer_structure.go +++ /dev/null @@ -1,79 +0,0 @@ -package filer2 - -import ( - "errors" - "os" - "time" - "path/filepath" - "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" - "strings" -) - -type FullPath string - -func NewFullPath(dir, name string) FullPath { - if strings.HasSuffix(dir, "/") { - return FullPath(dir + name) - } - return FullPath(dir + "/" + name) -} - -func (fp FullPath) DirAndName() (string, string) { - dir, name := filepath.Split(string(fp)) - if dir == "/" { - return dir, name - } - if len(dir) < 1 { - return "/", "" - } - return dir[:len(dir)-1], name -} - -func (fp FullPath) Name() (string) { - _, name := filepath.Split(string(fp)) - return name -} - -type Attr struct { - Mtime time.Time // time of last modification - Crtime time.Time // time of creation (OS X only) - Mode os.FileMode // file mode - Uid uint32 // owner uid - Gid uint32 // group gid -} - -func (attr Attr) IsDirectory() (bool) { - return attr.Mode&os.ModeDir > 0 -} - -type Entry struct { - FullPath - - Attr - - // the following is for files - Chunks []*filer_pb.FileChunk `json:"chunks,omitempty"` -} - -func (entry Entry) Size() uint64 { - return TotalSize(entry.Chunks) -} - -func (entry Entry) Timestamp() time.Time { - if entry.IsDirectory() { - return entry.Crtime - } else { - return entry.Mtime - } -} - -var ErrNotFound = errors.New("filer: no entry is found in filer store") - -type FilerStore interface { - InsertEntry(*Entry) (error) - UpdateEntry(*Entry) (err error) - FindEntry(FullPath) (found bool, entry *Entry, err error) - DeleteEntry(FullPath) (fileEntry *Entry, err error) - - ListDirectoryEntries(dirPath FullPath, startFileName string, inclusive bool, limit int) ([]*Entry, error) -} diff --git a/weed/filer2/filerstore.go b/weed/filer2/filerstore.go new file mode 100644 index 000000000..e90ec15ed --- /dev/null +++ b/weed/filer2/filerstore.go @@ -0,0 +1,13 @@ +package filer2 + +import "errors" + +type FilerStore interface { + InsertEntry(*Entry) (error) + UpdateEntry(*Entry) (err error) + FindEntry(FullPath) (found bool, entry *Entry, err error) + DeleteEntry(FullPath) (fileEntry *Entry, err error) + ListDirectoryEntries(dirPath FullPath, startFileName string, inclusive bool, limit int) ([]*Entry, error) +} + +var ErrNotFound = errors.New("filer: no entry is found in filer store") diff --git a/weed/filer2/fullpath.go b/weed/filer2/fullpath.go new file mode 100644 index 000000000..20e42e9b9 --- /dev/null +++ b/weed/filer2/fullpath.go @@ -0,0 +1,31 @@ +package filer2 + +import ( + "path/filepath" + "strings" +) + +type FullPath string + +func NewFullPath(dir, name string) FullPath { + if strings.HasSuffix(dir, "/") { + return FullPath(dir + name) + } + return FullPath(dir + "/" + name) +} + +func (fp FullPath) DirAndName() (string, string) { + dir, name := filepath.Split(string(fp)) + if dir == "/" { + return dir, name + } + if len(dir) < 1 { + return "/", "" + } + return dir[:len(dir)-1], name +} + +func (fp FullPath) Name() (string) { + _, name := filepath.Split(string(fp)) + return name +}