forked from v3io/frames
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rowiter.go
171 lines (142 loc) · 3.57 KB
/
rowiter.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
/*
Copyright 2018 Iguazio Systems Ltd.
Licensed under the Apache License, Version 2.0 (the "License") with
an addition restriction as set forth herein. You may not use this
file except in compliance with the License. You may obtain a copy of
the License at http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
In addition, you may not use the software for any purposes that are
illegal under applicable law, and the grant of the foregoing license
under the Apache 2.0 license is conditioned upon your compliance with
such restriction.
*/
package frames
import (
"fmt"
"sync"
)
type rowIterator struct {
// names for columns with no name
noNames map[int]string
columns []Column
err error
frame Frame
includeIndex bool
numCols int
once sync.Once
row map[string]interface{}
rowNum int
}
func newRowIterator(frame Frame, includeIndex bool) *rowIterator {
return &rowIterator{
frame: frame,
includeIndex: includeIndex,
}
}
func (it *rowIterator) init() {
names := it.frame.Names()
it.numCols = len(names)
size := len(names)
if it.includeIndex {
size += len(it.frame.Indices())
}
nameMap := make(map[string]bool)
columns := make([]Column, size)
noNames := make(map[int]string)
for i, name := range names {
// FIXME: Currently we allow duplicate names
col, err := it.frame.Column(name)
if err != nil {
it.err = err
return
}
if name == "" {
name = fmt.Sprintf("col-%d", i)
noNames[i] = name
}
if _, ok := nameMap[name]; ok {
it.err = fmt.Errorf("duplicate column name - %q", name)
return
}
nameMap[name] = true
columns[i] = col
}
if it.includeIndex {
for i, col := range it.frame.Indices() {
name := col.Name()
if name == "" {
name = fmt.Sprintf("index-%d", i)
noNames[it.numCols+i] = name
}
if _, ok := nameMap[name]; ok {
it.err = fmt.Errorf("duplicate name in columns and indices - %q", name)
return
}
nameMap[name] = true
columns[it.numCols+i] = col
}
}
it.noNames = noNames
it.columns = columns
}
func (it *rowIterator) Next() bool {
it.once.Do(it.init)
if it.err != nil || it.rowNum >= it.frame.Len() {
return false
}
it.row, it.err = it.getRow()
if it.err != nil {
return false
}
it.rowNum++
return true
}
func (it *rowIterator) Err() error {
return it.err
}
func (it *rowIterator) Row() map[string]interface{} {
if it.err != nil {
return nil
}
return it.row
}
func (it *rowIterator) Indices() map[string]interface{} {
return nil // TODO
}
func (it *rowIterator) RowNum() int {
return it.rowNum - 1
}
func (it *rowIterator) getRow() (map[string]interface{}, error) {
row := make(map[string]interface{})
for colNum, col := range it.columns {
var value interface{}
var err error
switch col.DType() {
case IntType:
value, err = col.IntAt(it.rowNum)
case FloatType:
value, err = col.FloatAt(it.rowNum)
case StringType:
value, err = col.StringAt(it.rowNum)
case TimeType:
value, err = col.TimeAt(it.rowNum)
case BoolType:
value, err = col.BoolAt(it.rowNum)
default:
err = fmt.Errorf("%s:%d - unknown dtype - %d", col.Name(), it.rowNum, col.DType())
}
if err != nil {
return nil, err
}
name := col.Name()
if name == "" {
name = it.noNames[colNum]
}
row[name] = value
}
return row, nil
}