node.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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(dir, recursive bool, callback func(path string)) *etcdErr.Error {
  142. if n.IsDir() {
  143. if !dir {
  144. // cannot delete a directory without recursive set to true
  145. return etcdErr.NewError(etcdErr.EcodeNotFile, n.Path, n.store.Index())
  146. }
  147. if len(n.Children) != 0 && !recursive {
  148. // cannot delete a directory if it is not empty and the operation
  149. // is not recursive
  150. return etcdErr.NewError(etcdErr.EcodeDirNotEmpty, n.Path, n.store.Index())
  151. }
  152. }
  153. if !n.IsDir() { // key-value pair
  154. _, name := path.Split(n.Path)
  155. // find its parent and remove the node from the map
  156. if n.Parent != nil && n.Parent.Children[name] == n {
  157. delete(n.Parent.Children, name)
  158. }
  159. if callback != nil {
  160. callback(n.Path)
  161. }
  162. if !n.IsPermanent() {
  163. n.store.ttlKeyHeap.remove(n)
  164. }
  165. return nil
  166. }
  167. for _, child := range n.Children { // delete all children
  168. child.Remove(true, true, callback)
  169. }
  170. // delete self
  171. _, name := path.Split(n.Path)
  172. if n.Parent != nil && n.Parent.Children[name] == n {
  173. delete(n.Parent.Children, name)
  174. if callback != nil {
  175. callback(n.Path)
  176. }
  177. if !n.IsPermanent() {
  178. n.store.ttlKeyHeap.remove(n)
  179. }
  180. }
  181. return nil
  182. }
  183. func (n *node) Repr(recurisive, sorted bool) *NodeExtern {
  184. if n.IsDir() {
  185. node := &NodeExtern{
  186. Key: n.Path,
  187. Dir: true,
  188. ModifiedIndex: n.ModifiedIndex,
  189. CreatedIndex: n.CreatedIndex,
  190. }
  191. node.Expiration, node.TTL = n.ExpirationAndTTL()
  192. if !recurisive {
  193. return node
  194. }
  195. children, _ := n.List()
  196. node.Nodes = make(NodeExterns, len(children))
  197. // we do not use the index in the children slice directly
  198. // we need to skip the hidden one
  199. i := 0
  200. for _, child := range children {
  201. if child.IsHidden() { // get will not list hidden node
  202. continue
  203. }
  204. node.Nodes[i] = child.Repr(recurisive, sorted)
  205. i++
  206. }
  207. // eliminate hidden nodes
  208. node.Nodes = node.Nodes[:i]
  209. if sorted {
  210. sort.Sort(node.Nodes)
  211. }
  212. return node
  213. }
  214. node := &NodeExtern{
  215. Key: n.Path,
  216. Value: n.Value,
  217. ModifiedIndex: n.ModifiedIndex,
  218. CreatedIndex: n.CreatedIndex,
  219. }
  220. node.Expiration, node.TTL = n.ExpirationAndTTL()
  221. return node
  222. }
  223. func (n *node) UpdateTTL(expireTime time.Time) {
  224. if !n.IsPermanent() {
  225. if expireTime.IsZero() {
  226. // from ttl to permanent
  227. // remove from ttl heap
  228. n.store.ttlKeyHeap.remove(n)
  229. } else {
  230. // update ttl
  231. n.ExpireTime = expireTime
  232. // update ttl heap
  233. n.store.ttlKeyHeap.update(n)
  234. }
  235. } else {
  236. if !expireTime.IsZero() {
  237. // from permanent to ttl
  238. n.ExpireTime = expireTime
  239. // push into ttl heap
  240. n.store.ttlKeyHeap.push(n)
  241. }
  242. }
  243. }
  244. func (n *node) Compare(prevValue string, prevIndex uint64) bool {
  245. compareValue := (prevValue == "" || n.Value == prevValue)
  246. compareIndex := (prevIndex == 0 || n.ModifiedIndex == prevIndex)
  247. return compareValue && compareIndex
  248. }
  249. // Clone function clone the node recursively and return the new node.
  250. // If the node is a directory, it will clone all the content under this directory.
  251. // If the node is a key-value pair, it will clone the pair.
  252. func (n *node) Clone() *node {
  253. if !n.IsDir() {
  254. return newKV(n.store, n.Path, n.Value, n.CreatedIndex, n.Parent, n.ACL, n.ExpireTime)
  255. }
  256. clone := newDir(n.store, n.Path, n.CreatedIndex, n.Parent, n.ACL, n.ExpireTime)
  257. for key, child := range n.Children {
  258. clone.Children[key] = child.Clone()
  259. }
  260. return clone
  261. }
  262. // recoverAndclean function help to do recovery.
  263. // Two things need to be done: 1. recovery structure; 2. delete expired nodes
  264. // If the node is a directory, it will help recover children's parent pointer and recursively
  265. // call this function on its children.
  266. // We check the expire last since we need to recover the whole structure first and add all the
  267. // notifications into the event history.
  268. func (n *node) recoverAndclean() {
  269. if n.IsDir() {
  270. for _, child := range n.Children {
  271. child.Parent = n
  272. child.store = n.store
  273. child.recoverAndclean()
  274. }
  275. }
  276. if !n.ExpireTime.IsZero() {
  277. n.store.ttlKeyHeap.push(n)
  278. }
  279. }