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

transactional: implement storer.PackfileWriter #1093

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions storage/transactional/storage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package transactional

import (
"io"

"gopkg.in/src-d/go-git.v4/plumbing/storer"
"gopkg.in/src-d/go-git.v4/storage"
)

Expand All @@ -10,7 +13,13 @@ import (
//
// The API and functionality of this package are considered EXPERIMENTAL and is
// not considered stable nor production ready.
type Storage struct {
type Storage interface {
storage.Storer
Commit() error
}

// basic implements the Storage interface.
type basic struct {
s, temporal storage.Storer

*ObjectStorage
Expand All @@ -20,11 +29,18 @@ type Storage struct {
*ConfigStorage
}

// packageWriter implements storer.PackfileWriter interface over
// a Storage with a temporal storer that supports it.
type packageWriter struct {
*basic
pw storer.PackfileWriter
}

// NewStorage returns a new Storage based on two repositories, base is the base
// repository where the read operations are read and temportal is were all
// repository where the read operations are read and temporal is were all
// the write operations are stored.
func NewStorage(base, temporal storage.Storer) *Storage {
return &Storage{
func NewStorage(base, temporal storage.Storer) Storage {
st := &basic{
s: base,
temporal: temporal,

Expand All @@ -34,10 +50,20 @@ func NewStorage(base, temporal storage.Storer) *Storage {
ShallowStorage: NewShallowStorage(base, temporal),
ConfigStorage: NewConfigStorage(base, temporal),
}

pw, ok := temporal.(storer.PackfileWriter)
if ok {
return &packageWriter{
basic: st,
pw: pw,
}
}

return st
}

// Module it honors the storage.ModuleStorer interface.
func (s *Storage) Module(name string) (storage.Storer, error) {
func (s *basic) Module(name string) (storage.Storer, error) {
base, err := s.s.Module(name)
if err != nil {
return nil, err
Expand All @@ -52,7 +78,7 @@ func (s *Storage) Module(name string) (storage.Storer, error) {
}

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

return nil
}

// PackfileWriter honors storage.PackfileWriter.
func (s *packageWriter) PackfileWriter() (io.WriteCloser, error) {
return s.pw.PackfileWriter()
}
33 changes: 30 additions & 3 deletions storage/transactional/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import (
"testing"

. "gopkg.in/check.v1"
"gopkg.in/src-d/go-billy.v4/memfs"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/cache"
"gopkg.in/src-d/go-git.v4/plumbing/storer"
"gopkg.in/src-d/go-git.v4/storage"
"gopkg.in/src-d/go-git.v4/storage/filesystem"
"gopkg.in/src-d/go-git.v4/storage/memory"
"gopkg.in/src-d/go-git.v4/storage/test"
)
Expand All @@ -13,21 +18,33 @@ func Test(t *testing.T) { TestingT(t) }

type StorageSuite struct {
test.BaseStorageSuite
temporal func() storage.Storer
}

var _ = Suite(&StorageSuite{})
var _ = Suite(&StorageSuite{
temporal: func() storage.Storer {
return memory.NewStorage()
},
})

var _ = Suite(&StorageSuite{
temporal: func() storage.Storer {
fs := memfs.New()
return filesystem.NewStorage(fs, cache.NewObjectLRUDefault())
},
})

func (s *StorageSuite) SetUpTest(c *C) {
base := memory.NewStorage()
temporal := memory.NewStorage()
temporal := s.temporal()

s.BaseStorageSuite = test.NewBaseStorageSuite(NewStorage(base, temporal))
s.BaseStorageSuite.SetUpTest(c)
}

func (s *StorageSuite) TestCommit(c *C) {
base := memory.NewStorage()
temporal := memory.NewStorage()
temporal := s.temporal()
st := NewStorage(base, temporal)

commit := base.NewEncodedObject()
Expand All @@ -50,3 +67,13 @@ func (s *StorageSuite) TestCommit(c *C) {
c.Assert(err, IsNil)
c.Assert(obj.Hash(), Equals, commit.Hash())
}

func (s *StorageSuite) TestTransactionalPackfileWriter(c *C) {
base := memory.NewStorage()
temporal := s.temporal()
st := NewStorage(base, temporal)

_, tmpOK := temporal.(storer.PackfileWriter)
_, ok := st.(storer.PackfileWriter)
c.Assert(ok, Equals, tmpOK)
}