forked from coreos/torus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
181 lines (159 loc) · 3.92 KB
/
base.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package blockset
import (
"sync/atomic"
"golang.org/x/net/context"
"github.com/RoaringBitmap/roaring"
"github.com/coreos/pkg/capnslog"
"github.com/coreos/torus"
)
type baseBlockset struct {
ids uint64
blocks []torus.BlockRef
store torus.BlockStore
blocksize uint64
}
var _ blockset = &baseBlockset{}
func init() {
RegisterBlockset(Base, func(_ string, store torus.BlockStore, _ blockset) (blockset, error) {
return newBaseBlockset(store), nil
})
}
func newBaseBlockset(store torus.BlockStore) *baseBlockset {
b := &baseBlockset{
blocks: make([]torus.BlockRef, 0),
store: store,
}
if store != nil {
b.blocksize = store.BlockSize()
}
return b
}
func (b *baseBlockset) Length() int {
return len(b.blocks)
}
func (b *baseBlockset) Kind() uint32 {
return uint32(Base)
}
func (b *baseBlockset) GetBlock(ctx context.Context, i int) ([]byte, error) {
if i >= len(b.blocks) {
return nil, torus.ErrBlockNotExist
}
if b.blocks[i].IsZero() {
return make([]byte, b.store.BlockSize()), nil
}
if torus.BlockLog.LevelAt(capnslog.TRACE) {
torus.BlockLog.Tracef("base: getting block %d at BlockID %s", i, b.blocks[i])
}
bytes, err := b.store.GetBlock(ctx, b.blocks[i])
if err != nil {
promBaseFail.Inc()
return nil, err
}
return bytes, err
}
func (b *baseBlockset) PutBlock(ctx context.Context, inode torus.INodeRef, i int, data []byte) error {
if i > len(b.blocks) {
return torus.ErrBlockNotExist
}
// if v, ok := ctx.Value("isEmpty").(bool); ok && v {
// clog.Debug("copying empty block")
// if i == len(b.blocks) {
// b.blocks = append(b.blocks, torus.ZeroBlock())
// } else {
// b.blocks[i] = torus.ZeroBlock()
// }
// return nil
// }
newBlockID := b.makeID(inode)
if torus.BlockLog.LevelAt(capnslog.TRACE) {
torus.BlockLog.Tracef("base: writing block %d at BlockID %s", i, newBlockID)
}
err := b.store.WriteBlock(ctx, newBlockID, data)
if err != nil {
return err
}
if i == len(b.blocks) {
b.blocks = append(b.blocks, newBlockID)
} else {
b.blocks[i] = newBlockID
}
return nil
}
func (b *baseBlockset) makeID(i torus.INodeRef) torus.BlockRef {
id := atomic.AddUint64(&b.ids, 1)
return torus.BlockRef{
INodeRef: i,
Index: torus.IndexID(id),
}
}
func (b *baseBlockset) Marshal() ([]byte, error) {
buf := make([]byte, len(b.blocks)*torus.BlockRefByteSize)
for i, x := range b.blocks {
x.ToBytesBuf(buf[(i * torus.BlockRefByteSize) : (i+1)*torus.BlockRefByteSize])
}
return buf, nil
}
func (b *baseBlockset) setStore(s torus.BlockStore) {
b.blocksize = s.BlockSize()
b.store = s
}
func (b *baseBlockset) getStore() torus.BlockStore {
return b.store
}
func (b *baseBlockset) Unmarshal(data []byte) error {
l := len(data) / torus.BlockRefByteSize
out := make([]torus.BlockRef, l)
for i := 0; i < l; i++ {
out[i] = torus.BlockRefFromBytes(data[(i * torus.BlockRefByteSize) : (i+1)*torus.BlockRefByteSize])
}
b.blocks = out
return nil
}
func (b *baseBlockset) GetSubBlockset() torus.Blockset { return nil }
func (b *baseBlockset) GetLiveINodes() *roaring.Bitmap {
out := roaring.NewBitmap()
for _, blk := range b.blocks {
if blk.IsZero() {
continue
}
out.Add(uint32(blk.INode))
}
return out
}
func (b *baseBlockset) Truncate(lastIndex int, _ uint64) error {
if lastIndex <= len(b.blocks) {
b.blocks = b.blocks[:lastIndex]
return nil
}
toadd := lastIndex - len(b.blocks)
for toadd != 0 {
b.blocks = append(b.blocks, torus.ZeroBlock())
toadd--
}
return nil
}
func (b *baseBlockset) Trim(from, to int) error {
if from >= len(b.blocks) {
return nil
}
if to > len(b.blocks) {
to = len(b.blocks)
}
for i := from; i < to; i++ {
b.blocks[i] = torus.ZeroBlock()
}
return nil
}
func (b *baseBlockset) GetAllBlockRefs() []torus.BlockRef {
out := make([]torus.BlockRef, len(b.blocks))
copy(out, b.blocks)
return out
}
func (b *baseBlockset) String() string {
out := "[\n"
for _, x := range b.blocks {
out += x.String() + "\n"
}
out += "]"
return out
}