aboutsummaryrefslogtreecommitdiffstats
path: root/storage/memory
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2016-09-05 07:59:20 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2016-09-05 07:59:20 +0200
commit65427f64599e9383ab926aed75fc02f2db32022d (patch)
tree5c81c00bc9dcfb54e0c6d0038ab2cbd8cddde389 /storage/memory
parent440cfd96cf88ffc9d04bbd32ec8a3b1afb42144c (diff)
downloadgo-git-65427f64599e9383ab926aed75fc02f2db32022d.tar.gz
core: ObjectStorage.Begin and TxObjectStorage
Diffstat (limited to 'storage/memory')
-rw-r--r--storage/memory/storage.go35
-rw-r--r--storage/memory/storage_test.go8
2 files changed, 39 insertions, 4 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.
diff --git a/storage/memory/storage_test.go b/storage/memory/storage_test.go
index 61a810e..2c90738 100644
--- a/storage/memory/storage_test.go
+++ b/storage/memory/storage_test.go
@@ -6,19 +6,19 @@ import (
. "gopkg.in/check.v1"
"gopkg.in/src-d/go-git.v4/core"
- . "gopkg.in/src-d/go-git.v4/storage/test"
+ "gopkg.in/src-d/go-git.v4/storage/test"
)
func Test(t *testing.T) { TestingT(t) }
type StorageSuite struct {
- BaseStorageSuite
+ test.BaseStorageSuite
}
var _ = Suite(&StorageSuite{})
-func (s *StorageSuite) SetUpSuite(c *C) {
- s.BaseStorageSuite = NewBaseStorageSuite(NewStorage().ObjectStorage())
+func (s *StorageSuite) SetUpTest(c *C) {
+ s.BaseStorageSuite = test.NewBaseStorageSuite(NewStorage().ObjectStorage())
}
func (s *StorageSuite) TestStorageObjectStorage(c *C) {