/
range.go
72 lines (60 loc) · 1.52 KB
/
range.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
package ledgerbackend
import (
"encoding/json"
"fmt"
)
// Range represents a range of ledger sequence numbers.
type Range struct {
from uint32
to uint32
bounded bool
}
type jsonRange struct {
From uint32 `json:"from"`
To uint32 `json:"to"`
Bounded bool `json:"bounded"`
}
func (r *Range) UnmarshalJSON(b []byte) error {
var s jsonRange
if err := json.Unmarshal(b, &s); err != nil {
return err
}
r.from = s.From
r.to = s.To
r.bounded = s.Bounded
return nil
}
func (r Range) MarshalJSON() ([]byte, error) {
return json.Marshal(jsonRange{
From: r.from,
To: r.to,
Bounded: r.bounded,
})
}
func (r Range) String() string {
if r.bounded {
return fmt.Sprintf("[%d,%d]", r.from, r.to)
}
return fmt.Sprintf("[%d,latest)", r.from)
}
func (r Range) Contains(other Range) bool {
if r.bounded && !other.bounded {
return false
}
if r.bounded && other.bounded {
return r.from <= other.from && r.to >= other.to
}
return r.from <= other.from
}
// SingleLedgerRange constructs a bounded range containing a single ledger.
func SingleLedgerRange(ledger uint32) Range {
return Range{from: ledger, to: ledger, bounded: true}
}
// BoundedRange constructs a bounded range of ledgers with a fixed starting ledger and ending ledger.
func BoundedRange(from uint32, to uint32) Range {
return Range{from: from, to: to, bounded: true}
}
// BoundedRange constructs a unbounded range of ledgers with a fixed starting ledger.
func UnboundedRange(from uint32) Range {
return Range{from: from, bounded: false}
}