This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
frame.go
372 lines (325 loc) · 9.88 KB
/
frame.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
Copyright 2014 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
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.
*/
package godata
import (
"bytes"
"fmt"
"github.com/google/btree"
"github.com/google/godata/group"
"github.com/google/godata/row"
)
// Frame represents multiple rows and multiple columns of data.
type Frame struct {
bt *btree.BTree
indexer row.Indexer
}
// NewFrame returns a Frame for the given indexer.
func NewFrame(indexer row.Indexer) *Frame {
return &Frame{
bt: btree.New(2),
indexer: indexer,
}
}
// Put inserts the data into the frame, replacing and returning the existing
// data if an entry already exists. Returns error if the data cannot be
// indexed.
func (f *Frame) Put(data row.Data) (row.Data, error) {
index, err := f.indexer.Index(data)
if err != nil {
return nil, fmt.Errorf("AddRow: %v", err)
}
got := f.bt.ReplaceOrInsert(row.Row{
Index: index,
Data: data,
})
if got == nil {
return nil, nil
}
return got.(row.Row).Data, nil
}
// Get returns the data for the given key. Returns error if the given key is
// invalid. Returns nil if there is no data for the given key.
func (f *Frame) Get(key row.Data) (row.Data, error) {
index, err := f.indexer.Index(key)
if err != nil {
return nil, err
}
got := f.bt.Get(index)
if got == nil {
return nil, nil
}
return got.(row.Row).Data, nil
}
// Pop returns the data for the given key and deletes it from the Frame.
// Returns error if the given key is invalid. Returns nil if there is no data
// for the given key.
func (f *Frame) Pop(key row.Data) (row.Data, error) {
index, err := f.indexer.Index(key)
if err != nil {
return nil, err
}
got := f.bt.Delete(index)
if got == nil {
return nil, nil
}
return got.(row.Row).Data, nil
}
// rangeOptions represents a begin and end point for range functions.
type rangeOptions struct {
greaterOrEqual row.Data
lessThan row.Data
}
// rangeArg mutate a rangeOptions based on a given argument.
type rangeArg func(*rangeOptions)
// rangeArgsToOptions converts the given rangeArgs into an options struct.
func rangeArgsToOptions(args []rangeArg) *rangeOptions {
var opts rangeOptions
for _, a := range args {
a(&opts)
}
return &opts
}
// GreaterOrEqual returns a range option that filters on rows greater than or
// equal to the given value.
func GreaterOrEqual(val row.Data) rangeArg {
return func(opts *rangeOptions) {
opts.greaterOrEqual = val
}
}
// LessThan returns a range option that filters on rows less than or equal to
// the given value.
func LessThan(val row.Data) rangeArg {
return func(opts *rangeOptions) {
opts.lessThan = val
}
}
// RowAction performs an operation on the given row and optionally returns a
// value. RowAction must not mutate the Data.
type RowAction func(row.Data) (interface{}, error)
// rowAction performs an operation on the given Row. Actions may mutate the
// data, but must also re-insert the row into the btree to maintain
// synchronization of the index.
type rowAction func(row.Row) (interface{}, error)
// forRange performs an action for a given key range and returns the array of
// results, one for each row.
func (f *Frame) forRange(opts *rangeOptions, action rowAction) ([]interface{}, error) {
var (
returnError error
returnValues []interface{}
)
iterator := func(item btree.Item) bool {
val, err := action(item.(row.Row))
if err != nil {
returnError = err
return false
}
returnValues = append(returnValues, val)
return true
}
if opts.lessThan == nil && opts.greaterOrEqual == nil {
f.bt.Ascend(iterator)
} else if opts.lessThan == nil {
pivot, err := f.indexer.Index(opts.greaterOrEqual)
if err != nil {
return nil, err
}
f.bt.AscendGreaterOrEqual(pivot, iterator)
} else if opts.greaterOrEqual == nil {
pivot, err := f.indexer.Index(opts.lessThan)
if err != nil {
return nil, err
}
f.bt.AscendLessThan(pivot, iterator)
} else {
begin, err := f.indexer.Index(opts.greaterOrEqual)
if err != nil {
return nil, err
}
end, err := f.indexer.Index(opts.lessThan)
if err != nil {
return nil, err
}
f.bt.AscendRange(begin, end, iterator)
}
return returnValues, returnError
}
// String returns the string representation of the Frame.
//
// TODO: The output is ugly and potentially very long. Refactor the forRange to
// take a limit, and then print out a partial representation of the Frame if
// there are too many rows.
func (f *Frame) String() string {
rows, err := f.GetRange()
if err != nil {
return fmt.Sprintf("Frame.String: %v", err)
}
var buf bytes.Buffer
for i, r := range rows {
buf.WriteString(fmt.Sprintf("%d: %v\n", i, r))
}
return buf.String()
}
// GetRange returns a list of all values in the given range. See GreaterOrEqual
// and LessThan. If no range is given, then this function returns all rows. If
// only a begin range is given, then this function returns all rows beginning
// with the given value. If only an end range is given, then this function
// returns all rows up to the given value.
func (f *Frame) GetRange(args ...rangeArg) ([]row.Data, error) {
opts := rangeArgsToOptions(args)
rows, err := f.forRange(opts, func(row row.Row) (interface{}, error) {
return row.Data, nil
})
if err != nil {
return nil, err
}
var castRows []row.Data
for _, r := range rows {
castRows = append(castRows, r.(row.Data))
}
return castRows, nil
}
// PopRange returns a list of all values in the given range and deletes them
// from the Frame. See GetRange for details on the arguments.
func (f *Frame) PopRange(args ...rangeArg) ([]row.Data, error) {
opts := rangeArgsToOptions(args)
rows, err := f.forRange(opts, func(row row.Row) (interface{}, error) {
return row, nil
})
if err != nil {
return nil, err
}
var (
indices []row.Index
data []row.Data
)
for _, r := range rows {
indices = append(indices, r.(row.Row).Index)
data = append(data, r.(row.Row).Data)
}
for _, i := range indices {
f.bt.Delete(i)
}
return data, nil
}
// Joined returns a new Frame object that contains the joined contents of the
// two frames. The indices of the frames must be compatible. The resulting
// Frame contains one row for each key in the union of keys for the left and
// right frames. The Data contains a JoinResult for each column of data,
// where Left is populated with the left side contents, and Right is populated
// with the right side contents. Left and Right are nil if they don't exist in
// the left and right sides.
func (f *Frame) Joined(frame *Frame) (*Frame, error) {
fr := NewFrame(JoinResultIndexer{f.indexer})
// Add all left rows.
all, err := f.GetRange()
if err != nil {
return nil, err
}
for _, row := range all {
// Convert each column to a JoinResult.
projected := make(map[string]interface{})
for col, val := range row {
projected[col] = &JoinResult{Left: val}
}
_, err := fr.Put(projected)
if err != nil {
return nil, err
}
}
// Add all right rows.
all, err = frame.GetRange()
if err != nil {
return nil, err
}
for _, row := range all {
projected, err := fr.Get(row)
if err != nil {
return nil, err
}
if projected == nil {
projected = make(map[string]interface{})
}
// Add Right to each column JoinResult, or add a JoinResult if the column doesn't exist yet.
for col, val := range row {
joinResult, ok := projected[col]
if ok {
jd, ok := joinResult.(*JoinResult)
if !ok {
return nil, fmt.Errorf("Frame: column %q in %v is not a JoinResult", col, joinResult)
}
jd.Right = val
} else {
projected[col] = &JoinResult{Right: val}
}
}
fr.Put(projected)
if err != nil {
return nil, err
}
}
return fr, nil
}
// WithIndexer returns a new Frame object with the same underlying data indexed
// by a new indexer. Returns error if the data cannot be indexed by the new
// indexer. Note that mutating rows in the returned Frame will also mutate the
// rows in the existing Frame. However, adding to or deleting rows from the
// returned Frame will not add to and delete from the existing Frame.
// WithIndexer assumes that the given Indexer gives each existing row a unique
// Index. If rows share an index, then one of the rows will be dropped. The
// dropped row is not defined by the API, and is subject to change.
func (f *Frame) WithIndexer(indexer row.Indexer) (*Frame, error) {
var returnErr error
nf := NewFrame(indexer)
iter := func(item btree.Item) bool {
_, err := nf.Put(item.(row.Row).Data)
if err != nil {
returnErr = err
return false
}
return true
}
f.bt.Ascend(iter)
return nf, returnErr
}
// GroupBy returns a new Frame object containing rows grouped by the given
// indexer. Each index corresponds to a row containing a single column
// group.Column, which is a slice of rows with that index.
func (f *Frame) GroupBy(indexer row.Indexer) (*Frame, error) {
var returnErr error
nf := NewFrame(group.Indexer{indexer})
iter := func(item btree.Item) bool {
r := item.(row.Row)
index, err := indexer.Index(r.Data)
if err != nil {
returnErr = err
return false
}
existing := nf.bt.Get(index)
var existingRow row.Row
if existing != nil {
existingRow = existing.(row.Row)
} else {
existingRow = row.Row{
Index: index,
Data: map[string]interface{}{group.Column: group.Group(nil)},
}
}
// Conversion errors imply the group is nil.
existingGroup, _ := existingRow.Data[group.Column].(group.Group)
existingRow.Data[group.Column] = append(existingGroup, r.Data)
nf.bt.ReplaceOrInsert(existingRow)
return true
}
f.bt.Ascend(iter)
return nf, returnErr
}