aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
Diffstat (limited to 'internal')
-rw-r--r--internal/feed/cache/cache.go16
-rw-r--r--internal/feed/cache/v1.go4
-rw-r--r--internal/feed/cache/v2.go63
3 files changed, 76 insertions, 7 deletions
diff --git a/internal/feed/cache/cache.go b/internal/feed/cache/cache.go
index 9613f06..1ea8eaf 100644
--- a/internal/feed/cache/cache.go
+++ b/internal/feed/cache/cache.go
@@ -18,7 +18,7 @@ import (
type Version byte
const (
- currentVersion Version = v1Version
+ currentVersion Version = v2Version
)
type Impl interface {
@@ -56,6 +56,8 @@ 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)
}
@@ -169,11 +171,15 @@ func Load(fileName string) (Cache, error) {
return Cache{}, fmt.Errorf("decoding for version '%d' from '%s': %w", version, fileName, err)
}
- if cache, err = cache.transformTo(currentVersion); err != nil {
- return Cache{}, fmt.Errorf("cannot transform from version %d to %d: %w", version, currentVersion, err)
- }
+ if 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)
+ 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
}
diff --git a/internal/feed/cache/v1.go b/internal/feed/cache/v1.go
index 17a0346..0363303 100644
--- a/internal/feed/cache/v1.go
+++ b/internal/feed/cache/v1.go
@@ -179,8 +179,8 @@ func newV1Cache() *v1Cache {
func (cache *v1Cache) transformTo(v Version) (Impl, error) {
switch v {
- case v1Version:
- return cache, nil
+ case v2Version:
+ return (*v2Cache)(cache), nil
default:
return nil, fmt.Errorf("Transformation not supported")
}
diff --git a/internal/feed/cache/v2.go b/internal/feed/cache/v2.go
new file mode 100644
index 0000000..87636b6
--- /dev/null
+++ b/internal/feed/cache/v2.go
@@ -0,0 +1,63 @@
+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) 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()
+}