| 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 memory | 5 package memory |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "encoding/binary" | 9 "encoding/binary" |
| 10 "fmt" | 10 "fmt" |
| 11 "math" | 11 "math" |
| 12 "time" | 12 "time" |
| 13 | 13 |
| 14 "appengine" | 14 "appengine" |
| 15 | 15 |
| 16 » "github.com/luci/luci-go/common/funnybase" | 16 » "github.com/luci/luci-go/common/cmpbin" |
| 17 ) | 17 ) |
| 18 | 18 |
| 19 func writeString(buf *bytes.Buffer, s string) { | 19 func writeString(buf *bytes.Buffer, s string) { |
| 20 » funnybase.WriteUint(buf, uint64(len(s))) | 20 » cmpbin.WriteUint(buf, uint64(len(s))) |
| 21 buf.WriteString(s) | 21 buf.WriteString(s) |
| 22 } | 22 } |
| 23 | 23 |
| 24 func readString(buf *bytes.Buffer) (string, error) { | 24 func readString(buf *bytes.Buffer) (string, error) { |
| 25 b, err := readBytes(buf) | 25 b, err := readBytes(buf) |
| 26 if err != nil { | 26 if err != nil { |
| 27 return "", err | 27 return "", err |
| 28 } | 28 } |
| 29 return string(b), nil | 29 return string(b), nil |
| 30 } | 30 } |
| 31 | 31 |
| 32 func writeBytes(buf *bytes.Buffer, b []byte) { | 32 func writeBytes(buf *bytes.Buffer, b []byte) { |
| 33 » funnybase.WriteUint(buf, uint64(len(b))) | 33 » cmpbin.WriteUint(buf, uint64(len(b))) |
| 34 buf.Write(b) | 34 buf.Write(b) |
| 35 } | 35 } |
| 36 | 36 |
| 37 func readBytes(buf *bytes.Buffer) ([]byte, error) { | 37 func readBytes(buf *bytes.Buffer) ([]byte, error) { |
| 38 » val, err := funnybase.ReadUint(buf) | 38 » val, _, err := cmpbin.ReadUint(buf) |
| 39 if err != nil { | 39 if err != nil { |
| 40 return nil, err | 40 return nil, err |
| 41 } | 41 } |
| 42 if val > 2*1024*1024 { // 2MB | 42 if val > 2*1024*1024 { // 2MB |
| 43 return nil, fmt.Errorf("readBytes: tried to read %d bytes (> 2MB
)", val) | 43 return nil, fmt.Errorf("readBytes: tried to read %d bytes (> 2MB
)", val) |
| 44 } | 44 } |
| 45 retBuf := make([]byte, val) | 45 retBuf := make([]byte, val) |
| 46 n, _ := buf.Read(retBuf) // err is either io.EOF or nil for bytes.Buffer | 46 n, _ := buf.Read(retBuf) // err is either io.EOF or nil for bytes.Buffer |
| 47 if uint64(n) != val { | 47 if uint64(n) != val { |
| 48 return nil, fmt.Errorf("readBytes: expected %d bytes but read %d
", val, n) | 48 return nil, fmt.Errorf("readBytes: expected %d bytes but read %d
", val, n) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 66 if err != nil { | 66 if err != nil { |
| 67 return 0, err | 67 return 0, err |
| 68 } | 68 } |
| 69 bits := binary.BigEndian.Uint64(data) | 69 bits := binary.BigEndian.Uint64(data) |
| 70 return math.Float64frombits(bits ^ (((bits >> 63) - 1) | (1 << 63))), ni
l | 70 return math.Float64frombits(bits ^ (((bits >> 63) - 1) | (1 << 63))), ni
l |
| 71 } | 71 } |
| 72 | 72 |
| 73 // We truncate this to microseconds and drop the timezone, because that's the | 73 // We truncate this to microseconds and drop the timezone, because that's the |
| 74 // way that the appengine SDK does it. Awesome, right? Also: its not documented. | 74 // way that the appengine SDK does it. Awesome, right? Also: its not documented. |
| 75 func writeTime(buf *bytes.Buffer, t time.Time) { | 75 func writeTime(buf *bytes.Buffer, t time.Time) { |
| 76 » funnybase.WriteUint(buf, uint64(t.Unix())*1e6+uint64(t.Nanosecond()/1e3)
) | 76 » cmpbin.WriteUint(buf, uint64(t.Unix())*1e6+uint64(t.Nanosecond()/1e3)) |
| 77 } | 77 } |
| 78 | 78 |
| 79 func readTime(buf *bytes.Buffer) (time.Time, error) { | 79 func readTime(buf *bytes.Buffer) (time.Time, error) { |
| 80 » v, err := funnybase.ReadUint(buf) | 80 » v, _, err := cmpbin.ReadUint(buf) |
| 81 if err != nil { | 81 if err != nil { |
| 82 return time.Time{}, err | 82 return time.Time{}, err |
| 83 } | 83 } |
| 84 return time.Unix(int64(v/1e6), int64((v%1e6)*1e3)), nil | 84 return time.Unix(int64(v/1e6), int64((v%1e6)*1e3)), nil |
| 85 } | 85 } |
| 86 | 86 |
| 87 func writeGeoPoint(buf *bytes.Buffer, gp appengine.GeoPoint) { | 87 func writeGeoPoint(buf *bytes.Buffer, gp appengine.GeoPoint) { |
| 88 writeFloat64(buf, gp.Lat) | 88 writeFloat64(buf, gp.Lat) |
| 89 writeFloat64(buf, gp.Lng) | 89 writeFloat64(buf, gp.Lng) |
| 90 } | 90 } |
| 91 | 91 |
| 92 func readGeoPoint(buf *bytes.Buffer) (pt appengine.GeoPoint, err error) { | 92 func readGeoPoint(buf *bytes.Buffer) (pt appengine.GeoPoint, err error) { |
| 93 if pt.Lat, err = readFloat64(buf); err != nil { | 93 if pt.Lat, err = readFloat64(buf); err != nil { |
| 94 return | 94 return |
| 95 } | 95 } |
| 96 pt.Lng, err = readFloat64(buf) | 96 pt.Lng, err = readFloat64(buf) |
| 97 return | 97 return |
| 98 } | 98 } |
| OLD | NEW |