node_extern.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package store
  2. import (
  3. "sort"
  4. "time"
  5. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/jonboulle/clockwork"
  6. )
  7. // NodeExtern is the external representation of the
  8. // internal node with additional fields
  9. // PrevValue is the previous value of the node
  10. // TTL is time to live in second
  11. type NodeExtern struct {
  12. Key string `json:"key,omitempty"`
  13. Value *string `json:"value,omitempty"`
  14. Dir bool `json:"dir,omitempty"`
  15. Expiration *time.Time `json:"expiration,omitempty"`
  16. TTL int64 `json:"ttl,omitempty"`
  17. Nodes NodeExterns `json:"nodes,omitempty"`
  18. ModifiedIndex uint64 `json:"modifiedIndex,omitempty"`
  19. CreatedIndex uint64 `json:"createdIndex,omitempty"`
  20. }
  21. func (eNode *NodeExtern) loadInternalNode(n *node, recursive, sorted bool, clock clockwork.Clock) {
  22. if n.IsDir() { // node is a directory
  23. eNode.Dir = true
  24. children, _ := n.List()
  25. eNode.Nodes = make(NodeExterns, len(children))
  26. // we do not use the index in the children slice directly
  27. // we need to skip the hidden one
  28. i := 0
  29. for _, child := range children {
  30. if child.IsHidden() { // get will not return hidden nodes
  31. continue
  32. }
  33. eNode.Nodes[i] = child.Repr(recursive, sorted, clock)
  34. i++
  35. }
  36. // eliminate hidden nodes
  37. eNode.Nodes = eNode.Nodes[:i]
  38. if sorted {
  39. sort.Sort(eNode.Nodes)
  40. }
  41. } else { // node is a file
  42. value, _ := n.Read()
  43. eNode.Value = &value
  44. }
  45. eNode.Expiration, eNode.TTL = n.expirationAndTTL(clock)
  46. }
  47. type NodeExterns []*NodeExtern
  48. // interfaces for sorting
  49. func (ns NodeExterns) Len() int {
  50. return len(ns)
  51. }
  52. func (ns NodeExterns) Less(i, j int) bool {
  53. return ns[i].Key < ns[j].Key
  54. }
  55. func (ns NodeExterns) Swap(i, j int) {
  56. ns[i], ns[j] = ns[j], ns[i]
  57. }