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 --- core/memory.go | 7 +++--- core/object.go | 25 ++++--------------- core/object_test.go | 8 +++--- core/reference.go | 8 +++--- core/storage.go | 72 ++++++++++++++++++++++++++++------------------------- 5 files changed, 55 insertions(+), 65 deletions(-) (limited to 'core') diff --git a/core/memory.go b/core/memory.go index 97b1b9b..dabee3a 100644 --- a/core/memory.go +++ b/core/memory.go @@ -1,7 +1,8 @@ -package core +package core import ( "bytes" + "io" "io/ioutil" ) @@ -39,12 +40,12 @@ func (o *MemoryObject) Size() int64 { return o.sz } func (o *MemoryObject) SetSize(s int64) { o.sz = s } // Reader returns a ObjectReader used to read the object's content. -func (o *MemoryObject) Reader() (ObjectReader, error) { +func (o *MemoryObject) Reader() (io.ReadCloser, error) { return ioutil.NopCloser(bytes.NewBuffer(o.cont)), nil } // Writer returns a ObjectWriter used to write the object's content. -func (o *MemoryObject) Writer() (ObjectWriter, error) { +func (o *MemoryObject) Writer() (io.WriteCloser, error) { return o, nil } diff --git a/core/object.go b/core/object.go index 30beff8..8125465 100644 --- a/core/object.go +++ b/core/object.go @@ -12,21 +12,6 @@ var ( ErrInvalidType = errors.New("invalid object type") ) -// TODO: Consider adding a Hash function to the ObjectReader and ObjectWriter -// interfaces that returns the hash calculated for the reader or writer. - -// ObjectReader is a generic representation of an object reader. -// -// ObjectReader implements io.ReadCloser. Close should be called when finished -// with it. -type ObjectReader io.ReadCloser - -// ObjectWriter is a generic representation of an object writer. -// -// ObjectWriter implements io.WriterCloser. Close should be called when finished -// with it. -type ObjectWriter io.WriteCloser - // Object is a generic representation of any git object type Object interface { Hash() Hash @@ -34,8 +19,8 @@ type Object interface { SetType(ObjectType) Size() int64 SetSize(int64) - Reader() (ObjectReader, error) - Writer() (ObjectWriter, error) + Reader() (io.ReadCloser, error) + Writer() (io.WriteCloser, error) } // ObjectType internal object type @@ -116,7 +101,7 @@ func ParseObjectType(value string) (typ ObjectType, err error) { // The ObjectLookupIter must be closed with a call to Close() when it is no // longer needed. type ObjectLookupIter struct { - storage ObjectStorage + storage ObjectStorer series []Hash t ObjectType pos int @@ -124,7 +109,7 @@ type ObjectLookupIter struct { // NewObjectLookupIter returns an object iterator given an object storage and // a slice of object hashes. -func NewObjectLookupIter(storage ObjectStorage, t ObjectType, series []Hash) *ObjectLookupIter { +func NewObjectLookupIter(storage ObjectStorer, t ObjectType, series []Hash) *ObjectLookupIter { return &ObjectLookupIter{ storage: storage, series: series, @@ -142,7 +127,7 @@ func (iter *ObjectLookupIter) Next() (Object, error) { } hash := iter.series[iter.pos] - obj, err := iter.storage.Get(iter.t, hash) + obj, err := iter.storage.Object(iter.t, hash) if err == nil { iter.pos++ } diff --git a/core/object_test.go b/core/object_test.go index d087aae..5ebf26a 100644 --- a/core/object_test.go +++ b/core/object_test.go @@ -159,18 +159,18 @@ func (o *MockObjectStorage) NewObject() Object { return nil } -func (o *MockObjectStorage) Set(obj Object) (Hash, error) { +func (o *MockObjectStorage) SetObject(obj Object) (Hash, error) { return ZeroHash, nil } -func (o *MockObjectStorage) Get(t ObjectType, h Hash) (Object, error) { +func (o *MockObjectStorage) Object(t ObjectType, h Hash) (Object, error) { return &MemoryObject{h: h}, nil } -func (o *MockObjectStorage) Iter(t ObjectType) (ObjectIter, error) { +func (o *MockObjectStorage) IterObjects(t ObjectType) (ObjectIter, error) { return nil, nil } -func (o *MockObjectStorage) Begin() TxObjectStorage { +func (o *MockObjectStorage) Begin() Transaction { return nil } diff --git a/core/reference.go b/core/reference.go index 16b60ac..ee55a6a 100644 --- a/core/reference.go +++ b/core/reference.go @@ -203,15 +203,15 @@ func (iter *ReferenceSliceIter) Close() { iter.pos = len(iter.series) } -func ResolveReference(s ReferenceStorage, n ReferenceName) (*Reference, error) { - r, err := s.Get(n) +func ResolveReference(s ReferenceStorer, n ReferenceName) (*Reference, error) { + r, err := s.Reference(n) if err != nil || r == nil { return r, err } return resolveReference(s, r, 0) } -func resolveReference(s ReferenceStorage, r *Reference, recursion int) (*Reference, error) { +func resolveReference(s ReferenceStorer, r *Reference, recursion int) (*Reference, error) { if r.Type() != SymbolicReference { return r, nil } @@ -220,7 +220,7 @@ func resolveReference(s ReferenceStorage, r *Reference, recursion int) (*Referen return nil, ErrMaxResolveRecursion } - t, err := s.Get(r.Target()) + t, err := s.Reference(r.Target()) if err != nil { return nil, err } diff --git a/core/storage.go b/core/storage.go index 739dfb6..101115b 100644 --- a/core/storage.go +++ b/core/storage.go @@ -7,45 +7,49 @@ import ( var ( //ErrStop is used to stop a ForEach function in an Iter - ErrStop = errors.New("stop iter") - ErrNotImplemented = errors.New("method not-implemented") + ErrStop = errors.New("stop iter") ) -// ObjectStorage generic storage of objects -type ObjectStorage interface { +// ObjectStorer generic storage of objects +type ObjectStorer interface { // NewObject returns a new Object, the real type of the object can be a // custom implementation or the defaul one, MemoryObject NewObject() Object - // Set save an object into the storage, the object shuld be create with - // the NewObject, method, and file if the type is not supported. - Set(Object) (Hash, error) - // Get an object by hash with the given ObjectType. Implementors should - // return (nil, ErrObjectNotFound) if an object doesn't exist with both the - // given hash and object type. + // SetObject save an object into the storage, the object shuld be create + // with the NewObject, method, and file if the type is not supported. + SetObject(Object) (Hash, error) + // Object an object by hash with the given ObjectType. Implementors + // should return (nil, ErrObjectNotFound) if an object doesn't exist with + // both the given hash and object type. // // Valid ObjectType values are CommitObject, BlobObject, TagObject, - // TreeObject and AnyObject. - // - // If AnyObject is given, the object must be looked up regardless of its type. - Get(ObjectType, Hash) (Object, error) - // Iter returns a custom ObjectIter over all the object on the storage. + // TreeObject and AnyObject. If AnyObject is given, the object must be + // looked up regardless of its type. + Object(ObjectType, Hash) (Object, error) + // IterObjects returns a custom ObjectIter over all the object on the + // storage. // // Valid ObjectType values are CommitObject, BlobObject, TagObject, - Iter(ObjectType) (ObjectIter, error) + IterObjects(ObjectType) (ObjectIter, error) +} + +// Transactioner is a optional method for ObjectStorer, it enable transaction +// base write and read operations in the storage +type Transactioner interface { // Begin starts a transaction. - Begin() TxObjectStorage + Begin() Transaction } -// ObjectStorageWrite is a optional method for ObjectStorage, it enable direct -// write of packfile to the storage -type ObjectStorageWrite interface { - // Writer retuns a writer for writing a packfile to the Storage, this method - // is optional, if not implemented the ObjectStorage should return a - // ErrNotImplemented error. +// PackfileWriter is a optional method for ObjectStorer, it enable direct write +// of packfile to the storage +type PackfileWriter interface { + // PackfileWriter retuns a writer for writing a packfile to the Storage, + // this method is optional, if not implemented the ObjectStorer should + // return a ErrNotImplemented error. // // If the implementation not implements Writer the objects should be written // using the Set method. - Writer() (io.WriteCloser, error) + PackfileWriter() (io.WriteCloser, error) } // ObjectIter is a generic closable interface for iterating over objects. @@ -55,20 +59,20 @@ type ObjectIter interface { Close() } -// TxObjectStorage is an in-progress storage transaction. -// A transaction must end with a call to Commit or Rollback. -type TxObjectStorage interface { - Set(Object) (Hash, error) - Get(ObjectType, Hash) (Object, error) +// Transaction is an in-progress storage transaction. A transaction must end +// with a call to Commit or Rollback. +type Transaction interface { + SetObject(Object) (Hash, error) + Object(ObjectType, Hash) (Object, error) Commit() error Rollback() error } -// ReferenceStorage generic storage of references -type ReferenceStorage interface { - Set(*Reference) error - Get(ReferenceName) (*Reference, error) - Iter() (ReferenceIter, error) +// ReferenceStorer generic storage of references +type ReferenceStorer interface { + SetReference(*Reference) error + Reference(ReferenceName) (*Reference, error) + IterReferences() (ReferenceIter, error) } // ReferenceIter is a generic closable interface for iterating over references -- cgit