aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/cache/cache.go
blob: 311e8713d77e17d40d88192df7dfa6024c090ad7 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package cache

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"time"

	"github.com/nightlyone/lockfile"

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

type Version byte

const (
	currentVersion Version = v2Version
)

type Impl interface {
	cachedFeed(*feed.Feed) CachedFeed
	transformTo(Version) (Impl, error)
	cleanup(knownDescriptors map[feed.Descriptor]bool)
	load(io.Reader) error
	store(io.Writer) error
	Version() Version
	Info() string
	SpecificInfo(interface{}) string
}

type Cache struct {
	Impl
	lock   lockfile.Lockfile
	locked bool
}

type CachedFeed interface {
	// Checked marks the feed as being a failure or a success on last check.
	Checked(withFailure bool)
	// Failures of this feed up to now.
	Failures() int
	// The Last time, this feed has been checked
	Last() time.Time
	// Filter the given items against the cached items.
	Filter(items []feed.Item, ignoreHash bool, alwaysNew bool) []feed.Item
	// Commit any changes done to the cache state.
	Commit()
	// The Feed, that is cached.
	Feed() *feed.Feed
}

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

func lockName(fileName string) (string, error) {
	return filepath.Abs(fileName + ".lck")
}

func lock(fileName string) (lock lockfile.Lockfile, err error) {
	var lockFile string

	if lockFile, err = lockName(fileName); err != nil {
		return
	}
	log.Debugf("Handling lock file '%s'", lockFile)

	if lock, err = lockfile.New(lockFile); err != nil {
		err = fmt.Errorf("Creating lock file: %w", err)
		return
	}

	if err = lock.TryLock(); err != nil {
		err = fmt.Errorf("Locking cache: %w", err)
		return
	}

	return
}

func (cache *Cache) store(fileName string) error {
	if cache.Impl == 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)
	}

	if err = cache.Impl.store(writer); err != nil {
		return fmt.Errorf("encoding cache: %w", err)
	}

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

	return cache.Unlock()
}

func (cache *Cache) Unlock() error {
	if cache.locked {
		if err := cache.lock.Unlock(); err != nil {
			return fmt.Errorf("Unlocking cache: %w", err)
		}
	}
	cache.locked = false
	return nil
}

func create() (Cache, error) {
	cache, err := forVersion(currentVersion)
	if err != nil {
		return Cache{}, err
	}
	return Cache{
		Impl:   cache,
		locked: false,
	}, nil
}

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

	lock, err := lock(fileName)
	if err != nil {
		return Cache{}, err
	}

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

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

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

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

	if upgrade && currentVersion != cache.Version() {
		if cache, err = cache.transformTo(currentVersion); err != nil {
			return Cache{}, 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)
	} else {
		log.Printf("Loaded cache (version %d)", version)
	}

	return Cache{cache, lock, true}, nil
}