dsebywql016137 2014-11-12 05:50
浏览 174
已采纳

在Go中对Gob数据使用Snappy压缩?

I need to save a structure to disk and read it in again later, I'm trying to keep IO down to a minimum, but also not spend ages compressing and uncompressing the file, so I intend to use Snappy for compression as it's very fast and relatively efficient.

Normally I would gzip compress a gob when saving it to file, like this:

func (t *Object) Save(filename string) error {
    // Open file for writing
    fi, err := os.Create(filename)
    if err != nil {
        return err
    }
    defer fi.Close()
    // Attach gzip writer
    fz := gzip.NewWriter(fi)
    defer fz.Close()
    // Push from the gob encoder
    encoder := gob.NewEncoder(fz)
    err = encoder.Encode(t.Classifier)
    if err != nil {
        return err
    }
    return nil
}

But Snappy does not attach to these Reader/Writer interfaces that everything else seems to use. Instead it just provides the basic functionality: https://godoc.org/code.google.com/p/snappy-go/snappy

func Encode(dst, src []byte) ([]byte, error)

func Decode(dst, src []byte) ([]byte, error)

What would be the most efficient way to use this Snappy package for compressing the Gob data as its being saved to file (and also reading it back)? Ideally I don't want to use ioutil.ReadAll to just read from the gob reader into a slice of bytes and then compress that all over again as that seems to be a very heavy way of doing it which would create a lot of wasted memory.

I admit that I don't fully understand how the reader and writer interfaces work.

  • 写回答

1条回答 默认 最新

  • doudianhuo1129 2014-11-12 06:13
    关注

    package snappystream

    import "github.com/mreiferson/go-snappystream"
    

    snappystream wraps snappy-go and supplies a Reader and Writer for the snappy framed stream format.

    Have you considered package snappystream? For example,

    package main
    
    import (
        "encoding/gob"
        "fmt"
        "os"
    
        "github.com/mreiferson/go-snappystream"
    )
    
    type Object struct {
        Classifier struct{}
    }
    
    func (t *Object) Save(filename string) error {
        // Open file for writing
        fi, err := os.Create(filename)
        if err != nil {
            return err
        }
        defer fi.Close()
        // Attach snappy writer
        fs := snappystream.NewBufferedWriter(fi)
        // Push from the gob encoder
        encoder := gob.NewEncoder(fs)
        err = encoder.Encode(t.Classifier)
        if err != nil {
            return err
        }
        err = fs.Close()
        if err != nil {
            return err
        }
        err = fi.Close()
        if err != nil {
            return err
        }
        return nil
    }
    
    func main() {
        obj := Object{}
        err := obj.Save("obj.sz")
        if err != nil {
            fmt.Println(err)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?