This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
datastream_batch.go
200 lines (174 loc) · 4.06 KB
/
datastream_batch.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
188
189
190
191
192
193
194
195
196
197
198
199
200
package iop
import (
"strings"
"time"
"github.com/flarco/g"
"github.com/samber/lo"
)
type Batch struct {
id int
Columns Columns
Rows chan []any
Previous *Batch
Count int64
ds *Datastream
closed bool
closeChan chan struct{}
transforms []func(row []any) []any
context *g.Context
}
// NewBatch create new batch with fixed columns
// should be used each time column type changes, or columns are added
func (ds *Datastream) NewBatch(columns Columns) *Batch {
ctx := g.NewContext(ds.Context.Ctx)
batch := &Batch{
id: len(ds.Batches),
Columns: columns,
Rows: MakeRowsChan(),
Previous: ds.LatestBatch(),
ds: ds,
closeChan: make(chan struct{}),
transforms: []func(row []any) []any{},
context: &ctx,
}
if batch.Previous != nil && !batch.Previous.closed {
batch.Previous.Close() // close previous batch
}
ds.Batches = append(ds.Batches, batch)
ds.CurrentBatch = batch
if !ds.closed {
ds.BatchChan <- batch
}
if !ds.NoDebug {
g.Trace("new batch %s", batch.ID())
}
return batch
}
func (ds *Datastream) LatestBatch() *Batch {
if len(ds.Batches) > 0 {
return ds.Batches[len(ds.Batches)-1]
}
return nil
}
func (b *Batch) ID() string {
return g.F("%s-%d", b.ds.ID, b.id)
}
func (b *Batch) Ds() *Datastream {
return b.ds
}
func (b *Batch) IsFirst() bool {
return b.id == 0
}
func (b *Batch) Close() {
b.context.Lock()
defer b.context.Unlock()
if !b.closed {
timer := time.NewTimer(4 * time.Millisecond)
select {
case b.closeChan <- struct{}{}:
case <-timer.C:
}
b.closed = true
close(b.Rows)
if !b.ds.NoDebug {
g.Trace("closed %s", b.ID())
}
}
}
func (b *Batch) ColumnsChanged() bool {
if pB := b.Previous; pB != nil {
if len(pB.Columns) != len(b.Columns) {
return true
}
for i := range b.Columns {
if b.Columns[i].Type != pB.Columns[i].Type {
return true
} else if b.Columns[i].Name != pB.Columns[i].Name {
return true
}
}
}
return false
}
func (b *Batch) Shape(tgtColumns Columns, pause ...bool) (err error) {
doPause := true
if len(pause) > 0 {
doPause = pause[0]
}
srcColumns := b.Columns
if len(tgtColumns) < len(srcColumns) {
return g.Error("number of target columns is smaller than number of source columns")
}
// determine diff, and match order of target columns
tgtColNames := lo.Map(tgtColumns, func(c Column, i int) string { return strings.ToLower(c.Name) })
diffCols := len(tgtColumns) != len(srcColumns)
colMap := map[int]int{}
for s, col := range srcColumns {
t := lo.IndexOf(tgtColNames, strings.ToLower(col.Name))
if t == -1 {
return g.Error("column %s not found in target columns", col.Name)
}
colMap[s] = t
diffCols = s != t ||
!strings.EqualFold(tgtColumns[t].Name, srcColumns[s].Name)
}
if !diffCols {
return nil
}
mapRowCol := func(srcRow []any) []any {
tgtRow := make([]any, len(tgtColumns))
for len(srcRow) < len(tgtRow) {
srcRow = append(srcRow, nil)
}
for s, t := range colMap {
tgtRow[t] = srcRow[s]
}
return tgtRow
}
if doPause {
b.ds.Pause()
}
b.Columns = tgtColumns // should match the target columns
b.AddTransform(mapRowCol)
g.DebugLow("%s | added mapRowCol, len(b.transforms) = %d", b.ID(), len(b.transforms))
if doPause {
b.ds.Unpause()
}
return nil
}
func (b *Batch) AddTransform(transf func(row []any) []any) {
b.transforms = append(b.transforms, transf)
}
func (b *Batch) Push(row []any) {
newRow := row
for _, f := range b.transforms {
newRow = f(newRow) // allows transformations
}
for len(newRow) < len(b.Columns) {
newRow = append(newRow, nil)
}
b.context.Lock()
if b.closed {
b.context.Unlock()
b.ds.it.Reprocess <- row
return
}
b.context.Unlock()
select {
case <-b.ds.Context.Ctx.Done():
b.ds.Close()
case <-b.ds.pauseChan:
<-b.ds.unpauseChan // wait for unpause
b.ds.it.Reprocess <- row
case <-b.closeChan:
b.ds.it.Reprocess <- row
case v := <-b.ds.schemaChgChan:
b.ds.it.Reprocess <- row
b.ds.schemaChgChan <- v
case b.Rows <- newRow:
b.Count++
b.ds.Count++
b.ds.bwRows <- newRow
b.ds.Sp.commitChecksum()
}
}