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 datastore | 5 package datastore |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | |
8 "encoding/base64" | 9 "encoding/base64" |
9 "errors" | 10 "errors" |
10 "fmt" | 11 "fmt" |
11 "math" | 12 "math" |
12 "reflect" | 13 "reflect" |
13 "time" | 14 "time" |
14 | 15 |
15 "github.com/luci/gae/service/blobstore" | 16 "github.com/luci/gae/service/blobstore" |
16 ) | 17 ) |
17 | 18 |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
167 "PTUnknown (and therefore PropertyType) exceeds 0x7e. Th is conflicts " + | 168 "PTUnknown (and therefore PropertyType) exceeds 0x7e. Th is conflicts " + |
168 "with serialize.WriteProperty's use of the high bit to indicate " + | 169 "with serialize.WriteProperty's use of the high bit to indicate " + |
169 "NoIndex and/or \"impl/memory\".increment's abil ity to guarantee " + | 170 "NoIndex and/or \"impl/memory\".increment's abil ity to guarantee " + |
170 "incrementability.") | 171 "incrementability.") |
171 } | 172 } |
172 } | 173 } |
173 | 174 |
174 // Property is a value plus an indicator of whether the value should be | 175 // Property is a value plus an indicator of whether the value should be |
175 // indexed. Name and Multiple are stored in the PropertyMap object. | 176 // indexed. Name and Multiple are stored in the PropertyMap object. |
176 type Property struct { | 177 type Property struct { |
177 » value interface{} | 178 » // value is the Property's value. It is stored in an internal, opaque ty pe and |
179 » // should not be directly exported to consumers. Rather, it can be acces sed | |
180 » // in its original original value via Value(). | |
181 » // | |
182 » // Specifically: | |
183 » //» - []byte- and string-based values are stored in a bytesByteSeque nce and | |
184 » //» stringByteSequence respectively. | |
185 » //» - time.Time is stored as an int64. | |
186 » value interface{} | |
187 | |
178 indexSetting IndexSetting | 188 indexSetting IndexSetting |
179 propType PropertyType | 189 propType PropertyType |
180 } | 190 } |
181 | 191 |
182 // MkProperty makes a new indexed* Property and returns it. If val is an | 192 // MkProperty makes a new indexed* Property and returns it. If val is an |
183 // invalid value, this panics (so don't do it). If you want to handle the error | 193 // invalid value, this panics (so don't do it). If you want to handle the error |
184 // normally, use SetValue(..., ShouldIndex) instead. | 194 // normally, use SetValue(..., ShouldIndex) instead. |
185 // | 195 // |
186 // *indexed if val is not an unindexable type like []byte. | 196 // *indexed if val is not an unindexable type like []byte. |
187 func MkProperty(val interface{}) Property { | 197 func MkProperty(val interface{}) Property { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
238 err := error(nil) | 248 err := error(nil) |
239 if checkValid && !x.Valid() { | 249 if checkValid && !x.Valid() { |
240 err = errors.New("invalid GeoPoint value") | 250 err = errors.New("invalid GeoPoint value") |
241 } | 251 } |
242 return PTGeoPoint, err | 252 return PTGeoPoint, err |
243 default: | 253 default: |
244 return PTUnknown, fmt.Errorf("gae: Property has bad type %T", v) | 254 return PTUnknown, fmt.Errorf("gae: Property has bad type %T", v) |
245 } | 255 } |
246 } | 256 } |
247 | 257 |
258 // RoundTime rounds a time.Time to microseconds, which is the (undocumented) | |
259 // way that the AppEngine SDK stores it. | |
260 func RoundTime(t time.Time) time.Time { | |
261 return t.Round(time.Microsecond) | |
262 } | |
263 | |
248 // timeLocationIsUTC tests if two time.Location are equal. | 264 // timeLocationIsUTC tests if two time.Location are equal. |
249 // | 265 // |
250 // This is tricky using the standard time API, as time is implicitly normalized | 266 // This is tricky using the standard time API, as time is implicitly normalized |
251 // to UTC and all equality checks are performed relative to that normalized | 267 // to UTC and all equality checks are performed relative to that normalized |
252 // time. To compensate, we instantiate two new time.Time using the respective | 268 // time. To compensate, we instantiate two new time.Time using the respective |
253 // Locations. | 269 // Locations. |
254 func timeLocationIsUTC(l *time.Location) bool { | 270 func timeLocationIsUTC(l *time.Location) bool { |
255 return time.Date(1970, 1, 1, 0, 0, 0, 0, l).Equal(utcTestTime) | 271 return time.Date(1970, 1, 1, 0, 0, 0, 0, l).Equal(utcTestTime) |
256 } | 272 } |
257 | 273 |
(...skipping 17 matching lines...) Expand all Loading... | |
275 o = v.String() | 291 o = v.String() |
276 } | 292 } |
277 case reflect.Float32, reflect.Float64: | 293 case reflect.Float32, reflect.Float64: |
278 o = v.Float() | 294 o = v.Float() |
279 case reflect.Slice: | 295 case reflect.Slice: |
280 if t.Elem().Kind() == reflect.Uint8 { | 296 if t.Elem().Kind() == reflect.Uint8 { |
281 o = v.Bytes() | 297 o = v.Bytes() |
282 } | 298 } |
283 case reflect.Struct: | 299 case reflect.Struct: |
284 if t == typeOfTime { | 300 if t == typeOfTime { |
285 // time in a Property can only hold microseconds | |
286 tim := v.Interface().(time.Time) | 301 tim := v.Interface().(time.Time) |
287 if !tim.IsZero() { | 302 if !tim.IsZero() { |
288 » » » » o = v.Interface().(time.Time).Round(time.Microse cond) | 303 » » » » o = RoundTime(v.Interface().(time.Time)) |
289 } | 304 } |
290 } | 305 } |
291 } | 306 } |
292 return o | 307 return o |
293 } | 308 } |
294 | 309 |
295 // Value returns the current value held by this property. It's guaranteed to | 310 // Value returns the current value held by this property. It's guaranteed to |
296 // be a valid value type (i.e. `p.SetValue(p.Value(), true)` will never return | 311 // be a valid value type (i.e. `p.SetValue(p.Value(), true)` will never return |
297 // an error). | 312 // an error). |
298 func (p *Property) Value() interface{} { return p.value } | 313 func (p *Property) Value() (v interface{}) { |
314 » v = p.value | |
315 | |
316 » switch p.propType { | |
317 » case PTBytes: | |
318 » » v = v.(byteSequence).Bytes() | |
319 » case PTString: | |
320 » » v = v.(byteSequence).String() | |
321 » case PTBlobKey: | |
322 » » v = blobstore.Key(v.(byteSequence).String()) | |
323 | |
324 » case PTTime: | |
325 » » v = IntToTime(v.(int64)) | |
326 » } | |
327 | |
328 » return | |
329 } | |
299 | 330 |
300 // IndexSetting says whether or not the datastore should create indicies for | 331 // IndexSetting says whether or not the datastore should create indicies for |
301 // this value. | 332 // this value. |
302 func (p *Property) IndexSetting() IndexSetting { return p.indexSetting } | 333 func (p *Property) IndexSetting() IndexSetting { return p.indexSetting } |
303 | 334 |
304 // Type is the PT* type of the data contained in Value(). | 335 // Type is the PT* type of the data contained in Value(). |
305 func (p *Property) Type() PropertyType { return p.propType } | 336 func (p *Property) Type() PropertyType { return p.propType } |
306 | 337 |
307 // SetValue sets the Value field of a Property, and ensures that its value | 338 // SetValue sets the Value field of a Property, and ensures that its value |
308 // conforms to the permissible types. That way, you're guaranteed that if you | 339 // conforms to the permissible types. That way, you're guaranteed that if you |
(...skipping 26 matching lines...) Expand all Loading... | |
335 // a nil-valued property into a struct will set that field to the zero | 366 // a nil-valued property into a struct will set that field to the zero |
336 // value. | 367 // value. |
337 func (p *Property) SetValue(value interface{}, is IndexSetting) (err error) { | 368 func (p *Property) SetValue(value interface{}, is IndexSetting) (err error) { |
338 pt := PTNull | 369 pt := PTNull |
339 if value != nil { | 370 if value != nil { |
340 value = UpconvertUnderlyingType(value) | 371 value = UpconvertUnderlyingType(value) |
341 if pt, err = PropertyTypeOf(value, true); err != nil { | 372 if pt, err = PropertyTypeOf(value, true); err != nil { |
342 return | 373 return |
343 } | 374 } |
344 } | 375 } |
376 | |
377 // Convert value to underlying datastore type. | |
378 switch t := value.(type) { | |
379 case string: | |
380 value = stringByteSequence(t) | |
381 case blobstore.Key: | |
382 value = stringByteSequence(t) | |
383 case []byte: | |
384 value = bytesByteSequence(t) | |
385 case time.Time: | |
386 t = RoundTime(t) | |
387 if t.IsZero() { | |
388 value = int64(0) | |
389 } else { | |
390 value = t.Unix()*1e6 + int64(t.Nanosecond()/1e3) | |
391 } | |
392 } | |
393 | |
345 p.propType = pt | 394 p.propType = pt |
346 p.value = value | 395 p.value = value |
347 p.indexSetting = is | 396 p.indexSetting = is |
348 return | 397 return |
349 } | 398 } |
350 | 399 |
351 // ForIndex gets a new Property with its value and type converted as if it were | 400 // ForIndex gets a new Property with its value and type converted as if it were |
352 // being stored in a datastore index. See the doc on PropertyType for more info. | 401 // being stored in a datastore index. See the doc on PropertyType for more info. |
353 func (p Property) ForIndex() Property { | 402 func (p Property) ForIndex() (PropertyType, interface{}) { |
354 » switch p.propType { | 403 » switch t := p.propType; t { |
355 » case PTNull, PTInt, PTBool, PTString, PTFloat, PTGeoPoint, PTKey: | 404 » case PTNull, PTInt, PTBool, PTFloat, PTGeoPoint, PTKey: |
356 » » return p | 405 » » return t, p.Value() |
357 | 406 |
358 case PTTime: | 407 case PTTime: |
359 » » v, _ := p.Project(PTInt) | 408 » » return PTInt, p.value |
360 » » return Property{v, p.indexSetting, PTInt} | |
361 | 409 |
362 » case PTBytes, PTBlobKey: | 410 » case PTString, PTBytes, PTBlobKey: |
363 » » v, _ := p.Project(PTString) | 411 » » return PTString, p.value.(byteSequence).Value() |
iannucci
2016/01/02 04:11:07
but this is still wrong... if it's PTBytes, this w
dnj
2016/01/02 06:39:45
That is correct, and that's a new and now-desired
| |
364 » » return Property{v, p.indexSetting, PTString} | 412 |
413 » default: | |
414 » » panic(fmt.Errorf("unknown PropertyType: %s", t)) | |
365 } | 415 } |
366 » panic(fmt.Errorf("unknown PropertyType: %s", p.propType)) | 416 } |
417 | |
418 // TimeToInt converts a time value to a datastore-appropraite integer value. | |
419 // | |
420 // This method truncates the time to microseconds and drops the timezone, | |
421 // because that's the (undocumented) way that the appengine SDK does it. | |
422 func TimeToInt(t time.Time) int64 { | |
423 » t = RoundTime(t) | |
424 » return t.Unix()*1e6 + int64(t.Nanosecond()/1e3) | |
425 } | |
426 | |
427 // IntToTime converts a datastore time integer into its time.Time value. | |
428 func IntToTime(v int64) time.Time { | |
429 » if v == 0 { | |
430 » » return time.Time{} | |
431 » } | |
432 » return RoundTime(time.Unix(int64(v/1e6), int64((v%1e6)*1e3))).UTC() | |
367 } | 433 } |
368 | 434 |
369 // Project can be used to project a Property retrieved from a Projection query | 435 // Project can be used to project a Property retrieved from a Projection query |
370 // into a different datatype. For example, if you have a PTInt property, you | 436 // into a different datatype. For example, if you have a PTInt property, you |
371 // could Project(PTTime) to convert it to a time.Time. The following conversions | 437 // could Project(PTTime) to convert it to a time.Time. The following conversions |
372 // are supported: | 438 // are supported: |
439 // PTString <-> PTBlobKey | |
440 // PTString <-> PTBytes | |
373 // PTXXX <-> PTXXX (i.e. identity) | 441 // PTXXX <-> PTXXX (i.e. identity) |
374 // PTInt <-> PTTime | 442 // PTInt <-> PTTime |
375 // PTString <-> PTBlobKey | |
376 // PTString <-> PTBytes | |
377 // PTNull <-> Anything | 443 // PTNull <-> Anything |
378 func (p *Property) Project(to PropertyType) (interface{}, error) { | 444 func (p *Property) Project(to PropertyType) (interface{}, error) { |
379 » switch { | 445 » if to == PTNull { |
380 » case to == p.propType: | 446 » » return nil, nil |
381 » » return p.value, nil | 447 » } |
382 | 448 |
383 » case to == PTInt && p.propType == PTTime: | 449 » pt, v := p.propType, p.value |
384 » » t := p.value.(time.Time) | 450 » switch pt { |
385 » » v := uint64(t.Unix())*1e6 + uint64(t.Nanosecond()/1e3) | 451 » case PTBytes, PTString, PTBlobKey: |
386 » » return int64(v), nil | 452 » » v := v.(byteSequence) |
453 » » switch to { | |
454 » » case PTBytes: | |
455 » » » return v.Bytes(), nil | |
456 » » case PTString: | |
457 » » » return v.String(), nil | |
458 » » case PTBlobKey: | |
459 » » » return blobstore.Key(v.String()), nil | |
460 » » } | |
387 | 461 |
388 » case to == PTTime && p.propType == PTInt: | 462 » case PTTime: |
389 » » v := p.value.(int64) | 463 » » switch to { |
390 » » t := time.Unix(int64(v/1e6), int64((v%1e6)*1e3)) | 464 » » case PTTime: |
391 » » if t.IsZero() { | 465 » » » return IntToTime(v.(int64)), nil |
392 » » » return time.Time{}, nil | 466 » » case PTInt: |
467 » » » return v, nil | |
393 } | 468 } |
394 return t.UTC(), nil | |
395 | 469 |
396 » case to == PTString && p.propType == PTBytes: | 470 » case PTInt: |
397 » » return string(p.value.([]byte)), nil | 471 » » switch to { |
472 » » case PTInt: | |
473 » » » return v, nil | |
474 » » case PTTime: | |
475 » » » return IntToTime(v.(int64)), nil | |
476 » » } | |
398 | 477 |
399 » case to == PTString && p.propType == PTBlobKey: | 478 » case to: |
400 » » return string(p.value.(blobstore.Key)), nil | 479 » » return v, nil |
401 | 480 |
402 » case to == PTBytes && p.propType == PTString: | 481 » case PTNull: |
403 » » return []byte(p.value.(string)), nil | |
404 | |
405 » case to == PTBlobKey && p.propType == PTString: | |
406 » » return blobstore.Key(p.value.(string)), nil | |
407 | |
408 » case to == PTNull: | |
409 » » return nil, nil | |
410 | |
411 » case p.propType == PTNull: | |
412 switch to { | 482 switch to { |
413 case PTInt: | 483 case PTInt: |
414 return int64(0), nil | 484 return int64(0), nil |
415 case PTTime: | 485 case PTTime: |
416 return time.Time{}, nil | 486 return time.Time{}, nil |
417 case PTBool: | 487 case PTBool: |
418 return false, nil | 488 return false, nil |
419 case PTBytes: | 489 case PTBytes: |
420 return []byte(nil), nil | 490 return []byte(nil), nil |
421 case PTString: | 491 case PTString: |
422 return "", nil | 492 return "", nil |
423 case PTFloat: | 493 case PTFloat: |
424 return float64(0), nil | 494 return float64(0), nil |
425 case PTGeoPoint: | 495 case PTGeoPoint: |
426 return GeoPoint{}, nil | 496 return GeoPoint{}, nil |
427 case PTKey: | 497 case PTKey: |
428 return nil, nil | 498 return nil, nil |
429 case PTBlobKey: | 499 case PTBlobKey: |
430 return blobstore.Key(""), nil | 500 return blobstore.Key(""), nil |
431 } | 501 } |
432 fallthrough | |
433 default: | |
434 return nil, fmt.Errorf("unable to project %s to %s", p.propType, to) | |
435 } | 502 } |
503 return nil, fmt.Errorf("unable to project %s to %s", pt, to) | |
436 } | 504 } |
437 | 505 |
438 func cmpVals(a, b interface{}, t PropertyType) int { | 506 func cmpFloat(a, b float64) int { |
439 » cmpFloat := func(a, b float64) int { | 507 » if a == b { |
440 » » if a == b { | 508 » » return 0 |
441 » » » return 0 | 509 » } |
442 » » } | 510 » if a > b { |
443 » » if a > b { | 511 » » return 1 |
444 » » » return 1 | 512 » } |
445 » » } | 513 » return -1 |
514 } | |
515 | |
516 // Less returns true iff p would sort before other. | |
517 // | |
518 // This uses datastore's index rules for sorting (e.g. | |
519 // []byte("hello") == "hello") | |
520 func (p *Property) Less(other *Property) bool { | |
521 » return p.Compare(other) < 0 | |
522 } | |
523 | |
524 // Equal returns true iff p and other have identical index representations. | |
525 // | |
526 // This uses datastore's index rules for sorting (e.g. | |
527 // []byte("hello") == "hello") | |
528 func (p *Property) Equal(other *Property) bool { | |
529 » return p.Compare(other) == 0 | |
530 } | |
531 | |
532 // Compare compares this Property to another, returning a trinary value | |
533 // indicating where it would sort relative to the other in datastore. | |
534 // | |
535 // It returns: | |
536 //» <0 if the Property would sort before `other`. | |
537 //» >0 if the Property would after before `other`. | |
538 //» 0 if the Property equals `other`. | |
539 func (p *Property) Compare(other *Property) int { | |
540 » if p.indexSetting && !other.indexSetting { | |
541 » » return 1 | |
542 » } else if !p.indexSetting && other.indexSetting { | |
446 return -1 | 543 return -1 |
447 } | 544 } |
448 | 545 |
449 » switch t { | 546 » at, av := p.ForIndex() |
547 » bt, bv := other.ForIndex() | |
548 » if cmp := int(at) - int(bt); cmp != 0 { | |
549 » » return cmp | |
550 » } | |
551 | |
552 » switch t := at; t { | |
450 case PTNull: | 553 case PTNull: |
451 return 0 | 554 return 0 |
452 | 555 |
453 case PTBool: | 556 case PTBool: |
454 » » a, b := a.(bool), b.(bool) | 557 » » a, b := av.(bool), bv.(bool) |
455 if a == b { | 558 if a == b { |
456 return 0 | 559 return 0 |
457 } | 560 } |
458 if a && !b { | 561 if a && !b { |
459 return 1 | 562 return 1 |
460 } | 563 } |
461 return -1 | 564 return -1 |
462 | 565 |
463 case PTInt: | 566 case PTInt: |
464 » » a, b := a.(int64), b.(int64) | 567 » » a, b := av.(int64), bv.(int64) |
465 if a == b { | 568 if a == b { |
466 return 0 | 569 return 0 |
467 } | 570 } |
468 if a > b { | 571 if a > b { |
469 return 1 | 572 return 1 |
470 } | 573 } |
471 return -1 | 574 return -1 |
472 | 575 |
473 case PTString: | 576 case PTString: |
474 » » a, b := a.(string), b.(string) | 577 » » return cmpByteSequence(p.value.(byteSequence), other.value.(byte Sequence)) |
475 » » if a == b { | |
476 » » » return 0 | |
477 » » } | |
478 » » if a > b { | |
479 » » » return 1 | |
480 » » } | |
481 » » return -1 | |
482 | 578 |
483 case PTFloat: | 579 case PTFloat: |
484 » » return cmpFloat(a.(float64), b.(float64)) | 580 » » return cmpFloat(av.(float64), bv.(float64)) |
485 | 581 |
486 case PTGeoPoint: | 582 case PTGeoPoint: |
487 » » a, b := a.(GeoPoint), b.(GeoPoint) | 583 » » a, b := av.(GeoPoint), bv.(GeoPoint) |
488 cmp := cmpFloat(a.Lat, b.Lat) | 584 cmp := cmpFloat(a.Lat, b.Lat) |
489 if cmp != 0 { | 585 if cmp != 0 { |
490 return cmp | 586 return cmp |
491 } | 587 } |
492 return cmpFloat(a.Lng, b.Lng) | 588 return cmpFloat(a.Lng, b.Lng) |
493 | 589 |
494 case PTKey: | 590 case PTKey: |
495 » » a, b := a.(*Key), b.(*Key) | 591 » » a, b := av.(*Key), bv.(*Key) |
496 if a.Equal(b) { | 592 if a.Equal(b) { |
497 return 0 | 593 return 0 |
498 } | 594 } |
499 if b.Less(a) { | 595 if b.Less(a) { |
500 return 1 | 596 return 1 |
501 } | 597 } |
502 return -1 | 598 return -1 |
503 | 599 |
504 default: | 600 default: |
505 panic(fmt.Errorf("uncomparable type: %s", t)) | 601 panic(fmt.Errorf("uncomparable type: %s", t)) |
506 } | 602 } |
507 } | 603 } |
508 | 604 |
509 // Less returns true iff p would sort before other. | |
510 // | |
511 // This uses datastore's index rules for sorting (e.g. | |
512 // []byte("hello") == "hello") | |
513 func (p *Property) Less(other *Property) bool { | |
514 if p.indexSetting && !other.indexSetting { | |
515 return true | |
516 } else if !p.indexSetting && other.indexSetting { | |
517 return false | |
518 } | |
519 a, b := p.ForIndex(), other.ForIndex() | |
520 cmp := int(a.propType) - int(b.propType) | |
521 if cmp < 0 { | |
522 return true | |
523 } else if cmp > 0 { | |
524 return false | |
525 } | |
526 return cmpVals(a.value, b.value, a.propType) < 0 | |
527 } | |
528 | |
529 // Equal returns true iff p and other have identical index representations. | |
530 // | |
531 // This uses datastore's index rules for sorting (e.g. | |
532 // []byte("hello") == "hello") | |
533 func (p *Property) Equal(other *Property) bool { | |
534 ret := p.indexSetting == other.indexSetting | |
535 if ret { | |
536 a, b := p.ForIndex(), other.ForIndex() | |
537 ret = a.propType == b.propType && cmpVals(a.value, b.value, a.pr opType) == 0 | |
538 } | |
539 return ret | |
540 } | |
541 | |
542 // GQL returns a correctly formatted Cloud Datastore GQL literal which | 605 // GQL returns a correctly formatted Cloud Datastore GQL literal which |
543 // is valid for a comparison value in the `WHERE` clause. | 606 // is valid for a comparison value in the `WHERE` clause. |
544 // | 607 // |
545 // The flavor of GQL that this emits is defined here: | 608 // The flavor of GQL that this emits is defined here: |
546 // https://cloud.google.com/datastore/docs/apis/gql/gql_reference | 609 // https://cloud.google.com/datastore/docs/apis/gql/gql_reference |
547 // | 610 // |
548 // NOTE: GeoPoint values are emitted with speculated future syntax. There is | 611 // NOTE: GeoPoint values are emitted with speculated future syntax. There is |
549 // currently no syntax for literal GeoPoint values. | 612 // currently no syntax for literal GeoPoint values. |
550 func (p *Property) GQL() string { | 613 func (p *Property) GQL() string { |
614 v := p.Value() | |
551 switch p.propType { | 615 switch p.propType { |
552 case PTNull: | 616 case PTNull: |
553 return "NULL" | 617 return "NULL" |
554 | 618 |
555 case PTInt, PTFloat, PTBool: | 619 case PTInt, PTFloat, PTBool: |
556 » » return fmt.Sprint(p.value) | 620 » » return fmt.Sprint(v) |
557 | 621 |
558 case PTString: | 622 case PTString: |
559 » » return gqlQuoteString(p.value.(string)) | 623 » » return gqlQuoteString(v.(string)) |
560 | 624 |
561 case PTBytes: | 625 case PTBytes: |
562 return fmt.Sprintf("BLOB(%q)", | 626 return fmt.Sprintf("BLOB(%q)", |
563 » » » base64.URLEncoding.EncodeToString(p.value.([]byte))) | 627 » » » base64.URLEncoding.EncodeToString(v.([]byte))) |
564 | 628 |
565 case PTBlobKey: | 629 case PTBlobKey: |
566 return fmt.Sprintf("BLOBKEY(%s)", gqlQuoteString( | 630 return fmt.Sprintf("BLOBKEY(%s)", gqlQuoteString( |
567 » » » string(p.value.(blobstore.Key)))) | 631 » » » string(v.(blobstore.Key)))) |
568 | 632 |
569 case PTKey: | 633 case PTKey: |
570 » » return p.value.(*Key).GQL() | 634 » » return v.(*Key).GQL() |
571 | 635 |
572 case PTTime: | 636 case PTTime: |
573 » » return fmt.Sprintf("DATETIME(%s)", p.value.(time.Time).Format(ti me.RFC3339Nano)) | 637 » » return fmt.Sprintf("DATETIME(%s)", v.(time.Time).Format(time.RFC 3339Nano)) |
574 | 638 |
575 case PTGeoPoint: | 639 case PTGeoPoint: |
576 // note that cloud SQL doesn't support this yet, but take a good guess at | 640 // note that cloud SQL doesn't support this yet, but take a good guess at |
577 // it. | 641 // it. |
578 » » v := p.value.(GeoPoint) | 642 » » v := v.(GeoPoint) |
579 return fmt.Sprintf("GEOPOINT(%v, %v)", v.Lat, v.Lng) | 643 return fmt.Sprintf("GEOPOINT(%v, %v)", v.Lat, v.Lng) |
580 } | 644 } |
581 panic(fmt.Errorf("bad type: %s", p.propType)) | 645 panic(fmt.Errorf("bad type: %s", p.propType)) |
582 } | 646 } |
583 | 647 |
584 // PropertySlice is a slice of Properties. It implements sort.Interface. | 648 // PropertySlice is a slice of Properties. It implements sort.Interface. |
585 type PropertySlice []Property | 649 type PropertySlice []Property |
586 | 650 |
587 func (s PropertySlice) Len() int { return len(s) } | 651 func (s PropertySlice) Len() int { return len(s) } |
588 func (s PropertySlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | 652 func (s PropertySlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
589 func (s PropertySlice) Less(i, j int) bool { return s[i].Less(&s[j]) } | 653 func (s PropertySlice) Less(i, j int) bool { return s[i].Less(&s[j]) } |
590 | 654 |
591 // EstimateSize estimates the amount of space that this Property would consume | 655 // EstimateSize estimates the amount of space that this Property would consume |
592 // if it were committed as part of an entity in the real production datastore. | 656 // if it were committed as part of an entity in the real production datastore. |
593 // | 657 // |
594 // It uses https://cloud.google.com/appengine/articles/storage_breakdown?csw=1 | 658 // It uses https://cloud.google.com/appengine/articles/storage_breakdown?csw=1 |
595 // as a guide for these values. | 659 // as a guide for these values. |
596 func (p *Property) EstimateSize() int64 { | 660 func (p *Property) EstimateSize() int64 { |
597 switch p.Type() { | 661 switch p.Type() { |
598 case PTNull: | 662 case PTNull: |
599 return 1 | 663 return 1 |
600 case PTBool: | 664 case PTBool: |
601 return 1 + 4 | 665 return 1 + 4 |
602 case PTInt, PTTime, PTFloat: | 666 case PTInt, PTTime, PTFloat: |
603 return 1 + 8 | 667 return 1 + 8 |
604 case PTGeoPoint: | 668 case PTGeoPoint: |
605 return 1 + (8 * 2) | 669 return 1 + (8 * 2) |
606 case PTString: | 670 case PTString: |
607 » » return 1 + int64(len(p.value.(string))) | 671 » » return 1 + int64(len(p.Value().(string))) |
608 case PTBlobKey: | 672 case PTBlobKey: |
609 » » return 1 + int64(len(p.value.(blobstore.Key))) | 673 » » return 1 + int64(len(p.Value().(blobstore.Key))) |
610 case PTBytes: | 674 case PTBytes: |
611 » » return 1 + int64(len(p.value.([]byte))) | 675 » » return 1 + int64(len(p.Value().([]byte))) |
612 case PTKey: | 676 case PTKey: |
613 » » return 1 + p.value.(*Key).EstimateSize() | 677 » » return 1 + p.Value().(*Key).EstimateSize() |
614 } | 678 } |
615 panic(fmt.Errorf("Unknown property type: %s", p.Type().String())) | 679 panic(fmt.Errorf("Unknown property type: %s", p.Type().String())) |
616 } | 680 } |
617 | 681 |
618 // MetaGetter is a subinterface of PropertyLoadSaver, but is also used to | 682 // MetaGetter is a subinterface of PropertyLoadSaver, but is also used to |
619 // abstract the meta argument for RawInterface.GetMulti. | 683 // abstract the meta argument for RawInterface.GetMulti. |
620 type MetaGetter interface { | 684 type MetaGetter interface { |
621 // GetMeta will get information about the field which has the struct tag in | 685 // GetMeta will get information about the field which has the struct tag in |
622 // the form of `gae:"$<key>[,<default>]?"`. | 686 // the form of `gae:"$<key>[,<default>]?"`. |
623 // | 687 // |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
821 // Example: | 885 // Example: |
822 // pls.GetMetaDefault("foo", 100).(int64) | 886 // pls.GetMetaDefault("foo", 100).(int64) |
823 func GetMetaDefault(getter MetaGetter, key string, dflt interface{}) interface{} { | 887 func GetMetaDefault(getter MetaGetter, key string, dflt interface{}) interface{} { |
824 dflt = UpconvertUnderlyingType(dflt) | 888 dflt = UpconvertUnderlyingType(dflt) |
825 cur, ok := getter.GetMeta(key) | 889 cur, ok := getter.GetMeta(key) |
826 if !ok || (dflt != nil && reflect.TypeOf(cur) != reflect.TypeOf(dflt)) { | 890 if !ok || (dflt != nil && reflect.TypeOf(cur) != reflect.TypeOf(dflt)) { |
827 return dflt | 891 return dflt |
828 } | 892 } |
829 return cur | 893 return cur |
830 } | 894 } |
895 | |
896 // byteSequence is a generic interface for an object that can be represented as | |
897 // a sequence of bytes. Its implementations are used internally by Property to | |
898 // enable zero-copy conversion and comparisons between byte sequence types. | |
899 type byteSequence interface { | |
900 Len() int | |
901 Get(int) byte | |
902 Value() interface{} | |
903 String() string | |
904 Bytes() []byte | |
905 FastCmp(o byteSequence) (int, bool) | |
906 } | |
907 | |
908 func cmpByteSequence(a, b byteSequence) int { | |
909 if v, ok := a.FastCmp(b); ok { | |
910 return v | |
911 } | |
912 | |
913 ld := a.Len() - b.Len() | |
914 if ld < 0 { | |
915 ld = -ld | |
916 } | |
917 for i := 0; i < ld; i++ { | |
918 av, bv := a.Get(i), b.Get(i) | |
919 switch { | |
920 case av < bv: | |
921 return -1 | |
922 case av > bv: | |
923 return 1 | |
924 } | |
925 } | |
926 | |
927 return ld | |
928 } | |
929 | |
930 // bytesByteSequence is a byteSequence implementation for a byte slice. | |
931 type bytesByteSequence []byte | |
932 | |
933 func (s bytesByteSequence) Len() int { return len(s) } | |
934 func (s bytesByteSequence) Get(i int) byte { return s[i] } | |
935 func (s bytesByteSequence) Value() interface{} { return []byte(s) } | |
936 func (s bytesByteSequence) String() string { return string(s) } | |
937 func (s bytesByteSequence) Bytes() []byte { return []byte(s) } | |
938 func (s bytesByteSequence) FastCmp(o byteSequence) (int, bool) { | |
939 if t, ok := o.(bytesByteSequence); ok { | |
940 return bytes.Compare([]byte(s), []byte(t)), true | |
941 } | |
942 return 0, false | |
943 } | |
944 | |
945 // stringByteSequence is a byteSequence implementation for a string. | |
946 type stringByteSequence string | |
947 | |
948 func (s stringByteSequence) Len() int { return len(s) } | |
949 func (s stringByteSequence) Get(i int) byte { return s[i] } | |
950 func (s stringByteSequence) Value() interface{} { return string(s) } | |
951 func (s stringByteSequence) String() string { return string(s) } | |
952 func (s stringByteSequence) Bytes() []byte { return []byte(s) } | |
953 func (s stringByteSequence) FastCmp(o byteSequence) (int, bool) { | |
954 if t, ok := o.(stringByteSequence); ok { | |
955 if string(s) == string(t) { | |
956 return 0, true | |
957 } | |
958 if string(s) < string(t) { | |
959 return -1, true | |
960 } | |
961 return 0, true | |
962 } | |
963 return 0, false | |
964 } | |
OLD | NEW |