node.go 8.0 KB

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