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

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

Issue 1355783002: Refactor keys and queries in datastore service and implementation. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: Created 5 years, 3 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 datastore 5 package datastore
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "fmt"
10 "strings"
9 ) 11 )
10 12
11 const MaxIndexColumns = 64 13 const MaxIndexColumns = 64
12 14
13 type IndexDirection bool 15 type IndexDirection bool
14 16
15 const ( 17 const (
16 // ASCENDING is false so that it's the default (zero) value. 18 // ASCENDING is false so that it's the default (zero) value.
17 ASCENDING IndexDirection = false 19 ASCENDING IndexDirection = false
18 DESCENDING = true 20 DESCENDING = true
19 ) 21 )
20 22
21 func (i IndexDirection) String() string { 23 func (i IndexDirection) String() string {
22 if i == ASCENDING { 24 if i == ASCENDING {
23 return "ASCENDING" 25 return "ASCENDING"
24 } 26 }
25 return "DESCENDING" 27 return "DESCENDING"
26 } 28 }
27 29
28 type IndexColumn struct { 30 type IndexColumn struct {
29 Property string 31 Property string
30 Direction IndexDirection 32 Direction IndexDirection
31 } 33 }
32 34
35 func ParseIndexColumn(spec string) (IndexColumn, error) {
36 col := IndexColumn{}
37 if strings.HasPrefix(spec, "-") {
38 col.Direction = DESCENDING
39 col.Property = strings.TrimSpace(spec[1:])
40 } else if strings.HasPrefix(spec, "+") {
41 return col, fmt.Errorf("datastore: invalid order: %q", spec)
42 } else {
43 col.Direction = ASCENDING
44 col.Property = strings.TrimSpace(spec)
45 }
46 if col.Property == "" {
47 return col, fmt.Errorf("datastore: empty order")
48 }
49 return col, nil
50 }
51
33 func (i IndexColumn) cmp(o IndexColumn) int { 52 func (i IndexColumn) cmp(o IndexColumn) int {
34 // sort ascending first 53 // sort ascending first
35 if i.Direction == ASCENDING && o.Direction == DESCENDING { 54 if i.Direction == ASCENDING && o.Direction == DESCENDING {
36 return -1 55 return -1
37 } else if i.Direction == DESCENDING && o.Direction == ASCENDING { 56 } else if i.Direction == DESCENDING && o.Direction == ASCENDING {
38 return 1 57 return 1
39 } 58 }
40 return cmpString(i.Property, o.Property)() 59 return cmpString(i.Property, o.Property)()
41 } 60 }
42 61
62 func (i IndexColumn) String() string {
63 ret := ""
64 if i.Direction == DESCENDING {
65 ret = "-"
66 }
67 return ret + i.Property
68 }
69
70 func (i IndexColumn) GQL() string {
71 if i.Direction == DESCENDING {
72 return gqlQuoteName(i.Property) + " DESC"
73 }
74 return gqlQuoteName(i.Property)
75 }
76
43 type IndexDefinition struct { 77 type IndexDefinition struct {
44 Kind string 78 Kind string
45 Ancestor bool 79 Ancestor bool
46 SortBy []IndexColumn 80 SortBy []IndexColumn
47 } 81 }
48 82
49 func (id *IndexDefinition) Equal(o *IndexDefinition) bool { 83 func (id *IndexDefinition) Equal(o *IndexDefinition) bool {
50 if id.Kind != o.Kind || id.Ancestor != o.Ancestor || len(id.SortBy) != l en(o.SortBy) { 84 if id.Kind != o.Kind || id.Ancestor != o.Ancestor || len(id.SortBy) != l en(o.SortBy) {
51 return false 85 return false
52 } 86 }
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 } 230 }
197 for _, sb := range i.SortBy { 231 for _, sb := range i.SortBy {
198 ret.WriteRune('/') 232 ret.WriteRune('/')
199 if sb.Direction == DESCENDING { 233 if sb.Direction == DESCENDING {
200 ret.WriteRune('-') 234 ret.WriteRune('-')
201 } 235 }
202 ret.WriteString(sb.Property) 236 ret.WriteString(sb.Property)
203 } 237 }
204 return ret.String() 238 return ret.String()
205 } 239 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698