-
Notifications
You must be signed in to change notification settings - Fork 12
/
oneblockfile.go
187 lines (162 loc) · 5.01 KB
/
oneblockfile.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
182
183
184
185
186
187
// Copyright 2019 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bstream
import (
"bytes"
"context"
"fmt"
"io"
"strconv"
"strings"
"sync"
pbbstream "github.com/streamingfast/bstream/pb/sf/bstream/v1"
"github.com/streamingfast/dstore"
)
type OneBlockDownloaderFunc = func(ctx context.Context, oneBlockFile *OneBlockFile) (data []byte, err error)
func DecodeOneblockfileData(data []byte) (*pbbstream.Block, error) {
reader := bytes.NewReader(data)
blockReader, err := NewDBinBlockReader(reader)
if err != nil {
return nil, fmt.Errorf("unable to create block reader: %w", err)
}
blk, err := blockReader.Read()
if err != nil && err != io.EOF {
return nil, fmt.Errorf("block reader failed: %w", err)
}
return blk, nil
}
func decodeOneblockfileToBlockMeta(data []byte) (*pbbstream.BlockMeta, error) {
reader := bytes.NewReader(data)
blockReader, err := NewDBinBlockReader(reader)
if err != nil {
return nil, fmt.Errorf("unable to create block reader: %w", err)
}
blk, err := blockReader.ReadAsBlockMeta()
if err != nil && err != io.EOF {
return nil, fmt.Errorf("block meta reader failed: %w", err)
}
return blk, nil
}
// OneBlockFile is the representation of a single block inside one or more duplicate files, before they are merged
// It has a truncated ID
type OneBlockFile struct {
sync.Mutex
CanonicalName string
Filenames map[string]bool
ID string
Num uint64
LibNum uint64
PreviousID string
MemoizeData []byte
Deleted bool
}
func (f *OneBlockFile) ToBstreamBlock() *pbbstream.Block {
return &pbbstream.Block{
Id: f.ID,
Number: f.Num,
ParentId: f.PreviousID,
LibNum: f.LibNum,
}
}
func (f *OneBlockFile) String() string {
return fmt.Sprintf("#%d (ID %s, ParentID %s)", f.Num, f.ID, f.PreviousID)
}
func NewOneBlockFile(fileName string) (*OneBlockFile, error) {
blockNum, blockID, previousBlockID, libNum, canonicalName, err := ParseFilename(fileName)
if err != nil {
return nil, err
}
return &OneBlockFile{
CanonicalName: canonicalName,
Filenames: map[string]bool{
fileName: true,
},
ID: blockID,
Num: blockNum,
PreviousID: previousBlockID,
LibNum: libNum,
}, nil
}
func MustNewOneBlockFile(fileName string) *OneBlockFile {
out, err := NewOneBlockFile(fileName)
if err != nil {
panic(err)
}
return out
}
func (f *OneBlockFile) Data(ctx context.Context, oneBlockDownloader OneBlockDownloaderFunc) ([]byte, error) {
f.Lock()
defer f.Unlock()
if len(f.MemoizeData) == 0 {
data, err := oneBlockDownloader(ctx, f)
if err != nil {
return nil, err
}
f.MemoizeData = data
}
return f.MemoizeData, nil
}
// ParseFilename parses file names formatted like:
// * 0000000101-dbda3f44afee24dd-24a072678473e4ad-100-mindread1
// * 0000000101-dbda3f44afee24dd-24a072678473e4ad-100-mindread2
func ParseFilename(filename string) (blockNum uint64, blockIDSuffix string, previousBlockIDSuffix string, libNum uint64, canonicalName string, err error) {
parts := strings.Split(filename, "-")
if len(parts) != 5 {
err = fmt.Errorf("wrong filename format: %q", filename)
return
}
blockNumVal, parseErr := strconv.ParseUint(parts[0], 10, 32)
if parseErr != nil {
err = fmt.Errorf("failed parsing %q: %s", parts[0], parseErr)
return
}
blockNum = blockNumVal
blockIDSuffix = parts[1]
previousBlockIDSuffix = parts[2]
canonicalName = filename
libNum, parseErr = strconv.ParseUint(parts[3], 10, 32)
if parseErr != nil {
err = fmt.Errorf("failed parsing lib num %q: %s", parts[4], parseErr)
return
}
canonicalName = strings.Join(parts[0:4], "-")
return
}
func BlockFileName(block *pbbstream.Block) string {
return BlockFileNameWithSuffix(block, "generated")
}
func TruncateBlockID(in string) string {
if len(in) <= 16 {
return in
}
return in[len(in)-16:]
}
func BlockFileNameWithSuffix(block *pbbstream.Block, suffix string) string {
blockID := TruncateBlockID(block.Id)
previousID := TruncateBlockID(block.ParentId)
return fmt.Sprintf("%010d-%s-%s-%d-%s", block.Number, blockID, previousID, block.LibNum, suffix)
}
func OneBlockDownloaderFromStore(blocksStore dstore.Store) OneBlockDownloaderFunc {
return func(ctx context.Context, obf *OneBlockFile) ([]byte, error) {
for filename := range obf.Filenames {
reader, err := blocksStore.OpenObject(ctx, filename)
if err != nil {
return nil, fmt.Errorf("fetching %s from block store: %w", filename, err)
}
defer reader.Close()
return io.ReadAll(reader)
}
return nil, fmt.Errorf("no filename for this oneBlockFile")
}
}