/
column.go
69 lines (59 loc) · 1.39 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
package storage
import (
"github.com/elliotcourant/arkdb/pkg/types"
"github.com/elliotcourant/buffers"
)
var (
columnMinimumSize = 6
)
type Column struct {
ColumnID uint8
TableID uint8
ColumnName string
ColumnType types.Type
PrimaryKey bool
}
func (i Column) Encode() []byte {
buf := buffers.NewBytesBuffer()
buf.AppendUint8(i.ColumnID)
buf.AppendUint8(i.TableID)
buf.AppendString(i.ColumnName)
buf.AppendUint8(uint8(i.ColumnType))
buf.AppendBool(i.PrimaryKey)
return buf.Bytes()
}
func (i *Column) Decode(src []byte) error {
*i = Column{}
buf := buffers.NewBytesReader(src)
i.ColumnID = buf.NextUint8()
i.TableID = buf.NextUint8()
i.ColumnName = buf.NextString()
i.ColumnType = types.Type(buf.NextUint8())
i.PrimaryKey = buf.NextBool()
return nil
}
func (i Column) Size() int {
return columnMinimumSize + len(i.ColumnName)
}
func (i Column) ObjectIdPrefix() []byte {
buf := buffers.NewBytesBuffer()
buf.AppendByte(MetaPrefix_Column)
buf.AppendUint8(i.TableID)
return buf.Bytes()
}
func (i Column) Prefix() []byte {
buf := buffers.NewBytesBuffer()
buf.AppendByte(MetaPrefix_Column)
buf.AppendUint8(i.TableID)
if len(i.ColumnName) > 0 {
buf.AppendString(i.ColumnName)
}
return buf.Bytes()
}
func (i Column) Path() []byte {
buf := buffers.NewBytesBuffer()
buf.AppendByte(MetaPrefix_Column)
buf.AppendUint8(i.TableID)
buf.AppendString(i.ColumnName)
return buf.Bytes()
}