Browse Source

simplify tree list

Xiang Li 12 years ago
parent
commit
bebcf4b733
3 changed files with 13 additions and 16 deletions
  1. 4 2
      store/store.go
  2. 7 12
      store/tree.go
  3. 2 2
      store/tree_store_test.go

+ 4 - 2
store/store.go

@@ -322,7 +322,7 @@ func (s *Store) RawGet(key string) ([]*Response, error) {
 
 	key = path.Clean("/" + key)
 
-	nodes, keys, dirs, ok := s.Tree.list(key)
+	nodes, keys, ok := s.Tree.list(key)
 
 	if ok {
 
@@ -366,7 +366,7 @@ func (s *Store) RawGet(key string) ([]*Response, error) {
 				Key:    path.Join(key, keys[i]),
 			}
 
-			if !dirs[i] {
+			if len(nodes[i].Value) != 0 {
 				resps[i].Value = nodes[i].Value
 			} else {
 				resps[i].Dir = true
@@ -592,6 +592,8 @@ func (s *Store) clone() *Store {
 
 // Save the current state of the storage system
 func (s *Store) Save() ([]byte, error) {
+	// first we clone the store
+	// json is very slow, we cannot hold the lock for such a long time
 	s.mutex.Lock()
 	cloneStore := s.clone()
 	s.mutex.Unlock()

+ 7 - 12
store/tree.go

@@ -42,7 +42,7 @@ func (s tnWithKeySlice) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
 // CONSTANT VARIABLE
 
 // Represent an empty node
-var emptyNode = Node{".", PERMANENT, nil}
+var emptyNode = Node{"", PERMANENT, nil}
 
 //------------------------------------------------------------------------------
 //
@@ -159,33 +159,28 @@ func (t *tree) get(key string) (Node, bool) {
 }
 
 // get the internalNode of the key
-func (t *tree) list(directory string) (interface{}, []string, []bool, bool) {
+func (t *tree) list(directory string) (interface{}, []string, bool) {
 	treeNode, ok := t.internalGet(directory)
 
 	if !ok {
-		return nil, nil, nil, ok
+		return nil, nil, ok
+
 	} else {
 		if !treeNode.Dir {
-			return &treeNode.InternalNode, nil, nil, true
+			return &treeNode.InternalNode, nil, ok
 		}
 		length := len(treeNode.NodeMap)
 		nodes := make([]*Node, length)
 		keys := make([]string, length)
-		dirs := make([]bool, length)
-		i := 0
 
+		i := 0
 		for key, node := range treeNode.NodeMap {
 			nodes[i] = &node.InternalNode
 			keys[i] = key
-			if node.Dir {
-				dirs[i] = true
-			} else {
-				dirs[i] = false
-			}
 			i++
 		}
 
-		return nodes, keys, dirs, ok
+		return nodes, keys, ok
 	}
 }
 

+ 2 - 2
store/tree_store_test.go

@@ -64,7 +64,7 @@ func TestStoreGet(t *testing.T) {
 	ts.set("/hello/fooo", NewTestNode("barbarbar"))
 	ts.set("/hello/foooo/foo", NewTestNode("barbarbar"))
 
-	nodes, keys, dirs, ok := ts.list("/hello")
+	nodes, keys, ok := ts.list("/hello")
 
 	if !ok {
 		t.Fatalf("cannot list!")
@@ -73,7 +73,7 @@ func TestStoreGet(t *testing.T) {
 		length := len(nodes)
 
 		for i := 0; i < length; i++ {
-			fmt.Println(keys[i], "=", nodes[i].Value, "[", dirs[i], "]")
+			fmt.Println(keys[i], "=", nodes[i].Value)
 		}
 	}