-
Notifications
You must be signed in to change notification settings - Fork 3
/
options.go
73 lines (63 loc) · 1.73 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
package memlog
import (
"errors"
"github.com/benbjohnson/clock"
)
const (
// DefaultStartOffset is the start offset of the log
DefaultStartOffset = Offset(0)
// DefaultSegmentSize is the segment size, i.e. number of offsets, in the log
DefaultSegmentSize = 1024
// DefaultMaxRecordDataBytes is the maximum data (payload) size of a record
DefaultMaxRecordDataBytes = 1024 << 10 // 1MiB
)
// Option customizes a log
type Option func(*Log) error
var defaultOptions = []Option{
WithClock(clock.New()),
WithStartOffset(DefaultStartOffset),
WithMaxSegmentSize(DefaultSegmentSize),
WithMaxRecordDataSize(DefaultMaxRecordDataBytes),
}
// 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
}
}
// WithStartOffset sets the start offset of the log. Must be equal or greater
// than 0.
func WithStartOffset(offset 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
}
}