diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-09-05 07:59:20 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-09-05 07:59:20 +0200 |
commit | 65427f64599e9383ab926aed75fc02f2db32022d (patch) | |
tree | 5c81c00bc9dcfb54e0c6d0038ab2cbd8cddde389 /storage/memory/storage.go | |
parent | 440cfd96cf88ffc9d04bbd32ec8a3b1afb42144c (diff) | |
download | go-git-65427f64599e9383ab926aed75fc02f2db32022d.tar.gz |
core: ObjectStorage.Begin and TxObjectStorage
Diffstat (limited to 'storage/memory/storage.go')
-rw-r--r-- | storage/memory/storage.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/storage/memory/storage.go b/storage/memory/storage.go index c8a152d..01d5fa5 100644 --- a/storage/memory/storage.go +++ b/storage/memory/storage.go @@ -166,6 +166,41 @@ func flattenObjectMap(m map[core.Hash]core.Object) []core.Object { return objects } +func (o *ObjectStorage) Begin() core.TxObjectStorage { + return &TxObjectStorage{ + Storage: o, + Objects: make(map[core.Hash]core.Object, 0), + } +} + +type TxObjectStorage struct { + Storage *ObjectStorage + Objects map[core.Hash]core.Object +} + +func (tx *TxObjectStorage) Set(obj core.Object) (core.Hash, error) { + h := obj.Hash() + tx.Objects[h] = obj + + return h, nil +} + +func (tx *TxObjectStorage) Commit() error { + for h, obj := range tx.Objects { + delete(tx.Objects, h) + if _, err := tx.Storage.Set(obj); err != nil { + return err + } + } + + return nil +} + +func (tx *TxObjectStorage) Rollback() error { + tx.Objects = make(map[core.Hash]core.Object, 0) + return nil +} + type ReferenceStorage map[core.ReferenceName]*core.Reference // Set stores a reference. |