-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage.go
60 lines (51 loc) · 1.26 KB
/
storage.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
package main
import (
"log"
"errors"
"fmt"
"strings"
)
type TagStats map[string]int
type Storage interface {
Add(a Annotation) error
ListForTag(tag string, r, until int, out *[]Annotation) (err error)
TagStats() (TagStats, error)
AllTags() []string
Close()
Cleanup() // after tests
}
type Annotation struct {
CreatedAt int `json:"created_at,omitempty" gorethink:"created_at"`
Message string `json:"message" gorethink:"message"`
Tags []string `json:"tags,omitempty" gorethink:"tags"`
}
type Posts struct {
Posts []Annotation `json:"posts"`
}
func NewStorage(config string) (Storage, error) {
log.Printf("Storage config: %s", config)
parts := strings.SplitN(config, ":", 2)
if len(parts) != 2 {
return nil, errors.New("invalid config format")
}
switch parts[0] {
case "local":
{
return NewBoltDBStorage(parts[1])
}
case "rethinkdb":
{
return NewRethinkDBStorage(parts[1])
}
}
return nil, fmt.Errorf("invalid config, type \"%s\" not supported", parts[0])
}
func GetPosts(s Storage, tags []string, ra, until int) (res Posts, err error) {
res.Posts = make([]Annotation, 0)
for _, tag := range tags {
if s.ListForTag(tag, ra, until, &res.Posts) != nil {
return res, err
}
}
return res, nil
}