From 63a1fc6f31896dcf830fdefaf118f9a74b4e5be6 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 1 Aug 2013 17:25:26 +0300 Subject: [PATCH] refeactored history --- history/controller.go | 2 +- history/file_store.go | 72 ++++++++++++++++++++++++++++++++++ history/mock_stor.go | 1 + history/proxy_store.go | 24 ++++++++++++ history/store.go | 88 ------------------------------------------ 5 files changed, 98 insertions(+), 89 deletions(-) create mode 100644 history/file_store.go create mode 100644 history/mock_stor.go create mode 100644 history/proxy_store.go diff --git a/history/controller.go b/history/controller.go index 784d4fe15..0971196fb 100644 --- a/history/controller.go +++ b/history/controller.go @@ -22,7 +22,7 @@ func start() { if *masterAddr != "" { store, err := NewProxyStore(*masterAddr) } else { - store, err := NewHistoryStore(*dataFile) + store, err := NewFileStore(*dataFile) } rpc.RegisterName("Store", store) rpc.HandleHTTP() diff --git a/history/file_store.go b/history/file_store.go new file mode 100644 index 000000000..0d1550ad3 --- /dev/null +++ b/history/file_store.go @@ -0,0 +1,72 @@ +package history + +import ( + "bufio" + "encoding/json" + "errors" + "os" + "os/exec" + "sync" +) + +type FileStore struct { + sync.RWMutex + filename string + records records +} + +func NewFileStore(filename string) (*FileStore, error) { + // looking for git + _, err := exec.LookPath("git") + if err != nil { + return nil, errors.New("Please install git: " + err.Error()) + } + s := &FileStore{filename: filename} + return s, s.load() +} + +func (s *FileStore) Record(key string, obj interface{}) error { + s.Lock() + defer s.Unlock() + s.records = s.records.SetOrAdd(key, obj) + return nil +} + +func (s *FileStore) commit() error { + out, err := exec.Command("git", "commit", "-a", "-m", "'historic commit'").Output() + if err != nil { + return errors.New(string(out) + " " + err.Error()) + } + return nil +} + +func (s *FileStore) load() error { + f, err := os.Open(s.filename) + if err != nil { + return err + } + defer f.Close() + d := json.NewDecoder(f) + + if err := d.Decode(&s.records); err != nil { + return err + } + s.records.Sort() + return nil +} + +func (s *FileStore) save(filename string) error { + f, err := os.Create(filename) + if err != nil { + return err + } + b := bufio.NewWriter(f) + e := json.NewEncoder(b) + defer f.Close() + defer b.Flush() + s.records.Sort() + if err := e.Encode(s.records); err != nil { + return err + } + return s.commit() +} diff --git a/history/mock_stor.go b/history/mock_stor.go new file mode 100644 index 000000000..3dfb0b865 --- /dev/null +++ b/history/mock_stor.go @@ -0,0 +1 @@ +package history \ No newline at end of file diff --git a/history/proxy_store.go b/history/proxy_store.go new file mode 100644 index 000000000..61bedd497 --- /dev/null +++ b/history/proxy_store.go @@ -0,0 +1,24 @@ +package history + +import ( + "net/rpc" +) + +type ProxyStore struct { + client *rpc.Client +} + +func NewProxyStore(addr string) (*ProxyStore, error) { + client, err := rpc.DialHTTP("tcp", addr) + if err != nil { + return nil, err + } + return &ProxyStore{client: client}, nil +} + +func (ps *ProxyStore) Record(key string, obj interface{}) error { + if err := ps.client.Call("Store.Record", key, obj); err != nil { + return err + } + return nil +} diff --git a/history/store.go b/history/store.go index 4b2bcc145..0c1288666 100644 --- a/history/store.go +++ b/history/store.go @@ -1,26 +1,13 @@ package history import ( - "bufio" - "encoding/json" - "errors" - "net/rpc" - "os" - "os/exec" "sort" - "sync" ) type Store interface { Record(key string, obj interface{}) error } -type HistoryStore struct { - sync.RWMutex - filename string - records records -} - type record struct { Key string Object interface{} @@ -58,78 +45,3 @@ func (rs records) SetOrAdd(key string, obj interface{}) records { } return rs } - -func NewHistoryStore(filename string) (*HistoryStore, error) { - // looking for git - _, err := exec.LookPath("git") - if err != nil { - return nil, errors.New("Please install git: " + err.Error()) - } - s := &HistoryStore{filename: filename} - return s, s.load() -} - -func (s *HistoryStore) Record(key string, obj interface{}) error { - s.Lock() - defer s.Unlock() - s.records = s.records.SetOrAdd(key, obj) - return nil -} - -func (s *HistoryStore) commit() error { - out, err := exec.Command("git", "commit", "-a", "-m", "'historic commit'").Output() - if err != nil { - return errors.New(string(out) + " " + err.Error()) - } - return nil -} - -func (s *HistoryStore) load() error { - f, err := os.Open(s.filename) - if err != nil { - return err - } - defer f.Close() - d := json.NewDecoder(f) - - if err := d.Decode(&s.records); err != nil { - return err - } - s.records.Sort() - return nil -} - -func (s *HistoryStore) save(filename string) error { - f, err := os.Create(filename) - if err != nil { - return err - } - b := bufio.NewWriter(f) - e := json.NewEncoder(b) - defer f.Close() - defer b.Flush() - s.records.Sort() - if err := e.Encode(s.records); err != nil { - return err - } - return s.commit() -} - -type ProxyStore struct { - client *rpc.Client -} - -func NewProxyStore(addr string) (*ProxyStore, error) { - client, err := rpc.DialHTTP("tcp", addr) - if err != nil { - return nil, err - } - return &ProxyStore{client: client}, nil -} - -func (ps *ProxyStore) Record(key string, obj interface{}) error { - if err := ps.client.Call("Store.Record", key, obj); err != nil { - return err - } - return nil -}