|
17 | 17 | package array |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "sync/atomic" |
| 21 | + |
20 | 22 | "github.com/apache/arrow/go/arrow" |
| 23 | + "github.com/apache/arrow/go/arrow/internal/bitutil" |
| 24 | + "github.com/apache/arrow/go/arrow/internal/debug" |
| 25 | + "github.com/apache/arrow/go/arrow/memory" |
21 | 26 | ) |
22 | 27 |
|
23 | 28 | // List represents an immutable sequence of array values. |
@@ -56,6 +61,151 @@ func (a *List) Release() { |
56 | 61 | a.values.Release() |
57 | 62 | } |
58 | 63 |
|
| 64 | +type ListBuilder struct { |
| 65 | + builder |
| 66 | + |
| 67 | + etype arrow.DataType // data type of the list's elements. |
| 68 | + values Builder // value builder for the list's elements. |
| 69 | + offsets *Int32Builder |
| 70 | +} |
| 71 | + |
| 72 | +// NewListBuilder returns a builder, using the provided memory allocator. |
| 73 | +// The created list builder will create a list whose elements will be of type etype. |
| 74 | +func NewListBuilder(mem memory.Allocator, etype arrow.DataType) *ListBuilder { |
| 75 | + return &ListBuilder{ |
| 76 | + builder: builder{refCount: 1, mem: mem}, |
| 77 | + etype: etype, |
| 78 | + values: newBuilder(mem, etype), |
| 79 | + offsets: NewInt32Builder(mem), |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// Release decreases the reference count by 1. |
| 84 | +// When the reference count goes to zero, the memory is freed. |
| 85 | +func (b *ListBuilder) Release() { |
| 86 | + debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases") |
| 87 | + |
| 88 | + if atomic.AddInt64(&b.refCount, -1) == 0 { |
| 89 | + if b.nullBitmap != nil { |
| 90 | + b.nullBitmap.Release() |
| 91 | + b.nullBitmap = nil |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + b.values.Release() |
| 96 | + b.offsets.Release() |
| 97 | +} |
| 98 | + |
| 99 | +func (b *ListBuilder) appendNextOffset() { |
| 100 | + b.offsets.Append(int32(b.values.Len())) |
| 101 | +} |
| 102 | + |
| 103 | +func (b *ListBuilder) Append(v bool) { |
| 104 | + b.Reserve(1) |
| 105 | + b.unsafeAppendBoolToBitmap(v) |
| 106 | + b.appendNextOffset() |
| 107 | +} |
| 108 | + |
| 109 | +func (b *ListBuilder) AppendNull() { |
| 110 | + b.Reserve(1) |
| 111 | + b.unsafeAppendBoolToBitmap(false) |
| 112 | + b.appendNextOffset() |
| 113 | +} |
| 114 | + |
| 115 | +func (b *ListBuilder) AppendValues(offsets []int32, valid []bool) { |
| 116 | + b.Reserve(len(valid)) |
| 117 | + b.offsets.AppendValues(offsets, nil) |
| 118 | + b.builder.unsafeAppendBoolsToBitmap(valid, len(valid)) |
| 119 | +} |
| 120 | + |
| 121 | +func (b *ListBuilder) unsafeAppend(v bool) { |
| 122 | + bitutil.SetBit(b.nullBitmap.Bytes(), b.length) |
| 123 | + b.length++ |
| 124 | +} |
| 125 | + |
| 126 | +func (b *ListBuilder) unsafeAppendBoolToBitmap(isValid bool) { |
| 127 | + if isValid { |
| 128 | + bitutil.SetBit(b.nullBitmap.Bytes(), b.length) |
| 129 | + } else { |
| 130 | + b.nulls++ |
| 131 | + } |
| 132 | + b.length++ |
| 133 | +} |
| 134 | + |
| 135 | +func (b *ListBuilder) init(capacity int) { |
| 136 | + b.builder.init(capacity) |
| 137 | + b.offsets.init(capacity + 1) |
| 138 | +} |
| 139 | + |
| 140 | +// Reserve ensures there is enough space for appending n elements |
| 141 | +// by checking the capacity and calling Resize if necessary. |
| 142 | +func (b *ListBuilder) Reserve(n int) { |
| 143 | + b.builder.reserve(n, b.Resize) |
| 144 | +} |
| 145 | + |
| 146 | +// Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(), |
| 147 | +// additional memory will be allocated. If n is smaller, the allocated memory may reduced. |
| 148 | +func (b *ListBuilder) Resize(n int) { |
| 149 | + if n < minBuilderCapacity { |
| 150 | + n = minBuilderCapacity |
| 151 | + } |
| 152 | + |
| 153 | + if b.capacity == 0 { |
| 154 | + b.init(n) |
| 155 | + } else { |
| 156 | + b.builder.resize(n, b.builder.init) |
| 157 | + b.offsets.resize(n+1, b.offsets.init) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func (b *ListBuilder) ValueBuilder() Builder { |
| 162 | + return b.values |
| 163 | +} |
| 164 | + |
| 165 | +// NewArray creates a List array from the memory buffers used by the builder and resets the ListBuilder |
| 166 | +// so it can be used to build a new array. |
| 167 | +func (b *ListBuilder) NewArray() Interface { |
| 168 | + return b.NewListArray() |
| 169 | +} |
| 170 | + |
| 171 | +// NewListArray creates a List array from the memory buffers used by the builder and resets the ListBuilder |
| 172 | +// so it can be used to build a new array. |
| 173 | +func (b *ListBuilder) NewListArray() (a *List) { |
| 174 | + if b.offsets.Len() != b.length+1 { |
| 175 | + b.appendNextOffset() |
| 176 | + } |
| 177 | + data := b.newData() |
| 178 | + a = NewListData(data) |
| 179 | + data.Release() |
| 180 | + return |
| 181 | +} |
| 182 | + |
| 183 | +func (b *ListBuilder) newData() (data *Data) { |
| 184 | + values := b.values.NewArray() |
| 185 | + defer values.Release() |
| 186 | + |
| 187 | + var offsets *memory.Buffer |
| 188 | + if b.offsets != nil { |
| 189 | + arr := b.offsets.NewInt32Array() |
| 190 | + defer arr.Release() |
| 191 | + offsets = arr.Data().buffers[1] |
| 192 | + } |
| 193 | + |
| 194 | + data = NewData( |
| 195 | + arrow.ListOf(b.etype), b.length, |
| 196 | + []*memory.Buffer{ |
| 197 | + b.nullBitmap, |
| 198 | + offsets, |
| 199 | + }, |
| 200 | + []*Data{values.Data()}, |
| 201 | + b.nulls, |
| 202 | + ) |
| 203 | + b.reset() |
| 204 | + |
| 205 | + return |
| 206 | +} |
| 207 | + |
59 | 208 | var ( |
60 | 209 | _ Interface = (*List)(nil) |
| 210 | + _ Builder = (*ListBuilder)(nil) |
61 | 211 | ) |
0 commit comments