node_extern.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package store
  14. import (
  15. "sort"
  16. "time"
  17. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/jonboulle/clockwork"
  18. )
  19. // NodeExtern is the external representation of the
  20. // internal node with additional fields
  21. // PrevValue is the previous value of the node
  22. // TTL is time to live in second
  23. type NodeExtern struct {
  24. Key string `json:"key,omitempty"`
  25. Value *string `json:"value,omitempty"`
  26. Dir bool `json:"dir,omitempty"`
  27. Expiration *time.Time `json:"expiration,omitempty"`
  28. TTL int64 `json:"ttl,omitempty"`
  29. Nodes NodeExterns `json:"nodes,omitempty"`
  30. ModifiedIndex uint64 `json:"modifiedIndex,omitempty"`
  31. CreatedIndex uint64 `json:"createdIndex,omitempty"`
  32. }
  33. func (eNode *NodeExtern) loadInternalNode(n *node, recursive, sorted bool, clock clockwork.Clock) {
  34. if n.IsDir() { // node is a directory
  35. eNode.Dir = true
  36. children, _ := n.List()
  37. eNode.Nodes = make(NodeExterns, len(children))
  38. // we do not use the index in the children slice directly
  39. // we need to skip the hidden one
  40. i := 0
  41. for _, child := range children {
  42. if child.IsHidden() { // get will not return hidden nodes
  43. continue
  44. }
  45. eNode.Nodes[i] = child.Repr(recursive, sorted, clock)
  46. i++
  47. }
  48. // eliminate hidden nodes
  49. eNode.Nodes = eNode.Nodes[:i]
  50. if sorted {
  51. sort.Sort(eNode.Nodes)
  52. }
  53. } else { // node is a file
  54. value, _ := n.Read()
  55. eNode.Value = &value
  56. }
  57. eNode.Expiration, eNode.TTL = n.expirationAndTTL(clock)
  58. }
  59. func (eNode *NodeExtern) Clone() *NodeExtern {
  60. if eNode == nil {
  61. return nil
  62. }
  63. nn := &NodeExtern{
  64. Key: eNode.Key,
  65. Dir: eNode.Dir,
  66. TTL: eNode.TTL,
  67. ModifiedIndex: eNode.ModifiedIndex,
  68. CreatedIndex: eNode.CreatedIndex,
  69. }
  70. if eNode.Value != nil {
  71. s := *eNode.Value
  72. nn.Value = &s
  73. }
  74. if eNode.Expiration != nil {
  75. t := *eNode.Expiration
  76. nn.Expiration = &t
  77. }
  78. if eNode.Nodes != nil {
  79. nn.Nodes = make(NodeExterns, len(eNode.Nodes))
  80. for i, n := range eNode.Nodes {
  81. nn.Nodes[i] = n.Clone()
  82. }
  83. }
  84. return nn
  85. }
  86. type NodeExterns []*NodeExtern
  87. // interfaces for sorting
  88. func (ns NodeExterns) Len() int {
  89. return len(ns)
  90. }
  91. func (ns NodeExterns) Less(i, j int) bool {
  92. return ns[i].Key < ns[j].Key
  93. }
  94. func (ns NodeExterns) Swap(i, j int) {
  95. ns[i], ns[j] = ns[j], ns[i]
  96. }