This repository has been archived by the owner on Sep 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 172
/
blockset.go
70 lines (63 loc) · 2.26 KB
/
blockset.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
package torus
import (
"github.com/RoaringBitmap/roaring"
"github.com/coreos/torus/models"
"golang.org/x/net/context"
)
// Blockset is the interface representing the standardized methods to interact
// with a set of blocks.
type Blockset interface {
// Length returns the number of blocks in the Blockset.
Length() int
// Kind returns the kind of the Blockset.
Kind() uint32
// GetBlock returns the ith block in the Blockset.
GetBlock(ctx context.Context, i int) ([]byte, error)
// PutBlock puts a block with data `b` into the Blockset as its ith block.
// The block belongs to the given inode.
PutBlock(ctx context.Context, inode INodeRef, i int, b []byte) error
// GetLiveInodes returns the current INode representation of the Blockset.
// The returned INode might not be synced.
GetLiveINodes() *roaring.Bitmap
// GetAllBlockRefs returns the BlockRef of the blocks in the Blockset.
// The ith BlockRef in the returned slice is the Ref of the ith Block in the
// Blockset.
GetAllBlockRefs() []BlockRef
// Marshal returns the bytes representation of the Blockset.
Marshal() ([]byte, error)
// Unmarshal parses the bytes representation of the Blockset and stores the result
// in the Blockset.
Unmarshal(data []byte) error
// GetSubBlockset gets the sub-Blockset of the Blockset if exists.
// If there is no sub-Blockset, nil will be returned.
GetSubBlockset() Blockset
// Truncate changes the length of the Blockset and the block. If the Blockset has less
// blocks than the required size, truncate adds zero blocks. If the block has less bytes
// than required size, truncate add bytes into block.
Truncate(lastIndex int, blocksize uint64) error
// Trim zeros the blocks in range [from, to).
Trim(from, to int) error
// String implements the fmt.Stringer interface.
String() string
}
type BlockLayerKind int
type BlockLayer struct {
Kind BlockLayerKind
Options string
}
type BlockLayerSpec []BlockLayer
func MarshalBlocksetToProto(bs Blockset) ([]*models.BlockLayer, error) {
var out []*models.BlockLayer
var layer Blockset
for layer = bs; layer != nil; layer = layer.GetSubBlockset() {
m, err := layer.Marshal()
if err != nil {
return nil, err
}
out = append(out, &models.BlockLayer{
Type: layer.Kind(),
Content: m,
})
}
return out, nil
}