From c3e2332479ae1fc70cdb8cbc1ab70c59c9e21210 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Tue, 8 Oct 2013 21:25:56 -0700 Subject: [PATCH] refactor separate kvpair to kvpair.go; simplify sorting interface --- store/event.go | 37 ++++++++----------------------------- store/kv_pairs.go | 24 ++++++++++++++++++++++++ store/node.go | 2 +- store/store.go | 17 +++++++---------- 4 files changed, 40 insertions(+), 40 deletions(-) create mode 100644 store/kv_pairs.go diff --git a/store/event.go b/store/event.go index ba4149a79..15866d745 100644 --- a/store/event.go +++ b/store/event.go @@ -24,40 +24,19 @@ const ( ) type Event struct { - Action string `json:"action"` - Key string `json:"key, omitempty"` - Dir bool `json:"dir,omitempty"` - PrevValue string `json:"prevValue,omitempty"` - Value string `json:"value,omitempty"` - KVPairs []KeyValuePair `json:"kvs,omitempty"` - Expiration *time.Time `json:"expiration,omitempty"` - TTL int64 `json:"ttl,omitempty"` // Time to live in second + Action string `json:"action"` + Key string `json:"key, omitempty"` + Dir bool `json:"dir,omitempty"` + PrevValue string `json:"prevValue,omitempty"` + Value string `json:"value,omitempty"` + KVPairs kvPairs `json:"kvs,omitempty"` + Expiration *time.Time `json:"expiration,omitempty"` + TTL int64 `json:"ttl,omitempty"` // Time to live in second // The command index of the raft machine when the command is executed Index uint64 `json:"index"` Term uint64 `json:"term"` } -// When user list a directory, we add all the node into key-value pair slice -type KeyValuePair struct { - Key string `json:"key, omitempty"` - Value string `json:"value,omitempty"` - Dir bool `json:"dir,omitempty"` - KVPairs []KeyValuePair `json:"kvs,omitempty"` -} - -// interfaces for sorting -func (k KeyValuePair) Len() int { - return len(k.KVPairs) -} - -func (k KeyValuePair) Less(i, j int) bool { - return k.KVPairs[i].Key < k.KVPairs[j].Key -} - -func (k KeyValuePair) Swap(i, j int) { - k.KVPairs[i], k.KVPairs[j] = k.KVPairs[j], k.KVPairs[i] -} - func newEvent(action string, key string, index uint64, term uint64) *Event { return &Event{ Action: action, diff --git a/store/kv_pairs.go b/store/kv_pairs.go new file mode 100644 index 000000000..90e5c3fd6 --- /dev/null +++ b/store/kv_pairs.go @@ -0,0 +1,24 @@ +package store + +// When user list a directory, we add all the node into key-value pair slice +type KeyValuePair struct { + Key string `json:"key, omitempty"` + Value string `json:"value,omitempty"` + Dir bool `json:"dir,omitempty"` + KVPairs kvPairs `json:"kvs,omitempty"` +} + +type kvPairs []KeyValuePair + +// interfaces for sorting +func (kvs kvPairs) Len() int { + return len(kvs) +} + +func (kvs kvPairs) Less(i, j int) bool { + return kvs[i].Key < kvs[j].Key +} + +func (kvs kvPairs) Swap(i, j int) { + kvs[i], kvs[j] = kvs[j], kvs[i] +} diff --git a/store/node.go b/store/node.go index 8e67eeb0e..010a97dee 100644 --- a/store/node.go +++ b/store/node.go @@ -336,7 +336,7 @@ func (n *Node) Pair(recurisive, sorted bool) KeyValuePair { // eliminate hidden nodes pair.KVPairs = pair.KVPairs[:i] if sorted { - sort.Sort(pair) + sort.Sort(pair.KVPairs) } return pair diff --git a/store/store.go b/store/store.go index 5d41e45ed..2c0c9cdb4 100644 --- a/store/store.go +++ b/store/store.go @@ -24,13 +24,16 @@ type Store struct { func New() *Store { s := new(Store) - s.Root = newDir("/", 0, 0, nil, "", Permanent) + s.Root = newDir("/", UndefIndex, UndefTerm, nil, "", Permanent) s.Stats = newStats() s.WatcherHub = newWatchHub(1000) return s } +// get function returns a get event. +// If recursive is true, it will return all the content under the node path. +// If sorted is true, it will sort the content by keys. func (s *Store) Get(nodePath string, recursive, sorted bool, index uint64, term uint64) (*Event, error) { s.worldLock.RLock() defer s.worldLock.RUnlock() @@ -46,7 +49,7 @@ func (s *Store) Get(nodePath string, recursive, sorted bool, index uint64, term e := newEvent(Get, nodePath, index, term) - if n.IsDir() { // node is dir + if n.IsDir() { // node is a directory e.Dir = true children, _ := n.List() @@ -57,25 +60,19 @@ func (s *Store) Get(nodePath string, recursive, sorted bool, index uint64, term i := 0 for _, child := range children { - - if child.IsHidden() { // get will not list hidden node + if child.IsHidden() { // get will not return hidden nodes continue } e.KVPairs[i] = child.Pair(recursive, sorted) - i++ } // eliminate hidden nodes e.KVPairs = e.KVPairs[:i] - rootPairs := KeyValuePair{ - KVPairs: e.KVPairs, - } - if sorted { - sort.Sort(rootPairs) + sort.Sort(e.KVPairs) } } else { // node is file