-
Notifications
You must be signed in to change notification settings - Fork 35
/
reader.go
242 lines (199 loc) · 5.71 KB
/
reader.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
// Copyright ©2019 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rarrow // import "go-hep.org/x/hep/groot/rarrow"
import (
"fmt"
"sync/atomic"
"github.com/apache/arrow/go/arrow"
"github.com/apache/arrow/go/arrow/array"
"github.com/apache/arrow/go/arrow/memory"
"go-hep.org/x/hep/groot/rtree"
)
// Record is an in-memory Arrow Record backed by a ROOT Tree.
type Record struct {
refs int64
mem memory.Allocator
tree rtree.Tree
schema *arrow.Schema
nrows int64
ncols int64
offset int64 // entries offset
cols []array.Interface
}
// NewRecord creates a new in-memory Arrow Record from the provided ROOT Tree.
func NewRecord(t rtree.Tree, opts ...Option) *Record {
cfg := newConfig(opts)
if cfg.end < 0 {
cfg.end = t.Entries()
}
if cfg.beg <= 0 {
cfg.beg = 0
}
if cfg.beg > cfg.end {
panic("rarrow: invalid entry slice")
}
rec := &Record{
mem: cfg.mem,
tree: t,
refs: 1,
schema: SchemaFrom(t),
offset: cfg.beg,
nrows: cfg.end - cfg.beg,
ncols: int64(len(t.Branches())),
cols: make([]array.Interface, len(t.Branches())),
}
rec.load(cfg.beg, cfg.end)
return rec
}
func (rec *Record) load(beg, end int64) {
var (
rvars = rtree.NewReadVars(rec.tree)
r, err = rtree.NewReader(rec.tree, rvars, rtree.WithRange(beg, end))
)
if err != nil {
panic(fmt.Errorf("could not create reader from read-vars %#v: %+v", rvars, err))
}
defer r.Close()
blds := make([]array.Builder, rec.ncols)
for i, field := range rec.schema.Fields() {
blds[i] = builderFrom(rec.mem, field.Type, rec.nrows)
defer blds[i].Release()
}
err = r.Read(func(ctx rtree.RCtx) error {
for i, field := range rec.schema.Fields() {
appendData(blds[i], rvars[i], field.Type)
}
return nil
})
if err != nil {
panic(fmt.Errorf("could not read tree: %+v", err))
}
for i, bldr := range blds {
rec.cols[i] = bldr.NewArray()
}
}
// Retain increases the reference count by 1.
// Retain may be called simultaneously from multiple goroutines.
func (rec *Record) Retain() {
atomic.AddInt64(&rec.refs, 1)
}
// Release decreases the reference count by 1.
// When the reference count goes to zero, the memory is freed.
// Release may be called simultaneously from multiple goroutines.
func (rec *Record) Release() {
if atomic.LoadInt64(&rec.refs) <= 0 {
panic("groot/rarrow: too many releases")
}
if atomic.AddInt64(&rec.refs, -1) == 0 {
for i := range rec.cols {
rec.cols[i].Release()
}
rec.cols = nil
}
}
func (rec *Record) Schema() *arrow.Schema { return rec.schema }
func (rec *Record) NumRows() int64 { return rec.nrows }
func (rec *Record) NumCols() int64 { return rec.ncols }
func (rec *Record) Columns() []array.Interface { return rec.cols }
func (rec *Record) Column(i int) array.Interface { return rec.cols[i] }
func (rec *Record) ColumnName(i int) string { return rec.schema.Field(i).Name }
// NewSlice constructs a zero-copy slice of the record with the indicated
// indices i and j, corresponding to array[i:j].
// The returned record must be Release()'d after use.
//
// NewSlice panics if the slice is outside the valid range of the record array.
// NewSlice panics if j < i.
func (rec *Record) NewSlice(i, j int64) array.Record {
return NewRecord(rec.tree, WithStart(rec.offset+i), WithEnd(rec.offset+j))
}
// RecordReader is an ARROW RecordReader for ROOT Trees.
//
// RecordReader does not materialize more than one record at a time.
// The number of rows (or entries, in ROOT speak) that record loads can be configured
// at creation time with the WithChunk function.
// The default is one entry per record.
// One can pass -1 to WithChunk to create a record with all entries of the Tree or Chain.
type RecordReader struct {
refs int64
mem memory.Allocator
schema *arrow.Schema
tree rtree.Tree
beg int64 // first entry to read
end int64 // last entry to read
cur int64 // current entry
chunk int64 // number of entries to read for each record
rec *Record
}
// NewRecordReader creates a new ARROW RecordReader from the provided ROOT Tree.
func NewRecordReader(tree rtree.Tree, opts ...Option) *RecordReader {
cfg := newConfig(opts)
r := &RecordReader{
refs: 1,
mem: cfg.mem,
schema: SchemaFrom(tree),
tree: tree,
beg: cfg.beg,
end: cfg.end,
chunk: cfg.chunks,
}
if r.beg <= 0 {
r.beg = 0
}
if r.end <= 0 {
r.end = tree.Entries()
}
switch {
case r.chunk == 0:
r.chunk = 1
case r.chunk < 0:
r.chunk = tree.Entries()
}
r.cur = r.beg
return r
}
// Retain increases the reference count by 1.
// Retain may be called simultaneously from multiple goroutines.
func (r *RecordReader) Retain() {
atomic.AddInt64(&r.refs, 1)
}
// Release decreases the reference count by 1.
// When the reference count goes to zero, the memory is freed.
// Release may be called simultaneously from multiple goroutines.
func (r *RecordReader) Release() {
if atomic.LoadInt64(&r.refs) <= 0 {
panic("groot/rarrow: too many releases")
}
if atomic.AddInt64(&r.refs, -1) == 0 {
if r.rec != nil {
r.rec.Release()
}
}
}
func (r *RecordReader) Schema() *arrow.Schema { return r.schema }
func (r *RecordReader) Record() array.Record { return r.rec }
func (r *RecordReader) Next() bool {
if r.cur >= r.end {
return false
}
if r.rec != nil {
r.rec.Release()
}
end := minI64(r.cur+r.chunk, r.end)
r.load(r.cur, end)
r.cur += r.chunk
return true
}
func (r *RecordReader) load(beg, end int64) {
r.rec = NewRecord(r.tree, WithStart(beg), WithEnd(end), WithAllocator(r.mem))
}
var (
_ array.Record = (*Record)(nil)
_ array.RecordReader = (*RecordReader)(nil)
)
func minI64(a, b int64) int64 {
if a < b {
return a
}
return b
}