| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package dscache | 5 package dscache |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "compress/zlib" | 9 "compress/zlib" |
| 10 "io/ioutil" | 10 "io/ioutil" |
| 11 | 11 |
| 12 ds "github.com/luci/gae/service/datastore" | 12 ds "github.com/luci/gae/service/datastore" |
| 13 "github.com/luci/gae/service/datastore/serialize" |
| 13 ) | 14 ) |
| 14 | 15 |
| 15 func encodeItemValue(pm ds.PropertyMap) []byte { | 16 func encodeItemValue(pm ds.PropertyMap) []byte { |
| 16 pm, _ = pm.Save(false) | 17 pm, _ = pm.Save(false) |
| 17 | 18 |
| 18 buf := bytes.Buffer{} | 19 buf := bytes.Buffer{} |
| 19 // errs can't happen, since we're using a byte buffer. | 20 // errs can't happen, since we're using a byte buffer. |
| 20 _ = buf.WriteByte(byte(NoCompression)) | 21 _ = buf.WriteByte(byte(NoCompression)) |
| 21 » _ = pm.Write(&buf, ds.WithoutContext) | 22 » _ = serialize.WritePropertyMap(&buf, serialize.WithoutContext, pm) |
| 22 | 23 |
| 23 data := buf.Bytes() | 24 data := buf.Bytes() |
| 24 if buf.Len() > CompressionThreshold { | 25 if buf.Len() > CompressionThreshold { |
| 25 buf2 := bytes.NewBuffer(make([]byte, 0, len(data))) | 26 buf2 := bytes.NewBuffer(make([]byte, 0, len(data))) |
| 26 _ = buf2.WriteByte(byte(ZlibCompression)) | 27 _ = buf2.WriteByte(byte(ZlibCompression)) |
| 27 writer := zlib.NewWriter(buf2) | 28 writer := zlib.NewWriter(buf2) |
| 28 _, _ = writer.Write(data[1:]) // skip the NoCompression byte | 29 _, _ = writer.Write(data[1:]) // skip the NoCompression byte |
| 29 writer.Close() | 30 writer.Close() |
| 30 data = buf2.Bytes() | 31 data = buf2.Bytes() |
| 31 } | 32 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 48 if err != nil { | 49 if err != nil { |
| 49 return nil, err | 50 return nil, err |
| 50 } | 51 } |
| 51 defer reader.Close() | 52 defer reader.Close() |
| 52 data, err := ioutil.ReadAll(reader) | 53 data, err := ioutil.ReadAll(reader) |
| 53 if err != nil { | 54 if err != nil { |
| 54 return nil, err | 55 return nil, err |
| 55 } | 56 } |
| 56 buf = bytes.NewBuffer(data) | 57 buf = bytes.NewBuffer(data) |
| 57 } | 58 } |
| 58 » ret := ds.PropertyMap{} | 59 » return serialize.ReadPropertyMap(buf, serialize.WithoutContext, ns, aid) |
| 59 » err = ret.Read(buf, ds.WithoutContext, ns, aid) | |
| 60 » return ret, err | |
| 61 } | 60 } |
| OLD | NEW |