clean format with gofmt -w
parent
823691feaa
commit
9da955ca75
|
@ -22,4 +22,4 @@ type Keyword string
|
||||||
|
|
||||||
func (e Keyword) Error() string {
|
func (e Keyword) Error() string {
|
||||||
return string(e)
|
return string(e)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,6 @@ package store
|
||||||
|
|
||||||
// keywords for internal useage
|
// keywords for internal useage
|
||||||
var keywords = map[string]bool{
|
var keywords = map[string]bool{
|
||||||
"/acoounts": true,
|
"/acoounts": true,
|
||||||
"/ephemeralNodes": true,
|
"/ephemeralNodes": true,
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,7 +70,7 @@ type Response struct {
|
||||||
|
|
||||||
// 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"`
|
||||||
|
|
||||||
Expiration *time.Time `json:"expiration,omitempty"`
|
Expiration *time.Time `json:"expiration,omitempty"`
|
||||||
|
|
||||||
|
@ -146,9 +146,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,
|
||||||
}
|
}
|
||||||
|
|
||||||
// When the slow follower receive the set command
|
// When the slow follower receive the set command
|
||||||
|
@ -260,9 +260,9 @@ func (s *Store) internalGet(key string) *Response {
|
||||||
|
|
||||||
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
|
||||||
|
@ -280,7 +280,6 @@ func (s *Store) internalGet(key string) *Response {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Get all the items under key
|
// Get all the items under key
|
||||||
// If key is a file return the file
|
// If key is a file return the file
|
||||||
// If key is a directory reuturn an array of files
|
// If key is a directory reuturn an array of files
|
||||||
|
@ -301,8 +300,8 @@ func (s *Store) Get(key string) ([]byte, error) {
|
||||||
|
|
||||||
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]),
|
||||||
}
|
}
|
||||||
|
|
||||||
if !dirs[i] {
|
if !dirs[i] {
|
||||||
|
@ -342,17 +341,16 @@ 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,
|
||||||
}
|
}
|
||||||
|
|
||||||
if node.ExpireTime.Equal(PERMANENT) {
|
if node.ExpireTime.Equal(PERMANENT) {
|
||||||
|
|
||||||
s.Tree.delete(key)
|
s.Tree.delete(key)
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
resp.Expiration = &node.ExpireTime
|
resp.Expiration = &node.ExpireTime
|
||||||
// Kill the expire go routine
|
// Kill the expire go routine
|
||||||
|
@ -432,11 +430,11 @@ 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,
|
||||||
}
|
}
|
||||||
|
|
||||||
msg, err := json.Marshal(resp)
|
msg, err := json.Marshal(resp)
|
||||||
|
|
|
@ -2,9 +2,9 @@ package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
|
||||||
"sort"
|
"sort"
|
||||||
)
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
|
@ -21,12 +21,12 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// TreeNode with its key. We use it when we need to sort the treeNodes.
|
// TreeNode with its key. We use it when we need to sort the treeNodes.
|
||||||
type tnWithKey struct{
|
type tnWithKey struct {
|
||||||
key string
|
key string
|
||||||
tn *treeNode
|
tn *treeNode
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ func (t *tree) set(key string, value Node) bool {
|
||||||
newDir := false
|
newDir := false
|
||||||
|
|
||||||
// go through all the path
|
// 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 we meet a new directory, all the directory after it must be new
|
||||||
if newDir {
|
if newDir {
|
||||||
|
@ -115,14 +115,14 @@ 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++ {
|
||||||
node, ok := nodeMap[nodesName[i]]
|
node, ok := nodeMap[nodesName[i]]
|
||||||
if !ok || !node.Dir {
|
if !ok || !node.Dir {
|
||||||
return nil, false
|
return nil, false
|
||||||
|
@ -193,7 +193,7 @@ func (t *tree) delete(key string) bool {
|
||||||
|
|
||||||
var i int
|
var i int
|
||||||
|
|
||||||
for i = 0; i < len(nodesName) - 1; i++ {
|
for i = 0; i < len(nodesName)-1; i++ {
|
||||||
node, ok := nodeMap[nodesName[i]]
|
node, ok := nodeMap[nodesName[i]]
|
||||||
if !ok || !node.Dir {
|
if !ok || !node.Dir {
|
||||||
return false
|
return false
|
||||||
|
@ -202,7 +202,7 @@ func (t *tree) delete(key string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
node, ok := nodeMap[nodesName[i]]
|
node, ok := nodeMap[nodesName[i]]
|
||||||
if ok && !node.Dir{
|
if ok && !node.Dir {
|
||||||
delete(nodeMap, nodesName[i])
|
delete(nodeMap, nodesName[i])
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -223,10 +223,10 @@ func (t *tree) traverse(f func(string, *Node), sort bool) {
|
||||||
func dfs(key string, t *treeNode, f func(string, *Node)) {
|
func dfs(key string, t *treeNode, f func(string, *Node)) {
|
||||||
|
|
||||||
// base case
|
// base case
|
||||||
if len(t.NodeMap) == 0{
|
if len(t.NodeMap) == 0 {
|
||||||
f(key, &t.InternalNode)
|
f(key, &t.InternalNode)
|
||||||
|
|
||||||
// recursion
|
// recursion
|
||||||
} else {
|
} else {
|
||||||
for tnKey, tn := range t.NodeMap {
|
for tnKey, tn := range t.NodeMap {
|
||||||
tnKey := key + "/" + tnKey
|
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
|
// apply the func f to each internal node
|
||||||
func sortDfs(key string, t *treeNode, f func(string, *Node)) {
|
func sortDfs(key string, t *treeNode, f func(string, *Node)) {
|
||||||
// base case
|
// base case
|
||||||
if len(t.NodeMap) == 0{
|
if len(t.NodeMap) == 0 {
|
||||||
f(key, &t.InternalNode)
|
f(key, &t.InternalNode)
|
||||||
|
|
||||||
// recursion
|
// recursion
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
s := make(tnWithKeySlice, len(t.NodeMap))
|
s := make(tnWithKeySlice, len(t.NodeMap))
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
package store
|
package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
"fmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestStoreGet(t *testing.T) {
|
func TestStoreGet(t *testing.T) {
|
||||||
|
@ -60,7 +60,6 @@ func TestStoreGet(t *testing.T) {
|
||||||
t.Fatalf("Expect cannot delet /hello, but deleted! ")
|
t.Fatalf("Expect cannot delet /hello, but deleted! ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// test list
|
// test list
|
||||||
ts.set("/hello/fooo", CreateTestNode("barbarbar"))
|
ts.set("/hello/fooo", CreateTestNode("barbarbar"))
|
||||||
ts.set("/hello/foooo/foo", CreateTestNode("barbarbar"))
|
ts.set("/hello/foooo/foo", CreateTestNode("barbarbar"))
|
||||||
|
@ -73,12 +72,12 @@ func TestStoreGet(t *testing.T) {
|
||||||
length := len(nodes)
|
length := len(nodes)
|
||||||
|
|
||||||
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
|
||||||
for i:=0; i < 100; i++ {
|
for i := 0; i < 100; i++ {
|
||||||
key := "/"
|
key := "/"
|
||||||
depth := rand.Intn(10)
|
depth := rand.Intn(10)
|
||||||
for j := 0; j < depth; j++ {
|
for j := 0; j < depth; j++ {
|
||||||
|
@ -100,10 +99,10 @@ func TestStoreGet(t *testing.T) {
|
||||||
ts.traverse(f, true)
|
ts.traverse(f, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func f (key string, n *Node) {
|
func f(key string, n *Node) {
|
||||||
fmt.Println(key, "=", n.Value)
|
fmt.Println(key, "=", n.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateTestNode(value string) Node{
|
func CreateTestNode(value string) Node {
|
||||||
return Node{value, time.Unix(0,0), nil}
|
return Node{value, time.Unix(0, 0), nil}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
// Typedefs
|
// Typedefs
|
||||||
|
@ -18,7 +19,7 @@ type WatcherHub struct {
|
||||||
|
|
||||||
// Currently watcher only contains a response channel
|
// Currently watcher only contains a response channel
|
||||||
type Watcher struct {
|
type Watcher struct {
|
||||||
C chan Response
|
C chan Response
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new watcherHub
|
// Create a new watcherHub
|
||||||
|
@ -85,7 +86,6 @@ func checkResponse(prefix string, index uint64, resMap *map[string]Response) boo
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Notify the watcher a action happened
|
// Notify the watcher a action happened
|
||||||
func (w *WatcherHub) notify(resp Response) error {
|
func (w *WatcherHub) notify(resp Response) error {
|
||||||
resp.Key = path.Clean(resp.Key)
|
resp.Key = path.Clean(resp.Key)
|
||||||
|
|
Loading…
Reference in New Issue