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

Side by Side Diff: impl/memory/raw_datastore_query.go

Issue 1243323002: Refactor a bit. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: fix golint Created 5 years, 5 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
« no previous file with comments | « impl/memory/raw_datastore_data.go ('k') | impl/memory/raw_datastore_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 memory 5 package memory
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "errors" 9 "errors"
10 "fmt" 10 "fmt"
11 "math" 11 "math"
12 "strings" 12 "strings"
13 13
14 » "github.com/luci/gae" 14 » rds "github.com/luci/gae/service/rawdatastore"
15 » "github.com/luci/gae/helper"
16 "github.com/luci/gkvlite" 15 "github.com/luci/gkvlite"
17 "github.com/luci/luci-go/common/cmpbin" 16 "github.com/luci/luci-go/common/cmpbin"
18 ) 17 )
19 18
20 type qDirection bool 19 type qDirection bool
21 20
22 const ( 21 const (
23 qASC qDirection = true 22 qASC qDirection = true
24 qDEC = false 23 qDEC = false
25 ) 24 )
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 212
214 type queryCursor string 213 type queryCursor string
215 214
216 func (q queryCursor) String() string { return string(q) } 215 func (q queryCursor) String() string { return string(q) }
217 func (q queryCursor) Valid() bool { return q != "" } 216 func (q queryCursor) Valid() bool { return q != "" }
218 217
219 type queryImpl struct { 218 type queryImpl struct {
220 ns string 219 ns string
221 220
222 kind string 221 kind string
223 » ancestor gae.DSKey 222 » ancestor rds.Key
224 filter []queryFilter 223 filter []queryFilter
225 order []queryOrder 224 order []queryOrder
226 project []string 225 project []string
227 226
228 distinct bool 227 distinct bool
229 eventualConsistency bool 228 eventualConsistency bool
230 keysOnly bool 229 keysOnly bool
231 limit int32 230 limit int32
232 offset int32 231 offset int32
233 232
234 start queryCursor 233 start queryCursor
235 end queryCursor 234 end queryCursor
236 235
237 err error 236 err error
238 } 237 }
239 238
240 var _ gae.DSQuery = (*queryImpl)(nil) 239 var _ rds.Query = (*queryImpl)(nil)
241 240
242 type queryIterImpl struct { 241 type queryIterImpl struct {
243 idx *queryImpl 242 idx *queryImpl
244 } 243 }
245 244
246 var _ gae.RDSIterator = (*queryIterImpl)(nil) 245 var _ rds.Iterator = (*queryIterImpl)(nil)
247 246
248 func (q *queryIterImpl) Cursor() (gae.DSCursor, error) { 247 func (q *queryIterImpl) Cursor() (rds.Cursor, error) {
249 if q.idx.err != nil { 248 if q.idx.err != nil {
250 return nil, q.idx.err 249 return nil, q.idx.err
251 } 250 }
252 return nil, nil 251 return nil, nil
253 } 252 }
254 253
255 func (q *queryIterImpl) Next(dst gae.DSPropertyLoadSaver) (gae.DSKey, error) { 254 func (q *queryIterImpl) Next(dst rds.PropertyLoadSaver) (rds.Key, error) {
256 if q.idx.err != nil { 255 if q.idx.err != nil {
257 return nil, q.idx.err 256 return nil, q.idx.err
258 } 257 }
259 return nil, nil 258 return nil, nil
260 } 259 }
261 260
262 func (q *queryImpl) normalize() (ret *queryImpl) { 261 func (q *queryImpl) normalize() (ret *queryImpl) {
263 // ported from GAE SDK datastore_index.py;Normalize() 262 // ported from GAE SDK datastore_index.py;Normalize()
264 ret = q.clone() 263 ret = q.clone()
265 264
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 // "query app is x but ancestor app is x" 379 // "query app is x but ancestor app is x"
381 // if ret.ancestor.namespace() != current namespace 380 // if ret.ancestor.namespace() != current namespace
382 // "query namespace is x but ancestor namespace is x" 381 // "query namespace is x but ancestor namespace is x"
383 382
384 // if not all(g in orders for g in group_by) 383 // if not all(g in orders for g in group_by)
385 // "items in the group by clause must be specified first in the orderin g" 384 // "items in the group by clause must be specified first in the orderin g"
386 385
387 ineqPropName := "" 386 ineqPropName := ""
388 for _, f := range ret.filter { 387 for _, f := range ret.filter {
389 if f.field == "__key__" { 388 if f.field == "__key__" {
390 » » » k, ok := f.value.(gae.DSKey) 389 » » » k, ok := f.value.(rds.Key)
391 if !ok { 390 if !ok {
392 ret.err = errors.New( 391 ret.err = errors.New(
393 "gae/memory: __key__ filter value must b e a Key") 392 "gae/memory: __key__ filter value must b e a Key")
394 return 393 return
395 } 394 }
396 » » » if !helper.DSKeyValid(k, ret.ns, false) { 395 » » » if !rds.KeyValid(k, ret.ns, false) {
397 // See the comment in queryImpl.Ancestor; basica lly this check 396 // See the comment in queryImpl.Ancestor; basica lly this check
398 // never happens in the real env because the SDK silently swallows 397 // never happens in the real env because the SDK silently swallows
399 // this condition :/ 398 // this condition :/
400 » » » » ret.err = gae.ErrDSInvalidKey 399 » » » » ret.err = rds.ErrInvalidKey
401 return 400 return
402 } 401 }
403 // __key__ filter app is X but query app is X 402 // __key__ filter app is X but query app is X
404 // __key__ filter namespace is X but query namespace is X 403 // __key__ filter namespace is X but query namespace is X
405 } 404 }
406 // if f.op == qEqual and f.field in ret.project_fields 405 // if f.op == qEqual and f.field in ret.project_fields
407 // "cannot use projection on a proprety with an equality filte r" 406 // "cannot use projection on a proprety with an equality filte r"
408 407
409 if f.op.isINEQOp() { 408 if f.op.isINEQOp() {
410 if ineqPropName == "" { 409 if ineqPropName == "" {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 } 467 }
469 468
470 func (q *queryImpl) clone() *queryImpl { 469 func (q *queryImpl) clone() *queryImpl {
471 ret := *q 470 ret := *q
472 ret.filter = append([]queryFilter(nil), q.filter...) 471 ret.filter = append([]queryFilter(nil), q.filter...)
473 ret.order = append([]queryOrder(nil), q.order...) 472 ret.order = append([]queryOrder(nil), q.order...)
474 ret.project = append([]string(nil), q.project...) 473 ret.project = append([]string(nil), q.project...)
475 return &ret 474 return &ret
476 } 475 }
477 476
478 func (q *queryImpl) Ancestor(k gae.DSKey) gae.DSQuery { 477 func (q *queryImpl) Ancestor(k rds.Key) rds.Query {
479 q = q.clone() 478 q = q.clone()
480 q.ancestor = k 479 q.ancestor = k
481 if k == nil { 480 if k == nil {
482 // SDK has an explicit nil-check 481 // SDK has an explicit nil-check
483 q.err = errors.New("datastore: nil query ancestor") 482 q.err = errors.New("datastore: nil query ancestor")
484 » } else if !helper.DSKeyValid(k, q.ns, false) { 483 » } else if !rds.KeyValid(k, q.ns, false) {
485 // technically the SDK implementation does a Weird Thing (tm) if both the 484 // technically the SDK implementation does a Weird Thing (tm) if both the
486 // stringID and intID are set on a key; it only serializes the s tringID in 485 // stringID and intID are set on a key; it only serializes the s tringID in
487 // the proto. This means that if you set the Ancestor to an inva lid key, 486 // the proto. This means that if you set the Ancestor to an inva lid key,
488 // you'll never actually hear about it. Instead of doing that in sanity, we 487 // you'll never actually hear about it. Instead of doing that in sanity, we
489 // just swap to an error here. 488 // just swap to an error here.
490 » » q.err = gae.ErrDSInvalidKey 489 » » q.err = rds.ErrInvalidKey
491 } 490 }
492 return q 491 return q
493 } 492 }
494 493
495 func (q *queryImpl) Distinct() gae.DSQuery { 494 func (q *queryImpl) Distinct() rds.Query {
496 q = q.clone() 495 q = q.clone()
497 q.distinct = true 496 q.distinct = true
498 return q 497 return q
499 } 498 }
500 499
501 func (q *queryImpl) Filter(fStr string, val interface{}) gae.DSQuery { 500 func (q *queryImpl) Filter(fStr string, val interface{}) rds.Query {
502 q = q.clone() 501 q = q.clone()
503 f, err := parseFilter(fStr, val) 502 f, err := parseFilter(fStr, val)
504 if err != nil { 503 if err != nil {
505 q.err = err 504 q.err = err
506 return q 505 return q
507 } 506 }
508 q.filter = append(q.filter, f) 507 q.filter = append(q.filter, f)
509 return q 508 return q
510 } 509 }
511 510
512 func (q *queryImpl) Order(field string) gae.DSQuery { 511 func (q *queryImpl) Order(field string) rds.Query {
513 q = q.clone() 512 q = q.clone()
514 field = strings.TrimSpace(field) 513 field = strings.TrimSpace(field)
515 o := queryOrder{field, qASC} 514 o := queryOrder{field, qASC}
516 if strings.HasPrefix(field, "-") { 515 if strings.HasPrefix(field, "-") {
517 o.direction = qDEC 516 o.direction = qDEC
518 o.field = strings.TrimSpace(field[1:]) 517 o.field = strings.TrimSpace(field[1:])
519 } else if strings.HasPrefix(field, "+") { 518 } else if strings.HasPrefix(field, "+") {
520 q.err = fmt.Errorf("datastore: invalid order: %q", field) 519 q.err = fmt.Errorf("datastore: invalid order: %q", field)
521 return q 520 return q
522 } 521 }
523 if len(o.field) == 0 { 522 if len(o.field) == 0 {
524 q.err = errors.New("datastore: empty order") 523 q.err = errors.New("datastore: empty order")
525 return q 524 return q
526 } 525 }
527 q.order = append(q.order, o) 526 q.order = append(q.order, o)
528 return q 527 return q
529 } 528 }
530 529
531 func (q *queryImpl) Project(fieldName ...string) gae.DSQuery { 530 func (q *queryImpl) Project(fieldName ...string) rds.Query {
532 q = q.clone() 531 q = q.clone()
533 q.project = append(q.project, fieldName...) 532 q.project = append(q.project, fieldName...)
534 return q 533 return q
535 } 534 }
536 535
537 func (q *queryImpl) KeysOnly() gae.DSQuery { 536 func (q *queryImpl) KeysOnly() rds.Query {
538 q = q.clone() 537 q = q.clone()
539 q.keysOnly = true 538 q.keysOnly = true
540 return q 539 return q
541 } 540 }
542 541
543 func (q *queryImpl) Limit(limit int) gae.DSQuery { 542 func (q *queryImpl) Limit(limit int) rds.Query {
544 q = q.clone() 543 q = q.clone()
545 if limit < math.MinInt32 || limit > math.MaxInt32 { 544 if limit < math.MinInt32 || limit > math.MaxInt32 {
546 q.err = errors.New("datastore: query limit overflow") 545 q.err = errors.New("datastore: query limit overflow")
547 return q 546 return q
548 } 547 }
549 q.limit = int32(limit) 548 q.limit = int32(limit)
550 return q 549 return q
551 } 550 }
552 551
553 func (q *queryImpl) Offset(offset int) gae.DSQuery { 552 func (q *queryImpl) Offset(offset int) rds.Query {
554 q = q.clone() 553 q = q.clone()
555 if offset < 0 { 554 if offset < 0 {
556 q.err = errors.New("datastore: negative query offset") 555 q.err = errors.New("datastore: negative query offset")
557 return q 556 return q
558 } 557 }
559 if offset > math.MaxInt32 { 558 if offset > math.MaxInt32 {
560 q.err = errors.New("datastore: query offset overflow") 559 q.err = errors.New("datastore: query offset overflow")
561 return q 560 return q
562 } 561 }
563 q.offset = int32(offset) 562 q.offset = int32(offset)
564 return q 563 return q
565 } 564 }
566 565
567 func (q *queryImpl) Start(c gae.DSCursor) gae.DSQuery { 566 func (q *queryImpl) Start(c rds.Cursor) rds.Query {
568 q = q.clone() 567 q = q.clone()
569 curs := c.(queryCursor) 568 curs := c.(queryCursor)
570 if !curs.Valid() { 569 if !curs.Valid() {
571 q.err = errors.New("datastore: invalid cursor") 570 q.err = errors.New("datastore: invalid cursor")
572 return q 571 return q
573 } 572 }
574 q.start = curs 573 q.start = curs
575 return q 574 return q
576 } 575 }
577 576
578 func (q *queryImpl) End(c gae.DSCursor) gae.DSQuery { 577 func (q *queryImpl) End(c rds.Cursor) rds.Query {
579 q = q.clone() 578 q = q.clone()
580 curs := c.(queryCursor) 579 curs := c.(queryCursor)
581 if !curs.Valid() { 580 if !curs.Valid() {
582 q.err = errors.New("datastore: invalid cursor") 581 q.err = errors.New("datastore: invalid cursor")
583 return q 582 return q
584 } 583 }
585 q.end = curs 584 q.end = curs
586 return q 585 return q
587 } 586 }
588 587
589 func (q *queryImpl) EventualConsistency() gae.DSQuery { 588 func (q *queryImpl) EventualConsistency() rds.Query {
590 q = q.clone() 589 q = q.clone()
591 q.eventualConsistency = true 590 q.eventualConsistency = true
592 return q 591 return q
593 } 592 }
OLDNEW
« no previous file with comments | « impl/memory/raw_datastore_data.go ('k') | impl/memory/raw_datastore_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698