This repository has been archived by the owner on May 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
journal.go
78 lines (62 loc) · 1.72 KB
/
journal.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
package karigo
import (
"fmt"
"github.com/mfcochauxlaberge/karigo/drivers/memory"
"github.com/mfcochauxlaberge/karigo/drivers/psql"
)
// Journal ...
type Journal interface {
Service
// Reset empties the whole journal. The next append
// will add an entry add index 0.
Reset() error
// Append appends an entry to the journal.
Append([]byte) error
// Oldest returns the oldest known entry.
Oldest() (uint, []byte, error)
// Newest returns the newest entry.
Newest() (uint, []byte, error)
// At returns the entry indexed at i, or none if it
// does not exist.
At(i uint) ([]byte, error)
// Cut removes all entries from the oldest one to
// the one at i minus one.
//
// If i is lower than the oldest known index,
// nothing gets cut. If i is greater than the newest
// index, it will be interpreted as the newest index,
// and therefore everything will be cut except i,
// leaving a journal of length one.
Cut(i uint) error
// Range returns a slice of entries from indexes f
// to t (inclusively).
//
// It returns an error if it can't return the range,
// whether it is because the journal's history starts
// after f or t is greater than the newest index.
Range(f uint, t uint) ([][]byte, error)
}
func newJournal(params map[string]string) (Journal, error) {
if params == nil {
params = map[string]string{}
}
var jrnl Journal
switch params["driver"] {
case "", "memory":
jrnl = &memory.Journal{}
case "psql":
jrnl = &psql.Journal{}
default:
return nil, fmt.Errorf("unknown driver %q", params["driver"])
}
err := jrnl.Connect(params)
if err != nil {
return nil, err
}
return jrnl, nil
}
// source is a thin convenient wrapper for a Journal.
type journal struct {
jrnl Journal
alive bool
}