-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
column.go
315 lines (266 loc) · 7.88 KB
/
column.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package column
import (
"fmt"
"reflect"
"sync"
"github.com/kelindar/bitmap"
"github.com/kelindar/column/commit"
)
// columnType represents a type of a column.
type columnType uint8
const (
typeGeneric = columnType(0) // Generic column, every column should support this
typeNumeric = columnType(1 << 0) // Numeric column supporting float64, int64 or uint64
typeTextual = columnType(1 << 1) // Textual column supporting strings
)
// typeOf resolves all supported types of the column
func typeOf(column Column) (typ columnType) {
if _, ok := column.(Numeric); ok {
typ = typ | typeNumeric
}
if _, ok := column.(Textual); ok {
typ = typ | typeTextual
}
return
}
// --------------------------- Contracts ----------------------------
// Column represents a column implementation
type Column interface {
Grow(idx uint32)
Apply(commit.Chunk, *commit.Reader)
Value(idx uint32) (interface{}, bool)
Contains(idx uint32) bool
Index(commit.Chunk) bitmap.Bitmap
Snapshot(chunk commit.Chunk, dst *commit.Buffer)
}
// Numeric represents a column that stores numbers.
type Numeric interface {
Column
LoadFloat64(uint32) (float64, bool)
LoadUint64(uint32) (uint64, bool)
LoadInt64(uint32) (int64, bool)
FilterFloat64(commit.Chunk, bitmap.Bitmap, func(v float64) bool)
FilterUint64(commit.Chunk, bitmap.Bitmap, func(v uint64) bool)
FilterInt64(commit.Chunk, bitmap.Bitmap, func(v int64) bool)
}
// Textual represents a column that stores strings.
type Textual interface {
Column
LoadString(uint32) (string, bool)
FilterString(commit.Chunk, bitmap.Bitmap, func(v string) bool)
}
// --------------------------- Constructors ----------------------------
// Various column constructor functions for a specific types.
var (
ForString = makeStrings
ForFloat32 = makeFloat32s
ForFloat64 = makeFloat64s
ForInt = makeInts
ForInt16 = makeInt16s
ForInt32 = makeInt32s
ForInt64 = makeInt64s
ForUint = makeUints
ForUint16 = makeUint16s
ForUint32 = makeUint32s
ForUint64 = makeUint64s
ForBool = makeBools
ForEnum = makeEnum
ForKey = makeKey
)
// ForKind creates a new column instance for a specified reflect.Kind
func ForKind(kind reflect.Kind) (Column, error) {
switch kind {
case reflect.Float32:
return makeFloat32s(), nil
case reflect.Float64:
return makeFloat64s(), nil
case reflect.Int:
return makeInts(), nil
case reflect.Int16:
return makeInt16s(), nil
case reflect.Int32:
return makeInt32s(), nil
case reflect.Int64:
return makeInt64s(), nil
case reflect.Uint:
return makeUints(), nil
case reflect.Uint16:
return makeUint16s(), nil
case reflect.Uint32:
return makeUint32s(), nil
case reflect.Uint64:
return makeUint64s(), nil
case reflect.Bool:
return makeBools(), nil
case reflect.String:
return makeStrings(), nil
default:
return nil, fmt.Errorf("column: unsupported column kind (%v)", kind)
}
}
// --------------------------- Generic Options ----------------------------
// option represents options for variouos columns.
type option[T any] struct {
Merge func(value, delta T) T
}
// configure applies options
func configure[T any](opts []func(*option[T]), dst option[T]) option[T] {
for _, fn := range opts {
fn(&dst)
}
return dst
}
// WithMerge sets an optional merge function that allows you to merge a delta value to
// an existing value, atomically. The operation is performed transactionally.
func WithMerge[T any](fn func(value, delta T) T) func(*option[T]) {
return func(v *option[T]) {
v.Merge = fn
}
}
// --------------------------- Column ----------------------------
// column represents a column wrapper that synchronizes operations
type column struct {
Column
lock sync.RWMutex // The lock to protect the entire column
kind columnType // The type of the colum
name string // The name of the column
}
// columnFor creates a synchronized column for a column implementation
func columnFor(name string, v Column) *column {
return &column{
kind: typeOf(v),
name: name,
Column: v,
}
}
// IsIndex returns whether the column is an index
func (c *column) IsIndex() bool {
_, ok := c.Column.(*columnIndex)
return ok
}
// IsNumeric checks whether a column type supports certain numerical operations.
func (c *column) IsNumeric() bool {
return (c.kind & typeNumeric) == typeNumeric
}
// IsTextual checks whether a column type supports certain string operations.
func (c *column) IsTextual() bool {
return (c.kind & typeTextual) == typeTextual
}
// Grow grows the size of the column
func (c *column) Grow(idx uint32) {
c.lock.Lock()
defer c.lock.Unlock()
c.Column.Grow(idx)
}
// Apply performs a series of operations on a column.
func (c *column) Apply(chunk commit.Chunk, r *commit.Reader) {
c.lock.RLock()
defer c.lock.RUnlock()
r.Rewind()
c.Column.Apply(chunk, r)
}
// Index loads the appropriate column index for a given chunk
func (c *column) Index(chunk commit.Chunk) bitmap.Bitmap {
c.lock.RLock()
defer c.lock.RUnlock()
return c.Column.Index(chunk)
}
// Snapshot takes a snapshot of a column, skipping indexes
func (c *column) Snapshot(chunk commit.Chunk, buffer *commit.Buffer) bool {
if c.IsIndex() {
return false
}
buffer.Reset(c.name)
c.Column.Snapshot(chunk, buffer)
return true
}
// Value retrieves a value at a specified index
func (c *column) Value(idx uint32) (v interface{}, ok bool) {
v, ok = c.Column.Value(idx)
return
}
// --------------------------- Accessor ----------------------------
// Reader represents a generic reader
type reader[T any] struct {
cursor *uint32
reader T
}
// readerFor creates a read-only accessor
func readerFor[T any](txn *Txn, columnName string) reader[T] {
column, ok := txn.columnAt(columnName)
if !ok {
panic(fmt.Errorf("column: column '%s' does not exist", columnName))
}
target, ok := column.Column.(T)
if !ok {
var want T
panic(fmt.Errorf("column: column '%s' is not of specified type (has=%T, want=%T)",
columnName, column.Column, want))
}
return reader[T]{
cursor: &txn.cursor,
reader: target,
}
}
// --------------------------- Any Writer ----------------------------
// rwAny represents read-write accessor for any column type
type rwAny struct {
rdAny
writer *commit.Buffer
}
// Set sets the value at the current transaction cursor
func (s rwAny) Set(value any) error {
return s.writer.PutAny(commit.Put, *s.cursor, value)
}
// --------------------------- Any Reader ----------------------------
// rdAny represents a read-only accessor for any value
type rdAny reader[Column]
// Get loads the value at the current transaction cursor
func (s rdAny) Get() (any, bool) {
return s.reader.Value(*s.cursor)
}
// readAnyOf creates a new any reader
func readAnyOf(txn *Txn, columnName string) rdAny {
return rdAny(readerFor[Column](txn, columnName))
}
// Any returns a column accessor
func (txn *Txn) Any(columnName string) rwAny {
return rwAny{
rdAny: readAnyOf(txn, columnName),
writer: txn.bufferFor(columnName),
}
}
// --------------------------- segment list ----------------------------
// Chunks represents a chunked array storage
type chunks[T any] []struct {
fill bitmap.Bitmap // The fill-list
data []T // The actual values
}
// chunkAt loads the fill and data list at a particular chunk
func (s chunks[T]) chunkAt(chunk commit.Chunk) (bitmap.Bitmap, []T) {
fill := s[chunk].fill
data := s[chunk].data
return fill, data
}
// Grow grows a segment list
func (s *chunks[T]) Grow(idx uint32) {
chunk := int(commit.ChunkAt(idx))
for i := len(*s); i <= chunk; i++ {
*s = append(*s, struct {
fill bitmap.Bitmap
data []T
}{
fill: make(bitmap.Bitmap, chunkSize/64),
data: make([]T, chunkSize),
})
}
}
// Index returns the fill list for the segment
func (s chunks[T]) Index(chunk commit.Chunk) (fill bitmap.Bitmap) {
if int(chunk) < len(s) {
fill = s[chunk].fill
}
return
}