Chromium Code Reviews| 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 "bytes" |
| 9 ) | 9 ) |
| 10 | 10 |
| 11 const MaxIndexColumns = 64 | 11 const MaxIndexColumns = 64 |
| 12 | 12 |
| 13 type IndexDirection bool | 13 type IndexDirection bool |
| 14 | 14 |
| 15 const ( | 15 const ( |
| 16 // ASCENDING is false so that it's the default (zero) value. | 16 // ASCENDING is false so that it's the default (zero) value. |
| 17 ASCENDING IndexDirection = false | 17 ASCENDING IndexDirection = false |
| 18 DESCENDING = true | 18 DESCENDING = true |
| 19 ) | 19 ) |
| 20 | 20 |
| 21 func (i IndexDirection) String() string { | |
| 22 if i == ASCENDING { | |
| 23 return "ASCENDING" | |
| 24 } | |
| 25 return "DESCENDING" | |
| 26 } | |
| 27 | |
| 21 type IndexColumn struct { | 28 type IndexColumn struct { |
| 22 Property string | 29 Property string |
| 23 Direction IndexDirection | 30 Direction IndexDirection |
| 24 } | 31 } |
| 25 | 32 |
| 26 func (i IndexColumn) cmp(o IndexColumn) int { | 33 func (i IndexColumn) cmp(o IndexColumn) int { |
| 27 // sort ascending first | 34 // sort ascending first |
| 28 if i.Direction == ASCENDING && o.Direction == DESCENDING { | 35 if i.Direction == ASCENDING && o.Direction == DESCENDING { |
| 29 return -1 | 36 return -1 |
| 30 } else if i.Direction == DESCENDING && o.Direction == ASCENDING { | 37 } else if i.Direction == DESCENDING && o.Direction == ASCENDING { |
| 31 return 1 | 38 return 1 |
| 32 } | 39 } |
| 33 return cmpString(i.Property, o.Property)() | 40 return cmpString(i.Property, o.Property)() |
| 34 } | 41 } |
| 35 | 42 |
| 36 type IndexDefinition struct { | 43 type IndexDefinition struct { |
| 37 Kind string | 44 Kind string |
| 38 Ancestor bool | 45 Ancestor bool |
| 39 SortBy []IndexColumn | 46 SortBy []IndexColumn |
| 40 } | 47 } |
| 41 | 48 |
| 49 func (id *IndexDefinition) Equal(o *IndexDefinition) bool { | |
|
dnj (Google)
2015/08/23 06:50:07
Does "gofmt" really not complain about a lack of c
iannucci
2015/08/23 18:19:43
yeah I haven't been running golint on this repo (g
| |
| 50 if id.Kind != o.Kind || id.Ancestor != o.Ancestor || len(id.SortBy) != l en(o.SortBy) { | |
| 51 return false | |
| 52 } | |
| 53 for i, col := range id.SortBy { | |
| 54 if col != o.SortBy[i] { | |
| 55 return false | |
| 56 } | |
| 57 } | |
| 58 return true | |
| 59 } | |
| 60 | |
| 61 // NormalizeOrder returns the normalized SortBy value for this IndexDefinition. | |
| 62 // This includes prepending __ancestor__ (if id.Ancestor is true), and appending | |
| 63 // __key__ if it's not explicitly the last field in this IndexDefinition. | |
| 64 func (id *IndexDefinition) NormalizeOrder() []IndexColumn { | |
| 65 ret := make([]IndexColumn, 0, len(id.SortBy)) | |
| 66 if id.Ancestor { | |
| 67 ret = append(ret, IndexColumn{Property: "__ancestor__"}) | |
| 68 } | |
| 69 ret = append(ret, id.SortBy...) | |
| 70 if len(ret) == 0 || ret[len(ret)-1].Property != "__key__" { | |
| 71 ret = append(ret, IndexColumn{Property: "__key__"}) | |
| 72 } | |
| 73 return ret | |
| 74 } | |
| 75 | |
| 42 // Yeah who needs templates, right? | 76 // Yeah who needs templates, right? |
| 43 // <flames>This is fine.</flames> | 77 // <flames>This is fine.</flames> |
| 44 | 78 |
| 45 func cmpBool(a, b bool) func() int { | 79 func cmpBool(a, b bool) func() int { |
| 46 return func() int { | 80 return func() int { |
| 47 if a == b { | 81 if a == b { |
| 48 return 0 | 82 return 0 |
| 49 } | 83 } |
| 50 if a && !b { // > | 84 if a && !b { // > |
| 51 return 1 | 85 return 1 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 | 146 |
| 113 func (i *IndexDefinition) Builtin() bool { | 147 func (i *IndexDefinition) Builtin() bool { |
| 114 return !i.Ancestor && len(i.SortBy) <= 1 | 148 return !i.Ancestor && len(i.SortBy) <= 1 |
| 115 } | 149 } |
| 116 | 150 |
| 117 func (i *IndexDefinition) Compound() bool { | 151 func (i *IndexDefinition) Compound() bool { |
| 118 if i.Kind == "" || len(i.SortBy) <= 1 { | 152 if i.Kind == "" || len(i.SortBy) <= 1 { |
| 119 return false | 153 return false |
| 120 } | 154 } |
| 121 for _, sb := range i.SortBy { | 155 for _, sb := range i.SortBy { |
| 122 » » if sb.Property == "" { | 156 » » if sb.Property == "" || sb.Property == "__ancestor__" { |
| 123 return false | 157 return false |
| 124 } | 158 } |
| 125 } | 159 } |
| 126 return true | 160 return true |
| 127 } | 161 } |
| 128 | 162 |
| 129 func (i *IndexDefinition) String() string { | 163 func (i *IndexDefinition) String() string { |
| 130 ret := &bytes.Buffer{} | 164 ret := &bytes.Buffer{} |
| 131 if i.Builtin() { | 165 if i.Builtin() { |
| 132 ret.WriteRune('B') | 166 ret.WriteRune('B') |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 148 return ret.String() | 182 return ret.String() |
| 149 } | 183 } |
| 150 | 184 |
| 151 func IndexBuiltinQueryPrefix() []byte { | 185 func IndexBuiltinQueryPrefix() []byte { |
| 152 return []byte{0} | 186 return []byte{0} |
| 153 } | 187 } |
| 154 | 188 |
| 155 func IndexComplexQueryPrefix() []byte { | 189 func IndexComplexQueryPrefix() []byte { |
| 156 return []byte{1} | 190 return []byte{1} |
| 157 } | 191 } |
| OLD | NEW |