From 0ff9ef2b44c53e557c78bde0fd9c29847e5f0e23 Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Mon, 7 Nov 2016 20:29:58 +0100 Subject: global storage interface refactor (#112) * core: ObjectStorage, ReferenceStorage renamed to ObjectStorer and ReferenceStorer * rebase * general, changes request by @alcortes * general, changes request by @alcortes --- examples/storage/aerospike/storage.go | 163 +++++----------------------------- 1 file changed, 21 insertions(+), 142 deletions(-) (limited to 'examples') diff --git a/examples/storage/aerospike/storage.go b/examples/storage/aerospike/storage.go index a9312ce..776fc33 100644 --- a/examples/storage/aerospike/storage.go +++ b/examples/storage/aerospike/storage.go @@ -15,17 +15,13 @@ import ( const ( urlField = "url" referencesSet = "reference" - remotesSet = "remote" + configSet = "config" ) type Storage struct { client *driver.Client ns string url string - - os *ObjectStorage - rs *ReferenceStorage - cs *ConfigStorage } func NewStorage(client *driver.Client, ns, url string) (*Storage, error) { @@ -36,46 +32,11 @@ func NewStorage(client *driver.Client, ns, url string) (*Storage, error) { return &Storage{client: client, ns: ns, url: url}, nil } -func (s *Storage) ObjectStorage() core.ObjectStorage { - if s.os == nil { - s.os = &ObjectStorage{s.client, s.ns, s.url} - } - - return s.os -} - -func (s *Storage) ReferenceStorage() core.ReferenceStorage { - if s.rs == nil { - s.rs = &ReferenceStorage{s.client, s.ns, s.url} - } - - return s.rs -} - -func (s *Storage) ConfigStorage() config.ConfigStorage { - if s.cs == nil { - s.cs = &ConfigStorage{s.client, s.ns, s.url} - } - - return s.cs -} - -type ObjectStorage struct { - client *driver.Client - ns string - url string -} - -func (s *ObjectStorage) NewObject() core.Object { +func (s *Storage) NewObject() core.Object { return &core.MemoryObject{} } -// Writer method not supported, this method is optional to implemented. -func (s *ObjectStorage) Writer() (io.WriteCloser, error) { - return nil, core.ErrNotImplemented -} - -func (s *ObjectStorage) Set(obj core.Object) (core.Hash, error) { +func (s *Storage) SetObject(obj core.Object) (core.Hash, error) { key, err := s.buildKey(obj.Hash(), obj.Type()) if err != nil { return obj.Hash(), err @@ -102,7 +63,7 @@ func (s *ObjectStorage) Set(obj core.Object) (core.Hash, error) { return obj.Hash(), err } -func (s *ObjectStorage) Get(t core.ObjectType, h core.Hash) (core.Object, error) { +func (s *Storage) Object(t core.ObjectType, h core.Hash) (core.Object, error) { key, err := s.buildKey(h, t) if err != nil { return nil, err @@ -120,7 +81,7 @@ func (s *ObjectStorage) Get(t core.ObjectType, h core.Hash) (core.Object, error) return objectFromRecord(rec, t) } -func (s *ObjectStorage) Iter(t core.ObjectType) (core.ObjectIter, error) { +func (s *Storage) IterObjects(t core.ObjectType) (core.ObjectIter, error) { stmnt := driver.NewStatement(s.ns, t.String()) err := stmnt.Addfilter(driver.NewEqualFilter(urlField, s.url)) @@ -132,34 +93,10 @@ func (s *ObjectStorage) Iter(t core.ObjectType) (core.ObjectIter, error) { return &ObjectIter{t, rs.Records}, nil } -func (s *ObjectStorage) buildKey(h core.Hash, t core.ObjectType) (*driver.Key, error) { +func (s *Storage) buildKey(h core.Hash, t core.ObjectType) (*driver.Key, error) { return driver.NewKey(s.ns, t.String(), fmt.Sprintf("%s|%s", s.url, h.String())) } -func (s *ObjectStorage) Begin() core.TxObjectStorage { - return &TxObjectStorage{Storage: s} -} - -type TxObjectStorage struct { - Storage *ObjectStorage -} - -func (tx *TxObjectStorage) Get(t core.ObjectType, h core.Hash) (core.Object, error) { - return tx.Storage.Get(t, h) -} - -func (tx *TxObjectStorage) Set(obj core.Object) (core.Hash, error) { - return tx.Storage.Set(obj) -} - -func (tx *TxObjectStorage) Commit() error { - return nil -} - -func (tx *TxObjectStorage) Rollback() error { - return nil -} - type ObjectIter struct { t core.ObjectType ch chan *driver.Record @@ -212,15 +149,8 @@ func objectFromRecord(r *driver.Record, t core.ObjectType) (core.Object, error) return o, nil } -type ReferenceStorage struct { - client *driver.Client - ns string - url string -} - -// Set stores a reference. -func (s *ReferenceStorage) Set(ref *core.Reference) error { - key, err := s.buildKey(ref.Name()) +func (s *Storage) SetReference(ref *core.Reference) error { + key, err := s.buildReferenceKey(ref.Name()) if err != nil { return err } @@ -235,9 +165,8 @@ func (s *ReferenceStorage) Set(ref *core.Reference) error { return s.client.Put(nil, key, bins) } -// Get returns a stored reference with the given name -func (s *ReferenceStorage) Get(n core.ReferenceName) (*core.Reference, error) { - key, err := s.buildKey(n) +func (s *Storage) Reference(n core.ReferenceName) (*core.Reference, error) { + key, err := s.buildReferenceKey(n) if err != nil { return nil, err } @@ -253,12 +182,11 @@ func (s *ReferenceStorage) Get(n core.ReferenceName) (*core.Reference, error) { ), nil } -func (s *ReferenceStorage) buildKey(n core.ReferenceName) (*driver.Key, error) { +func (s *Storage) buildReferenceKey(n core.ReferenceName) (*driver.Key, error) { return driver.NewKey(s.ns, referencesSet, fmt.Sprintf("%s|%s", s.url, n)) } -// Iter returns a core.ReferenceIter -func (s *ReferenceStorage) Iter() (core.ReferenceIter, error) { +func (s *Storage) IterReferences() (core.ReferenceIter, error) { stmnt := driver.NewStatement(s.ns, referencesSet) err := stmnt.Addfilter(driver.NewEqualFilter(urlField, s.url)) if err != nil { @@ -281,14 +209,8 @@ func (s *ReferenceStorage) Iter() (core.ReferenceIter, error) { return core.NewReferenceSliceIter(refs), nil } -type ConfigStorage struct { - client *driver.Client - ns string - url string -} - -func (s *ConfigStorage) Remote(name string) (*config.RemoteConfig, error) { - key, err := s.buildRemoteKey(name) +func (s *Storage) Config() (*config.Config, error) { + key, err := s.buildConfigKey() if err != nil { return nil, err } @@ -298,44 +220,12 @@ func (s *ConfigStorage) Remote(name string) (*config.RemoteConfig, error) { return nil, err } - return remoteFromRecord(rec) -} - -func remoteFromRecord(r *driver.Record) (*config.RemoteConfig, error) { - content := r.Bins["blob"].([]byte) - - c := &config.RemoteConfig{} - return c, json.Unmarshal(content, c) -} - -func (s *ConfigStorage) Remotes() ([]*config.RemoteConfig, error) { - stmnt := driver.NewStatement(s.ns, remotesSet) - err := stmnt.Addfilter(driver.NewEqualFilter(urlField, s.url)) - if err != nil { - return nil, err - } - - rs, err := s.client.Query(nil, stmnt) - if err != nil { - return nil, err - return nil, err - } - - var remotes []*config.RemoteConfig - for r := range rs.Records { - remote, err := remoteFromRecord(r) - if err != nil { - return nil, err - } - - remotes = append(remotes, remote) - } - - return remotes, nil + c := &config.Config{} + return c, json.Unmarshal(rec.Bins["blob"].([]byte), c) } -func (s *ConfigStorage) SetRemote(r *config.RemoteConfig) error { - key, err := s.buildRemoteKey(r.Name) +func (s *Storage) SetConfig(r *config.Config) error { + key, err := s.buildConfigKey() if err != nil { return err } @@ -347,31 +237,20 @@ func (s *ConfigStorage) SetRemote(r *config.RemoteConfig) error { bins := driver.BinMap{ urlField: s.url, - "name": r.Name, "blob": json, } return s.client.Put(nil, key, bins) } -func (s *ConfigStorage) DeleteRemote(name string) error { - key, err := s.buildRemoteKey(name) - if err != nil { - return err - } - - _, err = s.client.Delete(nil, key) - return err -} - -func (s *ConfigStorage) buildRemoteKey(name string) (*driver.Key, error) { - return driver.NewKey(s.ns, remotesSet, fmt.Sprintf("%s|%s", s.url, name)) +func (s *Storage) buildConfigKey() (*driver.Key, error) { + return driver.NewKey(s.ns, configSet, fmt.Sprintf("%s|config", s.url)) } func createIndexes(c *driver.Client, ns string) error { for _, set := range [...]string{ referencesSet, - remotesSet, + configSet, core.BlobObject.String(), core.TagObject.String(), core.TreeObject.String(), -- cgit