Debian Codegraph @

dstream/sizechunk.go

raw symbols SCIP
1 package dstream
2
3 type maxchunksize struct {
4 xform
5
6 // Maximum chunk size
7 size int
8
9 // Position in the current chunk
10 pos int
11
12 // If true, need to call source.Next before proceeding
13 first bool
14 }
15
16 // MaxChunkSize splits the chunks of the input Dstream so that no
17 // chunk has more than size rows.
18 func MaxChunkSize(data Dstream, size int) Dstream {
19
20 sc := &maxchunksize{
21 xform: xform{
22 source: data,
23 },
24 size: size,
25 first: true,
26 }
27
28 // Read the first source chunk.
29 sc.source.Next()
30
31 nvar := sc.source.NumVar()
32 sc.names = sc.source.Names()
33 sc.bdata = make([]interface{}, nvar)
34
35 return sc
36 }
37
38 func (sc *maxchunksize) Next() bool {
39
40 if sc.first {
41 if !sc.source.Next() {
42 return false
43 }
44 sc.first = false
45 }
46
47 // Advance or return if there is no current data
48 x := sc.source.GetPos(0)
49 m := ilen(x) - sc.pos
50 if m <= 0 {
51 if !sc.source.Next() {
52 return false
53 }
54 sc.pos = 0
55 x = sc.source.GetPos(0)
56 m = ilen(x) - sc.pos
57 if m <= 0 {
58 return false
59 }
60 }
61 if m > sc.size {
62 m = sc.size
63 }
64
65 for j := 0; j < sc.NumVar(); j++ {
66
67 x := sc.source.GetPos(j)
68
69 // TODO generic types
70 switch x := x.(type) {
71 case []float64:
72 sc.bdata[j] = x[sc.pos : sc.pos+m]
73 case []string:
74 sc.bdata[j] = x[sc.pos : sc.pos+m]
75 default:
76 panic("unknown type")
77 }
78 }
79
80 sc.pos += m
81
82 return true
83 }
84
85 func (sc *maxchunksize) Reset() {
86 sc.source.Reset()
87 sc.first = true
88 sc.pos = 0
89 }