-
Notifications
You must be signed in to change notification settings - Fork 3
/
options.go
86 lines (74 loc) · 2 KB
/
options.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
package sharded
import (
"errors"
"github.com/benbjohnson/clock"
"github.com/embano1/memlog"
)
const (
// DefaultStartOffset is the start offset of each shard
DefaultStartOffset = memlog.DefaultStartOffset
// DefaultSegmentSize is the segment size of each shard
DefaultSegmentSize = memlog.DefaultSegmentSize
// DefaultMaxRecordDataBytes is the maximum data (payload) size of a record
DefaultMaxRecordDataBytes = memlog.DefaultMaxRecordDataBytes
)
// Option customizes a log
type Option func(*Log) error
var defaultOptions = []Option{
WithClock(clock.New()),
WithMaxRecordDataSize(DefaultMaxRecordDataBytes),
WithMaxSegmentSize(DefaultSegmentSize),
WithSharder(newDefaultSharder()),
WithStartOffset(DefaultStartOffset),
}
// WithClock uses the specified clock for setting record timestamps
func WithClock(c clock.Clock) Option {
return func(log *Log) error {
if c == nil {
return errors.New("clock must not be nil")
}
log.clock = c
return nil
}
}
// WithMaxRecordDataSize sets the maximum record data (payload) size in bytes
func WithMaxRecordDataSize(size int) Option {
return func(log *Log) error {
if size <= 0 {
return errors.New("size must be greater than 0")
}
log.conf.maxRecordSize = size
return nil
}
}
// WithMaxSegmentSize sets the maximum size, i.e. number of offsets, in a log
// segment. Must be greater than 0.
func WithMaxSegmentSize(size int) Option {
return func(log *Log) error {
if size <= 0 {
return errors.New("size must be greater than 0")
}
log.conf.segmentSize = size
return nil
}
}
func WithSharder(s Sharder) Option {
return func(log *Log) error {
if s == nil {
return errors.New("sharder must not be nil")
}
log.Sharder = s
return nil
}
}
// WithStartOffset sets the start offset of the log. Must be equal or greater
// than 0.
func WithStartOffset(offset memlog.Offset) Option {
return func(log *Log) error {
if offset < 0 {
return errors.New("start offset must not be negative")
}
log.conf.startOffset = offset
return nil
}
}