forked from kelindar/column
-
Notifications
You must be signed in to change notification settings - Fork 0
/
column_strings.go
361 lines (305 loc) · 9.91 KB
/
column_strings.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// 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"
"math"
"sync"
"github.com/kelindar/bitmap"
"github.com/kelindar/column/commit"
"github.com/kelindar/intmap"
"github.com/zeebo/xxh3"
)
// --------------------------- Enum ----------------------------
var _ Textual = new(columnEnum)
// columnEnum represents a string column
type columnEnum struct {
chunks[uint32]
seek *intmap.Sync // The hash->location table
data []string // The string data
}
// makeEnum creates a new column
func makeEnum() Column {
return &columnEnum{
chunks: make(chunks[uint32], 0, 4),
seek: intmap.NewSync(64, .95),
data: make([]string, 0, 64),
}
}
// Apply applies a set of operations to the column.
func (c *columnEnum) Apply(chunk commit.Chunk, r *commit.Reader) {
fill, locs := c.chunkAt(chunk)
for r.Next() {
offset := r.IndexAtChunk()
switch r.Type {
case commit.Put:
fill[offset>>6] |= 1 << (offset & 0x3f)
locs[offset] = c.findOrAdd(r.Bytes())
case commit.Delete:
fill.Remove(offset)
// TODO: remove unused strings, need some reference counting for that
// and can proably be done during vacuum() instead
}
}
}
// Search for the string or adds it and returns the offset
func (c *columnEnum) findOrAdd(v []byte) uint32 {
target := uint32(xxh3.Hash(v))
at, _ := c.seek.LoadOrStore(target, func() uint32 {
c.data = append(c.data, string(v))
return uint32(len(c.data)) - 1
})
return at
}
// readAt reads a string at a location
func (c *columnEnum) readAt(at uint32) string {
return c.data[at]
}
// Value retrieves a value at a specified index
func (c *columnEnum) Value(idx uint32) (v interface{}, ok bool) {
return c.LoadString(idx)
}
// LoadString retrieves a value at a specified index
func (c *columnEnum) LoadString(idx uint32) (v string, ok bool) {
chunk := commit.ChunkAt(idx)
index := idx - chunk.Min()
if int(chunk) < len(c.chunks) && c.chunks[chunk].fill.Contains(index) {
v, ok = c.readAt(c.chunks[chunk].data[index]), true
}
return
}
// FilterString filters down the values based on the specified predicate. The column for
// this filter must be a string.
func (c *columnEnum) FilterString(chunk commit.Chunk, index bitmap.Bitmap, predicate func(v string) bool) {
if int(chunk) >= len(c.chunks) {
return
}
fill, locs := c.chunkAt(chunk)
cache := struct {
index uint32 // Last seen offset
value bool // Last evaluated predicate
}{
index: math.MaxUint32,
value: false,
}
// Do a quick ellimination of elements which are NOT contained in this column, this
// allows us not to check contains during the filter itself
index.And(fill)
// Filters down the strings, if strings repeat we avoid reading every time by
// caching the last seen index/value combination.
index.Filter(func(idx uint32) bool {
if at := locs[idx]; at != cache.index {
cache.index = at
cache.value = predicate(c.readAt(at))
return cache.value
}
// The value is cached, avoid evaluating it
return cache.value
})
}
// Contains checks whether the column has a value at a specified index.
func (c *columnEnum) Contains(idx uint32) bool {
chunk := commit.ChunkAt(idx)
return c.chunks[chunk].fill.Contains(idx - chunk.Min())
}
// Snapshot writes the entire column into the specified destination buffer
func (c *columnEnum) Snapshot(chunk commit.Chunk, dst *commit.Buffer) {
fill, locs := c.chunkAt(chunk)
fill.Range(func(idx uint32) {
dst.PutString(commit.Put, idx, c.readAt(locs[idx]))
})
}
// rwEnum represents read-write accessor for enum
type rwEnum struct {
rdString[*columnEnum]
writer *commit.Buffer
}
// Set sets the value at the current transaction cursor
func (s rwEnum) Set(value string) {
s.writer.PutString(commit.Put, *s.cursor, value)
}
// Enum returns a enumerable column accessor
func (txn *Txn) Enum(columnName string) rwEnum {
return rwEnum{
rdString: readStringOf[*columnEnum](txn, columnName),
writer: txn.bufferFor(columnName),
}
}
// --------------------------- String ----------------------------
var _ Textual = new(columnString)
// columnString represents a string column
type columnString struct {
chunks[string]
option[string]
}
// makeString creates a new string column
func makeStrings(opts ...func(*option[string])) Column {
return &columnString{
chunks: make(chunks[string], 0, 4),
option: configure(opts, option[string]{
Merge: func(_, delta string) string { return delta },
}),
}
}
// Apply applies a set of operations to the column.
func (c *columnString) Apply(chunk commit.Chunk, r *commit.Reader) {
fill, data := c.chunkAt(chunk)
from := chunk.Min()
// Update the values of the column, for this one we can only process stores
for r.Next() {
offset := r.Offset - int32(from)
switch r.Type {
case commit.Put:
fill[offset>>6] |= 1 << (offset & 0x3f)
data[offset] = string(r.Bytes())
case commit.Merge:
fill[offset>>6] |= 1 << (offset & 0x3f)
data[offset] = r.SwapString(c.Merge(data[offset], r.String()))
case commit.Delete:
fill.Remove(uint32(offset))
}
}
}
// Value retrieves a value at a specified index
func (c *columnString) Value(idx uint32) (v interface{}, ok bool) {
return c.LoadString(idx)
}
// Contains checks whether the column has a value at a specified index.
func (c *columnString) Contains(idx uint32) bool {
chunk := commit.ChunkAt(idx)
index := idx - chunk.Min()
return c.chunks[chunk].fill.Contains(index)
}
// LoadString retrieves a value at a specified index
func (c *columnString) LoadString(idx uint32) (v string, ok bool) {
chunk := commit.ChunkAt(idx)
index := idx - chunk.Min()
if int(chunk) < len(c.chunks) && c.chunks[chunk].fill.Contains(index) {
v, ok = c.chunks[chunk].data[index], true
}
return
}
// FilterString filters down the values based on the specified predicate. The column for
// this filter must be a string.
func (c *columnString) FilterString(chunk commit.Chunk, index bitmap.Bitmap, predicate func(v string) bool) {
if int(chunk) < len(c.chunks) {
fill, data := c.chunkAt(chunk)
index.And(fill)
index.Filter(func(idx uint32) bool {
return predicate(data[idx])
})
}
}
// Snapshot writes the entire column into the specified destination buffer
func (c *columnString) Snapshot(chunk commit.Chunk, dst *commit.Buffer) {
fill, data := c.chunkAt(chunk)
fill.Range(func(x uint32) {
dst.PutString(commit.Put, chunk.Min()+x, data[x])
})
}
// rwString represents read-write accessor for strings
type rwString struct {
rdString[*columnString]
writer *commit.Buffer
}
// Set sets the value at the current transaction cursor
func (s rwString) Set(value string) {
s.writer.PutString(commit.Put, *s.cursor, value)
}
// Merge merges the value at the current transaction cursor
func (s rwString) Merge(value string) {
s.writer.PutString(commit.Merge, *s.cursor, value)
}
// String returns a string column accessor
func (txn *Txn) String(columnName string) rwString {
return rwString{
rdString: readStringOf[*columnString](txn, columnName),
writer: txn.bufferFor(columnName),
}
}
// --------------------------- Key ----------------------------
// columnKey represents the primary key column implementation
type columnKey struct {
columnString
name string // Name of the column
lock sync.RWMutex // Lock to protect the lookup table
seek map[string]uint32 // Lookup table for O(1) index seek
}
// makeKey creates a new primary key column
func makeKey() Column {
return &columnKey{
seek: make(map[string]uint32, 64),
columnString: columnString{
chunks: make(chunks[string], 0, 4),
},
}
}
// Apply applies a set of operations to the column.
func (c *columnKey) Apply(chunk commit.Chunk, r *commit.Reader) {
fill, data := c.chunkAt(chunk)
from := chunk.Min()
for r.Next() {
offset := r.Offset - int32(from)
switch r.Type {
case commit.Put:
value := string(r.Bytes())
fill[offset>>6] |= 1 << (offset & 0x3f)
data[offset] = value
c.lock.Lock()
c.seek[value] = uint32(r.Offset)
c.lock.Unlock()
case commit.Delete:
fill.Remove(uint32(offset))
c.lock.Lock()
delete(c.seek, string(data[offset]))
c.lock.Unlock()
}
}
}
// OffsetOf returns the offset for a particular value
func (c *columnKey) OffsetOf(v string) (uint32, bool) {
c.lock.RLock()
idx, ok := c.seek[v]
c.lock.RUnlock()
return idx, ok
}
// rwKey represents read-write accessor for primary keys.
type rwKey struct {
cursor *uint32
writer *commit.Buffer
reader *columnKey
}
// Set sets the value at the current transaction index
func (s rwKey) Set(value string) error {
if _, ok := s.reader.OffsetOf(value); !ok {
s.writer.PutString(commit.Put, *s.cursor, value)
return nil
}
return fmt.Errorf("column: unable to set duplicate key '%s'", value)
}
// Get loads the value at the current transaction index
func (s rwKey) Get() (string, bool) {
return s.reader.LoadString(*s.cursor)
}
// Enum returns a enumerable column accessor
func (txn *Txn) Key() rwKey {
if txn.owner.pk == nil {
panic(fmt.Errorf("column: primary key column does not exist"))
}
return rwKey{
cursor: &txn.cursor,
writer: txn.bufferFor(txn.owner.pk.name),
reader: txn.owner.pk,
}
}
// --------------------------- Reader ----------------------------
// rdString represents a read-only accessor for strings
type rdString[T Textual] reader[T]
// Get loads the value at the current transaction cursor
func (s rdString[T]) Get() (string, bool) {
return s.reader.LoadString(*s.cursor)
}
// readStringOf creates a new string reader
func readStringOf[T Textual](txn *Txn, columnName string) rdString[T] {
return rdString[T](readerFor[T](txn, columnName))
}