From e1f7b169aaa99d07fe8ad8e71a0cba2322e7d35f Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Fri, 12 Aug 2016 12:49:46 +0200 Subject: small improvements --- core/memory.go | 8 ++----- core/object.go | 14 ------------- core/reference.go | 62 ++++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 47 insertions(+), 37 deletions(-) (limited to 'core') diff --git a/core/memory.go b/core/memory.go index 6477f74..e3063d6 100644 --- a/core/memory.go +++ b/core/memory.go @@ -13,11 +13,6 @@ type MemoryObject struct { sz int64 } -// NewMemoryObject creates a new MemoryObject -func NewMemoryObject(t ObjectType, len int64, cont []byte) *MemoryObject { - return &MemoryObject{t: t, sz: len, cont: cont} -} - // Hash return the object Hash, the hash is calculated on-the-fly the first // time is called, the subsequent calls the same Hash is returned even if the // type or the content has changed. The Hash is only generated if the size of @@ -39,7 +34,8 @@ func (o *MemoryObject) SetType(t ObjectType) { o.t = t } // Size return the size of the object func (o *MemoryObject) Size() int64 { return o.sz } -// SetSize set the object size, the given size should be written afterwards +// SetSize set the object size, a content of the given size should be written +// afterwards func (o *MemoryObject) SetSize(s int64) { o.sz = s } // Content returns the contents of the object diff --git a/core/object.go b/core/object.go index e5f9a5e..d73ab0f 100644 --- a/core/object.go +++ b/core/object.go @@ -39,14 +39,6 @@ type Object interface { Writer() (ObjectWriter, error) } -// ObjectStorage generic storage of objects -type ObjectStorage interface { - NewObject() Object - Set(Object) (Hash, error) - Get(Hash) (Object, error) - Iter(ObjectType) (ObjectIter, error) -} - // ObjectType internal object type's type ObjectType int8 @@ -110,12 +102,6 @@ func ParseObjectType(value string) (typ ObjectType, err error) { return } -// ObjectIter is a generic closable interface for iterating over objects. -type ObjectIter interface { - Next() (Object, error) - Close() -} - // 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 diff --git a/core/reference.go b/core/reference.go index 9114b58..ec00787 100644 --- a/core/reference.go +++ b/core/reference.go @@ -1,6 +1,10 @@ package core -import "strings" +import ( + "errors" + "io" + "strings" +) const ( refPrefix = "refs/" @@ -11,6 +15,10 @@ const ( symrefPrefix = "ref: " ) +var ( + ErrReferenceNotFound = errors.New("reference not found") +) + // ReferenceType reference type's type ReferenceType int8 @@ -39,17 +47,14 @@ type Reference struct { // the resulting reference can be a SymbolicReference or a HashReference base // on the target provided func NewReferenceFromStrings(name, target string) *Reference { - r := &Reference{n: ReferenceName(name)} + n := ReferenceName(name) if strings.HasPrefix(target, symrefPrefix) { - r.t = SymbolicReference - r.target = ReferenceName(target[len(symrefPrefix):]) - return r + target := ReferenceName(target[len(symrefPrefix):]) + return NewSymbolicReference(n, target) } - r.t = HashReference - r.h = NewHash(target) - return r + return NewHashReference(n, NewHash(target)) } // NewSymbolicReference creates a new SymbolicReference reference @@ -110,15 +115,38 @@ func (r *Reference) IsTag() bool { return strings.HasPrefix(string(r.n), refTagPrefix) } -// ReferenceStorage generic storage of references -type ReferenceStorage interface { - Set(Reference) error - Get(ReferenceName) (Reference, error) - Iter(ObjectType) (ReferenceIter, error) +// ReferenceSliceIter implements ReferenceIter. It iterates over a series of +// references stored in a slice and yields each one in turn when Next() is +// called. +// +// The ReferenceSliceIter must be closed with a call to Close() when it is no +// longer needed. +type ReferenceSliceIter struct { + series []*Reference + pos int +} + +// NewReferenceSliceIter returns a reference iterator for the given slice of +// objects. +func NewReferenceSliceIter(series []*Reference) *ReferenceSliceIter { + return &ReferenceSliceIter{ + series: series, + } +} + +// Next returns the next reference from the iterator. If the iterator has +// reached the end it will return io.EOF as an error. +func (iter *ReferenceSliceIter) Next() (*Reference, error) { + if iter.pos >= len(iter.series) { + return nil, io.EOF + } + + obj := iter.series[iter.pos] + iter.pos++ + return obj, nil } -// ReferenceIter is a generic closable interface for iterating over references -type ReferenceIter interface { - Next() (Reference, error) - Close() +// Close releases any resources used by the iterator. +func (iter *ReferenceSliceIter) Close() { + iter.pos = len(iter.series) } -- cgit