-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogstore_resolution.go
115 lines (94 loc) · 2.99 KB
/
logstore_resolution.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
package ingest_resolution
import (
"fmt"
"github.com/kwilteam/kwil-db/core/types/serialize"
"math/big"
"strconv"
)
type LogStoreIngestMessage struct {
Id string
Content string
Timestamp uint
}
func (m *LogStoreIngestMessage) MarshalBinary() ([]byte, error) {
return serialize.Encode(m)
}
func (m *LogStoreIngestMessage) UnmarshalBinary(rawData []byte) error {
return serialize.DecodeInto(rawData, m)
}
type LogStoreIngestDataResolution struct {
Messages []LogStoreIngestMessage
}
func (r *LogStoreIngestDataResolution) NewData() IngestDataResolution {
return &LogStoreIngestDataResolution{}
}
func (r *LogStoreIngestDataResolution) MarshalBinary() ([]byte, error) {
return serialize.Encode(r)
}
func (r *LogStoreIngestDataResolution) UnmarshalBinary(rawData []byte) error {
return serialize.DecodeInto(rawData, &r)
}
func (r *LogStoreIngestDataResolution) MarshalIntoChunks(maxChunkSize int) ([][]byte, []IngestDataResolution, []error) {
binaryData, err := r.MarshalBinary()
if err != nil {
return nil, nil, []error{err}
}
binarySize := len(binaryData)
var chunks [][]byte
var rs []IngestDataResolution
var errs []error
if binarySize < maxChunkSize {
chunks = append(chunks, binaryData)
rs = append(rs, r)
return chunks, rs, nil
}
if len(r.Messages) == 1 {
msgId := r.Messages[0].Id
return nil, nil, []error{fmt.Errorf("message with id %s is too large to be chunked", msgId)}
}
// split the data into chunks
splitChunks := r.split(2)
for _, rawChunk := range splitChunks {
newChunks, newRs, newErrs := rawChunk.MarshalIntoChunks(maxChunkSize)
// here we won't return in case of errors, because it might be partially successful.
// before it, we returned because they were about single message or fatal errors
errs = append(errs, newErrs...)
chunks = append(chunks, newChunks...)
rs = append(rs, newRs...)
}
return chunks, rs, nil
}
func (r *LogStoreIngestDataResolution) split(numberOfChunks int) []*LogStoreIngestDataResolution {
chunkSize := (len(r.Messages) + numberOfChunks - 1) / numberOfChunks
chunks := make([]*LogStoreIngestDataResolution, 0, numberOfChunks)
for i := 0; i < len(r.Messages); i += chunkSize {
end := i + chunkSize
if end > len(r.Messages) {
end = len(r.Messages)
}
resolution := &LogStoreIngestDataResolution{
Messages: r.Messages[i:end],
}
chunks = append(chunks, resolution)
}
return chunks
}
func (r *LogStoreIngestDataResolution) GetArgs() [][]*string {
var argsSet [][]*string
for _, message := range r.Messages {
var args []*string
args = append(args, &message.Id)
args = append(args, &message.Content)
tsString := strconv.Itoa(int(message.Timestamp))
args = append(args, &tsString)
argsSet = append(argsSet, args)
}
return argsSet
}
var LogStoreIngestResolution = &IngestResolution[*LogStoreIngestDataResolution]{
RefundThreshold: big.NewRat(1, 3),
ConfirmationThreshold: big.NewRat(2, 3),
// aprox 1 hour, assuming 6s block time
ExpirationPeriod: 600,
ResolutionName: "log_store_ingest",
}