本文整理汇总了Golang中github.com/golang/snappy.Encode函数的典型用法代码示例。如果您正苦于以下问题:Golang Encode函数的具体用法?Golang Encode怎么用?Golang Encode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Encode函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: WritePoints
func (l *Log) WritePoints(points []models.Point, fields map[string]*tsdb.MeasurementFields, series []*tsdb.SeriesCreate) error {
// add everything to the cache, or return an error if we've hit our max memory
if addedToCache := l.addToCache(points, fields, series, true); !addedToCache {
return fmt.Errorf("WAL backed up flushing to index, hit max memory")
}
// make the write durable if specified
if !l.SkipDurability {
// write the points
pointStrings := make([]string, len(points))
for i, p := range points {
pointStrings[i] = p.String()
}
data := strings.Join(pointStrings, "\n")
compressed := snappy.Encode(nil, []byte(data))
if err := l.writeToLog(pointsEntry, compressed); err != nil {
return err
}
// write the new fields
if len(fields) > 0 {
data, err := json.Marshal(fields)
if err != nil {
return err
}
compressed = snappy.Encode(compressed, data)
if err := l.writeToLog(fieldsEntry, compressed); err != nil {
return err
}
}
// write the new series
if len(series) > 0 {
data, err := json.Marshal(series)
if err != nil {
return err
}
compressed = snappy.Encode(compressed, data)
if err := l.writeToLog(seriesEntry, compressed); err != nil {
return err
}
}
}
// usually skipping the cache is only for testing purposes and this was the easiest
// way to represent the logic (to cache and then immediately flush)
if l.SkipCache {
if err := l.flush(idleFlush); err != nil {
return err
}
}
return nil
}
开发者ID:jturello-ulink,项目名称:influxdb,代码行数:55,代码来源:wal.go
示例2: writeBlock
func (w *Writer) writeBlock(buf *util.Buffer, compression opt.Compression) (bh blockHandle, err error) {
// Compress the buffer if necessary.
var b []byte
if compression == opt.SnappyCompression {
// Allocate scratch enough for compression and block trailer.
if n := snappy.MaxEncodedLen(buf.Len()) + blockTrailerLen; len(w.compressionScratch) < n {
w.compressionScratch = make([]byte, n)
}
compressed := snappy.Encode(w.compressionScratch, buf.Bytes())
n := len(compressed)
b = compressed[:n+blockTrailerLen]
b[n] = blockTypeSnappyCompression
} else {
tmp := buf.Alloc(blockTrailerLen)
tmp[0] = blockTypeNoCompression
b = buf.Bytes()
}
// Calculate the checksum.
n := len(b) - 4
checksum := util.NewCRC(b[:n]).Value()
binary.LittleEndian.PutUint32(b[n:], checksum)
// Write the buffer to the file.
_, err = w.writer.Write(b)
if err != nil {
return
}
bh = blockHandle{w.offset, uint64(len(b) - blockTrailerLen)}
w.offset += uint64(len(b))
return
}
开发者ID:lessos,项目名称:lessdb,代码行数:32,代码来源:writer.go
示例3: Send
func (p *Protocol) Send(msg proto.Message) error {
if p.w == nil {
return ErrNoWriter
}
data, err := proto.Marshal(msg)
if err != nil {
return err
}
if p.compress {
data = snappy.Encode(nil, data)
}
sizeBuf := make([]byte, 4)
binary.LittleEndian.PutUint32(sizeBuf, uint32(len(data)))
if _, err := p.w.Write(sizeBuf); err != nil {
return err
}
if _, err := p.w.Write(data); err != nil {
return err
}
return nil
}
开发者ID:disorganizer,项目名称:brig,代码行数:26,代码来源:protocol.go
示例4: SendFrame
func SendFrame(w io.Writer, data proto.Message) (err error) {
// marshal response
pbData := []byte{}
if data == nil {
return errors.New("data is nil")
}
pbData, err = proto.Marshal(data)
if err != nil {
return err
}
compressedPb := snappy.Encode(nil, pbData)
err = sendFrame(w, compressedPb)
if err != nil {
return err
}
if data == nil {
return errors.New("data is nil")
}
err = proto.Unmarshal(pbData, data)
return
}
开发者ID:asyoume,项目名称:paas_srv,代码行数:28,代码来源:proto.go
示例5: writeSeries
func (e *Engine) writeSeries(series map[string]*tsdb.Series) error {
data, err := json.Marshal(series)
if err != nil {
return err
}
fn := filepath.Join(e.path, SeriesFileExtension+"tmp")
ff, err := os.OpenFile(fn, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
return err
}
_, err = ff.Write(snappy.Encode(nil, data))
if err != nil {
return err
}
if err := ff.Close(); err != nil {
return err
}
seriesFileName := filepath.Join(e.path, SeriesFileExtension)
if _, err := os.Stat(seriesFileName); !os.IsNotExist(err) {
if err := os.Remove(seriesFileName); err != nil && err != os.ErrNotExist {
return err
}
}
return os.Rename(fn, seriesFileName)
}
开发者ID:nrshrivatsan,项目名称:influxdb,代码行数:28,代码来源:tsm1.go
示例6: Fuzz
func Fuzz(data []byte) int {
n, err := snappy.DecodedLen(data)
if err != nil || n > 1e6 {
return 0
}
if n < 0 {
panic("negative decoded len")
}
dec, err := snappy.Decode(nil, data)
if err != nil {
if dec != nil {
panic("dec is not nil")
}
return 0
}
if len(dec) != n {
println(len(dec), n)
panic("bad decoded len")
}
n = snappy.MaxEncodedLen(len(dec))
enc := snappy.Encode(nil, dec)
if len(enc) > n {
panic("bad encoded len")
}
dec1, err := snappy.Decode(nil, enc)
if err != nil {
panic(err)
}
if bytes.Compare(dec, dec1) != 0 {
panic("not equal")
}
return 1
}
开发者ID:stephens2424,项目名称:go-fuzz,代码行数:34,代码来源:main.go
示例7: writeSeriesAndFields
// writeSeriesAndFields will write the compressed fields and series to the meta file. This file persists the data
// in case the server gets shutdown before the WAL has a chance to flush everything to the cache. By default this
// file is flushed on start when bz1 calls LoadMetaDataIndex
func (l *Log) writeSeriesAndFields(fields map[string]*tsdb.MeasurementFields, series []*tsdb.SeriesCreate) error {
if len(fields) == 0 && len(series) == 0 {
return nil
}
sf := &seriesAndFields{Fields: fields, Series: series}
b, err := json.Marshal(sf)
if err != nil {
return err
}
cb := snappy.Encode(nil, b)
l.mu.Lock()
defer l.mu.Unlock()
if _, err := l.metaFile.Write(u64tob(uint64(len(cb)))); err != nil {
return err
}
if _, err := l.metaFile.Write(cb); err != nil {
return err
}
return l.metaFile.Sync()
}
开发者ID:bwolf,项目名称:influxdb,代码行数:28,代码来源:wal.go
示例8: TestSnappyCompressor
func TestSnappyCompressor(t *testing.T) {
c := SnappyCompressor{}
if c.Name() != "snappy" {
t.Fatalf("expected name to be 'snappy', got %v", c.Name())
}
str := "My Test String"
//Test Encoding
expected := snappy.Encode(nil, []byte(str))
if res, err := c.Encode([]byte(str)); err != nil {
t.Fatalf("failed to encode '%v' with error %v", str, err)
} else if bytes.Compare(expected, res) != 0 {
t.Fatal("failed to match the expected encoded value with the result encoded value.")
}
val, err := c.Encode([]byte(str))
if err != nil {
t.Fatalf("failed to encode '%v' with error '%v'", str, err)
}
//Test Decoding
if expected, err := snappy.Decode(nil, val); err != nil {
t.Fatalf("failed to decode '%v' with error %v", val, err)
} else if res, err := c.Decode(val); err != nil {
t.Fatalf("failed to decode '%v' with error %v", val, err)
} else if bytes.Compare(expected, res) != 0 {
t.Fatal("failed to match the expected decoded value with the result decoded value.")
}
}
开发者ID:CrowdStrike,项目名称:gocql,代码行数:29,代码来源:compressor_test.go
示例9: Save
// Save stores data to a file.
func (d *Data) Save() error {
// open or create file
fh, err := os.OpenFile(d.File, os.O_WRONLY|os.O_CREATE|os.O_APPEND|os.O_TRUNC, 0655)
if err != nil {
return err
}
defer fh.Close()
// encode content in a buffer
buf := new(bytes.Buffer)
endata := gob.NewEncoder(buf)
endata.Encode(d.Store)
b := buf.Bytes()
// compress content
da := snappy.Encode(nil, b)
// write content to file
_, err = fh.Write(da)
if err != nil {
return err
}
return nil
}
开发者ID:catinello,项目名称:sgob,代码行数:26,代码来源:sgob.go
示例10: Put
func (b *EncryptBackend) Put(hash string, rawData []byte) (err error) {
// #blobstash/secretbox\n
// data hash\n
// data
var nonce [24]byte
//out := make([]byte, len(data) + secretbox.Overhead + 24 + headerSize)
if err := GenerateNonce(&nonce); err != nil {
return err
}
// First we compress the data with snappy
data := snappy.Encode(nil, rawData)
var out bytes.Buffer
out.WriteString("#blobstash/secretbox\n")
out.WriteString(fmt.Sprintf("%v\n", hash))
encData := make([]byte, len(data)+secretbox.Overhead)
secretbox.Seal(encData[0:0], data, &nonce, b.key)
out.Write(nonce[:])
out.Write(encData)
encHash := fmt.Sprintf("%x", blake2b.Sum256(out.Bytes()))
b.dest.Put(encHash, out.Bytes())
b.Lock()
b.index[hash] = encHash
defer b.Unlock()
blobsUploaded.Add(b.dest.String(), 1)
bytesUploaded.Add(b.dest.String(), int64(len(out.Bytes())))
return
}
开发者ID:marsmensch,项目名称:blobstash,代码行数:28,代码来源:encrypt.go
示例11: writeToLog
func (l *WAL) writeToLog(entry WALEntry) (int, error) {
// encode and compress the entry while we're not locked
bytes := make([]byte, defaultBufLen)
b, err := entry.Encode(bytes)
if err != nil {
return -1, err
}
compressed := snappy.Encode(b, b)
l.mu.Lock()
defer l.mu.Unlock()
// Make sure the log has not been closed
select {
case <-l.closing:
return -1, ErrWALClosed
default:
}
// roll the segment file if needed
if err := l.rollSegment(); err != nil {
return -1, fmt.Errorf("error rolling WAL segment: %v", err)
}
// write and sync
if err := l.currentSegmentWriter.Write(entry.Type(), compressed); err != nil {
return -1, fmt.Errorf("error writing WAL entry: %v", err)
}
l.lastWriteTime = time.Now()
return l.currentSegmentID, l.currentSegmentWriter.sync()
}
开发者ID:ThiruKumar,项目名称:go_ws,代码行数:35,代码来源:wal.go
示例12: Write
func (w *WALSegmentWriter) Write(e WALEntry) error {
bytes := make([]byte, defaultBufLen)
b, err := e.Encode(bytes)
if err != nil {
return err
}
compressed := snappy.Encode(b, b)
w.mu.Lock()
defer w.mu.Unlock()
if _, err := w.w.Write([]byte{byte(e.Type())}); err != nil {
return err
}
if _, err = w.w.Write(u32tob(uint32(len(compressed)))); err != nil {
return err
}
if _, err = w.w.Write(compressed); err != nil {
return err
}
// 5 is the 1 byte type + 4 byte uint32 length
w.size += len(compressed) + 5
return nil
}
开发者ID:kim-racktop,项目名称:influxdb,代码行数:30,代码来源:wal.go
示例13: compressor
func compressor(fw *Writer, toCompress <-chan *writerBlock, toWrite chan<- *writerBlock) {
switch fw.CompressionCodec {
case CompressionDeflate:
bb := new(bytes.Buffer)
comp, _ := flate.NewWriter(bb, flate.DefaultCompression)
for block := range toCompress {
_, block.err = comp.Write(block.encoded.Bytes())
block.err = comp.Close()
if block.err == nil {
block.compressed = bb.Bytes()
toWrite <- block
}
bb = new(bytes.Buffer)
comp.Reset(bb)
}
case CompressionNull:
for block := range toCompress {
block.compressed = block.encoded.Bytes()
toWrite <- block
}
case CompressionSnappy:
for block := range toCompress {
block.compressed = snappy.Encode(block.compressed, block.encoded.Bytes())
toWrite <- block
}
}
close(toWrite)
}
开发者ID:hhkbp2,项目名称:goavro,代码行数:28,代码来源:ocf_writer.go
示例14: writeFields
func (e *Engine) writeFields(fields map[string]*tsdb.MeasurementFields) error {
// compress and save everything
data, err := json.Marshal(fields)
if err != nil {
return err
}
fn := filepath.Join(e.path, FieldsFileExtension+"tmp")
ff, err := os.OpenFile(fn, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
return err
}
_, err = ff.Write(snappy.Encode(nil, data))
if err != nil {
return err
}
if err := ff.Close(); err != nil {
return err
}
fieldsFileName := filepath.Join(e.path, FieldsFileExtension)
if _, err := os.Stat(fieldsFileName); !os.IsNotExist(err) {
if err := os.Remove(fieldsFileName); err != nil {
return err
}
}
return os.Rename(fn, fieldsFileName)
}
开发者ID:nrshrivatsan,项目名称:influxdb,代码行数:29,代码来源:tsm1.go
示例15: WriteRecord
// Write a record
func (rw *Writer) WriteRecord(data []byte, flags Flags) error {
if rw.Err != nil {
return rw.Err
}
flags = flags | rw.Flags
if flags&NoCompression == 0 {
data = snappy.Encode(rw.compressBuf, data)
}
header := recordHeader{bodyLength: uint32(len(data)), flags: flags}
var headerBuf [recordHeaderStorageSize]byte
header.encode(headerBuf[:])
if size, _ := rw.bytesWriter.Write(headerBuf[:]); size != recordHeaderStorageSize {
return rw.err(ErrWriteBytes)
}
bodyWriter := checksumWriter{writer: rw.bytesWriter, crc: crc32.NewIEEE()}
if size, _ := bodyWriter.Write(data); size != len(data) {
return rw.err(ErrWriteBytes)
}
var checksumBuf [4]byte
binary.LittleEndian.PutUint32(checksumBuf[:], bodyWriter.checksum())
if size, _ := rw.bytesWriter.Write(checksumBuf[:]); size != len(checksumBuf) {
return rw.err(ErrWriteBytes)
}
return nil
}
开发者ID:kuangyh,项目名称:recordio,代码行数:26,代码来源:recordio.go
示例16: compressData
func (snappyMessageCompressor) compressData(dst, src []byte) (n int, err error) {
p := snappy.Encode(dst, src)
if len(p) > len(dst) {
err = io.ErrShortBuffer
}
n = len(p)
return
}
开发者ID:Machyne,项目名称:mongo,代码行数:8,代码来源:compress.go
示例17: writeSeries
func (e *Engine) writeSeries(tx *bolt.Tx, series map[string]*tsdb.Series) error {
data, err := json.Marshal(series)
if err != nil {
return err
}
return tx.Bucket([]byte("meta")).Put([]byte("series"), snappy.Encode(nil, data))
}
开发者ID:rmillner,项目名称:influxdb,代码行数:8,代码来源:bz1.go
示例18: writeDeleteEntry
func (l *Log) writeDeleteEntry(d *deleteData) error {
js, err := json.Marshal(d)
if err != nil {
return err
}
data := snappy.Encode(nil, js)
return l.writeToLog(deleteEntry, data)
}
开发者ID:jturello-ulink,项目名称:influxdb,代码行数:8,代码来源:wal.go
示例19: putByKey
func (l *internalLevelDBStore) putByKey(key []byte, c Chunk) {
l.concurrentWriteLimit <- struct{}{}
data := snappy.Encode(nil, c.Data())
err := l.db.Put(key, data, nil)
d.Chk.NoError(err)
l.putCount++
l.putBytes += int64(len(data))
<-l.concurrentWriteLimit
}
开发者ID:willhite,项目名称:noms-old,代码行数:9,代码来源:leveldb_store.go
示例20: writeFields
func (e *Engine) writeFields(tx *bolt.Tx, fields map[string]*tsdb.MeasurementFields) error {
// compress and save everything
data, err := json.Marshal(fields)
if err != nil {
return err
}
return tx.Bucket([]byte("meta")).Put([]byte("fields"), snappy.Encode(nil, data))
}
开发者ID:rmillner,项目名称:influxdb,代码行数:9,代码来源:bz1.go
注:本文中的github.com/golang/snappy.Encode函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论