aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/cache.go
blob: 0b2f90545696890e5d0c89d5c8e19bd6ce725c1e (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package feed

import (
	"bufio"
	"encoding/gob"
	"errors"
	"fmt"
	"os"
	"time"

	"github.com/Necoro/feed2imap-go/pkg/log"
)

type Version byte

const (
	currentVersion Version = 1
)

type Cache interface {
	findItem(*Feed) CachedFeed
	Version() Version
	transformToCurrent() (Cache, error)
}

type CachedFeed interface {
	Checked(withFailure bool)
	Failures() uint
	Last() time.Time
	filterItems(items []feeditem, ignoreHash bool, alwaysNew bool) []feeditem
	Commit()
}

func cacheForVersion(version Version) (Cache, error) {
	switch version {
	case v1Version:
		return newV1Cache(), nil
	default:
		return nil, fmt.Errorf("unknown cache version '%d'", version)
	}
}

func storeCache(cache Cache, fileName string) error {
	if cache == nil {
		return fmt.Errorf("trying to store nil cache")
	}
	if cache.Version() != currentVersion {
		return fmt.Errorf("trying to store cache with unsupported version '%d' (current: '%d')", cache.Version(), currentVersion)
	}

	f, err := os.Create(fileName)
	if err != nil {
		return fmt.Errorf("trying to store cache to '%s': %w", fileName, err)
	}
	defer f.Close()

	writer := bufio.NewWriter(f)
	if err = writer.WriteByte(byte(currentVersion)); err != nil {
		return fmt.Errorf("writing to '%s': %w", fileName, err)
	}

	encoder := gob.NewEncoder(writer)
	if err = encoder.Encode(cache); err != nil {
		return fmt.Errorf("encoding cache: %w", err)
	}

	writer.Flush()
	log.Printf("Stored cache to '%s'.", fileName)

	return nil
}

func newCache() (Cache, error) {
	return cacheForVersion(currentVersion)
}

func loadCache(fileName string) (Cache, error) {
	f, err := os.Open(fileName)
	if err != nil {
		if errors.Is(err, os.ErrNotExist) {
			// no cache there yet -- make new
			return newCache()
		}
		return nil, fmt.Errorf("opening cache at '%s': %w", fileName, err)
	}
	defer f.Close()

	log.Printf("Loading cache from '%s'", fileName)

	reader := bufio.NewReader(f)
	version, err := reader.ReadByte()
	if err != nil {
		return nil, fmt.Errorf("reading from '%s': %w", fileName, err)
	}

	cache, err := cacheForVersion(Version(version))
	if err != nil {
		return nil, err
	}

	decoder := gob.NewDecoder(reader)
	if err = decoder.Decode(cache); err != nil {
		return nil, fmt.Errorf("decoding for version '%d' from '%s': %w", version, fileName, err)
	}

	if cache, err = cache.transformToCurrent(); err != nil {
		return nil, fmt.Errorf("cannot transform from version %d to %d: %w", version, currentVersion, err)
	}

	log.Printf("Loaded cache (version %d), transformed to version %d.", version, currentVersion)

	return cache, nil
}