Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package dscache | |
| 6 | |
| 7 import ( | |
| 8 "bytes" | |
| 9 "compress/zlib" | |
| 10 "io/ioutil" | |
| 11 | |
| 12 ds "github.com/luci/gae/service/datastore" | |
| 13 ) | |
| 14 | |
| 15 func encodeItemValue(pm ds.PropertyMap) []byte { | |
| 16 pm, _ = pm.Save(false) | |
| 17 | |
| 18 buf := bytes.Buffer{} | |
| 19 // errs can't happen, since we're using a byte buffer. | |
| 20 _ = buf.WriteByte(byte(NoCompression)) | |
| 21 _ = pm.Write(&buf, ds.WithoutContext) | |
| 22 | |
| 23 data := buf.Bytes() | |
| 24 if buf.Len() > CompressionThreshold { | |
| 25 buf2 := bytes.NewBuffer(make([]byte, 0, len(data))) | |
| 26 _ = buf2.WriteByte(byte(ZlibCompression)) | |
| 27 writer := zlib.NewWriter(buf2) | |
|
Vadim Sh.
2015/08/06 01:23:34
does native SDK uses same trick? I thought compres
iannucci
2015/08/06 02:37:33
This is for compressing the values inside of memca
| |
| 28 _, _ = writer.Write(data[1:]) // skip the NoCompression byte | |
| 29 writer.Close() | |
| 30 data = buf2.Bytes() | |
| 31 } | |
| 32 | |
| 33 return data | |
| 34 } | |
| 35 | |
| 36 func decodeItemValue(val []byte, ns, aid string) (ds.PropertyMap, error) { | |
| 37 if len(val) == 0 { | |
| 38 return nil, ds.ErrNoSuchEntity | |
| 39 } | |
| 40 buf := bytes.NewBuffer(val) | |
| 41 compTypeByte, err := buf.ReadByte() | |
| 42 if err != nil { | |
| 43 return nil, err | |
| 44 } | |
| 45 | |
| 46 if CompressionType(compTypeByte) == ZlibCompression { | |
| 47 reader, err := zlib.NewReader(buf) | |
| 48 if err != nil { | |
| 49 return nil, err | |
| 50 } | |
| 51 defer reader.Close() | |
| 52 data, err := ioutil.ReadAll(reader) | |
| 53 if err != nil { | |
| 54 return nil, err | |
| 55 } | |
| 56 buf = bytes.NewBuffer(data) | |
| 57 } | |
| 58 ret := ds.PropertyMap{} | |
| 59 err = ret.Read(buf, ds.WithoutContext, ns, aid) | |
| 60 return ret, err | |
| 61 } | |
| OLD | NEW |