forked from kelindar/column
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolumn_strings.go
305 lines (257 loc) · 8.28 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
// 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"
"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]))
})
}
// enumReader represents a read-only accessor for enum strings
type enumReader struct {
cursor *uint32
reader *columnEnum
}
// Get loads the value at the current transaction cursor
func (s enumReader) Get() (string, bool) {
return s.reader.LoadString(*s.cursor)
}
// enumReaderFor creates a new enum string reader
func enumReaderFor(txn *Txn, columnName string) enumReader {
column, ok := txn.columnAt(columnName)
if !ok {
panic(fmt.Errorf("column: column '%s' does not exist", columnName))
}
reader, ok := column.Column.(*columnEnum)
if !ok {
panic(fmt.Errorf("column: column '%s' is not of type string", columnName))
}
return enumReader{
cursor: &txn.cursor,
reader: reader,
}
}
// slice accessor for enums
type enumSlice struct {
enumReader
writer *commit.Buffer
}
// Set sets the value at the current transaction cursor
func (s enumSlice) Set(value string) {
s.writer.PutString(commit.Put, *s.cursor, value)
}
// Enum returns a enumerable column accessor
func (txn *Txn) Enum(columnName string) enumSlice {
return enumSlice{
enumReader: enumReaderFor(txn, columnName),
writer: txn.bufferFor(columnName),
}
}
// --------------------------- String ----------------------------
var _ Textual = new(columnString)
// columnString represents a string column
type columnString struct {
chunks[string]
}
// makeString creates a new string column
func makeStrings() Column {
return &columnString{
chunks: make(chunks[string], 0, 4),
}
}
// 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.Delete:
fill.Remove(uint32(offset))
}
}
}
// Value retrieves a value at a specified index
func (c *columnString) Value(idx uint32) (v interface{}, 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
}
// 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) (string, bool) {
v, has := c.Value(idx)
s, ok := v.(string)
return s, has && ok
}
// 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])
})
}
// stringReader represents a read-only accessor for strings
type stringReader struct {
cursor *uint32
reader *columnString
}
// Get loads the value at the current transaction cursor
func (s stringReader) Get() (string, bool) {
return s.reader.LoadString(*s.cursor)
}
// stringReaderFor creates a new string reader
func stringReaderFor(txn *Txn, columnName string) stringReader {
column, ok := txn.columnAt(columnName)
if !ok {
panic(fmt.Errorf("column: column '%s' does not exist", columnName))
}
reader, ok := column.Column.(*columnString)
if !ok {
panic(fmt.Errorf("column: column '%s' is not of type string", columnName))
}
return stringReader{
cursor: &txn.cursor,
reader: reader,
}
}
// stringWriter represents read-write accessor for strings
type stringWriter struct {
stringReader
writer *commit.Buffer
}
// Set sets the value at the current transaction cursor
func (s stringWriter) Set(value string) {
s.writer.PutString(commit.Put, *s.cursor, value)
}
// String returns a string column accessor
func (txn *Txn) String(columnName string) stringWriter {
return stringWriter{
stringReader: stringReaderFor(txn, columnName),
writer: txn.bufferFor(columnName),
}
}