-
Notifications
You must be signed in to change notification settings - Fork 4
/
store_ceph.go
165 lines (130 loc) · 3.17 KB
/
store_ceph.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
163
164
165
package fsdup
import (
"bytes"
"compress/gzip"
"fmt"
"github.com/ceph/go-ceph/rados"
"io/ioutil"
)
type cephChunkStore struct {
configFile string
pool string
compress bool
ctx *rados.IOContext
chunkMap map[string]bool
buffer []byte
}
func NewCephStore(configFile string, pool string, compress bool) *cephChunkStore {
return &cephChunkStore{
configFile: configFile,
pool: pool,
compress: compress,
chunkMap: make(map[string]bool, 0),
buffer: make([]byte, 128 * 1024 * 1024), // FIXME: This is a hack, this means that only chunks <= 128M are supported!
}
}
func (idx *cephChunkStore) Stat(checksum []byte) error {
if err := idx.openPool(); err != nil {
return err
}
checksumStr := fmt.Sprintf("%x", checksum)
if _, ok := idx.chunkMap[checksumStr]; ok {
return nil
}
_, err := idx.ctx.Stat(checksumStr)
return err
}
func (idx *cephChunkStore) ReadAt(checksum []byte, buffer []byte, offset int64) (int, error) {
if err := idx.openPool(); err != nil {
return 0, err
}
checksumStr := fmt.Sprintf("%x", checksum)
if idx.compress {
// FIXME this reads the ENTIRE object, gunzips it, and then only reads the requested section.
// don't kill me this is a PoC
read, err := idx.ctx.Read(checksumStr, idx.buffer, 0)
if err != nil {
return 0, err
}
reader, err := gzip.NewReader(bytes.NewReader(idx.buffer[:read]))
if err != nil {
return 0, err
}
decompressed, err := ioutil.ReadAll(reader)
if err != nil {
return 0, err
}
length := len(buffer)
end := int64(offset)+int64(length)
copy(buffer, decompressed[offset:end]) // FIXME urgh ...
//fmt.Printf("%d - %d = %x\n", from, end, buffer)
return length, nil
} else {
read, err := idx.ctx.Read(checksumStr, buffer, uint64(offset))
if err != nil {
return 0, err
}
return read, nil
}
}
func (idx *cephChunkStore) Write(checksum []byte, buffer []byte) error {
if err := idx.openPool(); err != nil {
return err
}
checksumStr := fmt.Sprintf("%x", checksum)
if _, ok := idx.chunkMap[checksumStr]; !ok {
if _, err := idx.ctx.Stat(checksumStr); err != nil {
if idx.compress {
var b bytes.Buffer
writer := gzip.NewWriter(&b)
if _, err := writer.Write(buffer); err != nil {
return err
}
if err := writer.Close(); err != nil {
return err
}
if err := idx.ctx.Write(checksumStr, b.Bytes(), 0); err != nil {
return err
}
} else {
if err := idx.ctx.Write(checksumStr, buffer, 0); err != nil {
return err
}
}
}
idx.chunkMap[checksumStr] = true
}
return nil
}
func (idx *cephChunkStore) Remove(checksum []byte) error {
if err := idx.openPool(); err != nil {
return err
}
checksumStr := fmt.Sprintf("%x", checksum)
err := idx.ctx.Delete(checksumStr)
if err != nil {
return err
}
delete(idx.chunkMap, checksumStr)
return nil
}
func (idx *cephChunkStore) openPool() error {
if idx.ctx != nil {
return nil
}
conn, err := rados.NewConn()
if err != nil {
return err
}
if err := conn.ReadConfigFile(idx.configFile); err != nil {
return err
}
if err := conn.Connect(); err != nil {
return err
}
idx.ctx, err = conn.OpenIOContext(idx.pool)
if err != nil {
return err
}
return nil
}