/
column_texts_streamer.go
145 lines (122 loc) · 3.13 KB
/
column_texts_streamer.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
package helper
import (
"context"
"fmt"
"io"
"log"
"runtime/debug"
"github.com/bytehouse-cloud/driver-go/driver/lib/bytepool"
"github.com/bytehouse-cloud/driver-go/driver/lib/data"
"github.com/bytehouse-cloud/driver-go/driver/lib/data/column"
)
type TableReader interface {
ReadFirstColumnTexts(fb *bytepool.FrameBuffer, numRows int, cols []*column.CHColumn) (int, error)
ReadColumnTextsCont(fb *bytepool.FrameBuffer, numRows int, cols []*column.CHColumn) (int, error)
}
type ReadColumnTexts func(fb *bytepool.FrameBuffer, rows int, cols []*column.CHColumn) (int, error)
type ColumnTextsStreamer struct {
cols []*column.CHColumn
blockSize int
tReader TableReader
ctPool *ColumnTextsPool
rowRead int
err error
canceled bool
done chan struct{}
}
func NewColumnTextsStreamer(sample *data.Block, blockSize int, tReader TableReader) *ColumnTextsStreamer {
return &ColumnTextsStreamer{
cols: sample.Columns,
blockSize: blockSize,
tReader: tReader,
ctPool: NewColumnTextsPool(sample.NumColumns, blockSize),
}
}
func (c *ColumnTextsStreamer) Start(ctx context.Context) <-chan *ColumnTextsResult {
go c.watchCtx(ctx)
c.done = make(chan struct{}, 1)
outputStream := make(chan *ColumnTextsResult, 1)
go func() {
defer func() {
if r := recover(); r != nil {
log.Printf("A runtime panic has occurred with err = [%s], stacktrace = [%s]\n",
r,
string(debug.Stack()))
}
}()
defer close(c.done)
defer close(outputStream)
var err error
defer func() {
if err != nil {
if err == io.EOF {
return
}
c.err = fmt.Errorf("error reading row at index %v: %s", c.rowRead, err)
}
}()
c.rowRead, err = c.readFirst(ctx, outputStream)
if err != nil {
return
}
var n int
for {
if c.canceled {
return
}
n, err = c.readCont(ctx, outputStream)
c.rowRead += n
if err != nil {
return
}
}
}()
return outputStream
}
func (c *ColumnTextsStreamer) watchCtx(ctx context.Context) {
select {
case <-ctx.Done():
c.canceled = true
c.err = ctx.Err()
case <-c.done:
return
}
}
func (c *ColumnTextsStreamer) Finish() (int, error) {
defer func() {
if r := recover(); r != nil {
log.Printf("A runtime panic has occurred with err = [%s], stacktrace = [%s]\n",
r,
string(debug.Stack()))
}
}()
<-c.done
return c.rowRead, c.err
}
func (c *ColumnTextsStreamer) readFirst(ctx context.Context, des chan<- *ColumnTextsResult) (int, error) {
return c.read(ctx, des, c.tReader.ReadFirstColumnTexts)
}
func (c *ColumnTextsStreamer) readCont(ctx context.Context, des chan<- *ColumnTextsResult) (int, error) {
return c.read(ctx, des, c.tReader.ReadColumnTextsCont)
}
func (c *ColumnTextsStreamer) read(ctx context.Context, des chan<- *ColumnTextsResult, readColumnTexts ReadColumnTexts) (int, error) {
if c.canceled {
return 0, c.err
}
fb := bytepool.NewFrameBuffer()
n, err := readColumnTexts(fb, c.blockSize, c.cols)
if err != nil {
if err != io.EOF {
return n, err
}
if n == 0 {
return n, io.EOF
}
}
select {
case des <- c.ctPool.NewColumnTextsResult(fb):
return n, nil
case <-ctx.Done():
return n, context.Canceled
}
}