node.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package store
  2. import (
  3. "path"
  4. "sort"
  5. "time"
  6. etcdErr "github.com/coreos/etcd/error"
  7. )
  8. var Permanent time.Time
  9. // Node is the basic element in the store system.
  10. // A key-value pair will have a string value
  11. // A directory will have a children map
  12. type Node struct {
  13. Path string
  14. CreateIndex uint64
  15. ModifiedIndex uint64
  16. Parent *Node `json:"-"` // should not encode this field! avoid circular dependency.
  17. ExpireTime time.Time
  18. ACL string
  19. Value string // for key-value pair
  20. Children map[string]*Node // for directory
  21. // A reference to the store this node is attached to.
  22. store *store
  23. }
  24. // newKV creates a Key-Value pair
  25. func newKV(store *store, nodePath string, value string, createIndex uint64,
  26. parent *Node, ACL string, expireTime time.Time) *Node {
  27. return &Node{
  28. Path: nodePath,
  29. CreateIndex: createIndex,
  30. ModifiedIndex: createIndex,
  31. Parent: parent,
  32. ACL: ACL,
  33. store: store,
  34. ExpireTime: expireTime,
  35. Value: value,
  36. }
  37. }
  38. // newDir creates a directory
  39. func newDir(store *store, nodePath string, createIndex uint64, parent *Node,
  40. ACL string, expireTime time.Time) *Node {
  41. return &Node{
  42. Path: nodePath,
  43. CreateIndex: createIndex,
  44. Parent: parent,
  45. ACL: ACL,
  46. ExpireTime: expireTime,
  47. Children: make(map[string]*Node),
  48. store: store,
  49. }
  50. }
  51. // IsHidden function checks if the node is a hidden node. A hidden node
  52. // will begin with '_'
  53. // A hidden node will not be shown via get command under a directory
  54. // For example if we have /foo/_hidden and /foo/notHidden, get "/foo"
  55. // will only return /foo/notHidden
  56. func (n *Node) IsHidden() bool {
  57. _, name := path.Split(n.Path)
  58. return name[0] == '_'
  59. }
  60. // IsPermanent function checks if the node is a permanent one.
  61. func (n *Node) IsPermanent() bool {
  62. // we use a uninitialized time.Time to indicate the node is a
  63. // permanent one.
  64. // the uninitialized time.Time should equal zero.
  65. return n.ExpireTime.IsZero()
  66. }
  67. // IsDir function checks whether the node is a directory.
  68. // If the node is a directory, the function will return true.
  69. // Otherwise the function will return false.
  70. func (n *Node) IsDir() bool {
  71. return !(n.Children == nil)
  72. }
  73. // Read function gets the value of the node.
  74. // If the receiver node is not a key-value pair, a "Not A File" error will be returned.
  75. func (n *Node) Read() (string, *etcdErr.Error) {
  76. if n.IsDir() {
  77. return "", etcdErr.NewError(etcdErr.EcodeNotFile, "", n.store.Index())
  78. }
  79. return n.Value, nil
  80. }
  81. // Write function set the value of the node to the given value.
  82. // If the receiver node is a directory, a "Not A File" error will be returned.
  83. func (n *Node) Write(value string, index uint64) *etcdErr.Error {
  84. if n.IsDir() {
  85. return etcdErr.NewError(etcdErr.EcodeNotFile, "", n.store.Index())
  86. }
  87. n.Value = value
  88. n.ModifiedIndex = index
  89. return nil
  90. }
  91. func (n *Node) ExpirationAndTTL() (*time.Time, int64) {
  92. if !n.IsPermanent() {
  93. return &n.ExpireTime, int64(n.ExpireTime.Sub(time.Now())/time.Second) + 1
  94. }
  95. return nil, 0
  96. }
  97. // List function return a slice of nodes under the receiver node.
  98. // If the receiver node is not a directory, a "Not A Directory" error will be returned.
  99. func (n *Node) List() ([]*Node, *etcdErr.Error) {
  100. if !n.IsDir() {
  101. return nil, etcdErr.NewError(etcdErr.EcodeNotDir, "", n.store.Index())
  102. }
  103. nodes := make([]*Node, len(n.Children))
  104. i := 0
  105. for _, node := range n.Children {
  106. nodes[i] = node
  107. i++
  108. }
  109. return nodes, nil
  110. }
  111. // GetChild function returns the child node under the directory node.
  112. // On success, it returns the file node
  113. func (n *Node) GetChild(name string) (*Node, *etcdErr.Error) {
  114. if !n.IsDir() {
  115. return nil, etcdErr.NewError(etcdErr.EcodeNotDir, n.Path, n.store.Index())
  116. }
  117. child, ok := n.Children[name]
  118. if ok {
  119. return child, nil
  120. }
  121. return nil, nil
  122. }
  123. // Add function adds a node to the receiver node.
  124. // If the receiver is not a directory, a "Not A Directory" error will be returned.
  125. // If there is a existing node with the same name under the directory, a "Already Exist"
  126. // error will be returned
  127. func (n *Node) Add(child *Node) *etcdErr.Error {
  128. if !n.IsDir() {
  129. return etcdErr.NewError(etcdErr.EcodeNotDir, "", n.store.Index())
  130. }
  131. _, name := path.Split(child.Path)
  132. _, ok := n.Children[name]
  133. if ok {
  134. return etcdErr.NewError(etcdErr.EcodeNodeExist, "", n.store.Index())
  135. }
  136. n.Children[name] = child
  137. return nil
  138. }
  139. // Remove function remove the node.
  140. func (n *Node) Remove(recursive bool, callback func(path string)) *etcdErr.Error {
  141. if n.IsDir() && !recursive {
  142. // cannot delete a directory without set recursive to true
  143. return etcdErr.NewError(etcdErr.EcodeNotFile, "", n.store.Index())
  144. }
  145. if !n.IsDir() { // key-value pair
  146. _, name := path.Split(n.Path)
  147. // find its parent and remove the node from the map
  148. if n.Parent != nil && n.Parent.Children[name] == n {
  149. delete(n.Parent.Children, name)
  150. }
  151. if callback != nil {
  152. callback(n.Path)
  153. }
  154. if !n.IsPermanent() {
  155. n.store.ttlKeyHeap.remove(n)
  156. }
  157. return nil
  158. }
  159. for _, child := range n.Children { // delete all children
  160. child.Remove(true, callback)
  161. }
  162. // delete self
  163. _, name := path.Split(n.Path)
  164. if n.Parent != nil && n.Parent.Children[name] == n {
  165. delete(n.Parent.Children, name)
  166. if callback != nil {
  167. callback(n.Path)
  168. }
  169. if !n.IsPermanent() {
  170. n.store.ttlKeyHeap.remove(n)
  171. }
  172. }
  173. return nil
  174. }
  175. func (n *Node) Pair(recurisive, sorted bool) KeyValuePair {
  176. if n.IsDir() {
  177. pair := KeyValuePair{
  178. Key: n.Path,
  179. Dir: true,
  180. }
  181. pair.Expiration, pair.TTL = n.ExpirationAndTTL()
  182. if !recurisive {
  183. return pair
  184. }
  185. children, _ := n.List()
  186. pair.KVPairs = make([]KeyValuePair, len(children))
  187. // we do not use the index in the children slice directly
  188. // we need to skip the hidden one
  189. i := 0
  190. for _, child := range children {
  191. if child.IsHidden() { // get will not list hidden node
  192. continue
  193. }
  194. pair.KVPairs[i] = child.Pair(recurisive, sorted)
  195. i++
  196. }
  197. // eliminate hidden nodes
  198. pair.KVPairs = pair.KVPairs[:i]
  199. if sorted {
  200. sort.Sort(pair.KVPairs)
  201. }
  202. return pair
  203. }
  204. pair := KeyValuePair{
  205. Key: n.Path,
  206. Value: n.Value,
  207. }
  208. pair.Expiration, pair.TTL = n.ExpirationAndTTL()
  209. return pair
  210. }
  211. func (n *Node) UpdateTTL(expireTime time.Time) {
  212. if !n.IsPermanent() {
  213. if expireTime.IsZero() {
  214. // from ttl to permanent
  215. // remove from ttl heap
  216. n.store.ttlKeyHeap.remove(n)
  217. } else {
  218. // update ttl
  219. n.ExpireTime = expireTime
  220. // update ttl heap
  221. n.store.ttlKeyHeap.update(n)
  222. }
  223. } else {
  224. if !expireTime.IsZero() {
  225. // from permanent to ttl
  226. n.ExpireTime = expireTime
  227. // push into ttl heap
  228. n.store.ttlKeyHeap.push(n)
  229. }
  230. }
  231. }
  232. // Clone function clone the node recursively and return the new node.
  233. // If the node is a directory, it will clone all the content under this directory.
  234. // If the node is a key-value pair, it will clone the pair.
  235. func (n *Node) Clone() *Node {
  236. if !n.IsDir() {
  237. return newKV(n.store, n.Path, n.Value, n.CreateIndex, n.Parent, n.ACL, n.ExpireTime)
  238. }
  239. clone := newDir(n.store, n.Path, n.CreateIndex, n.Parent, n.ACL, n.ExpireTime)
  240. for key, child := range n.Children {
  241. clone.Children[key] = child.Clone()
  242. }
  243. return clone
  244. }
  245. // recoverAndclean function help to do recovery.
  246. // Two things need to be done: 1. recovery structure; 2. delete expired nodes
  247. // If the node is a directory, it will help recover children's parent pointer and recursively
  248. // call this function on its children.
  249. // We check the expire last since we need to recover the whole structure first and add all the
  250. // notifications into the event history.
  251. func (n *Node) recoverAndclean() {
  252. if n.IsDir() {
  253. for _, child := range n.Children {
  254. child.Parent = n
  255. child.store = n.store
  256. child.recoverAndclean()
  257. }
  258. }
  259. if !n.ExpireTime.IsZero() {
  260. n.store.ttlKeyHeap.push(n)
  261. }
  262. }