From 67b4c27d5dae314b99558eb823435490bc1a65d2 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Thu, 28 Nov 2013 21:34:38 -0500 Subject: [PATCH] refactor change node_repr to node_extern --- server/v2/tests/delete_handler_test.go | 2 +- server/v2/tests/put_handler_test.go | 2 +- store/event.go | 6 ++--- store/event_test.go | 12 ++++----- store/node.go | 10 ++++--- store/node_extern.go | 36 ++++++++++++++++++++++++++ store/node_repr.go | 35 ------------------------- store/store.go | 4 +-- store/watcher_test.go | 6 ++--- 9 files changed, 58 insertions(+), 55 deletions(-) create mode 100644 store/node_extern.go delete mode 100644 store/node_repr.go diff --git a/server/v2/tests/delete_handler_test.go b/server/v2/tests/delete_handler_test.go index f2f6decbc..4907e102d 100644 --- a/server/v2/tests/delete_handler_test.go +++ b/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{}) body := tests.ReadBody(resp) 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}}`, "") }) } diff --git a/server/v2/tests/put_handler_test.go b/server/v2/tests/put_handler_test.go index dd98b065f..3a89790dd 100644 --- a/server/v2/tests/put_handler_test.go +++ b/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) body := tests.ReadBody(resp) 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}}`, "") }) } diff --git a/store/event.go b/store/event.go index 9a5a835c0..6a379985b 100644 --- a/store/event.go +++ b/store/event.go @@ -11,12 +11,12 @@ const ( ) 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 { - n := &Node{ + n := &NodeExtern{ Key: key, ModifiedIndex: modifiedIndex, CreatedIndex: createdIndex, diff --git a/store/event_test.go b/store/event_test.go index dc30ce44d..39dc7fee1 100644 --- a/store/event_test.go +++ b/store/event_test.go @@ -13,7 +13,7 @@ func TestEventQueue(t *testing.T) { // Add for i := 0; i < 200; i++ { - e := newEvent(Create, "/foo", uint64(i)) + e := newEvent(Create, "/foo", uint64(i), uint64(i)) eh.addEvent(e) } @@ -35,11 +35,11 @@ func TestScanHistory(t *testing.T) { eh := newEventHistory(100) // 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) if err != nil || e.Index() != 1 { diff --git a/store/node.go b/store/node.go index 809b4cadb..a165add3b 100644 --- a/store/node.go +++ b/store/node.go @@ -223,12 +223,13 @@ func (n *node) Remove(recursive bool, callback func(path string)) *etcdErr.Error return nil } -func (n *node) Repr(recurisive, sorted bool) Node { +func (n *node) Repr(recurisive, sorted bool) NodeExtern { if n.IsDir() { - node := Node{ + node := NodeExtern{ Key: n.Path, Dir: true, ModifiedIndex: n.ModifiedIndex, + CreatedIndex: n.CreatedIndex, } node.Expiration, node.TTL = n.ExpirationAndTTL() @@ -237,7 +238,7 @@ func (n *node) Repr(recurisive, sorted bool) Node { } 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 need to skip the hidden one @@ -263,10 +264,11 @@ func (n *node) Repr(recurisive, sorted bool) Node { return node } - node := Node{ + node := NodeExtern{ Key: n.Path, Value: n.Value, ModifiedIndex: n.ModifiedIndex, + CreatedIndex: n.CreatedIndex, } node.Expiration, node.TTL = n.ExpirationAndTTL() return node diff --git a/store/node_extern.go b/store/node_extern.go new file mode 100644 index 000000000..319378f74 --- /dev/null +++ b/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] +} diff --git a/store/node_repr.go b/store/node_repr.go deleted file mode 100644 index 2dabf95be..000000000 --- a/store/node_repr.go +++ /dev/null @@ -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] -} diff --git a/store/store.go b/store/store.go index 95db10037..8f17d5a3c 100644 --- a/store/store.go +++ b/store/store.go @@ -120,7 +120,7 @@ func (s *store) Get(nodePath string, recursive, sorted bool) (*Event, error) { eNode.Dir = true 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 need to skip the hidden one @@ -260,7 +260,7 @@ func (s *store) Delete(nodePath string, recursive bool) (*Event, error) { if n.IsDir() { eNode.Dir = true } else { - eNode.PrevValue = eNode.Value + eNode.PrevValue = n.Value } callback := func(path string) { // notify function diff --git a/store/watcher_test.go b/store/watcher_test.go index 386fec440..ae36eda28 100644 --- a/store/watcher_test.go +++ b/store/watcher_test.go @@ -35,7 +35,7 @@ func TestWatcher(t *testing.T) { // do nothing } - e := newEvent(Create, "/foo/bar", 1) + e := newEvent(Create, "/foo/bar", 1, 1) wh.notify(e) @@ -47,7 +47,7 @@ func TestWatcher(t *testing.T) { c, _ = wh.watch("/foo", false, 2) - e = newEvent(Create, "/foo/bar", 2) + e = newEvent(Create, "/foo/bar", 2, 2) wh.notify(e) @@ -58,7 +58,7 @@ func TestWatcher(t *testing.T) { // do nothing } - e = newEvent(Create, "/foo", 3) + e = newEvent(Create, "/foo", 3, 3) wh.notify(e)