support create directory

This commit is contained in:
Xiang Li 2013-09-06 23:36:11 -04:00
parent 4f99b60291
commit 948044093b
3 changed files with 43 additions and 11 deletions

View File

@ -19,7 +19,7 @@ type FileSystem struct {
func New() *FileSystem { func New() *FileSystem {
return &FileSystem{ return &FileSystem{
Root: newDir("/", 0, 0, nil, ""), Root: newDir("/", 0, 0, nil, "", Permanent),
WatcherHub: newWatchHub(1000), WatcherHub: newWatchHub(1000),
} }
@ -36,6 +36,7 @@ func (fs *FileSystem) Get(keyPath string, recusive bool, index uint64, term uint
e := newEvent(Get, keyPath, index, term) e := newEvent(Get, keyPath, index, term)
if n.IsDir() { // node is dir if n.IsDir() { // node is dir
e.Dir = true
children, _ := n.List() children, _ := n.List()
e.KVPairs = make([]KeyValuePair, len(children)) e.KVPairs = make([]KeyValuePair, len(children))
@ -57,7 +58,6 @@ func (fs *FileSystem) Get(keyPath string, recusive bool, index uint64, term uint
// eliminate hidden nodes // eliminate hidden nodes
e.KVPairs = e.KVPairs[:i] e.KVPairs = e.KVPairs[:i]
} else { // node is file } else { // node is file
e.Value = n.Value e.Value = n.Value
} }
@ -91,11 +91,22 @@ func (fs *FileSystem) Create(keyPath string, value string, expireTime time.Time,
} }
e := newEvent(Set, keyPath, fs.Index, fs.Term) e := newEvent(Set, keyPath, fs.Index, fs.Term)
e.Value = value
f := newFile(keyPath, value, fs.Index, fs.Term, d, "", expireTime) var n *Node
err = d.Add(f) if len(value) != 0 { // create file
e.Value = value
n = newFile(keyPath, value, fs.Index, fs.Term, d, "", expireTime)
} else { // create directory
e.Dir = true
n = newDir(keyPath, fs.Index, fs.Term, d, "", expireTime)
}
err = d.Add(n)
if err != nil { if err != nil {
return nil, err return nil, err
@ -103,8 +114,8 @@ func (fs *FileSystem) Create(keyPath string, value string, expireTime time.Time,
// Node with TTL // Node with TTL
if expireTime != Permanent { if expireTime != Permanent {
go f.Expire() go n.Expire()
e.Expiration = &f.ExpireTime e.Expiration = &n.ExpireTime
e.TTL = int64(expireTime.Sub(time.Now()) / time.Second) e.TTL = int64(expireTime.Sub(time.Now()) / time.Second)
} }
@ -268,7 +279,7 @@ func (fs *FileSystem) checkDir(parent *Node, dirName string) (*Node, error) {
return subDir, nil return subDir, nil
} }
n := newDir(path.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, Permanent)
parent.Children[dirName] = n parent.Children[dirName] = n

View File

@ -21,12 +21,32 @@ func TestCreateAndGet(t *testing.T) {
} }
// meet file, create should fail // meet file, create should fail
_, err = fs.Create("/foo/bar/bar", "bar", Permanent, 1, 1) _, err = fs.Create("/foo/bar/bar", "bar", Permanent, 2, 1)
if err == nil { if err == nil {
t.Fatal("Create should fail") t.Fatal("Create should fail")
} }
// create a directory
_, err = fs.Create("/fooDir", "", Permanent, 3, 1)
if err != nil {
t.Fatal("Cannot create /fooDir")
}
e, err := fs.Get("/fooDir", false, 3, 1)
if err != nil || e.Dir != true {
t.Fatal("Cannot create /fooDir ")
}
// create a file under directory
_, err = fs.Create("/fooDir/bar", "bar", Permanent, 4, 1)
if err != nil {
t.Fatal("Cannot create /fooDir/bar = bar")
}
} }
func TestUpdateFile(t *testing.T) { func TestUpdateFile(t *testing.T) {

View File

@ -49,7 +49,7 @@ func newFile(keyPath string, value string, createIndex uint64, createTerm uint64
} }
} }
func newDir(keyPath string, createIndex uint64, createTerm uint64, parent *Node, ACL string) *Node { func newDir(keyPath string, createIndex uint64, createTerm uint64, parent *Node, ACL string, expireTime time.Time) *Node {
return &Node{ return &Node{
Path: keyPath, Path: keyPath,
CreateIndex: createIndex, CreateIndex: createIndex,
@ -57,6 +57,7 @@ func newDir(keyPath string, createIndex uint64, createTerm uint64, parent *Node,
Parent: parent, Parent: parent,
ACL: ACL, ACL: ACL,
stopExpire: make(chan bool, 1), stopExpire: make(chan bool, 1),
ExpireTime: expireTime,
Children: make(map[string]*Node), Children: make(map[string]*Node),
} }
} }
@ -210,7 +211,7 @@ func (n *Node) Clone() *Node {
return newFile(n.Path, n.Value, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL, n.ExpireTime) return newFile(n.Path, n.Value, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL, n.ExpireTime)
} }
clone := newDir(n.Path, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL) clone := newDir(n.Path, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL, n.ExpireTime)
for key, child := range n.Children { for key, child := range n.Children {
clone.Children[key] = child.Clone() clone.Children[key] = child.Clone()