aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/storer/object.go
diff options
context:
space:
mode:
authorSantiago M. Mola <santi@mola.io>2016-12-14 23:12:44 +0100
committerMáximo Cuadros <mcuadros@gmail.com>2016-12-14 23:12:44 +0100
commit0af572dd21c0aa79d13745b633ee24ba6c4d6cf1 (patch)
tree49e81e74e82d84fd88b2fc1e4b0dc7c7bfe9c40f /plumbing/storer/object.go
parentdf0f38af83f972f026d7e14150f3d37b95f13484 (diff)
downloadgo-git-0af572dd21c0aa79d13745b633ee24ba6c4d6cf1.tar.gz
move plumbing from top level package to plumbing (#183)
* plumbing: rename Object -> EncodedObject. * plumbing/storer: rename ObjectStorer -> EncodedObjectStorer. * move difftree to plumbing/difftree. * move diff -> utils/diff * make Object/Tag/Blob/Tree/Commit/File depend on storer. * Object and its implementations now depend only on storer.EncodedObjectStorer, not git.Repository. * Tests are decoupled accordingly. * move Object/Commit/File/Tag/Tree to plumbing/object. * move Object/Commit/File/Tag/Tree to plumbing/object. * move checkClose to utils/ioutil. * move RevListObjects to plumbing/revlist.Objects. * move DiffTree to plumbing/difftree package. * rename files with plural nouns to singular * plumbing/object: add GetBlob/GetCommit/GetTag/GetTree.
Diffstat (limited to 'plumbing/storer/object.go')
-rw-r--r--plumbing/storer/object.go131
1 files changed, 69 insertions, 62 deletions
diff --git a/plumbing/storer/object.go b/plumbing/storer/object.go
index 60b9171..a733ee6 100644
--- a/plumbing/storer/object.go
+++ b/plumbing/storer/object.go
@@ -12,27 +12,30 @@ var (
ErrStop = errors.New("stop iter")
)
-// ObjectStorer generic storage of objects
-type ObjectStorer interface {
- // NewObject returns a new plumbing.Object, the real type of the object can
- // be a custom implementation or the defaul one, plumbing.MemoryObject
- NewObject() plumbing.Object
- // 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(plumbing.Object) (plumbing.Hash, error)
- // Object get an object by hash with the given plumbing.ObjectType.
- // Implementors should return (nil, plumbing.ErrObjectNotFound) if an object
- // doesn't exist with both the given hash and object type.
+// EncodedObjectStorer generic storage of objects
+type EncodedObjectStorer interface {
+ // NewEncodedObject returns a new plumbing.EncodedObject, the real type
+ // of the object can be a custom implementation or the default one,
+ // plumbing.MemoryObject.
+ NewEncodedObject() plumbing.EncodedObject
+ // SetEncodedObject saves an object into the storage, the object should
+ // be create with the NewEncodedObject, method, and file if the type is
+ // not supported.
+ SetEncodedObject(plumbing.EncodedObject) (plumbing.Hash, error)
+ // EncodedObject gets an object by hash with the given
+ // plumbing.ObjectType. Implementors should return
+ // (nil, plumbing.ErrObjectNotFound) if an object doesn't exist with
+ // both the given hash and object type.
//
// Valid plumbing.ObjectType values are CommitObject, BlobObject, TagObject,
// TreeObject and AnyObject. If plumbing.AnyObject is given, the object must
// be looked up regardless of its type.
- Object(plumbing.ObjectType, plumbing.Hash) (plumbing.Object, error)
- // IterObjects returns a custom ObjectIter over all the object on the
- // storage.
+ EncodedObject(plumbing.ObjectType, plumbing.Hash) (plumbing.EncodedObject, error)
+ // IterObjects returns a custom EncodedObjectStorer over all the object
+ // on the storage.
//
// Valid plumbing.ObjectType values are CommitObject, BlobObject, TagObject,
- IterObjects(plumbing.ObjectType) (ObjectIter, error)
+ IterEncodedObjects(plumbing.ObjectType) (EncodedObjectIter, error)
}
// Transactioner is a optional method for ObjectStorer, it enable transaction
@@ -52,41 +55,41 @@ type PackfileWriter interface {
PackfileWriter() (io.WriteCloser, error)
}
-// ObjectIter is a generic closable interface for iterating over objects.
-type ObjectIter interface {
- Next() (plumbing.Object, error)
- ForEach(func(plumbing.Object) error) error
+// EncodedObjectIter is a generic closable interface for iterating over objects.
+type EncodedObjectIter interface {
+ Next() (plumbing.EncodedObject, error)
+ ForEach(func(plumbing.EncodedObject) error) error
Close()
}
// Transaction is an in-progress storage transaction. A transaction must end
// with a call to Commit or Rollback.
type Transaction interface {
- SetObject(plumbing.Object) (plumbing.Hash, error)
- Object(plumbing.ObjectType, plumbing.Hash) (plumbing.Object, error)
+ SetEncodedObject(plumbing.EncodedObject) (plumbing.Hash, error)
+ EncodedObject(plumbing.ObjectType, plumbing.Hash) (plumbing.EncodedObject, error)
Commit() error
Rollback() error
}
-// ObjectLookupIter implements ObjectIter. It iterates over a series of object
-// hashes and yields their associated objects by retrieving each one from
-// object storage. The retrievals are lazy and only occur when the iterator
-// moves forward with a call to Next().
+// EncodedObjectLookupIter implements EncodedObjectIter. It iterates over a
+// series of object hashes and yields their associated objects by retrieving
+// each one from object storage. The retrievals are lazy and only occur when the
+// iterator moves forward with a call to Next().
//
-// The ObjectLookupIter must be closed with a call to Close() when it is no
-// longer needed.
-type ObjectLookupIter struct {
- storage ObjectStorer
+// The EncodedObjectLookupIter must be closed with a call to Close() when it is
+// no longer needed.
+type EncodedObjectLookupIter struct {
+ storage EncodedObjectStorer
series []plumbing.Hash
t plumbing.ObjectType
pos int
}
-// NewObjectLookupIter returns an object iterator given an object storage and
-// a slice of object hashes.
-func NewObjectLookupIter(
- storage ObjectStorer, t plumbing.ObjectType, series []plumbing.Hash) *ObjectLookupIter {
- return &ObjectLookupIter{
+// NewEncodedObjectLookupIter returns an object iterator given an object storage
+// and a slice of object hashes.
+func NewEncodedObjectLookupIter(
+ storage EncodedObjectStorer, t plumbing.ObjectType, series []plumbing.Hash) *EncodedObjectLookupIter {
+ return &EncodedObjectLookupIter{
storage: storage,
series: series,
t: t,
@@ -97,13 +100,13 @@ func NewObjectLookupIter(
// the end it will return io.EOF as an error. If the object can't be found in
// the object storage, it will return plumbing.ErrObjectNotFound as an error.
// If the object is retreieved successfully error will be nil.
-func (iter *ObjectLookupIter) Next() (plumbing.Object, error) {
+func (iter *EncodedObjectLookupIter) Next() (plumbing.EncodedObject, error) {
if iter.pos >= len(iter.series) {
return nil, io.EOF
}
hash := iter.series[iter.pos]
- obj, err := iter.storage.Object(iter.t, hash)
+ obj, err := iter.storage.EncodedObject(iter.t, hash)
if err == nil {
iter.pos++
}
@@ -114,28 +117,30 @@ func (iter *ObjectLookupIter) Next() (plumbing.Object, error) {
// ForEach call the cb function for each object contained on this iter until
// an error happends or the end of the iter is reached. If ErrStop is sent
// the iteration is stop but no error is returned. The iterator is closed.
-func (iter *ObjectLookupIter) ForEach(cb func(plumbing.Object) error) error {
+func (iter *EncodedObjectLookupIter) ForEach(cb func(plumbing.EncodedObject) error) error {
return ForEachIterator(iter, cb)
}
// Close releases any resources used by the iterator.
-func (iter *ObjectLookupIter) Close() {
+func (iter *EncodedObjectLookupIter) Close() {
iter.pos = len(iter.series)
}
-// ObjectSliceIter implements ObjectIter. It iterates over a series of objects
-// stored in a slice and yields each one in turn when Next() is called.
+// EncodedObjectSliceIter implements EncodedObjectIter. It iterates over a
+// series of objects stored in a slice and yields each one in turn when Next()
+// is called.
//
-// The ObjectSliceIter must be closed with a call to Close() when it is no
-// longer needed.
-type ObjectSliceIter struct {
- series []plumbing.Object
+// The EncodedObjectSliceIter must be closed with a call to Close() when it is
+// no longer needed.
+type EncodedObjectSliceIter struct {
+ series []plumbing.EncodedObject
pos int
}
-// NewObjectSliceIter returns an object iterator for the given slice of objects.
-func NewObjectSliceIter(series []plumbing.Object) *ObjectSliceIter {
- return &ObjectSliceIter{
+// NewEncodedObjectSliceIter returns an object iterator for the given slice of
+// objects.
+func NewEncodedObjectSliceIter(series []plumbing.EncodedObject) *EncodedObjectSliceIter {
+ return &EncodedObjectSliceIter{
series: series,
}
}
@@ -143,7 +148,7 @@ func NewObjectSliceIter(series []plumbing.Object) *ObjectSliceIter {
// Next returns the next object from the iterator. If the iterator has reached
// the end it will return io.EOF as an error. If the object is retreieved
// successfully error will be nil.
-func (iter *ObjectSliceIter) Next() (plumbing.Object, error) {
+func (iter *EncodedObjectSliceIter) Next() (plumbing.EncodedObject, error) {
if len(iter.series) == 0 {
return nil, io.EOF
}
@@ -157,32 +162,34 @@ func (iter *ObjectSliceIter) Next() (plumbing.Object, error) {
// ForEach call the cb function for each object contained on this iter until
// an error happends or the end of the iter is reached. If ErrStop is sent
// the iteration is stop but no error is returned. The iterator is closed.
-func (iter *ObjectSliceIter) ForEach(cb func(plumbing.Object) error) error {
+func (iter *EncodedObjectSliceIter) ForEach(cb func(plumbing.EncodedObject) error) error {
return ForEachIterator(iter, cb)
}
// Close releases any resources used by the iterator.
-func (iter *ObjectSliceIter) Close() {
- iter.series = []plumbing.Object{}
+func (iter *EncodedObjectSliceIter) Close() {
+ iter.series = []plumbing.EncodedObject{}
}
-// MultiObjectIter implements ObjectIter. It iterates over several ObjectIter,
+// MultiEncodedObjectIter implements EncodedObjectIter. It iterates over several
+// EncodedObjectIter,
//
// The MultiObjectIter must be closed with a call to Close() when it is no
// longer needed.
-type MultiObjectIter struct {
- iters []ObjectIter
+type MultiEncodedObjectIter struct {
+ iters []EncodedObjectIter
pos int
}
-// NewMultiObjectIter returns an object iterator for the given slice of objects.
-func NewMultiObjectIter(iters []ObjectIter) ObjectIter {
- return &MultiObjectIter{iters: iters}
+// NewMultiEncodedObjectIter returns an object iterator for the given slice of
+// objects.
+func NewMultiEncodedObjectIter(iters []EncodedObjectIter) EncodedObjectIter {
+ return &MultiEncodedObjectIter{iters: iters}
}
// Next returns the next object from the iterator, if one iterator reach io.EOF
// is removed and the next one is used.
-func (iter *MultiObjectIter) Next() (plumbing.Object, error) {
+func (iter *MultiEncodedObjectIter) Next() (plumbing.EncodedObject, error) {
if len(iter.iters) == 0 {
return nil, io.EOF
}
@@ -200,25 +207,25 @@ func (iter *MultiObjectIter) Next() (plumbing.Object, error) {
// ForEach call the cb function for each object contained on this iter until
// an error happends or the end of the iter is reached. If ErrStop is sent
// the iteration is stop but no error is returned. The iterator is closed.
-func (iter *MultiObjectIter) ForEach(cb func(plumbing.Object) error) error {
+func (iter *MultiEncodedObjectIter) ForEach(cb func(plumbing.EncodedObject) error) error {
return ForEachIterator(iter, cb)
}
// Close releases any resources used by the iterator.
-func (iter *MultiObjectIter) Close() {
+func (iter *MultiEncodedObjectIter) Close() {
for _, i := range iter.iters {
i.Close()
}
}
type bareIterator interface {
- Next() (plumbing.Object, error)
+ Next() (plumbing.EncodedObject, error)
Close()
}
// ForEachIterator is a helper function to build iterators without need to
// rewrite the same ForEach function each time.
-func ForEachIterator(iter bareIterator, cb func(plumbing.Object) error) error {
+func ForEachIterator(iter bareIterator, cb func(plumbing.EncodedObject) error) error {
defer iter.Close()
for {
obj, err := iter.Next()