node.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. CreatedIndex 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, createdIndex uint64,
  26. parent *node, ACL string, expireTime time.Time) *node {
  27. return &node{
  28. Path: nodePath,
  29. CreatedIndex: createdIndex,
  30. ModifiedIndex: createdIndex,
  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, createdIndex uint64, parent *node,
  40. ACL string, expireTime time.Time) *node {
  41. return &node{
  42. Path: nodePath,
  43. CreatedIndex: createdIndex,
  44. ModifiedIndex: createdIndex,
  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) Repr(recurisive, sorted bool) NodeExtern {
  177. if n.IsDir() {
  178. node := NodeExtern{
  179. Key: n.Path,
  180. Dir: true,
  181. ModifiedIndex: n.ModifiedIndex,
  182. CreatedIndex: n.CreatedIndex,
  183. }
  184. node.Expiration, node.TTL = n.ExpirationAndTTL()
  185. if !recurisive {
  186. return node
  187. }
  188. children, _ := n.List()
  189. node.Nodes = make(NodeExterns, len(children))
  190. // we do not use the index in the children slice directly
  191. // we need to skip the hidden one
  192. i := 0
  193. for _, child := range children {
  194. if child.IsHidden() { // get will not list hidden node
  195. continue
  196. }
  197. node.Nodes[i] = child.Repr(recurisive, sorted)
  198. i++
  199. }
  200. // eliminate hidden nodes
  201. node.Nodes = node.Nodes[:i]
  202. if sorted {
  203. sort.Sort(node.Nodes)
  204. }
  205. return node
  206. }
  207. node := NodeExtern{
  208. Key: n.Path,
  209. Value: n.Value,
  210. ModifiedIndex: n.ModifiedIndex,
  211. CreatedIndex: n.CreatedIndex,
  212. }
  213. node.Expiration, node.TTL = n.ExpirationAndTTL()
  214. return node
  215. }
  216. func (n *node) UpdateTTL(expireTime time.Time) {
  217. if !n.IsPermanent() {
  218. if expireTime.IsZero() {
  219. // from ttl to permanent
  220. // remove from ttl heap
  221. n.store.ttlKeyHeap.remove(n)
  222. } else {
  223. // update ttl
  224. n.ExpireTime = expireTime
  225. // update ttl heap
  226. n.store.ttlKeyHeap.update(n)
  227. }
  228. } else {
  229. if !expireTime.IsZero() {
  230. // from permanent to ttl
  231. n.ExpireTime = expireTime
  232. // push into ttl heap
  233. n.store.ttlKeyHeap.push(n)
  234. }
  235. }
  236. }
  237. // Clone function clone the node recursively and return the new node.
  238. // If the node is a directory, it will clone all the content under this directory.
  239. // If the node is a key-value pair, it will clone the pair.
  240. func (n *node) Clone() *node {
  241. if !n.IsDir() {
  242. return newKV(n.store, n.Path, n.Value, n.CreatedIndex, n.Parent, n.ACL, n.ExpireTime)
  243. }
  244. clone := newDir(n.store, n.Path, n.CreatedIndex, n.Parent, n.ACL, n.ExpireTime)
  245. for key, child := range n.Children {
  246. clone.Children[key] = child.Clone()
  247. }
  248. return clone
  249. }
  250. // recoverAndclean function help to do recovery.
  251. // Two things need to be done: 1. recovery structure; 2. delete expired nodes
  252. // If the node is a directory, it will help recover children's parent pointer and recursively
  253. // call this function on its children.
  254. // We check the expire last since we need to recover the whole structure first and add all the
  255. // notifications into the event history.
  256. func (n *node) recoverAndclean() {
  257. if n.IsDir() {
  258. for _, child := range n.Children {
  259. child.Parent = n
  260. child.store = n.store
  261. child.recoverAndclean()
  262. }
  263. }
  264. if !n.ExpireTime.IsZero() {
  265. n.store.ttlKeyHeap.push(n)
  266. }
  267. }