]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/go/arrow/array/bufferbuilder.go
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / go / arrow / array / bufferbuilder.go
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 "sync/atomic"
21
22 "github.com/apache/arrow/go/v6/arrow/bitutil"
23 "github.com/apache/arrow/go/v6/arrow/internal/debug"
24 "github.com/apache/arrow/go/v6/arrow/memory"
25 )
26
27 // A bufferBuilder provides common functionality for populating memory with a sequence of type-specific values.
28 // Specialized implementations provide type-safe APIs for appending and accessing the memory.
29 type bufferBuilder struct {
30 refCount int64
31 mem memory.Allocator
32 buffer *memory.Buffer
33 length int
34 capacity int
35
36 bytes []byte
37 }
38
39 // Retain increases the reference count by 1.
40 // Retain may be called simultaneously from multiple goroutines.
41 func (b *bufferBuilder) Retain() {
42 atomic.AddInt64(&b.refCount, 1)
43 }
44
45 // Release decreases the reference count by 1.
46 // When the reference count goes to zero, the memory is freed.
47 // Release may be called simultaneously from multiple goroutines.
48 func (b *bufferBuilder) Release() {
49 debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases")
50
51 if atomic.AddInt64(&b.refCount, -1) == 0 {
52 if b.buffer != nil {
53 b.buffer.Release()
54 b.buffer, b.bytes = nil, nil
55 }
56 }
57 }
58
59 // Len returns the length of the memory buffer in bytes.
60 func (b *bufferBuilder) Len() int { return b.length }
61
62 // Cap returns the total number of bytes that can be stored without allocating additional memory.
63 func (b *bufferBuilder) Cap() int { return b.capacity }
64
65 // Bytes returns a slice of length b.Len().
66 // The slice is only valid for use until the next buffer modification. That is, until the next call
67 // to Advance, Reset, Finish or any Append function. The slice aliases the buffer content at least until the next
68 // buffer modification.
69 func (b *bufferBuilder) Bytes() []byte { return b.bytes[:b.length] }
70
71 func (b *bufferBuilder) resize(elements int) {
72 if b.buffer == nil {
73 b.buffer = memory.NewResizableBuffer(b.mem)
74 }
75
76 b.buffer.Resize(elements)
77 oldCapacity := b.capacity
78 b.capacity = b.buffer.Cap()
79 b.bytes = b.buffer.Buf()
80
81 if b.capacity > oldCapacity {
82 memory.Set(b.bytes[oldCapacity:], 0)
83 }
84 }
85
86 // Advance increases the buffer by length and initializes the skipped bytes to zero.
87 func (b *bufferBuilder) Advance(length int) {
88 if b.capacity < b.length+length {
89 newCapacity := bitutil.NextPowerOf2(b.length + length)
90 b.resize(newCapacity)
91 }
92 b.length += length
93 }
94
95 // Append appends the contents of v to the buffer, resizing it if necessary.
96 func (b *bufferBuilder) Append(v []byte) {
97 if b.capacity < b.length+len(v) {
98 newCapacity := bitutil.NextPowerOf2(b.length + len(v))
99 b.resize(newCapacity)
100 }
101 b.unsafeAppend(v)
102 }
103
104 // Reset returns the buffer to an empty state. Reset releases the memory and sets the length and capacity to zero.
105 func (b *bufferBuilder) Reset() {
106 if b.buffer != nil {
107 b.buffer.Release()
108 }
109 b.buffer, b.bytes = nil, nil
110 b.capacity, b.length = 0, 0
111 }
112
113 // Finish TODO(sgc)
114 func (b *bufferBuilder) Finish() (buffer *memory.Buffer) {
115 if b.length > 0 {
116 b.buffer.ResizeNoShrink(b.length)
117 }
118 buffer = b.buffer
119 b.buffer = nil
120 b.Reset()
121 return
122 }
123
124 func (b *bufferBuilder) unsafeAppend(data []byte) {
125 copy(b.bytes[b.length:], data)
126 b.length += len(data)
127 }