-
Notifications
You must be signed in to change notification settings - Fork 0
/
persist.go
68 lines (58 loc) · 1.67 KB
/
persist.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
package lizt
import "fmt"
// PersistentIterator is an iterator that persists the pointer.
type PersistentIterator struct {
Persister
PointerIterator
}
// PersistentIteratorConfig is the config for a persistent iterator.
type PersistentIteratorConfig struct {
PointerIter PointerIterator
Persister Persister
}
// NewPersistentIterator returns a new persistent iterator. It will set the pointer to the last known pointer.
func NewPersistentIterator(cfg PersistentIteratorConfig) (*PersistentIterator, error) {
if val, err := cfg.Persister.Get(cfg.PointerIter.Name()); err == nil {
cfg.PointerIter.SetPointer(val)
}
return &PersistentIterator{
PointerIterator: cfg.PointerIter,
Persister: cfg.Persister,
}, nil
}
// Next returns the next line from the iterator.
func (pi *PersistentIterator) Next(count int) ([]string, error) {
next, err := pi.PointerIterator.Next(count)
if err != nil {
return nil, fmt.Errorf("next: name: %s -> %w", pi.Name(), err)
}
err = pi.Set(pi.Name(), pi.Pointer())
if err != nil {
return nil, err
}
return next, nil
}
// MustNext returns the next lines, of a given count, from the iterator. Panics on error.
func (pi *PersistentIterator) MustNext(count int) []string {
lines, err := pi.Next(count)
if err != nil {
panic(err)
}
return lines
}
// NextOne returns the next line from the iterator.
func (pi *PersistentIterator) NextOne() (string, error) {
lines, err := pi.Next(1)
if err != nil {
return "", err
}
return lines[0], nil
}
// MustNextOne returns the next line from the iterator. Panics on error.
func (pi *PersistentIterator) MustNextOne() string {
line, err := pi.NextOne()
if err != nil {
panic(err)
}
return line
}