OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package datastore |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 ) |
| 10 |
| 11 type IndexDirection bool |
| 12 |
| 13 const ( |
| 14 // ASCENDING is false so that it's the default (zero) value. |
| 15 ASCENDING IndexDirection = false |
| 16 DESCENDING = true |
| 17 ) |
| 18 |
| 19 type IndexColumn struct { |
| 20 Property string |
| 21 Direction IndexDirection |
| 22 } |
| 23 |
| 24 type IndexDefinition struct { |
| 25 Kind string |
| 26 Ancestor bool |
| 27 SortBy []IndexColumn |
| 28 } |
| 29 |
| 30 func (i *IndexDefinition) Less(o *IndexDefinition) bool { |
| 31 // yes, this is inefficient.... however I'm disinclined to care, because
the |
| 32 // actual comparison function is really ugly, and sorting IndexDefintion
s is |
| 33 // not performance critical. If you're here because you profiled this an
d |
| 34 // determined that it's a bottleneck, then feel free to rewrite :). |
| 35 // |
| 36 // At the time of writing, this function is only used during the tests o
f |
| 37 // impl/memory and this package. |
| 38 ibuf, obuf := &bytes.Buffer{}, &bytes.Buffer{} |
| 39 // we know these can't return an error because we're using bytes.Buffer |
| 40 _ = i.Write(ibuf) |
| 41 _ = o.Write(obuf) |
| 42 return bytes.Compare(ibuf.Bytes(), obuf.Bytes()) < 0 |
| 43 } |
| 44 |
| 45 func (i *IndexDefinition) Builtin() bool { |
| 46 return !i.Ancestor && len(i.SortBy) <= 1 |
| 47 } |
| 48 |
| 49 func (i *IndexDefinition) Compound() bool { |
| 50 if i.Kind == "" || len(i.SortBy) <= 1 { |
| 51 return false |
| 52 } |
| 53 for _, sb := range i.SortBy { |
| 54 if sb.Property == "" { |
| 55 return false |
| 56 } |
| 57 } |
| 58 return true |
| 59 } |
| 60 |
| 61 func (i *IndexDefinition) String() string { |
| 62 ret := &bytes.Buffer{} |
| 63 if i.Builtin() { |
| 64 ret.WriteRune('B') |
| 65 } else { |
| 66 ret.WriteRune('C') |
| 67 } |
| 68 ret.WriteRune(':') |
| 69 ret.WriteString(i.Kind) |
| 70 if i.Ancestor { |
| 71 ret.WriteString("|A") |
| 72 } |
| 73 for _, sb := range i.SortBy { |
| 74 ret.WriteRune('/') |
| 75 if sb.Direction == DESCENDING { |
| 76 ret.WriteRune('-') |
| 77 } |
| 78 ret.WriteString(sb.Property) |
| 79 } |
| 80 return ret.String() |
| 81 } |
| 82 |
| 83 func IndexBuiltinQueryPrefix() []byte { |
| 84 return []byte{0} |
| 85 } |
| 86 |
| 87 func IndexComplexQueryPrefix() []byte { |
| 88 return []byte{1} |
| 89 } |
OLD | NEW |