diff --git a/store.go b/store.go index 6e61578dd..2686b1bb9 100644 --- a/store.go +++ b/store.go @@ -6,10 +6,25 @@ import ( "encoding/json" ) +// CONSTANTS +const ( + ERROR = -(1 + iota) + SET + DELETE +) + type Store struct { Nodes map[string]string `json:"nodes"` } +// global store +var s *Store + +func init() { + s = createStore() +} + +// make a new stroe func createStore() *Store{ s := new(Store) s.Nodes = make(map[string]string) @@ -25,14 +40,14 @@ func (s *Store) Set(key string, value string) (string, bool) { if ok { s.Nodes[key] = value + w.notify(SET, key, oldValue, value) return oldValue, true } else { - s.Nodes[key] = value + w.notify(SET, key, "", value) return "", false } - } // get the value of the key @@ -56,6 +71,9 @@ func (s *Store) Delete(key string) (string, error) { if ok { delete(s.Nodes, key) + + w.notify(DELETE, key, oldValue, "") + return oldValue, nil } else { return "", errors.New("Key does not exist") diff --git a/store_test.go b/store_test.go index 9d3cc2bac..934b80a6b 100644 --- a/store_test.go +++ b/store_test.go @@ -5,18 +5,17 @@ import ( ) func TestStoreGet(t *testing.T) { - store := createStore() - store.Set("foo", "bar") + s.Set("foo", "bar") - value, err := store.Get("foo") + value, err := s.Get("foo") if err!= nil || value != "bar" { t.Fatalf("Cannot get stored value") } - store.Delete("foo") - value, err = store.Get("foo") + s.Delete("foo") + value, err = s.Get("foo") if err == nil{ t.Fatalf("Got deleted value") @@ -24,11 +23,10 @@ func TestStoreGet(t *testing.T) { } func TestSaveAndRecovery(t *testing.T) { - store := createStore() - store.Set("foo", "bar") + s.Set("foo", "bar") - state, err := store.Save() + state, err := s.Save() if err != nil { t.Fatalf("Cannot Save") diff --git a/watcher.go b/watcher.go index 2799aa744..d111f302d 100644 --- a/watcher.go +++ b/watcher.go @@ -6,7 +6,6 @@ import ( "fmt" ) -// CONSTANTS type Watcher struct { chanMap map[string][]chan Notification @@ -19,12 +18,22 @@ type Notification struct { newValue string } +// global watcher +var w *Watcher + +// init the global watcher +func init() { + w = createWatcher() +} + +// create a new watcher func createWatcher() *Watcher { w := new(Watcher) w.chanMap = make(map[string][]chan Notification) return w } +// register a function with channel and prefix to the watcher func (w *Watcher) add(prefix string, c chan Notification, f func(chan Notification)) error { prefix = path.Clean(prefix) @@ -44,6 +53,7 @@ func (w *Watcher) add(prefix string, c chan Notification, f func(chan Notificati return nil } +// notify the watcher a action happened func (w *Watcher) notify(action int, key string, oldValue string, newValue string) error { key = path.Clean(key) diff --git a/watcher_test.go b/watcher_test.go index 2f899b77a..108d63144 100644 --- a/watcher_test.go +++ b/watcher_test.go @@ -6,12 +6,12 @@ import ( ) func TestWatch(t *testing.T) { - watcher := createWatcher() + // watcher := createWatcher() c := make(chan Notification) d := make(chan Notification) - watcher.add("/", c, say) - watcher.add("/prefix/", d, say) - watcher.notify(0, "/prefix/hihihi", "1", "1") + w.add("/", c, say) + w.add("/prefix/", d, say) + s.Set("/prefix/foo", "bar") } func say(c chan Notification) {