Browse Source

refactor(Node) do not expose node struct

Xiang Li 12 years ago
parent
commit
08c59895b5
4 changed files with 45 additions and 46 deletions
  1. 1 1
      store/heap_test.go
  2. 25 25
      store/node.go
  3. 10 11
      store/store.go
  4. 9 9
      store/ttl_key_heap.go

+ 1 - 1
store/heap_test.go

@@ -33,7 +33,7 @@ func TestHeapPushPop(t *testing.T) {
 func TestHeapUpdate(t *testing.T) {
 	h := newTtlKeyHeap()
 
-	kvs := make([]*Node, 10)
+	kvs := make([]*node, 10)
 
 	// add from older expire time to earlier expire time
 	// the path is equal to ttl from now

+ 25 - 25
store/node.go

@@ -10,21 +10,21 @@ import (
 
 var Permanent time.Time
 
-// Node is the basic element in the store system.
+// node is the basic element in the store system.
 // A key-value pair will have a string value
 // A directory will have a children map
-type Node struct {
+type node struct {
 	Path string
 
 	CreateIndex   uint64
 	ModifiedIndex uint64
 
-	Parent *Node `json:"-"` // should not encode this field! avoid circular dependency.
+	Parent *node `json:"-"` // should not encode this field! avoid circular dependency.
 
 	ExpireTime time.Time
 	ACL        string
 	Value      string           // for key-value pair
-	Children   map[string]*Node // for directory
+	Children   map[string]*node // for directory
 
 	// A reference to the store this node is attached to.
 	store *store
@@ -32,9 +32,9 @@ type Node struct {
 
 // newKV creates a Key-Value pair
 func newKV(store *store, nodePath string, value string, createIndex uint64,
-	parent *Node, ACL string, expireTime time.Time) *Node {
+	parent *node, ACL string, expireTime time.Time) *node {
 
-	return &Node{
+	return &node{
 		Path:          nodePath,
 		CreateIndex:   createIndex,
 		ModifiedIndex: createIndex,
@@ -47,17 +47,17 @@ func newKV(store *store, nodePath string, value string, createIndex uint64,
 }
 
 // newDir creates a directory
-func newDir(store *store, nodePath string, createIndex uint64, parent *Node,
-	ACL string, expireTime time.Time) *Node {
+func newDir(store *store, nodePath string, createIndex uint64, parent *node,
+	ACL string, expireTime time.Time) *node {
 
-	return &Node{
+	return &node{
 		Path:          nodePath,
 		CreateIndex:   createIndex,
 		ModifiedIndex: createIndex,
 		Parent:        parent,
 		ACL:           ACL,
 		ExpireTime:    expireTime,
-		Children:      make(map[string]*Node),
+		Children:      make(map[string]*node),
 		store:         store,
 	}
 }
@@ -67,14 +67,14 @@ func newDir(store *store, nodePath string, createIndex uint64, parent *Node,
 // A hidden node will not be shown via get command under a directory
 // For example if we have /foo/_hidden and /foo/notHidden, get "/foo"
 // will only return /foo/notHidden
-func (n *Node) IsHidden() bool {
+func (n *node) IsHidden() bool {
 	_, name := path.Split(n.Path)
 
 	return name[0] == '_'
 }
 
 // IsPermanent function checks if the node is a permanent one.
-func (n *Node) IsPermanent() bool {
+func (n *node) IsPermanent() bool {
 	// we use a uninitialized time.Time to indicate the node is a
 	// permanent one.
 	// the uninitialized time.Time should equal zero.
@@ -84,13 +84,13 @@ func (n *Node) IsPermanent() bool {
 // IsDir function checks whether the node is a directory.
 // If the node is a directory, the function will return true.
 // Otherwise the function will return false.
-func (n *Node) IsDir() bool {
+func (n *node) IsDir() bool {
 	return !(n.Children == nil)
 }
 
 // Read function gets the value of the node.
 // If the receiver node is not a key-value pair, a "Not A File" error will be returned.
-func (n *Node) Read() (string, *etcdErr.Error) {
+func (n *node) Read() (string, *etcdErr.Error) {
 	if n.IsDir() {
 		return "", etcdErr.NewError(etcdErr.EcodeNotFile, "", n.store.Index())
 	}
@@ -100,7 +100,7 @@ func (n *Node) Read() (string, *etcdErr.Error) {
 
 // Write function set the value of the node to the given value.
 // If the receiver node is a directory, a "Not A File" error will be returned.
-func (n *Node) Write(value string, index uint64) *etcdErr.Error {
+func (n *node) Write(value string, index uint64) *etcdErr.Error {
 	if n.IsDir() {
 		return etcdErr.NewError(etcdErr.EcodeNotFile, "", n.store.Index())
 	}
@@ -111,7 +111,7 @@ func (n *Node) Write(value string, index uint64) *etcdErr.Error {
 	return nil
 }
 
-func (n *Node) ExpirationAndTTL() (*time.Time, int64) {
+func (n *node) ExpirationAndTTL() (*time.Time, int64) {
 	if !n.IsPermanent() {
 		return &n.ExpireTime, int64(n.ExpireTime.Sub(time.Now())/time.Second) + 1
 	}
@@ -120,12 +120,12 @@ func (n *Node) ExpirationAndTTL() (*time.Time, int64) {
 
 // List function return a slice of nodes under the receiver node.
 // If the receiver node is not a directory, a "Not A Directory" error will be returned.
-func (n *Node) List() ([]*Node, *etcdErr.Error) {
+func (n *node) List() ([]*node, *etcdErr.Error) {
 	if !n.IsDir() {
 		return nil, etcdErr.NewError(etcdErr.EcodeNotDir, "", n.store.Index())
 	}
 
-	nodes := make([]*Node, len(n.Children))
+	nodes := make([]*node, len(n.Children))
 
 	i := 0
 	for _, node := range n.Children {
@@ -138,7 +138,7 @@ func (n *Node) List() ([]*Node, *etcdErr.Error) {
 
 // GetChild function returns the child node under the directory node.
 // On success, it returns the file node
-func (n *Node) GetChild(name string) (*Node, *etcdErr.Error) {
+func (n *node) GetChild(name string) (*node, *etcdErr.Error) {
 	if !n.IsDir() {
 		return nil, etcdErr.NewError(etcdErr.EcodeNotDir, n.Path, n.store.Index())
 	}
@@ -156,7 +156,7 @@ func (n *Node) GetChild(name string) (*Node, *etcdErr.Error) {
 // If the receiver is not a directory, a "Not A Directory" error will be returned.
 // If there is a existing node with the same name under the directory, a "Already Exist"
 // error will be returned
-func (n *Node) Add(child *Node) *etcdErr.Error {
+func (n *node) Add(child *node) *etcdErr.Error {
 	if !n.IsDir() {
 		return etcdErr.NewError(etcdErr.EcodeNotDir, "", n.store.Index())
 	}
@@ -175,7 +175,7 @@ func (n *Node) Add(child *Node) *etcdErr.Error {
 }
 
 // Remove function remove the node.
-func (n *Node) Remove(recursive bool, callback func(path string)) *etcdErr.Error {
+func (n *node) Remove(recursive bool, callback func(path string)) *etcdErr.Error {
 
 	if n.IsDir() && !recursive {
 		// cannot delete a directory without set recursive to true
@@ -223,7 +223,7 @@ func (n *Node) Remove(recursive bool, callback func(path string)) *etcdErr.Error
 	return nil
 }
 
-func (n *Node) Pair(recurisive, sorted bool) KeyValuePair {
+func (n *node) Pair(recurisive, sorted bool) KeyValuePair {
 	if n.IsDir() {
 		pair := KeyValuePair{
 			Key:           n.Path,
@@ -272,7 +272,7 @@ func (n *Node) Pair(recurisive, sorted bool) KeyValuePair {
 	return pair
 }
 
-func (n *Node) UpdateTTL(expireTime time.Time) {
+func (n *node) UpdateTTL(expireTime time.Time) {
 
 	if !n.IsPermanent() {
 		if expireTime.IsZero() {
@@ -299,7 +299,7 @@ func (n *Node) UpdateTTL(expireTime time.Time) {
 // Clone function clone the node recursively and return the new node.
 // If the node is a directory, it will clone all the content under this directory.
 // If the node is a key-value pair, it will clone the pair.
-func (n *Node) Clone() *Node {
+func (n *node) Clone() *node {
 	if !n.IsDir() {
 		return newKV(n.store, n.Path, n.Value, n.CreateIndex, n.Parent, n.ACL, n.ExpireTime)
 	}
@@ -320,7 +320,7 @@ func (n *Node) Clone() *Node {
 // call this function on its children.
 // We check the expire last since we need to recover the whole structure first and add all the
 // notifications into the event history.
-func (n *Node) recoverAndclean() {
+func (n *node) recoverAndclean() {
 	if n.IsDir() {
 		for _, child := range n.Children {
 			child.Parent = n

+ 10 - 11
store/store.go

@@ -59,7 +59,7 @@ type Store interface {
 }
 
 type store struct {
-	Root           *Node
+	Root           *node
 	WatcherHub     *watcherHub
 	CurrentIndex   uint64
 	Stats          *Stats
@@ -152,7 +152,7 @@ func (s *store) Get(nodePath string, recursive, sorted bool) (*Event, error) {
 	return e, nil
 }
 
-// Create function creates the Node at nodePath. Create will help to create intermediate directories with no ttl.
+// Create function creates the node at nodePath. Create will help to create intermediate directories with no ttl.
 // If the node has already existed, create will fail.
 // If any node on the path is a file, create will fail.
 func (s *store) Create(nodePath string, value string, unique bool, expireTime time.Time) (*Event, error) {
@@ -171,7 +171,7 @@ func (s *store) Create(nodePath string, value string, unique bool, expireTime ti
 	return e, err
 }
 
-// Set function creates or replace the Node at nodePath.
+// Set function creates or replace the node at nodePath.
 func (s *store) Set(nodePath string, value string, expireTime time.Time) (*Event, error) {
 	nodePath = path.Clean(path.Join("/", nodePath))
 
@@ -309,7 +309,7 @@ func (s *store) Watch(prefix string, recursive bool, sinceIndex uint64) (<-chan
 }
 
 // walk function walks all the nodePath and apply the walkFunc on each directory
-func (s *store) walk(nodePath string, walkFunc func(prev *Node, component string) (*Node, *etcdErr.Error)) (*Node, *etcdErr.Error) {
+func (s *store) walk(nodePath string, walkFunc func(prev *node, component string) (*node, *etcdErr.Error)) (*node, *etcdErr.Error) {
 	components := strings.Split(nodePath, "/")
 
 	curr := s.Root
@@ -396,8 +396,7 @@ func (s *store) internalCreate(nodePath string, value string, unique bool, repla
 		expireTime = Permanent
 	}
 
-
-	dir, newNodeName := path.Split(nodePath)
+	dir, newnodeName := path.Split(nodePath)
 
 	// walk through the nodePath, create dirs and get the last directory node
 	d, err := s.walk(dir, s.checkDir)
@@ -410,7 +409,7 @@ func (s *store) internalCreate(nodePath string, value string, unique bool, repla
 
 	e := newEvent(action, nodePath, nextIndex)
 
-	n, _ := d.GetChild(newNodeName)
+	n, _ := d.GetChild(newnodeName)
 
 	// force will try to replace a existing file
 	if n != nil {
@@ -441,7 +440,7 @@ func (s *store) internalCreate(nodePath string, value string, unique bool, repla
 	// we are sure d is a directory and does not have the children with name n.Name
 	d.Add(n)
 
-	// Node with TTL
+	// node with TTL
 	if !n.IsPermanent() {
 		s.ttlKeyHeap.push(n)
 
@@ -455,10 +454,10 @@ func (s *store) internalCreate(nodePath string, value string, unique bool, repla
 }
 
 // InternalGet function get the node of the given nodePath.
-func (s *store) internalGet(nodePath string) (*Node, *etcdErr.Error) {
+func (s *store) internalGet(nodePath string) (*node, *etcdErr.Error) {
 	nodePath = path.Clean(path.Join("/", nodePath))
 
-	walkFunc := func(parent *Node, name string) (*Node, *etcdErr.Error) {
+	walkFunc := func(parent *node, name string) (*node, *etcdErr.Error) {
 
 		if !parent.IsDir() {
 			err := etcdErr.NewError(etcdErr.EcodeNotDir, parent.Path, s.CurrentIndex)
@@ -507,7 +506,7 @@ func (s *store) DeleteExpiredKeys(cutoff time.Time) {
 // If it is a directory, this function will return the pointer to that node.
 // If it does not exist, this function will create a new directory and return the pointer to that node.
 // If it is a file, this function will return error.
-func (s *store) checkDir(parent *Node, dirName string) (*Node, *etcdErr.Error) {
+func (s *store) checkDir(parent *node, dirName string) (*node, *etcdErr.Error) {
 	node, ok := parent.Children[dirName]
 
 	if ok {

+ 9 - 9
store/ttl_key_heap.go

@@ -6,12 +6,12 @@ import (
 
 // An TTLKeyHeap is a min-heap of TTLKeys order by expiration time
 type ttlKeyHeap struct {
-	array  []*Node
-	keyMap map[*Node]int
+	array  []*node
+	keyMap map[*node]int
 }
 
 func newTtlKeyHeap() *ttlKeyHeap {
-	h := &ttlKeyHeap{keyMap: make(map[*Node]int)}
+	h := &ttlKeyHeap{keyMap: make(map[*node]int)}
 	heap.Init(h)
 	return h
 }
@@ -34,7 +34,7 @@ func (h ttlKeyHeap) Swap(i, j int) {
 }
 
 func (h *ttlKeyHeap) Push(x interface{}) {
-	n, _ := x.(*Node)
+	n, _ := x.(*node)
 	h.keyMap[n] = len(h.array)
 	h.array = append(h.array, n)
 }
@@ -48,16 +48,16 @@ func (h *ttlKeyHeap) Pop() interface{} {
 	return x
 }
 
-func (h *ttlKeyHeap) top() *Node {
+func (h *ttlKeyHeap) top() *node {
 	if h.Len() != 0 {
 		return h.array[0]
 	}
 	return nil
 }
 
-func (h *ttlKeyHeap) pop() *Node {
+func (h *ttlKeyHeap) pop() *node {
 	x := heap.Pop(h)
-	n, _ := x.(*Node)
+	n, _ := x.(*node)
 	return n
 }
 
@@ -65,7 +65,7 @@ func (h *ttlKeyHeap) push(x interface{}) {
 	heap.Push(h, x)
 }
 
-func (h *ttlKeyHeap) update(n *Node) {
+func (h *ttlKeyHeap) update(n *node) {
 	index, ok := h.keyMap[n]
 	if ok {
 		heap.Remove(h, index)
@@ -73,7 +73,7 @@ func (h *ttlKeyHeap) update(n *Node) {
 	}
 }
 
-func (h *ttlKeyHeap) remove(n *Node) {
+func (h *ttlKeyHeap) remove(n *node) {
 	index, ok := h.keyMap[n]
 	if ok {
 		heap.Remove(h, index)