-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
column_bool.go
96 lines (78 loc) · 2.55 KB
/
column_bool.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
// 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 (
"github.com/kelindar/bitmap"
"github.com/kelindar/column/commit"
)
// columnBool represents a boolean column
type columnBool struct {
data bitmap.Bitmap
}
// makeBools creates a new boolean column
func makeBools() Column {
return &columnBool{
data: make(bitmap.Bitmap, 0, 4),
}
}
// Grow grows the size of the column until we have enough to store
func (c *columnBool) Grow(idx uint32) {
c.data.Grow(idx)
}
// Apply applies a set of operations to the column.
func (c *columnBool) Apply(chunk commit.Chunk, r *commit.Reader) {
for r.Next() {
v := uint64(1) << (r.Offset & 0x3f)
switch r.Type {
case commit.PutTrue:
c.data[r.Offset>>6] |= v
case commit.PutFalse: // also "delete"
c.data[r.Offset>>6] &^= v
}
}
}
// Value retrieves a value at a specified index
func (c *columnBool) Value(idx uint32) (interface{}, bool) {
value := c.data.Contains(idx)
return value, value
}
// Contains checks whether the column has a value at a specified index.
func (c *columnBool) Contains(idx uint32) bool {
return c.data.Contains(idx)
}
// Index returns the fill list for the column
func (c *columnBool) Index(chunk commit.Chunk) bitmap.Bitmap {
return chunk.OfBitmap(c.data)
}
// Snapshot writes the entire column into the specified destination buffer
func (c *columnBool) Snapshot(chunk commit.Chunk, dst *commit.Buffer) {
dst.PutBitmap(commit.PutTrue, chunk, c.data)
}
// --------------------------- Writer ----------------------------
// rwBool represents read-write accessor for boolean values
type rwBool struct {
rdBool
writer *commit.Buffer
}
// Set sets the value at the current transaction cursor
func (s rwBool) Set(value bool) {
s.writer.PutBool(*s.cursor, value)
}
// Bool returns a bool column accessor
func (txn *Txn) Bool(columnName string) rwBool {
return rwBool{
rdBool: readBoolOf(txn, columnName),
writer: txn.bufferFor(columnName),
}
}
// --------------------------- Reader ----------------------------
// rdBool represents a read-only accessor for boolean values
type rdBool reader[Column]
// Get loads the value at the current transaction cursor
func (s rdBool) Get() bool {
return s.reader.Contains(*s.cursor)
}
// readBoolOf creates a new boolean reader
func readBoolOf(txn *Txn, columnName string) rdBool {
return rdBool(readerFor[Column](txn, columnName))
}