refactor separate kvpair to kvpair.go; simplify sorting interface

release-0.4
Xiang Li 2013-10-08 21:25:56 -07:00
parent 3c7f9215d1
commit c3e2332479
4 changed files with 40 additions and 40 deletions

View File

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

24
store/kv_pairs.go Normal file
View File

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

View File

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

View File

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