|
| 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 |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 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. |
| 16 | + |
| 17 | +package array |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "math" |
| 22 | + "strings" |
| 23 | + "unsafe" |
| 24 | + |
| 25 | + "github.com/apache/arrow/go/arrow" |
| 26 | + "github.com/apache/arrow/go/arrow/memory" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + stringArrayMaximumCapacity = math.MaxInt32 |
| 31 | +) |
| 32 | + |
| 33 | +// A type which represents an immutable sequence of variable-length UTF-8 strings. |
| 34 | +type String struct { |
| 35 | + array |
| 36 | + offsets []int32 |
| 37 | + values string |
| 38 | +} |
| 39 | + |
| 40 | +// NewStringData constructs a new String array from data. |
| 41 | +func NewStringData(data *Data) *String { |
| 42 | + a := &String{} |
| 43 | + a.refCount = 1 |
| 44 | + a.setData(data) |
| 45 | + return a |
| 46 | +} |
| 47 | + |
| 48 | +// Value returns the slice at index i. This value should not be mutated. |
| 49 | +func (a *String) Value(i int) string { return a.values[a.offsets[i]:a.offsets[i+1]] } |
| 50 | + |
| 51 | +func (a *String) String() string { |
| 52 | + o := new(strings.Builder) |
| 53 | + o.WriteString("[") |
| 54 | + for i := 0; i < a.Len(); i++ { |
| 55 | + if i > 0 { |
| 56 | + o.WriteString(" ") |
| 57 | + } |
| 58 | + switch { |
| 59 | + case a.IsNull(i): |
| 60 | + o.WriteString("(null)") |
| 61 | + default: |
| 62 | + fmt.Fprintf(o, "%q", a.Value(i)) |
| 63 | + } |
| 64 | + } |
| 65 | + o.WriteString("]") |
| 66 | + return o.String() |
| 67 | +} |
| 68 | + |
| 69 | +func (a *String) setData(data *Data) { |
| 70 | + if len(data.buffers) != 3 { |
| 71 | + panic("arrow/array: len(data.buffers) != 3") |
| 72 | + } |
| 73 | + |
| 74 | + a.array.setData(data) |
| 75 | + |
| 76 | + if vdata := data.buffers[2]; vdata != nil { |
| 77 | + b := vdata.Bytes() |
| 78 | + a.values = *(*string)(unsafe.Pointer(&b)) |
| 79 | + } |
| 80 | + |
| 81 | + if offsets := data.buffers[1]; offsets != nil { |
| 82 | + a.offsets = arrow.Int32Traits.CastFromBytes(offsets.Bytes()) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// A StringBuilder is used to build a String array using the Append methods. |
| 87 | +type StringBuilder struct { |
| 88 | + builder *BinaryBuilder |
| 89 | +} |
| 90 | + |
| 91 | +func NewStringBuilder(mem memory.Allocator) *StringBuilder { |
| 92 | + b := &StringBuilder{ |
| 93 | + builder: NewBinaryBuilder(mem, arrow.BinaryTypes.String), |
| 94 | + } |
| 95 | + return b |
| 96 | +} |
| 97 | + |
| 98 | +// Release decreases the reference count by 1. |
| 99 | +// When the reference count goes to zero, the memory is freed. |
| 100 | +// Release may be called simultaneously from multiple goroutines. |
| 101 | +func (b *StringBuilder) Release() { |
| 102 | + b.builder.Release() |
| 103 | +} |
| 104 | + |
| 105 | +// Retain increases the reference count by 1. |
| 106 | +// Retain may be called simultaneously from multiple goroutines. |
| 107 | +func (b *StringBuilder) Retain() { |
| 108 | + b.builder.Retain() |
| 109 | +} |
| 110 | + |
| 111 | +// |
| 112 | +// Len returns the number of elements in the array builder. |
| 113 | +func (b *StringBuilder) Len() int { return b.builder.Len() } |
| 114 | + |
| 115 | +// Cap returns the total number of elements that can be stored without allocating additional memory. |
| 116 | +func (b *StringBuilder) Cap() int { return b.builder.Cap() } |
| 117 | + |
| 118 | +// NullN returns the number of null values in the array builder. |
| 119 | +func (b *StringBuilder) NullN() int { return b.builder.NullN() } |
| 120 | + |
| 121 | +func (b *StringBuilder) Append(v string) { |
| 122 | + b.builder.Append([]byte(v)) |
| 123 | +} |
| 124 | + |
| 125 | +func (b *StringBuilder) AppendNull() { |
| 126 | + b.builder.AppendNull() |
| 127 | +} |
| 128 | + |
| 129 | +// AppendValues will append the values in the v slice. The valid slice determines which values |
| 130 | +// in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty, |
| 131 | +// all values in v are appended and considered valid. |
| 132 | +func (b *StringBuilder) AppendValues(v []string, valid []bool) { |
| 133 | + b.builder.AppendStringValues(v, valid) |
| 134 | +} |
| 135 | + |
| 136 | +func (b *StringBuilder) Value(i int) string { |
| 137 | + return string(b.builder.Value(i)) |
| 138 | +} |
| 139 | + |
| 140 | +func (b *StringBuilder) init(capacity int) { |
| 141 | + b.builder.init(capacity) |
| 142 | +} |
| 143 | + |
| 144 | +func (b *StringBuilder) resize(newBits int, init func(int)) { |
| 145 | + b.builder.resize(newBits, init) |
| 146 | +} |
| 147 | + |
| 148 | +// Reserve ensures there is enough space for appending n elements |
| 149 | +// by checking the capacity and calling Resize if necessary. |
| 150 | +func (b *StringBuilder) Reserve(n int) { |
| 151 | + b.builder.Reserve(n) |
| 152 | +} |
| 153 | + |
| 154 | +// Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(), |
| 155 | +// additional memory will be allocated. If n is smaller, the allocated memory may reduced. |
| 156 | +func (b *StringBuilder) Resize(n int) { |
| 157 | + b.builder.Resize(n) |
| 158 | +} |
| 159 | + |
| 160 | +// NewArray creates a String array from the memory buffers used by the builder and resets the StringBuilder |
| 161 | +// so it can be used to build a new array. |
| 162 | +func (b *StringBuilder) NewArray() Interface { |
| 163 | + return b.NewStringArray() |
| 164 | +} |
| 165 | + |
| 166 | +// NewStringArray creates a String array from the memory buffers used by the builder and resets the StringBuilder |
| 167 | +// so it can be used to build a new array. |
| 168 | +func (b *StringBuilder) NewStringArray() (a *String) { |
| 169 | + data := b.builder.newData() |
| 170 | + a = NewStringData(data) |
| 171 | + data.Release() |
| 172 | + return |
| 173 | +} |
| 174 | + |
| 175 | +var ( |
| 176 | + _ Interface = (*String)(nil) |
| 177 | + _ Builder = (*StringBuilder)(nil) |
| 178 | +) |
0 commit comments