This repository has been archived by the owner on Nov 3, 2024. It is now read-only.
forked from streamingfast/bstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_payload.go
134 lines (102 loc) · 2.59 KB
/
block_payload.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
package bstream
import (
"fmt"
"os"
"time"
"go.uber.org/zap"
"github.com/streamingfast/atm"
"github.com/streamingfast/dstore"
)
var GetBlockPayloadSetter BlockPayloadSetter
type BlockPayloadSetter func(block *Block, data []byte) (*Block, error)
type BlockPayload interface {
Get() (data []byte, err error)
Size() int
}
type MemoryBlockPayload struct {
data []byte
}
func MemoryBlockPayloadSetter(block *Block, data []byte) (*Block, error) {
block.Payload = &MemoryBlockPayload{
data: data,
}
return block, nil
}
func (p *MemoryBlockPayload) Get() (data []byte, err error) {
return p.data, err
}
func (p *MemoryBlockPayload) Size() int {
return len(p.data)
}
var atmCache *atm.Cache
var store dstore.Store
func getCache() *atm.Cache {
if atmCache == nil {
panic("cache is not initialized")
}
return atmCache
}
func InitCache(storeUrl string, cachePath string, maxRecentEntryBytes int, maxEntryByAgeBytes int) {
if _, err := os.Stat(cachePath); os.IsNotExist(err) {
err := os.Mkdir(cachePath, os.ModePerm)
if err != nil {
panic(err)
}
}
var err error
s, err := dstore.NewDBinStore(storeUrl)
if err != nil {
panic(fmt.Sprintf("failed to initialize store: %s: %s", storeUrl, err))
}
store = s
atmCache, err = atm.NewInitializedCache(cachePath, maxRecentEntryBytes, maxEntryByAgeBytes, atm.NewFileIO())
if err != nil {
panic(fmt.Sprintf("failed to initialize cache: %s: %s", cachePath, err))
}
}
type ATMCachedBlockPayload struct {
blockId string
blockNum uint64
dataSize int
}
func (p *ATMCachedBlockPayload) Get() (data []byte, err error) {
data, found, err := getCache().Read(p.blockId)
if found && err != nil {
return nil, err
}
if len(data) == 0 {
zlog.Info("block data is empty. reading block from filesource", zap.String("block_id", p.blockId), zap.Uint64("block_num", p.blockNum))
var fs *FileSource
var block *Block
handler := HandlerFunc(func(blk *Block, obj interface{}) error {
if blk.Num() != p.blockNum || blk.ID() != p.blockId {
return nil
}
block = blk
fs.Shutdown(nil)
return nil
})
fs = NewFileSource(store, p.blockNum, handler, zlog)
fs.Run()
if fs.Err() != nil {
return nil, fs.Err()
}
return block.Payload.Get()
}
return
}
func (p *ATMCachedBlockPayload) Size() int {
return p.dataSize
}
func ATMCachedPayloadSetter(block *Block, data []byte) (*Block, error) {
_, err := getCache().Write(block.Id, block.Timestamp, time.Now(), data)
if err != nil {
return nil, err
}
block.Payload = &ATMCachedBlockPayload{
blockId: block.Id,
blockNum: block.Number,
dataSize: len(data),
}
return block, err
}