Browse Source

refactor change node_repr to node_extern

Xiang Li 12 years ago
parent
commit
67b4c27d5d

+ 1 - 1
server/v2/tests/delete_handler_test.go

@@ -24,6 +24,6 @@ func TestV2DeleteKey(t *testing.T) {
 		resp, err = tests.DeleteForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), url.Values{})
 		resp, err = tests.DeleteForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), url.Values{})
 		body := tests.ReadBody(resp)
 		body := tests.ReadBody(resp)
 		assert.Nil(t, err, "")
 		assert.Nil(t, err, "")
-		assert.Equal(t, string(body), `{"action":"delete","node":{"key":"/foo/bar","modifiedIndex":2}}`, "")
+		assert.Equal(t, string(body), `{"action":"delete","node":{"key":"/foo/bar","modifiedIndex":2,"createdIndex":1}}`, "")
 	})
 	})
 }
 }

+ 1 - 1
server/v2/tests/put_handler_test.go

@@ -22,7 +22,7 @@ func TestV2SetKey(t *testing.T) {
 		resp, err := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
 		resp, err := tests.PutForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), v)
 		body := tests.ReadBody(resp)
 		body := tests.ReadBody(resp)
 		assert.Nil(t, err, "")
 		assert.Nil(t, err, "")
-		assert.Equal(t, string(body), `{"action":"set","node":{"key":"/foo/bar","value":"XXX","modifiedIndex":1}}`, "")
+		assert.Equal(t, string(body), `{"action":"set","node":{"key":"/foo/bar","value":"XXX","modifiedIndex":1,"createdIndex":1}}`, "")
 	})
 	})
 }
 }
 
 

+ 3 - 3
store/event.go

@@ -11,12 +11,12 @@ const (
 )
 )
 
 
 type Event struct {
 type Event struct {
-	Action string `json:"action"`
-	Node   *Node  `json:"node,omitempty"`
+	Action string      `json:"action"`
+	Node   *NodeExtern `json:"node,omitempty"`
 }
 }
 
 
 func newEvent(action string, key string, modifiedIndex, createdIndex uint64) *Event {
 func newEvent(action string, key string, modifiedIndex, createdIndex uint64) *Event {
-	n := &Node{
+	n := &NodeExtern{
 		Key:           key,
 		Key:           key,
 		ModifiedIndex: modifiedIndex,
 		ModifiedIndex: modifiedIndex,
 		CreatedIndex:  createdIndex,
 		CreatedIndex:  createdIndex,

+ 6 - 6
store/event_test.go

@@ -13,7 +13,7 @@ func TestEventQueue(t *testing.T) {
 
 
 	// Add
 	// Add
 	for i := 0; i < 200; i++ {
 	for i := 0; i < 200; i++ {
-		e := newEvent(Create, "/foo", uint64(i))
+		e := newEvent(Create, "/foo", uint64(i), uint64(i))
 		eh.addEvent(e)
 		eh.addEvent(e)
 	}
 	}
 
 
@@ -35,11 +35,11 @@ func TestScanHistory(t *testing.T) {
 	eh := newEventHistory(100)
 	eh := newEventHistory(100)
 
 
 	// Add
 	// Add
-	eh.addEvent(newEvent(Create, "/foo", 1))
-	eh.addEvent(newEvent(Create, "/foo/bar", 2))
-	eh.addEvent(newEvent(Create, "/foo/foo", 3))
-	eh.addEvent(newEvent(Create, "/foo/bar/bar", 4))
-	eh.addEvent(newEvent(Create, "/foo/foo/foo", 5))
+	eh.addEvent(newEvent(Create, "/foo", 1, 1))
+	eh.addEvent(newEvent(Create, "/foo/bar", 2, 2))
+	eh.addEvent(newEvent(Create, "/foo/foo", 3, 3))
+	eh.addEvent(newEvent(Create, "/foo/bar/bar", 4, 4))
+	eh.addEvent(newEvent(Create, "/foo/foo/foo", 5, 5))
 
 
 	e, err := eh.scan("/foo", 1)
 	e, err := eh.scan("/foo", 1)
 	if err != nil || e.Index() != 1 {
 	if err != nil || e.Index() != 1 {

+ 6 - 4
store/node.go

@@ -223,12 +223,13 @@ func (n *node) Remove(recursive bool, callback func(path string)) *etcdErr.Error
 	return nil
 	return nil
 }
 }
 
 
-func (n *node) Repr(recurisive, sorted bool) Node {
+func (n *node) Repr(recurisive, sorted bool) NodeExtern {
 	if n.IsDir() {
 	if n.IsDir() {
-		node := Node{
+		node := NodeExtern{
 			Key:           n.Path,
 			Key:           n.Path,
 			Dir:           true,
 			Dir:           true,
 			ModifiedIndex: n.ModifiedIndex,
 			ModifiedIndex: n.ModifiedIndex,
+			CreatedIndex:  n.CreatedIndex,
 		}
 		}
 		node.Expiration, node.TTL = n.ExpirationAndTTL()
 		node.Expiration, node.TTL = n.ExpirationAndTTL()
 
 
@@ -237,7 +238,7 @@ func (n *node) Repr(recurisive, sorted bool) Node {
 		}
 		}
 
 
 		children, _ := n.List()
 		children, _ := n.List()
-		node.Nodes = make(Nodes, len(children))
+		node.Nodes = make(NodeExterns, len(children))
 
 
 		// we do not use the index in the children slice directly
 		// we do not use the index in the children slice directly
 		// we need to skip the hidden one
 		// we need to skip the hidden one
@@ -263,10 +264,11 @@ func (n *node) Repr(recurisive, sorted bool) Node {
 		return node
 		return node
 	}
 	}
 
 
-	node := Node{
+	node := NodeExtern{
 		Key:           n.Path,
 		Key:           n.Path,
 		Value:         n.Value,
 		Value:         n.Value,
 		ModifiedIndex: n.ModifiedIndex,
 		ModifiedIndex: n.ModifiedIndex,
+		CreatedIndex:  n.CreatedIndex,
 	}
 	}
 	node.Expiration, node.TTL = n.ExpirationAndTTL()
 	node.Expiration, node.TTL = n.ExpirationAndTTL()
 	return node
 	return node

+ 36 - 0
store/node_extern.go

@@ -0,0 +1,36 @@
+package store
+
+import (
+	"time"
+)
+
+// NodeExtern is the external representation of the
+// internal node with additional fields
+// PrevValue is the previous value of the node
+// TTL is time to live in second
+type NodeExtern struct {
+	Key           string      `json:"key, omitempty"`
+	PrevValue     string      `json:"prevValue,omitempty"`
+	Value         string      `json:"value,omitempty"`
+	Dir           bool        `json:"dir,omitempty"`
+	Expiration    *time.Time  `json:"expiration,omitempty"`
+	TTL           int64       `json:"ttl,omitempty"`
+	Nodes         NodeExterns `json:"nodes,omitempty"`
+	ModifiedIndex uint64      `json:"modifiedIndex,omitempty"`
+	CreatedIndex  uint64      `json:"createdIndex,omitempty"`
+}
+
+type NodeExterns []NodeExtern
+
+// interfaces for sorting
+func (ns NodeExterns) Len() int {
+	return len(ns)
+}
+
+func (ns NodeExterns) Less(i, j int) bool {
+	return ns[i].Key < ns[j].Key
+}
+
+func (ns NodeExterns) Swap(i, j int) {
+	ns[i], ns[j] = ns[j], ns[i]
+}

+ 0 - 35
store/node_repr.go

@@ -1,35 +0,0 @@
-package store
-
-import (
-	"time"
-)
-
-// Node is the representation of the internal node with additional fields
-// PrevValue is the previous value of the node
-// TTL is time to live in second
-type Node struct {
-	Key           string     `json:"key, omitempty"`
-	PrevValue     string     `json:"prevValue,omitempty"`
-	Value         string     `json:"value,omitempty"`
-	Dir           bool       `json:"dir,omitempty"`
-	Expiration    *time.Time `json:"expiration,omitempty"`
-	TTL           int64      `json:"ttl,omitempty"`
-	Nodes         Nodes      `json:"nodes,omitempty"`
-	ModifiedIndex uint64     `json:"modifiedIndex,omitempty"`
-	CreatedIndex  uint64     `json:"createdIndex,omitempty"`
-}
-
-type Nodes []Node
-
-// interfaces for sorting
-func (ns Nodes) Len() int {
-	return len(ns)
-}
-
-func (ns Nodes) Less(i, j int) bool {
-	return ns[i].Key < ns[j].Key
-}
-
-func (ns Nodes) Swap(i, j int) {
-	ns[i], ns[j] = ns[j], ns[i]
-}

+ 2 - 2
store/store.go

@@ -120,7 +120,7 @@ func (s *store) Get(nodePath string, recursive, sorted bool) (*Event, error) {
 		eNode.Dir = true
 		eNode.Dir = true
 
 
 		children, _ := n.List()
 		children, _ := n.List()
-		eNode.Nodes = make(Nodes, len(children))
+		eNode.Nodes = make(NodeExterns, len(children))
 
 
 		// we do not use the index in the children slice directly
 		// we do not use the index in the children slice directly
 		// we need to skip the hidden one
 		// we need to skip the hidden one
@@ -260,7 +260,7 @@ func (s *store) Delete(nodePath string, recursive bool) (*Event, error) {
 	if n.IsDir() {
 	if n.IsDir() {
 		eNode.Dir = true
 		eNode.Dir = true
 	} else {
 	} else {
-		eNode.PrevValue = eNode.Value
+		eNode.PrevValue = n.Value
 	}
 	}
 
 
 	callback := func(path string) { // notify function
 	callback := func(path string) { // notify function

+ 3 - 3
store/watcher_test.go

@@ -35,7 +35,7 @@ func TestWatcher(t *testing.T) {
 		// do nothing
 		// do nothing
 	}
 	}
 
 
-	e := newEvent(Create, "/foo/bar", 1)
+	e := newEvent(Create, "/foo/bar", 1, 1)
 
 
 	wh.notify(e)
 	wh.notify(e)
 
 
@@ -47,7 +47,7 @@ func TestWatcher(t *testing.T) {
 
 
 	c, _ = wh.watch("/foo", false, 2)
 	c, _ = wh.watch("/foo", false, 2)
 
 
-	e = newEvent(Create, "/foo/bar", 2)
+	e = newEvent(Create, "/foo/bar", 2, 2)
 
 
 	wh.notify(e)
 	wh.notify(e)
 
 
@@ -58,7 +58,7 @@ func TestWatcher(t *testing.T) {
 		// do nothing
 		// do nothing
 	}
 	}
 
 
-	e = newEvent(Create, "/foo", 3)
+	e = newEvent(Create, "/foo", 3, 3)
 
 
 	wh.notify(e)
 	wh.notify(e)