aboutsummaryrefslogtreecommitdiffstats
path: root/storage/transactional/storage.go
blob: b81b1043eef631091012ee8f0aaa06b7cd6612b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package transactional

import (
	"io"

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

// Storage is a transactional implementation of git.Storer, it demux the write
// and read operation of two separate storers, allowing to merge content calling
// Storage.Commit.
//
// The API and functionality of this package are considered EXPERIMENTAL and is
// not considered stable nor production ready.
type Storage interface {
	storage.Storer
	Commit() error
}

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

	*ObjectStorage
	*ReferenceStorage
	*IndexStorage
	*ShallowStorage
	*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 temporal is were all
// the write operations are stored.
func NewStorage(base, temporal storage.Storer) Storage {
	st := &basic{
		s:        base,
		temporal: temporal,

		ObjectStorage:    NewObjectStorage(base, temporal),
		ReferenceStorage: NewReferenceStorage(base, temporal),
		IndexStorage:     NewIndexStorage(base, temporal),
		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 *basic) Module(name string) (storage.Storer, error) {
	base, err := s.s.Module(name)
	if err != nil {
		return nil, err
	}

	temporal, err := s.temporal.Module(name)
	if err != nil {
		return nil, err
	}

	return NewStorage(base, temporal), nil
}

// Commit it copies the content of the temporal storage into the base storage.
func (s *basic) Commit() error {
	for _, c := range []interface{ Commit() error }{
		s.ObjectStorage,
		s.ReferenceStorage,
		s.IndexStorage,
		s.ShallowStorage,
		s.ConfigStorage,
	} {
		if err := c.Commit(); err != nil {
			return err
		}
	}

	return nil
}

// PackfileWriter honors storage.PackfileWriter.
func (s *packageWriter) PackfileWriter() (io.WriteCloser, error) {
	return s.pw.PackfileWriter()
}