node.go 7.7 KB

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