diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-02-17 00:24:31 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-02-17 11:46:00 +0100 |
commit | a964e32d92c53a47ce7c46d589a18c62133b8c50 (patch) | |
tree | 4c094cd6afb95cb2832f866b9411276418435819 /storage/memory/storage.go | |
parent | 1ac00554c3b5f88d2ddc2e28e7cfcdcad9d9f4bb (diff) | |
download | go-git-a964e32d92c53a47ce7c46d589a18c62133b8c50.tar.gz |
storages: memory object
Diffstat (limited to 'storage/memory/storage.go')
-rw-r--r-- | storage/memory/storage.go | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/storage/memory/storage.go b/storage/memory/storage.go new file mode 100644 index 0000000..4c106cc --- /dev/null +++ b/storage/memory/storage.go @@ -0,0 +1,83 @@ +package memory + +import ( + "fmt" + + "gopkg.in/src-d/go-git.v2/core" +) + +var ErrUnsupportedObjectType = fmt.Errorf("unsupported object type") + +// ObjectStorage is the implementation of core.ObjectStorage for memory.Object +type ObjectStorage struct { + Objects map[core.Hash]core.Object + Commits map[core.Hash]core.Object + Trees map[core.Hash]core.Object + Blobs map[core.Hash]core.Object +} + +// NewObjectStorage returns a new empty ObjectStorage +func NewObjectStorage() *ObjectStorage { + return &ObjectStorage{ + Objects: make(map[core.Hash]core.Object, 0), + Commits: make(map[core.Hash]core.Object, 0), + Trees: make(map[core.Hash]core.Object, 0), + Blobs: make(map[core.Hash]core.Object, 0), + } +} + +// New returns a new empty memory.Object +func (o *ObjectStorage) New() (core.Object, error) { + return &Object{}, nil +} + +// Set stores an object, the object should be properly filled before set it. +func (o *ObjectStorage) Set(obj core.Object) (core.Hash, error) { + h := obj.Hash() + o.Objects[h] = obj + + switch obj.Type() { + case core.CommitObject: + o.Commits[h] = o.Objects[h] + case core.TreeObject: + o.Trees[h] = o.Objects[h] + case core.BlobObject: + o.Blobs[h] = o.Objects[h] + default: + return h, ErrUnsupportedObjectType + } + + return h, nil +} + +// Get returns a object with the given hash +func (o *ObjectStorage) Get(h core.Hash) (core.Object, error) { + obj, ok := o.Objects[h] + if !ok { + return nil, core.ObjectNotFoundErr + } + + return obj, nil +} + +// Iter returns a core.ObjectIter for the given core.ObjectTybe +func (o *ObjectStorage) Iter(t core.ObjectType) core.ObjectIter { + var series []core.Object + switch t { + case core.CommitObject: + series = flattenObjectMap(o.Commits) + case core.TreeObject: + series = flattenObjectMap(o.Trees) + case core.BlobObject: + series = flattenObjectMap(o.Blobs) + } + return core.NewObjectSliceIter(series) +} + +func flattenObjectMap(m map[core.Hash]core.Object) []core.Object { + objects := make([]core.Object, 0, len(m)) + for _, obj := range m { + objects = append(objects, obj) + } + return objects +} |