forked from kelindar/column
-
Notifications
You must be signed in to change notification settings - Fork 0
/
column_bool.go
105 lines (87 loc) · 2.64 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
97
98
99
100
101
102
103
104
105
// 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"
"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)
}
// boolReader represents a read-only accessor for boolean values
type boolReader struct {
cursor *uint32
reader Column
}
// Get loads the value at the current transaction cursor
func (s boolReader) Get() bool {
return s.reader.Contains(*s.cursor)
}
// boolReaderFor creates a new reader
func boolReaderFor(txn *Txn, columnName string) boolReader {
column, ok := txn.columnAt(columnName)
if !ok {
panic(fmt.Errorf("column: column '%s' does not exist", columnName))
}
return boolReader{
cursor: &txn.cursor,
reader: column.Column,
}
}
// boolWriter represents read-write accessor for boolean values
type boolWriter struct {
boolReader
writer *commit.Buffer
}
// Set sets the value at the current transaction cursor
func (s boolWriter) Set(value bool) {
s.writer.PutBool(*s.cursor, value)
}
// Bool returns a bool column accessor
func (txn *Txn) Bool(columnName string) boolWriter {
return boolWriter{
boolReader: boolReaderFor(txn, columnName),
writer: txn.bufferFor(columnName),
}
}