| 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 gae | 5 package gae | 
| 6 | 6 | 
| 7 import ( | 7 import ( | 
| 8         "fmt" | 8         "fmt" | 
| 9 | 9 | 
| 10         "golang.org/x/net/context" | 10         "golang.org/x/net/context" | 
| (...skipping 20 matching lines...) Expand all  Loading... | 
| 31         Kind     string | 31         Kind     string | 
| 32         IntID    int64 | 32         IntID    int64 | 
| 33         StringID string | 33         StringID string | 
| 34 } | 34 } | 
| 35 | 35 | 
| 36 // DSCursor wraps datastore.Cursor. | 36 // DSCursor wraps datastore.Cursor. | 
| 37 type DSCursor interface { | 37 type DSCursor interface { | 
| 38         fmt.Stringer | 38         fmt.Stringer | 
| 39 } | 39 } | 
| 40 | 40 | 
| 41 // DSIterator wraps datastore.Iterator. |  | 
| 42 type DSIterator interface { |  | 
| 43         Cursor() (DSCursor, error) |  | 
| 44         Next(dst interface{}) (DSKey, error) |  | 
| 45 } |  | 
| 46 |  | 
| 47 // DSQuery wraps datastore.Query. | 41 // DSQuery wraps datastore.Query. | 
| 48 type DSQuery interface { | 42 type DSQuery interface { | 
| 49         Ancestor(ancestor DSKey) DSQuery | 43         Ancestor(ancestor DSKey) DSQuery | 
| 50         Distinct() DSQuery | 44         Distinct() DSQuery | 
| 51         End(c DSCursor) DSQuery | 45         End(c DSCursor) DSQuery | 
| 52         EventualConsistency() DSQuery | 46         EventualConsistency() DSQuery | 
| 53         Filter(filterStr string, value interface{}) DSQuery | 47         Filter(filterStr string, value interface{}) DSQuery | 
| 54         KeysOnly() DSQuery | 48         KeysOnly() DSQuery | 
| 55         Limit(limit int) DSQuery | 49         Limit(limit int) DSQuery | 
| 56         Offset(offset int) DSQuery | 50         Offset(offset int) DSQuery | 
| 57         Order(fieldName string) DSQuery | 51         Order(fieldName string) DSQuery | 
| 58         Project(fieldNames ...string) DSQuery | 52         Project(fieldNames ...string) DSQuery | 
| 59         Start(c DSCursor) DSQuery | 53         Start(c DSCursor) DSQuery | 
| 60 } | 54 } | 
| 61 | 55 | 
| 62 // CommonDatastore is the interface for the methods which are common between | 56 // CommonDatastore is the interface for the methods which are common between | 
| 63 // Datastore and RawDatastore. | 57 // helper.Datastore and RawDatastore. | 
| 64 type CommonDatastore interface { | 58 type CommonDatastore interface { | 
| 65         NewKey(kind, stringID string, intID int64, parent DSKey) DSKey | 59         NewKey(kind, stringID string, intID int64, parent DSKey) DSKey | 
| 66         DecodeKey(encoded string) (DSKey, error) | 60         DecodeKey(encoded string) (DSKey, error) | 
| 67 | 61 | 
| 68         NewQuery(kind string) DSQuery | 62         NewQuery(kind string) DSQuery | 
| 69         Run(q DSQuery) DSIterator |  | 
| 70         GetAll(q DSQuery, dst interface{}) ([]DSKey, error) |  | 
| 71         Count(q DSQuery) (int, error) | 63         Count(q DSQuery) (int, error) | 
| 72 | 64 | 
| 73         RunInTransaction(f func(c context.Context) error, opts *DSTransactionOpt
     ions) error | 65         RunInTransaction(f func(c context.Context) error, opts *DSTransactionOpt
     ions) error | 
| 74 } | 66 } | 
| 75 | 67 | 
| 76 // RawDatastore implements the datastore functionality as described by | 68 // RDSIterator wraps datastore.Iterator. | 
| 77 // the raw appengine documentation. No key inference occurs, nor does any | 69 type RDSIterator interface { | 
| 78 // caching. See Datastore for a nicer interface. | 70 »       Cursor() (DSCursor, error) | 
|  | 71 »       Next(dst DSPropertyLoadSaver) (DSKey, error) | 
|  | 72 } | 
|  | 73 | 
|  | 74 // RawDatastore implements the datastore functionality without any of the fancy | 
|  | 75 // reflection stuff. This is so that Filters can avoid doing lots of redundant | 
|  | 76 // reflection work. See helper.Datastore for a more user-friendly interface. | 
| 79 type RawDatastore interface { | 77 type RawDatastore interface { | 
| 80         CommonDatastore | 78         CommonDatastore | 
| 81 | 79 | 
| 82 »       Put(key DSKey, src interface{}) (DSKey, error) | 80 »       Run(q DSQuery) RDSIterator | 
| 83 »       Get(key DSKey, dst interface{}) error | 81 »       GetAll(q DSQuery, dst *[]DSPropertyMap) ([]DSKey, error) | 
|  | 82 | 
|  | 83 »       Put(key DSKey, src DSPropertyLoadSaver) (DSKey, error) | 
|  | 84 »       Get(key DSKey, dst DSPropertyLoadSaver) error | 
| 84         Delete(key DSKey) error | 85         Delete(key DSKey) error | 
| 85 | 86 | 
| 86         // These allow you to read and write a multiple datastore objects in | 87         // These allow you to read and write a multiple datastore objects in | 
| 87         // a non-atomic batch. | 88         // a non-atomic batch. | 
| 88         DeleteMulti(keys []DSKey) error | 89         DeleteMulti(keys []DSKey) error | 
| 89 »       GetMulti(keys []DSKey, dst interface{}) error | 90 »       GetMulti(keys []DSKey, dst []DSPropertyLoadSaver) error | 
| 90 »       PutMulti(keys []DSKey, src interface{}) ([]DSKey, error) | 91 »       PutMulti(keys []DSKey, src []DSPropertyLoadSaver) ([]DSKey, error) | 
| 91 } | 92 } | 
| 92 | 93 | 
| 93 // RDSFactory is the function signature for factory methods compatible with | 94 // RDSFactory is the function signature for factory methods compatible with | 
| 94 // SetRDSFactory. | 95 // SetRDSFactory. | 
| 95 type RDSFactory func(context.Context) RawDatastore | 96 type RDSFactory func(context.Context) RawDatastore | 
| 96 | 97 | 
| 97 // GetRDS gets the RawDatastore implementation from context. | 98 // GetRDS gets the RawDatastore implementation from context. | 
| 98 func GetRDS(c context.Context) RawDatastore { | 99 func GetRDS(c context.Context) RawDatastore { | 
| 99         if f, ok := c.Value(rawDatastoreKey).(RDSFactory); ok && f != nil { | 100         if f, ok := c.Value(rawDatastoreKey).(RDSFactory); ok && f != nil { | 
| 100                 return f(c) | 101                 return f(c) | 
| 101         } | 102         } | 
| 102         return nil | 103         return nil | 
| 103 } | 104 } | 
| 104 | 105 | 
| 105 // SetRDSFactory sets the function to produce Datastore instances, as returned b
     y | 106 // SetRDSFactory sets the function to produce Datastore instances, as returned b
     y | 
| 106 // the GetRDS method. | 107 // the GetRDS method. | 
| 107 func SetRDSFactory(c context.Context, rdsf RDSFactory) context.Context { | 108 func SetRDSFactory(c context.Context, rdsf RDSFactory) context.Context { | 
| 108         return context.WithValue(c, rawDatastoreKey, rdsf) | 109         return context.WithValue(c, rawDatastoreKey, rdsf) | 
| 109 } | 110 } | 
| 110 | 111 | 
| 111 // SetRDS sets the current Datastore object in the context. Useful for testing | 112 // SetRDS sets the current Datastore object in the context. Useful for testing | 
| 112 // with a quick mock. This is just a shorthand SetDSFactory invocation to set | 113 // with a quick mock. This is just a shorthand SetDSFactory invocation to set | 
| 113 // a factory which always returns the same object. | 114 // a factory which always returns the same object. | 
| 114 func SetRDS(c context.Context, rds RawDatastore) context.Context { | 115 func SetRDS(c context.Context, rds RawDatastore) context.Context { | 
| 115         return SetRDSFactory(c, func(context.Context) RawDatastore { return rds 
     }) | 116         return SetRDSFactory(c, func(context.Context) RawDatastore { return rds 
     }) | 
| 116 } | 117 } | 
| OLD | NEW | 
|---|