| 1 |
package storer |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"io" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/go-git/go-git/v5/plumbing" |
| 9 |
) |
| 10 |
|
| 11 |
var ( |
| 12 |
//ErrStop is used to stop a ForEach function in an Iter |
| 13 |
ErrStop = errors.New("stop iter") |
| 14 |
) |
| 15 |
|
| 16 |
// EncodedObjectStorer generic storage of objects |
| 17 |
type EncodedObjectStorer interface { |
| 18 |
// NewEncodedObject returns a new plumbing.EncodedObject, the real type |
| 19 |
// of the object can be a custom implementation or the default one, |
| 20 |
// plumbing.MemoryObject. |
| 21 |
NewEncodedObject() plumbing.EncodedObject |
| 22 |
// SetEncodedObject saves an object into the storage, the object should |
| 23 |
// be create with the NewEncodedObject, method, and file if the type is |
| 24 |
// not supported. |
| 25 |
SetEncodedObject(plumbing.EncodedObject) (plumbing.Hash, error) |
| 26 |
// EncodedObject gets an object by hash with the given |
| 27 |
// plumbing.ObjectType. Implementors should return |
| 28 |
// (nil, plumbing.ErrObjectNotFound) if an object doesn't exist with |
| 29 |
// both the given hash and object type. |
| 30 |
// |
| 31 |
// Valid plumbing.ObjectType values are CommitObject, BlobObject, TagObject, |
| 32 |
// TreeObject and AnyObject. If plumbing.AnyObject is given, the object must |
| 33 |
// be looked up regardless of its type. |
| 34 |
EncodedObject(plumbing.ObjectType, plumbing.Hash) (plumbing.EncodedObject, error) |
| 35 |
// IterObjects returns a custom EncodedObjectStorer over all the object |
| 36 |
// on the storage. |
| 37 |
// |
| 38 |
// Valid plumbing.ObjectType values are CommitObject, BlobObject, TagObject, |
| 39 |
IterEncodedObjects(plumbing.ObjectType) (EncodedObjectIter, error) |
| 40 |
// HasEncodedObject returns ErrObjNotFound if the object doesn't |
| 41 |
// exist. If the object does exist, it returns nil. |
| 42 |
HasEncodedObject(plumbing.Hash) error |
| 43 |
// EncodedObjectSize returns the plaintext size of the encoded object. |
| 44 |
EncodedObjectSize(plumbing.Hash) (int64, error) |
| 45 |
AddAlternate(remote string) error |
| 46 |
} |
| 47 |
|
| 48 |
// DeltaObjectStorer is an EncodedObjectStorer that can return delta |
| 49 |
// objects. |
| 50 |
type DeltaObjectStorer interface { |
| 51 |
// DeltaObject is the same as EncodedObject but without resolving deltas. |
| 52 |
// Deltas will be returned as plumbing.DeltaObject instances. |
| 53 |
DeltaObject(plumbing.ObjectType, plumbing.Hash) (plumbing.EncodedObject, error) |
| 54 |
} |
| 55 |
|
| 56 |
// Transactioner is a optional method for ObjectStorer, it enables transactional read and write |
| 57 |
// operations. |
| 58 |
type Transactioner interface { |
| 59 |
// Begin starts a transaction. |
| 60 |
Begin() Transaction |
| 61 |
} |
| 62 |
|
| 63 |
// LooseObjectStorer is an optional interface for managing "loose" |
| 64 |
// objects, i.e. those not in packfiles. |
| 65 |
type LooseObjectStorer interface { |
| 66 |
// ForEachObjectHash iterates over all the (loose) object hashes |
| 67 |
// in the repository without necessarily having to read those objects. |
| 68 |
// Objects only inside pack files may be omitted. |
| 69 |
// If ErrStop is sent the iteration is stop but no error is returned. |
| 70 |
ForEachObjectHash(func(plumbing.Hash) error) error |
| 71 |
// LooseObjectTime looks up the (m)time associated with the |
| 72 |
// loose object (that is not in a pack file). Some |
| 73 |
// implementations (e.g. without loose objects) |
| 74 |
// always return an error. |
| 75 |
LooseObjectTime(plumbing.Hash) (time.Time, error) |
| 76 |
// DeleteLooseObject deletes a loose object if it exists. |
| 77 |
DeleteLooseObject(plumbing.Hash) error |
| 78 |
} |
| 79 |
|
| 80 |
// PackedObjectStorer is an optional interface for managing objects in |
| 81 |
// packfiles. |
| 82 |
type PackedObjectStorer interface { |
| 83 |
// ObjectPacks returns hashes of object packs if the underlying |
| 84 |
// implementation has pack files. |
| 85 |
ObjectPacks() ([]plumbing.Hash, error) |
| 86 |
// DeleteOldObjectPackAndIndex deletes an object pack and the corresponding index file if they exist. |
| 87 |
// Deletion is only performed if the pack is older than the supplied time (or the time is zero). |
| 88 |
DeleteOldObjectPackAndIndex(plumbing.Hash, time.Time) error |
| 89 |
} |
| 90 |
|
| 91 |
// PackfileWriter is an optional method for ObjectStorer, it enables directly writing |
| 92 |
// a packfile to storage. |
| 93 |
type PackfileWriter interface { |
| 94 |
// PackfileWriter returns a writer for writing a packfile to the storage |
| 95 |
// |
| 96 |
// If the Storer not implements PackfileWriter the objects should be written |
| 97 |
// using the Set method. |
| 98 |
PackfileWriter() (io.WriteCloser, error) |
| 99 |
} |
| 100 |
|
| 101 |
// EncodedObjectIter is a generic closable interface for iterating over objects. |
| 102 |
type EncodedObjectIter interface { |
| 103 |
Next() (plumbing.EncodedObject, error) |
| 104 |
ForEach(func(plumbing.EncodedObject) error) error |
| 105 |
Close() |
| 106 |
} |
| 107 |
|
| 108 |
// Transaction is an in-progress storage transaction. A transaction must end |
| 109 |
// with a call to Commit or Rollback. |
| 110 |
type Transaction interface { |
| 111 |
SetEncodedObject(plumbing.EncodedObject) (plumbing.Hash, error) |
| 112 |
EncodedObject(plumbing.ObjectType, plumbing.Hash) (plumbing.EncodedObject, error) |
| 113 |
Commit() error |
| 114 |
Rollback() error |
| 115 |
} |
| 116 |
|
| 117 |
// EncodedObjectLookupIter implements EncodedObjectIter. It iterates over a |
| 118 |
// series of object hashes and yields their associated objects by retrieving |
| 119 |
// each one from object storage. The retrievals are lazy and only occur when the |
| 120 |
// iterator moves forward with a call to Next(). |
| 121 |
// |
| 122 |
// The EncodedObjectLookupIter must be closed with a call to Close() when it is |
| 123 |
// no longer needed. |
| 124 |
type EncodedObjectLookupIter struct { |
| 125 |
storage EncodedObjectStorer |
| 126 |
series []plumbing.Hash |
| 127 |
t plumbing.ObjectType |
| 128 |
pos int |
| 129 |
} |
| 130 |
|
| 131 |
// NewEncodedObjectLookupIter returns an object iterator given an object storage |
| 132 |
// and a slice of object hashes. |
| 133 |
func NewEncodedObjectLookupIter( |
| 134 |
storage EncodedObjectStorer, t plumbing.ObjectType, series []plumbing.Hash) *EncodedObjectLookupIter { |
| 135 |
return &EncodedObjectLookupIter{ |
| 136 |
storage: storage, |
| 137 |
series: series, |
| 138 |
t: t, |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// Next returns the next object from the iterator. If the iterator has reached |
| 143 |
// the end it will return io.EOF as an error. If the object can't be found in |
| 144 |
// the object storage, it will return plumbing.ErrObjectNotFound as an error. |
| 145 |
// If the object is retrieved successfully error will be nil. |
| 146 |
func (iter *EncodedObjectLookupIter) Next() (plumbing.EncodedObject, error) { |
| 147 |
if iter.pos >= len(iter.series) { |
| 148 |
return nil, io.EOF |
| 149 |
} |
| 150 |
|
| 151 |
hash := iter.series[iter.pos] |
| 152 |
obj, err := iter.storage.EncodedObject(iter.t, hash) |
| 153 |
if err == nil { |
| 154 |
iter.pos++ |
| 155 |
} |
| 156 |
|
| 157 |
return obj, err |
| 158 |
} |
| 159 |
|
| 160 |
// ForEach call the cb function for each object contained on this iter until |
| 161 |
// an error happens or the end of the iter is reached. If ErrStop is sent |
| 162 |
// the iteration is stop but no error is returned. The iterator is closed. |
| 163 |
func (iter *EncodedObjectLookupIter) ForEach(cb func(plumbing.EncodedObject) error) error { |
| 164 |
return ForEachIterator(iter, cb) |
| 165 |
} |
| 166 |
|
| 167 |
// Close releases any resources used by the iterator. |
| 168 |
func (iter *EncodedObjectLookupIter) Close() { |
| 169 |
iter.pos = len(iter.series) |
| 170 |
} |
| 171 |
|
| 172 |
// EncodedObjectSliceIter implements EncodedObjectIter. It iterates over a |
| 173 |
// series of objects stored in a slice and yields each one in turn when Next() |
| 174 |
// is called. |
| 175 |
// |
| 176 |
// The EncodedObjectSliceIter must be closed with a call to Close() when it is |
| 177 |
// no longer needed. |
| 178 |
type EncodedObjectSliceIter struct { |
| 179 |
series []plumbing.EncodedObject |
| 180 |
} |
| 181 |
|
| 182 |
// NewEncodedObjectSliceIter returns an object iterator for the given slice of |
| 183 |
// objects. |
| 184 |
func NewEncodedObjectSliceIter(series []plumbing.EncodedObject) *EncodedObjectSliceIter { |
| 185 |
return &EncodedObjectSliceIter{ |
| 186 |
series: series, |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
// Next returns the next object from the iterator. If the iterator has reached |
| 191 |
// the end it will return io.EOF as an error. If the object is retrieved |
| 192 |
// successfully error will be nil. |
| 193 |
func (iter *EncodedObjectSliceIter) Next() (plumbing.EncodedObject, error) { |
| 194 |
if len(iter.series) == 0 { |
| 195 |
return nil, io.EOF |
| 196 |
} |
| 197 |
|
| 198 |
obj := iter.series[0] |
| 199 |
iter.series = iter.series[1:] |
| 200 |
|
| 201 |
return obj, nil |
| 202 |
} |
| 203 |
|
| 204 |
// ForEach call the cb function for each object contained on this iter until |
| 205 |
// an error happens or the end of the iter is reached. If ErrStop is sent |
| 206 |
// the iteration is stop but no error is returned. The iterator is closed. |
| 207 |
func (iter *EncodedObjectSliceIter) ForEach(cb func(plumbing.EncodedObject) error) error { |
| 208 |
return ForEachIterator(iter, cb) |
| 209 |
} |
| 210 |
|
| 211 |
// Close releases any resources used by the iterator. |
| 212 |
func (iter *EncodedObjectSliceIter) Close() { |
| 213 |
iter.series = []plumbing.EncodedObject{} |
| 214 |
} |
| 215 |
|
| 216 |
// MultiEncodedObjectIter implements EncodedObjectIter. It iterates over several |
| 217 |
// EncodedObjectIter, |
| 218 |
// |
| 219 |
// The MultiObjectIter must be closed with a call to Close() when it is no |
| 220 |
// longer needed. |
| 221 |
type MultiEncodedObjectIter struct { |
| 222 |
iters []EncodedObjectIter |
| 223 |
} |
| 224 |
|
| 225 |
// NewMultiEncodedObjectIter returns an object iterator for the given slice of |
| 226 |
// EncodedObjectIters. |
| 227 |
func NewMultiEncodedObjectIter(iters []EncodedObjectIter) EncodedObjectIter { |
| 228 |
return &MultiEncodedObjectIter{iters: iters} |
| 229 |
} |
| 230 |
|
| 231 |
// Next returns the next object from the iterator, if one iterator reach io.EOF |
| 232 |
// is removed and the next one is used. |
| 233 |
func (iter *MultiEncodedObjectIter) Next() (plumbing.EncodedObject, error) { |
| 234 |
if len(iter.iters) == 0 { |
| 235 |
return nil, io.EOF |
| 236 |
} |
| 237 |
|
| 238 |
obj, err := iter.iters[0].Next() |
| 239 |
if err == io.EOF { |
| 240 |
iter.iters[0].Close() |
| 241 |
iter.iters = iter.iters[1:] |
| 242 |
return iter.Next() |
| 243 |
} |
| 244 |
|
| 245 |
return obj, err |
| 246 |
} |
| 247 |
|
| 248 |
// ForEach call the cb function for each object contained on this iter until |
| 249 |
// an error happens or the end of the iter is reached. If ErrStop is sent |
| 250 |
// the iteration is stop but no error is returned. The iterator is closed. |
| 251 |
func (iter *MultiEncodedObjectIter) ForEach(cb func(plumbing.EncodedObject) error) error { |
| 252 |
return ForEachIterator(iter, cb) |
| 253 |
} |
| 254 |
|
| 255 |
// Close releases any resources used by the iterator. |
| 256 |
func (iter *MultiEncodedObjectIter) Close() { |
| 257 |
for _, i := range iter.iters { |
| 258 |
i.Close() |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
type bareIterator interface { |
| 263 |
Next() (plumbing.EncodedObject, error) |
| 264 |
Close() |
| 265 |
} |
| 266 |
|
| 267 |
// ForEachIterator is a helper function to build iterators without need to |
| 268 |
// rewrite the same ForEach function each time. |
| 269 |
func ForEachIterator(iter bareIterator, cb func(plumbing.EncodedObject) error) error { |
| 270 |
defer iter.Close() |
| 271 |
for { |
| 272 |
obj, err := iter.Next() |
| 273 |
if err != nil { |
| 274 |
if err == io.EOF { |
| 275 |
return nil |
| 276 |
} |
| 277 |
|
| 278 |
return err |
| 279 |
} |
| 280 |
|
| 281 |
if err := cb(obj); err != nil { |
| 282 |
if err == ErrStop { |
| 283 |
return nil |
| 284 |
} |
| 285 |
|
| 286 |
return err |
| 287 |
} |
| 288 |
} |
| 289 |
} |