Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: service/datastore/serialize/serialize.go

Issue 1550903002: impl/memory: Fix time serialization encoding. (Closed) Base URL: https://github.com/luci/gae@master
Patch Set: Deduplicate time/int conversion logic. Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 serialize 5 package serialize
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "errors" 9 "errors"
10 "fmt" 10 "fmt"
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 182
183 gp.Lng, _, e = cmpbin.ReadFloat64(buf) 183 gp.Lng, _, e = cmpbin.ReadFloat64(buf)
184 panicIf(e) 184 panicIf(e)
185 185
186 if !gp.Valid() { 186 if !gp.Valid() {
187 err = fmt.Errorf("helper: decoded invalid GeoPoint: %v", gp) 187 err = fmt.Errorf("helper: decoded invalid GeoPoint: %v", gp)
188 } 188 }
189 return 189 return
190 } 190 }
191 191
192 // WriteTime writes a time.Time in a byte-sortable way. 192 // WriteTime writes a time to the buffer.
193 // 193 //
194 // This method truncates the time to microseconds and drops the timezone, 194 // The supplied time is rounded via datastore.RoundTime and written as a
195 // because that's the (undocumented) way that the appengine SDK does it. 195 // microseconds-since-epoch integer to comform to datastore storage standards.
196 func WriteTime(buf Buffer, t time.Time) error { 196 func WriteTime(buf Buffer, t time.Time) error {
197 name, off := t.Zone() 197 name, off := t.Zone()
198 if name != "UTC" || off != 0 { 198 if name != "UTC" || off != 0 {
199 panic(fmt.Errorf("helper: UTC OR DEATH: %s", t)) 199 panic(fmt.Errorf("helper: UTC OR DEATH: %s", t))
200 } 200 }
201 » _, err := cmpbin.WriteInt(buf, t.Unix()*1e6+int64(t.Nanosecond()/1e3)) 201
202 » _, err := cmpbin.WriteInt(buf, ds.TimeToInt(t))
202 return err 203 return err
203 } 204 }
204 205
205 // ReadTime reads a time.Time from the buffer. 206 // ReadTime reads a time.Time from the buffer.
206 func ReadTime(buf Buffer) (time.Time, error) { 207 func ReadTime(buf Buffer) (time.Time, error) {
207 v, _, err := cmpbin.ReadInt(buf) 208 v, _, err := cmpbin.ReadInt(buf)
208 if err != nil { 209 if err != nil {
209 return time.Time{}, err 210 return time.Time{}, err
210 } 211 }
211 » t := time.Unix(v/1e6, (v%1e6)*1e3) 212 » return ds.IntToTime(v), nil
212 » if t.IsZero() {
213 » » return time.Time{}, nil
214 » }
215 » return t.UTC(), nil
216 } 213 }
217 214
218 // WriteProperty writes a Property to the buffer. `context` behaves the same 215 // WriteProperty writes a Property to the buffer. `context` behaves the same
219 // way that it does for WriteKey, but only has an effect if `p` contains a 216 // way that it does for WriteKey, but only has an effect if `p` contains a
220 // Key as its Value. 217 // Key as its IndexValue.
221 func WriteProperty(buf Buffer, context KeyContext, p ds.Property) (err error) { 218 func WriteProperty(buf Buffer, context KeyContext, p ds.Property) error {
219 » return writePropertyImpl(buf, context, &p, false)
220 }
221
222 // WriteIndexProperty writes a Property to the buffer as its native index type.
223 // `context` behaves the same way that it does for WriteKey, but only has an
224 // effect if `p` contains a Key as its IndexValue.
225 func WriteIndexProperty(buf Buffer, context KeyContext, p ds.Property) error {
226 » return writePropertyImpl(buf, context, &p, true)
227 }
228
229 // writePropertyImpl is an implementation of WriteProperty and
230 // WriteIndexProperty.
231 func writePropertyImpl(buf Buffer, context KeyContext, p *ds.Property, index boo l) (err error) {
222 defer recoverTo(&err) 232 defer recoverTo(&err)
223 » typb := byte(p.Type()) 233
234 » it, v := p.ForIndex()
235 » if !index {
236 » » it = p.Type()
237 » }
238 » typb := byte(it)
224 if p.IndexSetting() != ds.NoIndex { 239 if p.IndexSetting() != ds.NoIndex {
225 typb |= 0x80 240 typb |= 0x80
226 } 241 }
227 panicIf(buf.WriteByte(typb)) 242 panicIf(buf.WriteByte(typb))
228 » switch p.Type() { 243
229 » case ds.PTNull: 244 » err = writeIndexValue(buf, context, v)
230 » case ds.PTBool: 245 » return
231 » » b := p.Value().(bool) 246 }
232 » » if b { 247
233 » » » err = buf.WriteByte(1) 248 func writeIndexValue(buf Buffer, context KeyContext, v interface{}) (err error) {
234 » » } else { 249 » switch t := v.(type) {
235 » » » err = buf.WriteByte(0) 250 » case nil:
251 » case bool:
252 » » b := byte(0)
253 » » if t {
254 » » » b = 1
236 } 255 }
237 » case ds.PTInt: 256 » » err = buf.WriteByte(b)
238 » » _, err = cmpbin.WriteInt(buf, p.Value().(int64)) 257 » case int64:
239 » case ds.PTFloat: 258 » » _, err = cmpbin.WriteInt(buf, t)
240 » » _, err = cmpbin.WriteFloat64(buf, p.Value().(float64)) 259 » case float64:
241 » case ds.PTString: 260 » » _, err = cmpbin.WriteFloat64(buf, t)
242 » » _, err = cmpbin.WriteString(buf, p.Value().(string)) 261 » case string:
243 » case ds.PTBytes: 262 » » _, err = cmpbin.WriteString(buf, t)
244 » » _, err = cmpbin.WriteBytes(buf, p.Value().([]byte)) 263 » case []byte:
245 » case ds.PTTime: 264 » » _, err = cmpbin.WriteBytes(buf, t)
246 » » err = WriteTime(buf, p.Value().(time.Time)) 265 » case ds.GeoPoint:
247 » case ds.PTGeoPoint: 266 » » err = WriteGeoPoint(buf, t)
248 » » err = WriteGeoPoint(buf, p.Value().(ds.GeoPoint)) 267 » case *ds.Key:
249 » case ds.PTKey: 268 » » err = WriteKey(buf, context, t)
250 » » err = WriteKey(buf, context, p.Value().(*ds.Key)) 269
251 » case ds.PTBlobKey: 270 » default:
252 » » _, err = cmpbin.WriteString(buf, string(p.Value().(blobstore.Key ))) 271 » » err = fmt.Errorf("unsupported type: %T", t)
253 } 272 }
254 return 273 return
255 } 274 }
256 275
257 // ReadProperty reads a Property from the buffer. `context`, `appid`, and 276 // ReadProperty reads a Property from the buffer. `context`, `appid`, and
258 // `namespace` behave the same way they do for ReadKey, but only have an 277 // `namespace` behave the same way they do for ReadKey, but only have an
259 // effect if the decoded property has a Key value. 278 // effect if the decoded property has a Key value.
260 func ReadProperty(buf Buffer, context KeyContext, appid, namespace string) (p ds .Property, err error) { 279 func ReadProperty(buf Buffer, context KeyContext, appid, namespace string) (p ds .Property, err error) {
261 val := interface{}(nil) 280 val := interface{}(nil)
262 b, err := buf.ReadByte() 281 b, err := buf.ReadByte()
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 func (s SerializedPslice) Less(i, j int) bool { return bytes.Compare(s[i], s[j]) < 0 } 478 func (s SerializedPslice) Less(i, j int) bool { return bytes.Compare(s[i], s[j]) < 0 }
460 479
461 // PropertySlice serializes a single row of a DSProperty map. 480 // PropertySlice serializes a single row of a DSProperty map.
462 func PropertySlice(vals ds.PropertySlice) SerializedPslice { 481 func PropertySlice(vals ds.PropertySlice) SerializedPslice {
463 dups := stringset.New(0) 482 dups := stringset.New(0)
464 ret := make(SerializedPslice, 0, len(vals)) 483 ret := make(SerializedPslice, 0, len(vals))
465 for _, v := range vals { 484 for _, v := range vals {
466 if v.IndexSetting() == ds.NoIndex { 485 if v.IndexSetting() == ds.NoIndex {
467 continue 486 continue
468 } 487 }
469 » » data := ToBytes(v.ForIndex()) 488
489 » » data := ToBytes(v)
470 dataS := string(data) 490 dataS := string(data)
471 if !dups.Add(dataS) { 491 if !dups.Add(dataS) {
472 continue 492 continue
473 } 493 }
474 ret = append(ret, data) 494 ret = append(ret, data)
475 } 495 }
476 return ret 496 return ret
477 } 497 }
478 498
479 // SerializedPmap maps from 499 // SerializedPmap maps from
(...skipping 17 matching lines...) Expand all
497 for k, vals := range pm { 517 for k, vals := range pm {
498 newVals := PropertySlice(vals) 518 newVals := PropertySlice(vals)
499 if len(newVals) > 0 { 519 if len(newVals) > 0 {
500 ret[k] = newVals 520 ret[k] = newVals
501 } 521 }
502 } 522 }
503 return 523 return
504 } 524 }
505 525
506 func toBytesErr(i interface{}, ctx KeyContext) (ret []byte, err error) { 526 func toBytesErr(i interface{}, ctx KeyContext) (ret []byte, err error) {
507 » buf := &bytes.Buffer{} 527 » buf := bytes.Buffer{}
508 » switch x := i.(type) {
509 » case ds.GeoPoint:
510 » » err = WriteGeoPoint(buf, x)
511 528
529 switch t := i.(type) {
512 case ds.IndexColumn: 530 case ds.IndexColumn:
513 » » err = WriteIndexColumn(buf, x) 531 » » err = WriteIndexColumn(&buf, t)
514 532
515 case ds.IndexDefinition: 533 case ds.IndexDefinition:
516 » » err = WriteIndexDefinition(buf, x) 534 » » err = WriteIndexDefinition(&buf, t)
517
518 » case *ds.Key:
519 » » err = WriteKey(buf, ctx, x)
520 535
521 case ds.KeyTok: 536 case ds.KeyTok:
522 » » err = WriteKeyTok(buf, x) 537 » » err = WriteKeyTok(&buf, t)
523 538
524 case ds.Property: 539 case ds.Property:
525 » » err = WriteProperty(buf, ctx, x) 540 » » err = WriteIndexProperty(&buf, ctx, t)
526 541
527 case ds.PropertyMap: 542 case ds.PropertyMap:
528 » » err = WritePropertyMap(buf, ctx, x) 543 » » err = WritePropertyMap(&buf, ctx, t)
529
530 » case time.Time:
531 » » err = WriteTime(buf, x)
532 544
533 default: 545 default:
534 » » err = fmt.Errorf("unknown type for ToBytes: %T", i) 546 » » _, v := ds.MkProperty(i).ForIndex()
547 » » err = writeIndexValue(&buf, ctx, v)
535 } 548 }
549
536 if err == nil { 550 if err == nil {
537 ret = buf.Bytes() 551 ret = buf.Bytes()
538 } 552 }
539 return 553 return
540 } 554 }
541 555
542 // ToBytesErr serializes i to a byte slice, if it's one of the type supported 556 // ToBytesErr serializes i to a byte slice, if it's one of the type supported
543 // by this library, otherwise it returns an error. 557 // by this library, otherwise it returns an error.
544 // 558 //
545 // Key types will be serialized using the 'WithoutContext' option (e.g. their 559 // Key types will be serialized using the 'WithoutContext' option (e.g. their
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
593 } 607 }
594 } 608 }
595 609
596 func recoverTo(err *error) { 610 func recoverTo(err *error) {
597 if r := recover(); r != nil { 611 if r := recover(); r != nil {
598 if rerr := r.(parseError); rerr != nil { 612 if rerr := r.(parseError); rerr != nil {
599 *err = error(rerr) 613 *err = error(rerr)
600 } 614 }
601 } 615 }
602 } 616 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698