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)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab