refactor separate kvpair to kvpair.go; simplify sorting interface
parent
3c7f9215d1
commit
c3e2332479
|
@ -24,40 +24,19 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Event struct {
|
type Event struct {
|
||||||
Action string `json:"action"`
|
Action string `json:"action"`
|
||||||
Key string `json:"key, omitempty"`
|
Key string `json:"key, omitempty"`
|
||||||
Dir bool `json:"dir,omitempty"`
|
Dir bool `json:"dir,omitempty"`
|
||||||
PrevValue string `json:"prevValue,omitempty"`
|
PrevValue string `json:"prevValue,omitempty"`
|
||||||
Value string `json:"value,omitempty"`
|
Value string `json:"value,omitempty"`
|
||||||
KVPairs []KeyValuePair `json:"kvs,omitempty"`
|
KVPairs kvPairs `json:"kvs,omitempty"`
|
||||||
Expiration *time.Time `json:"expiration,omitempty"`
|
Expiration *time.Time `json:"expiration,omitempty"`
|
||||||
TTL int64 `json:"ttl,omitempty"` // Time to live in second
|
TTL int64 `json:"ttl,omitempty"` // Time to live in second
|
||||||
// The command index of the raft machine when the command is executed
|
// The command index of the raft machine when the command is executed
|
||||||
Index uint64 `json:"index"`
|
Index uint64 `json:"index"`
|
||||||
Term uint64 `json:"term"`
|
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 {
|
func newEvent(action string, key string, index uint64, term uint64) *Event {
|
||||||
return &Event{
|
return &Event{
|
||||||
Action: action,
|
Action: action,
|
||||||
|
|
|
@ -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]
|
||||||
|
}
|
|
@ -336,7 +336,7 @@ func (n *Node) Pair(recurisive, sorted bool) KeyValuePair {
|
||||||
// eliminate hidden nodes
|
// eliminate hidden nodes
|
||||||
pair.KVPairs = pair.KVPairs[:i]
|
pair.KVPairs = pair.KVPairs[:i]
|
||||||
if sorted {
|
if sorted {
|
||||||
sort.Sort(pair)
|
sort.Sort(pair.KVPairs)
|
||||||
}
|
}
|
||||||
|
|
||||||
return pair
|
return pair
|
||||||
|
|
|
@ -24,13 +24,16 @@ type Store struct {
|
||||||
|
|
||||||
func New() *Store {
|
func New() *Store {
|
||||||
s := new(Store)
|
s := new(Store)
|
||||||
s.Root = newDir("/", 0, 0, nil, "", Permanent)
|
s.Root = newDir("/", UndefIndex, UndefTerm, nil, "", Permanent)
|
||||||
s.Stats = newStats()
|
s.Stats = newStats()
|
||||||
s.WatcherHub = newWatchHub(1000)
|
s.WatcherHub = newWatchHub(1000)
|
||||||
|
|
||||||
return s
|
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) {
|
func (s *Store) Get(nodePath string, recursive, sorted bool, index uint64, term uint64) (*Event, error) {
|
||||||
s.worldLock.RLock()
|
s.worldLock.RLock()
|
||||||
defer s.worldLock.RUnlock()
|
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)
|
e := newEvent(Get, nodePath, index, term)
|
||||||
|
|
||||||
if n.IsDir() { // node is dir
|
if n.IsDir() { // node is a directory
|
||||||
e.Dir = true
|
e.Dir = true
|
||||||
|
|
||||||
children, _ := n.List()
|
children, _ := n.List()
|
||||||
|
@ -57,25 +60,19 @@ func (s *Store) Get(nodePath string, recursive, sorted bool, index uint64, term
|
||||||
i := 0
|
i := 0
|
||||||
|
|
||||||
for _, child := range children {
|
for _, child := range children {
|
||||||
|
if child.IsHidden() { // get will not return hidden nodes
|
||||||
if child.IsHidden() { // get will not list hidden node
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
e.KVPairs[i] = child.Pair(recursive, sorted)
|
e.KVPairs[i] = child.Pair(recursive, sorted)
|
||||||
|
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
// eliminate hidden nodes
|
// eliminate hidden nodes
|
||||||
e.KVPairs = e.KVPairs[:i]
|
e.KVPairs = e.KVPairs[:i]
|
||||||
|
|
||||||
rootPairs := KeyValuePair{
|
|
||||||
KVPairs: e.KVPairs,
|
|
||||||
}
|
|
||||||
|
|
||||||
if sorted {
|
if sorted {
|
||||||
sort.Sort(rootPairs)
|
sort.Sort(e.KVPairs)
|
||||||
}
|
}
|
||||||
|
|
||||||
} else { // node is file
|
} else { // node is file
|
||||||
|
|
Loading…
Reference in New Issue