remove trailing space
parent
bac9bf59cf
commit
823691feaa
|
@ -68,7 +68,7 @@ type Response struct {
|
||||||
PrevValue string `json:"prevValue,omitempty"`
|
PrevValue string `json:"prevValue,omitempty"`
|
||||||
Value string `json:"value,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
|
// this field should be set to true
|
||||||
NewKey bool `json:"newKey,omitempty"`
|
NewKey bool `json:"newKey,omitempty"`
|
||||||
|
|
||||||
|
@ -145,9 +145,9 @@ func (s *Store) Set(key string, value string, expireTime time.Time, index uint64
|
||||||
|
|
||||||
// base response
|
// base response
|
||||||
resp := Response{
|
resp := Response{
|
||||||
Action: "SET",
|
Action: "SET",
|
||||||
Key: key,
|
Key: key,
|
||||||
Value: value,
|
Value: value,
|
||||||
Index: index,
|
Index: index,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,8 +164,8 @@ func (s *Store) Set(key string, value string, expireTime time.Time, index uint64
|
||||||
if isExpire {
|
if isExpire {
|
||||||
TTL = int64(expireTime.Sub(time.Now()) / time.Second)
|
TTL = int64(expireTime.Sub(time.Now()) / time.Second)
|
||||||
resp.Expiration = &expireTime
|
resp.Expiration = &expireTime
|
||||||
resp.TTL = TTL
|
resp.TTL = TTL
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the node
|
// Get the node
|
||||||
node, ok := s.Tree.get(key)
|
node, ok := s.Tree.get(key)
|
||||||
|
@ -259,18 +259,18 @@ func (s *Store) internalGet(key string) *Response {
|
||||||
isExpire = !node.ExpireTime.Equal(PERMANENT)
|
isExpire = !node.ExpireTime.Equal(PERMANENT)
|
||||||
|
|
||||||
resp := &Response{
|
resp := &Response{
|
||||||
Action: "GET",
|
Action: "GET",
|
||||||
Key: key,
|
Key: key,
|
||||||
Value: node.Value,
|
Value: node.Value,
|
||||||
Index: s.Index,
|
Index: s.Index,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update ttl
|
// Update ttl
|
||||||
if isExpire {
|
if isExpire {
|
||||||
TTL = int64(node.ExpireTime.Sub(time.Now()) / time.Second)
|
TTL = int64(node.ExpireTime.Sub(time.Now()) / time.Second)
|
||||||
resp.Expiration = &node.ExpireTime
|
resp.Expiration = &node.ExpireTime
|
||||||
resp.TTL = TTL
|
resp.TTL = TTL
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
|
@ -300,7 +300,7 @@ func (s *Store) Get(key string) ([]byte, error) {
|
||||||
isExpire = !nodes[i].ExpireTime.Equal(PERMANENT)
|
isExpire = !nodes[i].ExpireTime.Equal(PERMANENT)
|
||||||
|
|
||||||
resps[i] = Response{
|
resps[i] = Response{
|
||||||
Action: "GET",
|
Action: "GET",
|
||||||
Index: s.Index,
|
Index: s.Index,
|
||||||
Key: path.Join(key, keys[i]),
|
Key: path.Join(key, keys[i]),
|
||||||
}
|
}
|
||||||
|
@ -314,9 +314,9 @@ func (s *Store) Get(key string) ([]byte, error) {
|
||||||
// Update ttl
|
// Update ttl
|
||||||
if isExpire {
|
if isExpire {
|
||||||
TTL = int64(nodes[i].ExpireTime.Sub(time.Now()) / time.Second)
|
TTL = int64(nodes[i].ExpireTime.Sub(time.Now()) / time.Second)
|
||||||
resps[i].Expiration = &nodes[i].ExpireTime
|
resps[i].Expiration = &nodes[i].ExpireTime
|
||||||
resps[i].TTL = TTL
|
resps[i].TTL = TTL
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if len(resps) == 1 {
|
if len(resps) == 1 {
|
||||||
|
@ -342,9 +342,9 @@ func (s *Store) Delete(key string, index uint64) ([]byte, error) {
|
||||||
if ok {
|
if ok {
|
||||||
|
|
||||||
resp := Response{
|
resp := Response{
|
||||||
Action: "DELETE",
|
Action: "DELETE",
|
||||||
Key: key,
|
Key: key,
|
||||||
PrevValue: node.Value,
|
PrevValue: node.Value,
|
||||||
Index: index,
|
Index: index,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -358,7 +358,7 @@ func (s *Store) Delete(key string, index uint64) ([]byte, error) {
|
||||||
// Kill the expire go routine
|
// Kill the expire go routine
|
||||||
node.update <- PERMANENT
|
node.update <- PERMANENT
|
||||||
s.Tree.delete(key)
|
s.Tree.delete(key)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
msg, err := json.Marshal(resp)
|
msg, err := json.Marshal(resp)
|
||||||
|
@ -387,7 +387,7 @@ func (s *Store) TestAndSet(key string, prevValue string, value string, expireTim
|
||||||
|
|
||||||
if resp == nil {
|
if resp == nil {
|
||||||
err := NotFoundError(key)
|
err := NotFoundError(key)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.Value == prevValue {
|
if resp.Value == prevValue {
|
||||||
|
@ -432,10 +432,10 @@ func (s *Store) monitorExpiration(key string, update chan time.Time, expireTime
|
||||||
s.Tree.delete(key)
|
s.Tree.delete(key)
|
||||||
|
|
||||||
resp := Response{
|
resp := Response{
|
||||||
Action: "DELETE",
|
Action: "DELETE",
|
||||||
Key: key,
|
Key: key,
|
||||||
PrevValue: node.Value,
|
PrevValue: node.Value,
|
||||||
Expiration: &node.ExpireTime,
|
Expiration: &node.ExpireTime,
|
||||||
Index: s.Index,
|
Index: s.Index,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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)
|
// store its children nodes. Leaf nodes has no hashmap (a nil pointer)
|
||||||
type tree struct {
|
type tree struct {
|
||||||
Root *treeNode
|
Root *treeNode
|
||||||
|
@ -21,7 +21,7 @@ type tree struct {
|
||||||
// A treeNode wraps a Node. It has a hashmap to keep records of its children treeNodes.
|
// A treeNode wraps a Node. It has a hashmap to keep records of its children treeNodes.
|
||||||
type treeNode struct {
|
type treeNode struct {
|
||||||
InternalNode Node
|
InternalNode Node
|
||||||
Dir bool
|
Dir bool
|
||||||
NodeMap map[string]*treeNode
|
NodeMap map[string]*treeNode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +75,7 @@ func (t *tree) set(key string, value Node) bool {
|
||||||
|
|
||||||
// get the node from the nodeMap of the current level
|
// get the node from the nodeMap of the current level
|
||||||
tn, ok := nodeMap[nodesName[i]]
|
tn, ok := nodeMap[nodesName[i]]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
// add a new directory and set newDir to true
|
// add a new directory and set newDir to true
|
||||||
newDir = true
|
newDir = true
|
||||||
|
@ -85,7 +85,7 @@ func (t *tree) set(key string, value Node) bool {
|
||||||
|
|
||||||
} else if ok && !tn.Dir {
|
} 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
|
return false
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
@ -102,7 +102,7 @@ func (t *tree) set(key string, value Node) bool {
|
||||||
// we add a new treeNode
|
// we add a new treeNode
|
||||||
tn := &treeNode{value, false, nil}
|
tn := &treeNode{value, false, nil}
|
||||||
nodeMap[nodesName[i]] = tn
|
nodeMap[nodesName[i]] = tn
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if tn.Dir {
|
if tn.Dir {
|
||||||
return false
|
return false
|
||||||
|
@ -114,12 +114,12 @@ func (t *tree) set(key string, value Node) bool {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the tree node of the key
|
// Get the tree node of the key
|
||||||
func (t *tree)internalGet(key string) (*treeNode, bool) {
|
func (t *tree)internalGet(key string) (*treeNode, bool) {
|
||||||
nodesName := split(key)
|
nodesName := split(key)
|
||||||
|
|
||||||
nodeMap := t.Root.NodeMap
|
nodeMap := t.Root.NodeMap
|
||||||
|
|
||||||
var i int
|
var i int
|
||||||
|
|
||||||
for i = 0; i < len(nodesName) - 1; i++ {
|
for i = 0; i < len(nodesName) - 1; i++ {
|
||||||
|
@ -136,7 +136,7 @@ func (t *tree)internalGet(key string) (*treeNode, bool) {
|
||||||
} else {
|
} else {
|
||||||
return nil, ok
|
return nil, ok
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the internalNode of the key
|
// get the internalNode of the key
|
||||||
func (t *tree) get(key string) (Node, bool) {
|
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 := make([]Node, 1)
|
||||||
nodes[0] = treeNode.InternalNode
|
nodes[0] = treeNode.InternalNode
|
||||||
return nodes, make([]string, 1), make([]bool, 1), true
|
return nodes, make([]string, 1), make([]bool, 1), true
|
||||||
}
|
}
|
||||||
length := len(treeNode.NodeMap)
|
length := len(treeNode.NodeMap)
|
||||||
nodes := make([]Node, length)
|
nodes := make([]Node, length)
|
||||||
keys := make([]string, length)
|
keys := make([]string, length)
|
||||||
|
@ -190,7 +190,7 @@ func (t *tree) delete(key string) bool {
|
||||||
nodesName := split(key)
|
nodesName := split(key)
|
||||||
|
|
||||||
nodeMap := t.Root.NodeMap
|
nodeMap := t.Root.NodeMap
|
||||||
|
|
||||||
var i int
|
var i int
|
||||||
|
|
||||||
for i = 0; i < len(nodesName) - 1; i++ {
|
for i = 0; i < len(nodesName) - 1; i++ {
|
||||||
|
@ -214,11 +214,11 @@ func (t *tree) traverse(f func(string, *Node), sort bool) {
|
||||||
if sort {
|
if sort {
|
||||||
sortDfs("", t.Root, f)
|
sortDfs("", t.Root, f)
|
||||||
} else {
|
} 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
|
// apply the func f to each internal node
|
||||||
func dfs(key string, t *treeNode, f func(string, *Node)) {
|
func dfs(key string, t *treeNode, f func(string, *Node)) {
|
||||||
|
|
||||||
|
|
|
@ -10,13 +10,13 @@ import (
|
||||||
|
|
||||||
func TestStoreGet(t *testing.T) {
|
func TestStoreGet(t *testing.T) {
|
||||||
|
|
||||||
ts := &tree{
|
ts := &tree{
|
||||||
&treeNode{
|
&treeNode{
|
||||||
CreateTestNode("/"),
|
CreateTestNode("/"),
|
||||||
true,
|
true,
|
||||||
make(map[string]*treeNode),
|
make(map[string]*treeNode),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// create key
|
// create key
|
||||||
ts.set("/foo", CreateTestNode("bar"))
|
ts.set("/foo", CreateTestNode("bar"))
|
||||||
|
@ -49,13 +49,13 @@ func TestStoreGet(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete a key
|
// delete a key
|
||||||
ok = ts.delete("/foo")
|
ok = ts.delete("/foo")
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("cannot delete key")
|
t.Fatalf("cannot delete key")
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete a directory
|
// delete a directory
|
||||||
ok = ts.delete("/hello")
|
ok = ts.delete("/hello")
|
||||||
if ok {
|
if ok {
|
||||||
t.Fatalf("Expect cannot delet /hello, but deleted! ")
|
t.Fatalf("Expect cannot delet /hello, but deleted! ")
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,7 @@ func TestStoreGet(t *testing.T) {
|
||||||
|
|
||||||
for i := 0; i < length; i++ {
|
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
|
// speed test
|
||||||
|
|
|
@ -34,7 +34,7 @@ func CreateWatcher() *Watcher {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a watcher to the watcherHub
|
// 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 {
|
responseStartIndex uint64, currentIndex uint64, resMap *map[string]Response) error {
|
||||||
|
|
||||||
prefix = path.Clean("/" + prefix)
|
prefix = path.Clean("/" + prefix)
|
||||||
|
|
Loading…
Reference in New Issue