Browse Source

support recursive get

Xiang Li 12 years ago
parent
commit
450d0eb0da
3 changed files with 39 additions and 6 deletions
  1. 7 3
      file_system/file_system.go
  2. 4 1
      file_system/file_system_test.go
  3. 28 2
      file_system/node.go

+ 7 - 3
file_system/file_system.go

@@ -36,17 +36,21 @@ func (fs *FileSystem) Get(keyPath string, recusive bool, index uint64, term uint
 	e := newEvent(Get, keyPath, index, term)
 
 	if n.IsDir() { // node is dir
-		e.KVPairs = make([]KeyValuePair, len(n.Children))
 
+		children, _ := n.List()
+		e.KVPairs = make([]KeyValuePair, len(children))
+
+		// we do not use the index in the children slice directly
+		// we need to skip the hidden one
 		i := 0
 
-		for _, child := range n.Children {
+		for _, child := range children {
 
 			if child.IsHidden() { // get will not list hidden node
 				continue
 			}
 
-			e.KVPairs[i] = child.Pair()
+			e.KVPairs[i] = child.Pair(recusive)
 
 			i++
 		}

+ 4 - 1
file_system/file_system_test.go

@@ -49,7 +49,7 @@ func TestListDirectory(t *testing.T) {
 	// set key-value /foo/fooDir/foo=bar
 	fs.Set("/foo/fooDir/foo", "bar", Permanent, 2, 1)
 
-	e, err := fs.Get("/foo", false, 2, 1)
+	e, err := fs.Get("/foo", true, 2, 1)
 
 	if err != nil {
 		t.Fatalf("%v", err)
@@ -67,6 +67,9 @@ func TestListDirectory(t *testing.T) {
 		t.Fatalf("wrong kv [/foo/fooDir/ / %s] -> [true / %v]", e.KVPairs[1].Key, e.KVPairs[1].Dir)
 	}
 
+	if e.KVPairs[1].KVPairs[0].Key != "/foo/fooDir/foo" || e.KVPairs[1].KVPairs[0].Value != "bar" {
+		t.Fatalf("wrong kv [/foo/fooDir/foo / %s] -> [bar / %v]", e.KVPairs[1].KVPairs[0].Key, e.KVPairs[1].KVPairs[0].Value)
+	}
 	// test hidden node
 
 	// create dir /foo/_hidden

+ 28 - 2
file_system/node.go

@@ -268,13 +268,39 @@ func (n *Node) IsHidden() bool {
 	return false
 }
 
-func (n *Node) Pair() KeyValuePair {
+func (n *Node) Pair(recurisive bool) KeyValuePair {
 
 	if n.IsDir() {
-		return KeyValuePair{
+		pair := KeyValuePair{
 			Key: n.Path,
 			Dir: true,
 		}
+		if !recurisive {
+			return pair
+		}
+
+		children, _ := n.List()
+		pair.KVPairs = make([]KeyValuePair, len(children))
+
+		// we do not use the index in the children slice directly
+		// we need to skip the hidden one
+		i := 0
+
+		for _, child := range children {
+
+			if child.IsHidden() { // get will not list hidden node
+				continue
+			}
+
+			pair.KVPairs[i] = child.Pair(recurisive)
+
+			i++
+		}
+
+		// eliminate hidden nodes
+		pair.KVPairs = pair.KVPairs[:i]
+
+		return pair
 	}
 
 	return KeyValuePair{