-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitem_buffer.go
77 lines (64 loc) · 1.9 KB
/
item_buffer.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
package dynamoql
import (
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
// ItemBuffer efficiently hold and interact with collections of Amazon DynamoDB items.
type ItemBuffer struct {
buf []map[string]types.AttributeValue
}
// NewItemBuffer allocates a new ItemBuffer with the given size.
func NewItemBuffer(size int) *ItemBuffer {
return &ItemBuffer{
buf: make([]map[string]types.AttributeValue, 0, size),
}
}
// Cap retrieves internal buffer capacity.
func (b *ItemBuffer) Cap() int {
return cap(b.buf)
}
// Len retrieves internal buffer length.
func (b *ItemBuffer) Len() int {
return len(b.buf)
}
// IsFull checks if the internal buffer is full.
func (b *ItemBuffer) IsFull() bool {
return len(b.buf)-cap(b.buf) == 0
}
// Grow increases the memory allocation (malloc) of the internal buffer.
func (b *ItemBuffer) Grow(n int) {
growFactor := len(b.buf)*2 + n
newBuf := make([]map[string]types.AttributeValue, b.Len(), growFactor)
copy(newBuf, b.buf)
b.buf = nil
b.buf = newBuf
}
// Reset removes all items from the internal buffer, retaining the original capacity.
func (b *ItemBuffer) Reset() {
b.buf = b.buf[:0]
}
// Write stores the given value into the buffer.
func (b *ItemBuffer) Write(v map[string]types.AttributeValue) {
b.buf = append(b.buf, v)
}
// WriteItems stores the given set of values into the buffer.
func (b *ItemBuffer) WriteItems(v []map[string]types.AttributeValue) {
b.buf = append(b.buf, v...)
}
// Items retrieves the current collection of items.
func (b *ItemBuffer) Items() []map[string]types.AttributeValue {
return b.buf
}
// ItemAt retrieves a specific item using its position.
func (b *ItemBuffer) ItemAt(n int) map[string]types.AttributeValue {
if n >= len(b.buf) {
return nil
}
return b.buf[n]
}
// PeekAt checks if an item is present on the given position.
func (b *ItemBuffer) PeekAt(n int) bool {
if n >= len(b.buf) {
return false
}
return b.buf[n] != nil
}