Use gzip compression for index cache

This commit is contained in:
Jakob Borg 2014-01-01 16:31:04 -05:00
parent 232d715c37
commit 89a5aac6ea

19
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"compress/gzip"
"crypto/sha1" "crypto/sha1"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
@ -295,26 +296,36 @@ func updateLocalModel(m *Model) {
} }
func saveIndex(m *Model) { func saveIndex(m *Model) {
name := fmt.Sprintf("%x.idx", sha1.Sum([]byte(m.Dir()))) name := fmt.Sprintf("%x.idx.gz", sha1.Sum([]byte(m.Dir())))
fullName := path.Join(opts.ConfDir, name) fullName := path.Join(opts.ConfDir, name)
idxf, err := os.Create(fullName + ".tmp") idxf, err := os.Create(fullName + ".tmp")
if err != nil { if err != nil {
return return
} }
protocol.WriteIndex(idxf, m.ProtocolIndex())
gzw := gzip.NewWriter(idxf)
protocol.WriteIndex(gzw, m.ProtocolIndex())
gzw.Close()
idxf.Close() idxf.Close()
os.Rename(fullName+".tmp", fullName) os.Rename(fullName+".tmp", fullName)
} }
func loadIndex(m *Model) { func loadIndex(m *Model) {
fname := fmt.Sprintf("%x.idx", sha1.Sum([]byte(m.Dir()))) fname := fmt.Sprintf("%x.idx.gz", sha1.Sum([]byte(m.Dir())))
idxf, err := os.Open(path.Join(opts.ConfDir, fname)) idxf, err := os.Open(path.Join(opts.ConfDir, fname))
if err != nil { if err != nil {
return return
} }
defer idxf.Close() defer idxf.Close()
idx, err := protocol.ReadIndex(idxf) gzr, err := gzip.NewReader(idxf)
if err != nil {
return
}
defer gzr.Close()
idx, err := protocol.ReadIndex(gzr)
if err != nil { if err != nil {
return return
} }