aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/cache/v2.go
blob: 5d6ff9ef8ab3e3081bfa41abf08c7bde5ca61982 (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
package cache

import (
	"compress/gzip"
	"fmt"
	"io"

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

const v2Version Version = 2

// v2Cache is identical to v1Cache, but uses gzip compression for storage
type v2Cache v1Cache

func newV2Cache() *v2Cache {
	return (*v2Cache)(newV1Cache())
}

func (cache *v2Cache) asV1() *v1Cache {
	return (*v1Cache)(cache)
}

func (cache *v2Cache) cachedFeed(feed *feed.Feed) CachedFeed {
	return cache.asV1().cachedFeed(feed)
}

func (cache *v2Cache) transformTo(v Version) (Impl, error) {
	return nil, fmt.Errorf("Transformation not supported")
}

func (cache *v2Cache) cleanup(knownDescriptors map[feed.Descriptor]struct{}) {
	cache.asV1().cleanup(knownDescriptors)
}

func (cache *v2Cache) Version() Version {
	return v2Version
}

func (cache *v2Cache) Info() string {
	return cache.asV1().Info()
}

func (cache *v2Cache) SpecificInfo(i interface{}) string {
	return cache.asV1().SpecificInfo(i)
}

func (cache *v2Cache) load(reader io.Reader) error {
	gzipReader, err := gzip.NewReader(reader)
	if err != nil {
		return err
	}
	defer gzipReader.Close()

	return cache.asV1().load(gzipReader)
}

func (cache *v2Cache) store(writer io.Writer) error {
	gzipWriter := gzip.NewWriter(writer)
	defer gzipWriter.Close()

	if err := cache.asV1().store(gzipWriter); err != nil {
		return err
	}

	return gzipWriter.Flush()
}