-
Notifications
You must be signed in to change notification settings - Fork 16
/
storage.go
106 lines (85 loc) · 2.82 KB
/
storage.go
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
101
102
103
104
105
106
package chaintree
import (
"context"
"fmt"
"io"
"github.com/go-git/go-git/v5/config"
"github.com/quorumcontrol/chaintree/dag"
"github.com/quorumcontrol/dgit/storage"
"github.com/quorumcontrol/dgit/storage/siaskynet"
"github.com/go-git/go-git/v5/plumbing/storer"
gitstorage "github.com/go-git/go-git/v5/storage"
"github.com/go-git/go-git/v5/storage/memory"
)
var RepoConfigPath = []string{"tree", "data", "config"}
const defaultStorageProvider = "chaintree"
type ChaintreeStorage struct {
storer.EncodedObjectStorer
storer.ReferenceStorer
storer.ShallowStorer
storer.IndexStorer
config.ConfigStorer
}
func NewStorage(config *storage.Config) (gitstorage.Storer, error) {
ctx := context.Background()
objStorageProvider, err := getObjectStorageProvider(ctx, config.ChainTree.ChainTree.Dag)
if err != nil {
return nil, err
}
var objStorage storer.EncodedObjectStorer
switch objStorageProvider {
case "chaintree":
objStorage = NewObjectStorage(config)
case "siaskynet":
objStorage = siaskynet.NewObjectStorage(config)
default:
return nil, fmt.Errorf("unknown object storage type: %s", objStorageProvider)
}
return &ChaintreeStorage{
objStorage,
NewReferenceStorage(config),
memory.NewStorage(),
memory.NewStorage(),
memory.NewStorage(),
}, nil
}
func (s *ChaintreeStorage) Module(_ string) (gitstorage.Storer, error) {
return nil, fmt.Errorf("ChaintreeStorage.Module not implemented")
}
func getObjectStorageProvider(ctx context.Context, dag *dag.Dag) (string, error) {
configUncast, _, err := dag.Resolve(ctx, RepoConfigPath)
if err != nil {
return "", fmt.Errorf("could not resolve repo config in chaintree: %w", err)
}
// repo hasn't been configured yet
if configUncast == nil {
return defaultStorageProvider, nil
}
var (
ctConfig map[string]interface{}
ok bool
)
if ctConfig, ok = configUncast.(map[string]interface{}); !ok {
return "", fmt.Errorf("could not cast config to map[string]interface{}: was %T instead", configUncast)
}
objectStorageConfigUncast := ctConfig["objectStorage"]
var objectStorageConfig map[string]interface{}
if objectStorageConfig, ok = objectStorageConfigUncast.(map[string]interface{}); !ok {
return "", fmt.Errorf("could not cast objectStorage config to map[string]interface{}: was %T instead", objectStorageConfigUncast)
}
objStorageType, ok := objectStorageConfig["type"].(string)
if !ok {
return "", fmt.Errorf("could not cast objectStorage config type to string; was %T instead", objectStorageConfig["type"])
}
if objStorageType == "" {
return defaultStorageProvider, nil
}
return objStorageType, nil
}
func (s *ChaintreeStorage) PackfileWriter() (io.WriteCloser, error) {
pw, ok := s.EncodedObjectStorer.(storer.PackfileWriter)
if !ok {
return nil, fmt.Errorf("could not cast object storer to packfile writer")
}
return pw.PackfileWriter()
}