-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
147 lines (126 loc) · 2.73 KB
/
store.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
// Package cof only writes to data to files
// it has no idea how you fetch data
// reading and fetching is implemented by
// fetcher
package cof
import (
"encoding/binary"
"errors"
"fmt"
"io"
"github.com/golang/protobuf/proto"
"github.com/lafikl/cof/types"
)
var (
ErrNoTimeColumn = errors.New(`missing "time" column in Table`)
)
type Store struct {
BlockSize int
schema Table
offset int64
BatchSize int
Footer *types.Footer
}
type Table map[string]Column
type Record map[string]interface{}
func New(schema Table) (*Store, error) {
if _, ok := schema["time"]; !ok {
return nil, ErrNoTimeColumn
}
return &Store{
BlockSize: 1e4,
schema: schema,
Footer: &types.Footer{},
}, nil
}
func (s *Store) getMinMax() (min, max int64) {
mm, ok := s.schema["time"].(MinMaxer)
if !ok {
panic("time column is not MinMaxer")
}
return mm.Min(), mm.Max()
}
func (s *Store) WriteGroup(w io.Writer) (int64, error) {
min, max := s.getMinMax()
group := &types.Group{
Min: min,
Max: max,
Start: s.offset,
NumColumns: int64(len(s.schema)),
NumRows: int64(0),
}
for name, column := range s.schema {
if column.Rows() == 0 {
continue
}
written, err := column.WriteTo(w)
if err != nil {
return s.offset, err
}
group.NumRows = int64(column.Rows())
group.Columns = append(group.Columns, &types.Column{
Name: name,
Type: column.Type(),
Start: s.offset,
End: s.offset + written,
})
s.offset += written
}
group.End = s.offset
s.Footer.Groups = append(s.Footer.Groups, group)
s.BatchSize = 0
err := s.ResetAll()
return s.offset, err
}
func (s *Store) ResetAll() error {
for name, column := range s.schema {
err := column.Reset()
if err != nil {
return fmt.Errorf("%s: %s", name, err)
}
}
return nil
}
func (s *Store) WriteFooter(w io.Writer) (int64, error) {
out, err := proto.Marshal(s.Footer)
if err != nil {
return s.offset, err
}
written, err := w.Write(out)
if err != nil {
return s.offset, err
}
// writing the start of the footer in the last 8 bytes
foffset := make([]byte, 8)
binary.LittleEndian.PutUint64(foffset, uint64(s.offset))
s.offset += int64(written)
written, err = w.Write(foffset)
if err != nil {
return s.offset, err
}
s.offset += int64(written)
// @TODO(KL): put a crc checksum
return s.offset, nil
}
func (s *Store) Batch(records []Record) error {
var err error
for _, record := range records {
for k, v := range record {
if _, ok := s.schema[k]; !ok {
continue
}
err = s.schema[k].Add(v)
if err != nil {
return fmt.Errorf("%s=%s: %s", k, v, err)
}
}
}
s.BatchSize += len(records)
return nil
}
func (s *Store) ShouldFlush() bool {
if s.BatchSize >= s.BlockSize {
return true
}
return false
}