1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
25 "github.com/apache/arrow/go/v6/arrow"
26 "github.com/apache/arrow/go/v6/arrow/internal/debug"
27 "github.com/apache/arrow/go/v6/arrow/memory"
30 // Data represents the memory and metadata of an Arrow array.
37 buffers []*memory.Buffer // TODO(sgc): should this be an interface?
38 childData []*Data // TODO(sgc): managed by ListArray, StructArray and UnionArray types
41 // NewData creates a new Data.
42 func NewData(dtype arrow.DataType, length int, buffers []*memory.Buffer, childData []*Data, nulls, offset int) *Data {
43 for _, b := range buffers {
49 for _, child := range childData {
66 // Reset sets the Data for re-use.
67 func (d *Data) Reset(dtype arrow.DataType, length int, buffers []*memory.Buffer, childData []*Data, nulls, offset int) {
68 // Retain new buffers before releasing existing buffers in-case they're the same ones to prevent accidental premature
70 for _, b := range buffers {
75 for _, b := range d.buffers {
82 // Retain new children data before releasing existing children data in-case they're the same ones to prevent accidental
84 for _, d := range childData {
89 for _, d := range d.childData {
94 d.childData = childData
102 // Retain increases the reference count by 1.
103 // Retain may be called simultaneously from multiple goroutines.
104 func (d *Data) Retain() {
105 atomic.AddInt64(&d.refCount, 1)
108 // Release decreases the reference count by 1.
109 // When the reference count goes to zero, the memory is freed.
110 // Release may be called simultaneously from multiple goroutines.
111 func (d *Data) Release() {
112 debug.Assert(atomic.LoadInt64(&d.refCount) > 0, "too many releases")
114 if atomic.AddInt64(&d.refCount, -1) == 0 {
115 for _, b := range d.buffers {
121 for _, b := range d.childData {
124 d.buffers, d.childData = nil, nil
128 // DataType returns the DataType of the data.
129 func (d *Data) DataType() arrow.DataType { return d.dtype }
131 // NullN returns the number of nulls.
132 func (d *Data) NullN() int { return d.nulls }
134 // Len returns the length.
135 func (d *Data) Len() int { return d.length }
137 // Offset returns the offset.
138 func (d *Data) Offset() int { return d.offset }
140 // Buffers returns the buffers.
141 func (d *Data) Buffers() []*memory.Buffer { return d.buffers }
143 // NewSliceData returns a new slice that shares backing data with the input.
144 // The returned Data slice starts at i and extends j-i elements, such as:
145 // slice := data[i:j]
146 // The returned value must be Release'd after use.
148 // NewSliceData panics if the slice is outside the valid range of the input Data.
149 // NewSliceData panics if j < i.
150 func NewSliceData(data *Data, i, j int64) *Data {
151 if j > int64(data.length) || i > j || data.offset+int(i) > data.offset+data.length {
152 panic("arrow/array: index out of range")
155 for _, b := range data.buffers {
161 for _, child := range data.childData {
170 nulls: UnknownNullCount,
172 offset: data.offset + int(i),
173 buffers: data.buffers,
174 childData: data.childData,
184 func Hash(h *maphash.Hash, a *Data) {
185 h.Write((*[bits.UintSize / 8]byte)(unsafe.Pointer(&a.length))[:])
186 h.Write((*[bits.UintSize / 8]byte)(unsafe.Pointer(&a.length))[:])
187 if len(a.buffers) > 0 && a.buffers[0] != nil {
188 h.Write(a.buffers[0].Bytes())
190 for _, c := range a.childData {