-
Notifications
You must be signed in to change notification settings - Fork 31
/
flush.go
76 lines (62 loc) · 2.06 KB
/
flush.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
// Copyright 2019-2020 Grabtaxi Holdings PTE LTE (GRAB), All rights reserved.
// Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
package flush
import (
"github.com/kelindar/talaria/internal/encoding/block"
"github.com/kelindar/talaria/internal/encoding/key"
"github.com/kelindar/talaria/internal/encoding/typeof"
"github.com/kelindar/talaria/internal/monitor"
"github.com/kelindar/talaria/internal/storage"
)
// Writer represents a sink for the flusher.
type Writer interface {
Write(key key.Key, blocks []block.Block) error
}
// Flusher represents a flusher/merger.
type Flusher struct {
monitor monitor.Monitor // The monitor client
writer Writer // The underlying block writer
fileNameFunc func(map[string]interface{}) (string, error)
streamer storage.Streamer // The underlying row writer
}
// ForCompaction creates a new storage implementation.
func ForCompaction(monitor monitor.Monitor, writer Writer, fileNameFunc func(map[string]interface{}) (string, error)) (*Flusher, error) {
return &Flusher{
monitor: monitor,
writer: writer,
fileNameFunc: fileNameFunc,
}, nil
}
// TODO: ForStreaming
// WriteBlock writes a one or multiple blocks to the underlying writer.
func (s *Flusher) WriteBlock(blocks []block.Block, schema typeof.Schema) error {
if s.writer == nil || len(blocks) == 0 {
return nil
}
// Generate the file name and write the data to the underlying writer
return s.writer.Write(s.generateFileName(blocks[0]), blocks)
}
// WriteRow writes a single row to the underlying writer (i.e. streamer).
func (s *Flusher) WriteRow(r block.Row) error {
if s.streamer == nil {
return nil
}
// Stream the row
return s.streamer.Stream(r)
}
func (s *Flusher) generateFileName(b block.Block) []byte {
row, err := b.LastRow()
if err != nil {
return []byte{}
}
output, err := s.fileNameFunc(row)
if err != nil {
s.monitor.Error(err)
return []byte{}
}
return []byte(output)
}
// Close is used to gracefully close storage.
func (s *Flusher) Close() error {
return nil
}