node.go 7.7 KB

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