node_extern.go 1.6 KB

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