node.go 7.8 KB

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