-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathoffset.go
64 lines (53 loc) · 1.21 KB
/
offset.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
package wal
import (
"encoding/binary"
"fmt"
"time"
)
const (
// OffsetSize is the size in bytes of a WAL offset
OffsetSize = 16
)
// Offset records an offset in the WAL
type Offset []byte
// NewOffsetForTS creates an offset for a given timestamp.
func NewOffsetForTS(ts time.Time) Offset {
return NewOffset(tsToFileSequence(ts), 0)
}
func NewOffset(fileSequence int64, position int64) Offset {
o := make(Offset, OffsetSize)
binary.BigEndian.PutUint64(o, uint64(fileSequence))
binary.BigEndian.PutUint64(o[8:], uint64(position))
return o
}
func (o Offset) FileSequence() int64 {
if len(o) == 0 {
return 0
}
return int64(binary.BigEndian.Uint64(o))
}
func (o Offset) Position() int64 {
if len(o) == 0 {
return 0
}
return int64(binary.BigEndian.Uint64(o[8:]))
}
func (o Offset) TS() time.Time {
return sequenceToTime(o.FileSequence())
}
func (a Offset) After(b Offset) bool {
sequenceA := a.FileSequence()
sequenceB := b.FileSequence()
if sequenceA > sequenceB {
return true
}
if sequenceA < sequenceB {
return false
}
positionA := a.Position()
positionB := b.Position()
return positionA > positionB
}
func (o Offset) String() string {
return fmt.Sprintf("%d:%d", o.FileSequence(), o.Position())
}