Skip to content

Commit

Permalink
Merge pull request #5 from fengjingchao/fileSystem
Browse files Browse the repository at this point in the history
change filepath to path and fix path namespace collision
  • Loading branch information
xiang90 committed Sep 5, 2013
2 parents 4e2f9b4 + 03af286 commit 7ce8389
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
44 changes: 22 additions & 22 deletions file_system/file_system.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fileSystem

import (
"path/filepath"
"path"
"strings"
"time"

Expand All @@ -24,15 +24,15 @@ func New() *FileSystem {

}

func (fs *FileSystem) Get(path string, recusive bool, index uint64, term uint64) (*Event, error) {
func (fs *FileSystem) Get(key_path string, recusive bool, index uint64, term uint64) (*Event, error) {
// TODO: add recursive get
n, err := fs.InternalGet(path, index, term)
n, err := fs.InternalGet(key_path, index, term)

if err != nil {
return nil, err
}

e := newEvent(Get, path, index, term)
e := newEvent(Get, key_path, index, term)

if n.IsDir() { // node is dir
e.KVPairs = make([]KeyValuePair, len(n.Children))
Expand Down Expand Up @@ -60,23 +60,23 @@ func (fs *FileSystem) Get(path string, recusive bool, index uint64, term uint64)
return e, nil
}

func (fs *FileSystem) Set(path string, value string, expireTime time.Time, index uint64, term uint64) (*Event, error) {
path = filepath.Clean("/" + path)
func (fs *FileSystem) Set(key_path string, value string, expireTime time.Time, index uint64, term uint64) (*Event, error) {
key_path = path.Clean("/" + key_path)

// update file system known index and term
fs.Index, fs.Term = index, term

dir, name := filepath.Split(path)
dir, name := path.Split(key_path)

// walk through the path and get the last directory node
// walk through the key_path and get the last directory node
d, err := fs.walk(dir, fs.checkDir)

if err != nil {
return nil, err
}

f := newFile(path, value, fs.Index, fs.Term, d, "", expireTime)
e := newEvent(Set, path, fs.Index, fs.Term)
f := newFile(key_path, value, fs.Index, fs.Term, d, "", expireTime)
e := newEvent(Set, key_path, fs.Index, fs.Term)
e.Value = f.Value

// remove previous file if exist
Expand Down Expand Up @@ -107,16 +107,16 @@ func (fs *FileSystem) Set(path string, value string, expireTime time.Time, index
return e, nil
}

func (fs *FileSystem) TestAndSet(path string, recurisive bool, index uint64, term uint64) {
func (fs *FileSystem) TestAndSet(key_path string, recurisive bool, index uint64, term uint64) {

}

func (fs *FileSystem) TestIndexAndSet() {

}

func (fs *FileSystem) Delete(path string, recurisive bool, index uint64, term uint64) (*Event, error) {
n, err := fs.InternalGet(path, index, term)
func (fs *FileSystem) Delete(key_path string, recurisive bool, index uint64, term uint64) (*Event, error) {
n, err := fs.InternalGet(key_path, index, term)

if err != nil {
return nil, err
Expand All @@ -128,7 +128,7 @@ func (fs *FileSystem) Delete(path string, recurisive bool, index uint64, term ui
return nil, err
}

e := newEvent(Delete, path, index, term)
e := newEvent(Delete, key_path, index, term)

if n.IsDir() {
e.Dir = true
Expand All @@ -139,9 +139,9 @@ func (fs *FileSystem) Delete(path string, recurisive bool, index uint64, term ui
return e, nil
}

// walk function walks all the path and apply the walkFunc on each directory
func (fs *FileSystem) walk(path string, walkFunc func(prev *Node, component string) (*Node, error)) (*Node, error) {
components := strings.Split(path, "/")
// walk function walks all the key_path and apply the walkFunc on each directory
func (fs *FileSystem) walk(key_path string, walkFunc func(prev *Node, component string) (*Node, error)) (*Node, error) {
components := strings.Split(key_path, "/")

curr := fs.Root

Expand All @@ -161,9 +161,9 @@ func (fs *FileSystem) walk(path string, walkFunc func(prev *Node, component stri
return curr, nil
}

// InternalGet function get the node of the given path.
func (fs *FileSystem) InternalGet(path string, index uint64, term uint64) (*Node, error) {
path = filepath.Clean("/" + path)
// InternalGet function get the node of the given key_path.
func (fs *FileSystem) InternalGet(key_path string, index uint64, term uint64) (*Node, error) {
key_path = path.Clean("/" + key_path)

// update file system known index and term
fs.Index, fs.Term = index, term
Expand All @@ -177,7 +177,7 @@ func (fs *FileSystem) InternalGet(path string, index uint64, term uint64) (*Node
return nil, etcdErr.NewError(100, "get")
}

f, err := fs.walk(path, walkFunc)
f, err := fs.walk(key_path, walkFunc)

if err != nil {
return nil, err
Expand All @@ -198,7 +198,7 @@ func (fs *FileSystem) checkDir(parent *Node, dirName string) (*Node, error) {
return subDir, nil
}

n := newDir(filepath.Join(parent.Path, dirName), fs.Index, fs.Term, parent, parent.ACL)
n := newDir(path.Join(parent.Path, dirName), fs.Index, fs.Term, parent, parent.ACL)

parent.Children[dirName] = n

Expand Down
18 changes: 9 additions & 9 deletions file_system/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package fileSystem

import (
"fmt"
"path/filepath"
"path"
"sync"
"time"

Expand Down Expand Up @@ -32,9 +32,9 @@ type Node struct {
removeChan chan bool // remove channel
}

func newFile(path string, value string, createIndex uint64, createTerm uint64, parent *Node, ACL string, expireTime time.Time) *Node {
func newFile(key_path string, value string, createIndex uint64, createTerm uint64, parent *Node, ACL string, expireTime time.Time) *Node {
return &Node{
Path: path,
Path: key_path,
CreateIndex: createIndex,
CreateTerm: createTerm,
Parent: parent,
Expand All @@ -45,9 +45,9 @@ func newFile(path string, value string, createIndex uint64, createTerm uint64, p
}
}

func newDir(path string, createIndex uint64, createTerm uint64, parent *Node, ACL string) *Node {
func newDir(key_path string, createIndex uint64, createTerm uint64, parent *Node, ACL string) *Node {
return &Node{
Path: path,
Path: key_path,
CreateIndex: createIndex,
CreateTerm: createTerm,
Parent: parent,
Expand All @@ -69,7 +69,7 @@ func (n *Node) Remove(recursive bool) error {
}

if !n.IsDir() { // file node: key-value pair
_, name := filepath.Split(n.Path)
_, name := path.Split(n.Path)

if n.Parent.Children[name] == n {
// This is the only pointer to Node object
Expand All @@ -91,7 +91,7 @@ func (n *Node) Remove(recursive bool) error {
}

// delete self
_, name := filepath.Split(n.Path)
_, name := path.Split(n.Path)
if n.Parent.Children[name] == n {
delete(n.Parent.Children, name)
n.removeChan <- true
Expand Down Expand Up @@ -180,7 +180,7 @@ func (n *Node) Add(child *Node) error {
return etcdErr.NewError(104, "")
}

_, name := filepath.Split(child.Path)
_, name := path.Split(child.Path)

_, ok := n.Children[name]

Expand Down Expand Up @@ -255,7 +255,7 @@ func (n *Node) Expire() {
// For example if we have /foo/_hidden and /foo/notHidden, get "/foo"
// will only return /foo/notHidden
func (n *Node) IsHidden() bool {
_, name := filepath.Split(n.Path)
_, name := path.Split(n.Path)

if name[0] == '_' { //hidden
return true
Expand Down
4 changes: 2 additions & 2 deletions file_system/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package fileSystem

import (
"container/list"
"path/filepath"
"path"
"strings"
)

Expand Down Expand Up @@ -53,7 +53,7 @@ func (wh *watcherHub) notify(e *Event) {

// walk through all the paths
for _, segment := range segments {
currPath = filepath.Join(currPath, segment)
currPath = path.Join(currPath, segment)

l, ok := wh.watchers[currPath]

Expand Down

0 comments on commit 7ce8389

Please sign in to comment.