Bladeren bron

Merge pull request #23 from ccding/devd

remove trailing space
Xiang Li 12 jaren geleden
bovenliggende
commit
a90f652894
6 gewijzigde bestanden met toevoegingen van 79 en 82 verwijderingen
  1. 1 1
      store/error.go
  2. 3 3
      store/keywords.go
  3. 32 34
      store/store.go
  4. 25 25
      store/tree.go
  5. 15 16
      store/tree_store_test.go
  6. 3 3
      store/watcher.go

+ 1 - 1
store/error.go

@@ -22,4 +22,4 @@ type Keyword string
 
 func (e Keyword) Error() string {
 	return string(e)
-}
+}

+ 3 - 3
store/keywords.go

@@ -2,6 +2,6 @@ package store
 
 // keywords for internal useage
 var keywords = map[string]bool{
-    "/acoounts": true,
-    "/ephemeralNodes": true,
-}
+	"/acoounts":       true,
+	"/ephemeralNodes": true,
+}

+ 32 - 34
store/store.go

@@ -68,9 +68,9 @@ type Response struct {
 	PrevValue string `json:"prevValue,omitempty"`
 	Value     string `json:"value,omitempty"`
 
-	// If the key did not exist before the action, 
+	// If the key did not exist before the action,
 	// this field should be set to true
-	NewKey    bool `json:"newKey,omitempty"`
+	NewKey bool `json:"newKey,omitempty"`
 
 	Expiration *time.Time `json:"expiration,omitempty"`
 
@@ -145,10 +145,10 @@ func (s *Store) Set(key string, value string, expireTime time.Time, index uint64
 
 	// base response
 	resp := Response{
-		Action: "SET", 
-		Key: key, 
-		Value: value, 
-		Index: index,
+		Action: "SET",
+		Key:    key,
+		Value:  value,
+		Index:  index,
 	}
 
 	// When the slow follower receive the set command
@@ -164,8 +164,8 @@ func (s *Store) Set(key string, value string, expireTime time.Time, index uint64
 	if isExpire {
 		TTL = int64(expireTime.Sub(time.Now()) / time.Second)
 		resp.Expiration = &expireTime
-		resp.TTL = TTL 
-	} 
+		resp.TTL = TTL
+	}
 
 	// Get the node
 	node, ok := s.Tree.get(key)
@@ -259,18 +259,18 @@ func (s *Store) internalGet(key string) *Response {
 		isExpire = !node.ExpireTime.Equal(PERMANENT)
 
 		resp := &Response{
-			Action: "GET", 
-			Key: key, 
-			Value: node.Value, 
-			Index: s.Index,
+			Action: "GET",
+			Key:    key,
+			Value:  node.Value,
+			Index:  s.Index,
 		}
 
 		// Update ttl
 		if isExpire {
 			TTL = int64(node.ExpireTime.Sub(time.Now()) / time.Second)
-			resp.Expiration = &node.ExpireTime 
-			resp.TTL = TTL 
-		} 
+			resp.Expiration = &node.ExpireTime
+			resp.TTL = TTL
+		}
 
 		return resp
 
@@ -280,7 +280,6 @@ func (s *Store) internalGet(key string) *Response {
 	}
 }
 
-
 // Get all the items under key
 // If key is a file return the file
 // If key is a directory reuturn an array of files
@@ -300,9 +299,9 @@ func (s *Store) Get(key string) ([]byte, error) {
 			isExpire = !nodes[i].ExpireTime.Equal(PERMANENT)
 
 			resps[i] = Response{
-				Action: "GET", 
-				Index: s.Index,
-				Key: path.Join(key, keys[i]),
+				Action: "GET",
+				Index:  s.Index,
+				Key:    path.Join(key, keys[i]),
 			}
 
 			if !dirs[i] {
@@ -314,9 +313,9 @@ func (s *Store) Get(key string) ([]byte, error) {
 			// Update ttl
 			if isExpire {
 				TTL = int64(nodes[i].ExpireTime.Sub(time.Now()) / time.Second)
-				resps[i].Expiration = &nodes[i].ExpireTime 
-				resps[i].TTL = TTL 
-			} 
+				resps[i].Expiration = &nodes[i].ExpireTime
+				resps[i].TTL = TTL
+			}
 
 		}
 		if len(resps) == 1 {
@@ -342,23 +341,22 @@ func (s *Store) Delete(key string, index uint64) ([]byte, error) {
 	if ok {
 
 		resp := Response{
-			Action: "DELETE", 
-			Key: key, 
-			PrevValue: node.Value,   
-			Index: index,
+			Action:    "DELETE",
+			Key:       key,
+			PrevValue: node.Value,
+			Index:     index,
 		}
 
 		if node.ExpireTime.Equal(PERMANENT) {
 
 			s.Tree.delete(key)
 
-
 		} else {
 			resp.Expiration = &node.ExpireTime
 			// Kill the expire go routine
 			node.update <- PERMANENT
 			s.Tree.delete(key)
-			
+
 		}
 
 		msg, err := json.Marshal(resp)
@@ -387,7 +385,7 @@ func (s *Store) TestAndSet(key string, prevValue string, value string, expireTim
 
 	if resp == nil {
 		err := NotFoundError(key)
-		return nil, err 
+		return nil, err
 	}
 
 	if resp.Value == prevValue {
@@ -432,11 +430,11 @@ func (s *Store) monitorExpiration(key string, update chan time.Time, expireTime
 				s.Tree.delete(key)
 
 				resp := Response{
-					Action: "DELETE", 
-					Key: key, 
-					PrevValue: node.Value, 
-					Expiration: &node.ExpireTime,  
-					Index: s.Index,
+					Action:     "DELETE",
+					Key:        key,
+					PrevValue:  node.Value,
+					Expiration: &node.ExpireTime,
+					Index:      s.Index,
 				}
 
 				msg, err := json.Marshal(resp)

+ 25 - 25
store/tree.go

@@ -2,9 +2,9 @@ package store
 
 import (
 	"path"
-	"strings"
 	"sort"
-	)
+	"strings"
+)
 
 //------------------------------------------------------------------------------
 //
@@ -12,7 +12,7 @@ import (
 //
 //------------------------------------------------------------------------------
 
-// A file system like tree structure. Each non-leaf node of the tree has a hashmap to 
+// A file system like tree structure. Each non-leaf node of the tree has a hashmap to
 // store its children nodes. Leaf nodes has no hashmap (a nil pointer)
 type tree struct {
 	Root *treeNode
@@ -21,12 +21,12 @@ type tree struct {
 // A treeNode wraps a Node. It has a hashmap to keep records of its children treeNodes.
 type treeNode struct {
 	InternalNode Node
-	Dir bool 
-	NodeMap map[string]*treeNode
+	Dir          bool
+	NodeMap      map[string]*treeNode
 }
 
 // TreeNode with its key. We use it when we need to sort the treeNodes.
-type tnWithKey struct{
+type tnWithKey struct {
 	key string
 	tn  *treeNode
 }
@@ -63,7 +63,7 @@ func (t *tree) set(key string, value Node) bool {
 	newDir := false
 
 	// go through all the path
-	for i = 0; i < len(nodesName) - 1; i++ {
+	for i = 0; i < len(nodesName)-1; i++ {
 
 		// if we meet a new directory, all the directory after it must be new
 		if newDir {
@@ -75,7 +75,7 @@ func (t *tree) set(key string, value Node) bool {
 
 		// get the node from the nodeMap of the current level
 		tn, ok := nodeMap[nodesName[i]]
-	
+
 		if !ok {
 			// add a new directory and set newDir to true
 			newDir = true
@@ -85,7 +85,7 @@ func (t *tree) set(key string, value Node) bool {
 
 		} else if ok && !tn.Dir {
 
-			// if we meet a non-directory node, we cannot set the key 
+			// if we meet a non-directory node, we cannot set the key
 			return false
 		} else {
 
@@ -102,7 +102,7 @@ func (t *tree) set(key string, value Node) bool {
 		// we add a new treeNode
 		tn := &treeNode{value, false, nil}
 		nodeMap[nodesName[i]] = tn
-	
+
 	} else {
 		if tn.Dir {
 			return false
@@ -114,15 +114,15 @@ func (t *tree) set(key string, value Node) bool {
 
 }
 
-// Get the tree node of the key 
-func (t *tree)internalGet(key string) (*treeNode, bool) {
+// Get the tree node of the key
+func (t *tree) internalGet(key string) (*treeNode, bool) {
 	nodesName := split(key)
 
 	nodeMap := t.Root.NodeMap
-		
+
 	var i int
 
-	for i = 0; i < len(nodesName) - 1; i++ {
+	for i = 0; i < len(nodesName)-1; i++ {
 		node, ok := nodeMap[nodesName[i]]
 		if !ok || !node.Dir {
 			return nil, false
@@ -136,7 +136,7 @@ func (t *tree)internalGet(key string) (*treeNode, bool) {
 	} else {
 		return nil, ok
 	}
-} 
+}
 
 // get the internalNode of the key
 func (t *tree) get(key string) (Node, bool) {
@@ -163,7 +163,7 @@ func (t *tree) list(directory string) ([]Node, []string, []bool, bool) {
 			nodes := make([]Node, 1)
 			nodes[0] = treeNode.InternalNode
 			return nodes, make([]string, 1), make([]bool, 1), true
-		} 
+		}
 		length := len(treeNode.NodeMap)
 		nodes := make([]Node, length)
 		keys := make([]string, length)
@@ -190,10 +190,10 @@ func (t *tree) delete(key string) bool {
 	nodesName := split(key)
 
 	nodeMap := t.Root.NodeMap
-		
+
 	var i int
 
-	for i = 0; i < len(nodesName) - 1; i++ {
+	for i = 0; i < len(nodesName)-1; i++ {
 		node, ok := nodeMap[nodesName[i]]
 		if !ok || !node.Dir {
 			return false
@@ -202,7 +202,7 @@ func (t *tree) delete(key string) bool {
 	}
 
 	node, ok := nodeMap[nodesName[i]]
-	if ok && !node.Dir{
+	if ok && !node.Dir {
 		delete(nodeMap, nodesName[i])
 		return true
 	}
@@ -214,19 +214,19 @@ func (t *tree) traverse(f func(string, *Node), sort bool) {
 	if sort {
 		sortDfs("", t.Root, f)
 	} else {
-		dfs("", t.Root, f)	
+		dfs("", t.Root, f)
 	}
 }
 
-// deep first search to traverse the tree 
+// deep first search to traverse the tree
 // apply the func f to each internal node
 func dfs(key string, t *treeNode, f func(string, *Node)) {
 
 	// base case
-	if len(t.NodeMap) == 0{
+	if len(t.NodeMap) == 0 {
 		f(key, &t.InternalNode)
 
-	// recursion
+		// recursion
 	} else {
 		for tnKey, tn := range t.NodeMap {
 			tnKey := key + "/" + tnKey
@@ -239,10 +239,10 @@ func dfs(key string, t *treeNode, f func(string, *Node)) {
 // apply the func f to each internal node
 func sortDfs(key string, t *treeNode, f func(string, *Node)) {
 	// base case
-	if len(t.NodeMap) == 0{
+	if len(t.NodeMap) == 0 {
 		f(key, &t.InternalNode)
 
-	// recursion
+		// recursion
 	} else {
 
 		s := make(tnWithKeySlice, len(t.NodeMap))

+ 15 - 16
store/tree_store_test.go

@@ -1,22 +1,22 @@
 package store
 
 import (
-	"testing"
+	"fmt"
 	"math/rand"
 	"strconv"
+	"testing"
 	"time"
-	"fmt"
 )
 
 func TestStoreGet(t *testing.T) {
 
-	ts := &tree{ 
+	ts := &tree{
 		&treeNode{
-			CreateTestNode("/"), 
-			true, 
+			CreateTestNode("/"),
+			true,
 			make(map[string]*treeNode),
 		},
-	} 
+	}
 
 	// create key
 	ts.set("/foo", CreateTestNode("bar"))
@@ -49,18 +49,17 @@ func TestStoreGet(t *testing.T) {
 	}
 
 	// delete a key
-	ok = ts.delete("/foo") 
+	ok = ts.delete("/foo")
 	if !ok {
 		t.Fatalf("cannot delete key")
 	}
 
 	// delete a directory
-	ok = ts.delete("/hello") 
+	ok = ts.delete("/hello")
 	if ok {
 		t.Fatalf("Expect cannot delet /hello, but deleted! ")
 	}
 
-
 	// test list
 	ts.set("/hello/fooo", CreateTestNode("barbarbar"))
 	ts.set("/hello/foooo/foo", CreateTestNode("barbarbar"))
@@ -73,12 +72,12 @@ 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, "[", dirs[i], "]")
+		}
 	}
 
 	// speed test
-	for i:=0; i < 100; i++ {
+	for i := 0; i < 100; i++ {
 		key := "/"
 		depth := rand.Intn(10)
 		for j := 0; j < depth; j++ {
@@ -100,10 +99,10 @@ func TestStoreGet(t *testing.T) {
 	ts.traverse(f, true)
 }
 
-func f (key string, n *Node) {
+func f(key string, n *Node) {
 	fmt.Println(key, "=", n.Value)
 }
 
-func CreateTestNode(value string) Node{
-	return Node{value, time.Unix(0,0), nil}
-}
+func CreateTestNode(value string) Node {
+	return Node{value, time.Unix(0, 0), nil}
+}

+ 3 - 3
store/watcher.go

@@ -5,6 +5,7 @@ import (
 	"strconv"
 	"strings"
 )
+
 //------------------------------------------------------------------------------
 //
 // Typedefs
@@ -18,7 +19,7 @@ type WatcherHub struct {
 
 // Currently watcher only contains a response channel
 type Watcher struct {
-	C     chan Response
+	C chan Response
 }
 
 // Create a new watcherHub
@@ -34,7 +35,7 @@ func CreateWatcher() *Watcher {
 }
 
 // Add a watcher to the watcherHub
-func (w *WatcherHub) addWatcher(prefix string, watcher *Watcher, sinceIndex uint64, 
+func (w *WatcherHub) addWatcher(prefix string, watcher *Watcher, sinceIndex uint64,
 	responseStartIndex uint64, currentIndex uint64, resMap *map[string]Response) error {
 
 	prefix = path.Clean("/" + prefix)
@@ -85,7 +86,6 @@ func checkResponse(prefix string, index uint64, resMap *map[string]Response) boo
 	return false
 }
 
-
 // Notify the watcher a action happened
 func (w *WatcherHub) notify(resp Response) error {
 	resp.Key = path.Clean(resp.Key)