Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit b40af0b

Browse files
committed
transactional: implement storer.PackfileWriter
Signed-off-by: Javi Fontan <[email protected]>
1 parent 948b0c9 commit b40af0b

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

storage/transactional/storage.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package transactional
22

33
import (
4+
"io"
5+
6+
"gopkg.in/src-d/go-git.v4/plumbing/storer"
47
"gopkg.in/src-d/go-git.v4/storage"
58
)
69

@@ -10,7 +13,13 @@ import (
1013
//
1114
// The API and functionality of this package are considered EXPERIMENTAL and is
1215
// not considered stable nor production ready.
13-
type Storage struct {
16+
type Storage interface {
17+
storage.Storer
18+
Commit() error
19+
}
20+
21+
// basic implements the Storage interface.
22+
type basic struct {
1423
s, temporal storage.Storer
1524

1625
*ObjectStorage
@@ -20,11 +29,18 @@ type Storage struct {
2029
*ConfigStorage
2130
}
2231

32+
// packageWriter implements storer.PackfileWriter interface over
33+
// a Storage with a temporal storer that supports it.
34+
type packageWriter struct {
35+
*basic
36+
pw storer.PackfileWriter
37+
}
38+
2339
// NewStorage returns a new Storage based on two repositories, base is the base
24-
// repository where the read operations are read and temportal is were all
40+
// repository where the read operations are read and temporal is were all
2541
// the write operations are stored.
26-
func NewStorage(base, temporal storage.Storer) *Storage {
27-
return &Storage{
42+
func NewStorage(base, temporal storage.Storer) Storage {
43+
st := &basic{
2844
s: base,
2945
temporal: temporal,
3046

@@ -34,10 +50,20 @@ func NewStorage(base, temporal storage.Storer) *Storage {
3450
ShallowStorage: NewShallowStorage(base, temporal),
3551
ConfigStorage: NewConfigStorage(base, temporal),
3652
}
53+
54+
pw, ok := temporal.(storer.PackfileWriter)
55+
if ok {
56+
return &packageWriter{
57+
basic: st,
58+
pw: pw,
59+
}
60+
}
61+
62+
return st
3763
}
3864

3965
// Module it honors the storage.ModuleStorer interface.
40-
func (s *Storage) Module(name string) (storage.Storer, error) {
66+
func (s *basic) Module(name string) (storage.Storer, error) {
4167
base, err := s.s.Module(name)
4268
if err != nil {
4369
return nil, err
@@ -52,7 +78,7 @@ func (s *Storage) Module(name string) (storage.Storer, error) {
5278
}
5379

5480
// Commit it copies the content of the temporal storage into the base storage.
55-
func (s *Storage) Commit() error {
81+
func (s *basic) Commit() error {
5682
for _, c := range []interface{ Commit() error }{
5783
s.ObjectStorage,
5884
s.ReferenceStorage,
@@ -67,3 +93,8 @@ func (s *Storage) Commit() error {
6793

6894
return nil
6995
}
96+
97+
// PackfileWriter honors storage.PackfileWriter.
98+
func (s *packageWriter) PackfileWriter() (io.WriteCloser, error) {
99+
return s.pw.PackfileWriter()
100+
}

storage/transactional/storage_test.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import (
44
"testing"
55

66
. "gopkg.in/check.v1"
7+
"gopkg.in/src-d/go-billy.v4/memfs"
78
"gopkg.in/src-d/go-git.v4/plumbing"
9+
"gopkg.in/src-d/go-git.v4/plumbing/cache"
10+
"gopkg.in/src-d/go-git.v4/plumbing/storer"
11+
"gopkg.in/src-d/go-git.v4/storage"
12+
"gopkg.in/src-d/go-git.v4/storage/filesystem"
813
"gopkg.in/src-d/go-git.v4/storage/memory"
914
"gopkg.in/src-d/go-git.v4/storage/test"
1015
)
@@ -13,21 +18,33 @@ func Test(t *testing.T) { TestingT(t) }
1318

1419
type StorageSuite struct {
1520
test.BaseStorageSuite
21+
temporal func() storage.Storer
1622
}
1723

18-
var _ = Suite(&StorageSuite{})
24+
var _ = Suite(&StorageSuite{
25+
temporal: func() storage.Storer {
26+
return memory.NewStorage()
27+
},
28+
})
29+
30+
var _ = Suite(&StorageSuite{
31+
temporal: func() storage.Storer {
32+
fs := memfs.New()
33+
return filesystem.NewStorage(fs, cache.NewObjectLRUDefault())
34+
},
35+
})
1936

2037
func (s *StorageSuite) SetUpTest(c *C) {
2138
base := memory.NewStorage()
22-
temporal := memory.NewStorage()
39+
temporal := s.temporal()
2340

2441
s.BaseStorageSuite = test.NewBaseStorageSuite(NewStorage(base, temporal))
2542
s.BaseStorageSuite.SetUpTest(c)
2643
}
2744

2845
func (s *StorageSuite) TestCommit(c *C) {
2946
base := memory.NewStorage()
30-
temporal := memory.NewStorage()
47+
temporal := s.temporal()
3148
st := NewStorage(base, temporal)
3249

3350
commit := base.NewEncodedObject()
@@ -50,3 +67,13 @@ func (s *StorageSuite) TestCommit(c *C) {
5067
c.Assert(err, IsNil)
5168
c.Assert(obj.Hash(), Equals, commit.Hash())
5269
}
70+
71+
func (s *StorageSuite) TestTransactionalPackfileWriter(c *C) {
72+
base := memory.NewStorage()
73+
temporal := s.temporal()
74+
st := NewStorage(base, temporal)
75+
76+
_, tmpOK := temporal.(storer.PackfileWriter)
77+
_, ok := st.(storer.PackfileWriter)
78+
c.Assert(ok, Equals, tmpOK)
79+
}

0 commit comments

Comments
 (0)