simplify tree list

release-0.4
Xiang Li 2013-08-05 17:03:31 -07:00
parent 09cfd89298
commit bebcf4b733
3 changed files with 13 additions and 16 deletions

View File

@ -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()

View File

@ -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
}
}

View File

@ -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)
}
}