This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 883
/
cas.go
162 lines (136 loc) · 3.07 KB
/
cas.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package cas
import (
"bytes"
"crypto/sha256"
"fmt"
"io"
"path/filepath"
"github.com/coreos/rocket/app-container/aci"
"github.com/coreos/rocket/Godeps/_workspace/src/github.com/peterbourgon/diskv"
)
// TODO(philips): use a database for the secondary indexes like remoteType and
// appType. This is OK for now though.
const (
blobType int64 = iota
remoteType
tmpType
)
var otmap = [...]string{
"blob",
"remote",
"tmp",
}
type Store struct {
stores []*diskv.Diskv
}
func NewStore(base string) *Store {
ds := &Store{}
ds.stores = make([]*diskv.Diskv, len(otmap))
for i, p := range otmap {
ds.stores[i] = diskv.New(diskv.Options{
BasePath: filepath.Join(base, "cas", p),
Transform: blockTransform,
CacheSizeMax: 1024 * 1024, // 1MB
})
}
return ds
}
func (ds Store) ReadStream(key string) (io.ReadCloser, error) {
return ds.stores[blobType].ReadStream(key, false)
}
func (ds Store) WriteStream(key string, r io.Reader) error {
return ds.stores[blobType].WriteStream(key, r, true)
}
func (ds Store) WriteACI(tmpKey string, orig io.Reader) (string, error) {
var b bytes.Buffer
// TODO(philips): use go routines to parallelize this pipeline and make
// the file type detection happen without a second stream
_, err := io.Copy(&b, orig)
if err != nil {
return "", err
}
err = ds.stores[tmpType].WriteStream(tmpKey, &b, true)
if err != nil {
return "", err
}
// Detect the filetype
rs, err := ds.stores[tmpType].ReadStream(tmpKey, false)
if err != nil {
return "", err
}
defer rs.Close()
typ, err := aci.DetectFileType(rs)
if err != nil {
return "", err
}
rs, err = ds.stores[tmpType].ReadStream(tmpKey, false)
if err != nil {
return "", err
}
defer rs.Close()
// Generate the hash of the decompressed tar
dr, err := decompress(rs, typ)
if err != nil {
return "", err
}
hash := sha256.New()
_, err = io.Copy(hash, dr)
if err != nil {
return "", err
}
// Store the decompressed tar
rs, err = ds.stores[tmpType].ReadStream(tmpKey, false)
if err != nil {
return "", err
}
defer rs.Close()
dr, err = decompress(rs, typ)
if err != nil {
return "", err
}
key := fmt.Sprintf("sha256-%x", hash.Sum(nil))
err = ds.stores[blobType].WriteStream(key, dr, true)
if err != nil {
return "", err
}
ds.stores[tmpType].Erase(tmpKey)
return key, nil
}
type Index interface {
Hash() string
Marshal() []byte
Unmarshal([]byte)
Type() int64
}
func (ds Store) WriteIndex(i Index) {
ds.stores[i.Type()].Write(i.Hash(), i.Marshal())
}
func (ds Store) ReadIndex(i Index) error {
buf, err := ds.stores[i.Type()].Read(i.Hash())
if err != nil {
return err
}
i.Unmarshal(buf)
return nil
}
func (ds Store) Dump(hex bool) {
for _, s := range ds.stores {
var keyCount int
for key := range s.Keys() {
val, err := s.Read(key)
if err != nil {
panic(fmt.Sprintf("key %s had no value", key))
}
if len(val) > 128 {
val = val[:128]
}
out := string(val)
if hex {
out = fmt.Sprintf("%x", val)
}
fmt.Printf("%s/%s: %s\n", s.BasePath, key, out)
keyCount++
}
fmt.Printf("%d total keys\n", keyCount)
}
}