forked from kelindar/column
/
column_generate.go
208 lines (175 loc) · 5.61 KB
/
column_generate.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//go:build ignore
// +build ignore
package column
import (
"fmt"
"github.com/kelindar/bitmap"
"github.com/kelindar/column/commit"
"github.com/kelindar/genny/generic"
)
// --------------------------- Numbers ----------------------------
type number = generic.Number
// numberColumn represents a generic column
type numberColumn struct {
fill bitmap.Bitmap // The fill-list
data []number // The actual values
}
// makeNumbers creates a new vector for Numbers
func makeNumbers() Column {
return &numberColumn{
fill: make(bitmap.Bitmap, 0, 4),
data: make([]number, 0, 64),
}
}
// Grow grows the size of the column until we have enough to store
func (c *numberColumn) Grow(idx uint32) {
if idx < uint32(len(c.data)) {
return
}
if idx < uint32(cap(c.data)) {
c.fill.Grow(idx)
c.data = c.data[:idx+1]
return
}
c.fill.Grow(idx)
clone := make([]number, idx+1, resize(cap(c.data), idx+1))
copy(clone, c.data)
c.data = clone
}
// Apply applies a set of operations to the column.
func (c *numberColumn) Apply(r *commit.Reader) {
for r.Next() {
switch r.Type {
case commit.Put:
c.fill[r.Offset>>6] |= 1 << (r.Offset & 0x3f)
c.data[r.Offset] = r.Number()
// If this is an atomic increment/decrement, we need to change the operation to
// the final value, since after this update an index needs to be recalculated.
case commit.Add:
c.fill[r.Offset>>6] |= 1 << (r.Offset & 0x3f)
value := c.data[r.Offset] + r.Number()
c.data[r.Offset] = value
r.SwapNumber(value)
case commit.Delete:
c.fill.Remove(r.Index())
}
}
}
// Contains checks whether the column has a value at a specified index.
func (c *numberColumn) Contains(idx uint32) bool {
return c.fill.Contains(idx)
}
// Index returns the fill list for the column
func (c *numberColumn) Index() *bitmap.Bitmap {
return &c.fill
}
// Value retrieves a value at a specified index
func (c *numberColumn) Value(idx uint32) (v interface{}, ok bool) {
v = number(0)
if idx < uint32(len(c.data)) && c.fill.Contains(idx) {
v, ok = c.data[idx], true
}
return
}
// load retrieves a number value at a specified index
func (c *numberColumn) load(idx uint32) (v number, ok bool) {
if idx < uint32(len(c.data)) && c.fill.Contains(idx) {
v, ok = number(c.data[idx]), true
}
return
}
// LoadFloat64 retrieves a float64 value at a specified index
func (c *numberColumn) LoadFloat64(idx uint32) (v float64, ok bool) {
if idx < uint32(len(c.data)) && c.fill.Contains(idx) {
v, ok = float64(c.data[idx]), true
}
return
}
// LoadInt64 retrieves an int64 value at a specified index
func (c *numberColumn) LoadInt64(idx uint32) (v int64, ok bool) {
if idx < uint32(len(c.data)) && c.fill.Contains(idx) {
v, ok = int64(c.data[idx]), true
}
return
}
// LoadUint64 retrieves an uint64 value at a specified index
func (c *numberColumn) LoadUint64(idx uint32) (v uint64, ok bool) {
if idx < uint32(len(c.data)) && c.fill.Contains(idx) {
v, ok = uint64(c.data[idx]), true
}
return
}
// FilterFloat64 filters down the values based on the specified predicate.
func (c *numberColumn) FilterFloat64(offset uint32, index bitmap.Bitmap, predicate func(v float64) bool) {
index.And(c.fill[offset>>6 : int(offset>>6)+len(index)])
index.Filter(func(idx uint32) bool {
idx = offset + idx
return idx < uint32(len(c.data)) && predicate(float64(c.data[idx]))
})
}
// FilterInt64 filters down the values based on the specified predicate.
func (c *numberColumn) FilterInt64(offset uint32, index bitmap.Bitmap, predicate func(v int64) bool) {
index.And(c.fill[offset>>6 : int(offset>>6)+len(index)])
index.Filter(func(idx uint32) (match bool) {
idx = offset + idx
return idx < uint32(len(c.data)) && predicate(int64(c.data[idx]))
})
}
// FilterUint64 filters down the values based on the specified predicate.
func (c *numberColumn) FilterUint64(offset uint32, index bitmap.Bitmap, predicate func(v uint64) bool) {
index.And(c.fill[offset>>6 : int(offset>>6)+len(index)])
index.Filter(func(idx uint32) (match bool) {
idx = offset + idx
return idx < uint32(len(c.data)) && predicate(uint64(c.data[idx]))
})
}
// Snapshot writes the entire column into the specified destination buffer
func (c *numberColumn) Snapshot(chunk commit.Chunk, dst *commit.Buffer) {
chunk.Range(c.fill, func(idx uint32) {
dst.PutNumber(idx, c.data[idx])
})
}
// numberReader represents a read-only accessor for number
type numberReader struct {
cursor *uint32
reader *numberColumn
}
// Get loads the value at the current transaction cursor
func (s numberReader) Get() (number, bool) {
return s.reader.load(*s.cursor)
}
// numberReaderFor creates a new number reader
func numberReaderFor(txn *Txn, columnName string) numberReader {
column, ok := txn.columnAt(columnName)
if !ok {
panic(fmt.Errorf("column: column '%s' does not exist", columnName))
}
reader, ok := column.Column.(*numberColumn)
if !ok {
panic(fmt.Errorf("column: column '%s' is not of type %T", columnName, number(0)))
}
return numberReader{
cursor: &txn.cursor,
reader: reader,
}
}
// numberWriter represents a read-write accessor for number
type numberWriter struct {
numberReader
writer *commit.Buffer
}
// Set sets the value at the current transaction cursor
func (s numberWriter) Set(value number) {
s.writer.PutNumber(*s.cursor, value)
}
// Add atomically adds a delta to the value at the current transaction cursor
func (s numberWriter) Add(delta number) {
s.writer.AddNumber(*s.cursor, delta)
}
// Number returns a read-write accessor for number column
func (txn *Txn) Number(columnName string) numberWriter {
return numberWriter{
numberReader: numberReaderFor(txn, columnName),
writer: txn.bufferFor(columnName),
}
}