aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2021-02-27 22:26:34 +0100
committerRené 'Necoro' Neumann <necoro@necoro.eu>2021-02-27 22:26:34 +0100
commitf4ea0b4fca4fae270d785b84e9b105081826754f (patch)
tree79deeecf52872cfeaad45ba23d975e52f64c718b
parentae96e1bd2516ff2c70ad9dc010da84b7a03b9a35 (diff)
downloadfeed2imap-go-f4ea0b4fca4fae270d785b84e9b105081826754f.tar.gz
feed2imap-go-f4ea0b4fca4fae270d785b84e9b105081826754f.tar.bz2
feed2imap-go-f4ea0b4fca4fae270d785b84e9b105081826754f.zip
New cache format using gzip compression
-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()
+}