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 gae |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 |
| 10 "golang.org/x/net/context" |
| 11 ) |
| 12 |
| 13 /// Kinds + Keys |
| 14 |
| 15 // DSKey is the equivalent of *datastore.Key from the original SDK, except that |
| 16 // it can have multiple implementations. See helper.DSKey* methods for missing |
| 17 // methods like DSKeyIncomplete (and some new ones like DSKeyValid). |
| 18 type DSKey interface { |
| 19 Kind() string |
| 20 StringID() string |
| 21 IntID() int64 |
| 22 Parent() DSKey |
| 23 AppID() string |
| 24 Namespace() string |
| 25 |
| 26 String() string |
| 27 } |
| 28 |
| 29 // DSKeyTok is a single token from a multi-part DSKey. |
| 30 type DSKeyTok struct { |
| 31 Kind string |
| 32 IntID int64 |
| 33 StringID string |
| 34 } |
| 35 |
| 36 // DSCursor wraps datastore.Cursor. |
| 37 type DSCursor interface { |
| 38 fmt.Stringer |
| 39 } |
| 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. |
| 48 type DSQuery interface { |
| 49 Ancestor(ancestor DSKey) DSQuery |
| 50 Distinct() DSQuery |
| 51 End(c DSCursor) DSQuery |
| 52 EventualConsistency() DSQuery |
| 53 Filter(filterStr string, value interface{}) DSQuery |
| 54 KeysOnly() DSQuery |
| 55 Limit(limit int) DSQuery |
| 56 Offset(offset int) DSQuery |
| 57 Order(fieldName string) DSQuery |
| 58 Project(fieldNames ...string) DSQuery |
| 59 Start(c DSCursor) DSQuery |
| 60 } |
| 61 |
| 62 // CommonDatastore is the interface for the methods which are common between |
| 63 // Datastore and RawDatastore. |
| 64 type CommonDatastore interface { |
| 65 NewKey(kind, stringID string, intID int64, parent DSKey) DSKey |
| 66 DecodeKey(encoded string) (DSKey, error) |
| 67 |
| 68 NewQuery(kind string) DSQuery |
| 69 Run(q DSQuery) DSIterator |
| 70 GetAll(q DSQuery, dst interface{}) ([]DSKey, error) |
| 71 Count(q DSQuery) (int, error) |
| 72 |
| 73 RunInTransaction(f func(c context.Context) error, opts *DSTransactionOpt
ions) error |
| 74 } |
| 75 |
| 76 // RawDatastore implements the datastore functionality as described by |
| 77 // the raw appengine documentation. No key inference occurs, nor does any |
| 78 // caching. See Datastore for a nicer interface. |
| 79 type RawDatastore interface { |
| 80 CommonDatastore |
| 81 |
| 82 Put(key DSKey, src interface{}) (DSKey, error) |
| 83 Get(key DSKey, dst interface{}) error |
| 84 Delete(key DSKey) error |
| 85 |
| 86 // These allow you to read and write a multiple datastore objects in |
| 87 // a non-atomic batch. |
| 88 DeleteMulti(keys []DSKey) error |
| 89 GetMulti(keys []DSKey, dst interface{}) error |
| 90 PutMulti(keys []DSKey, src interface{}) ([]DSKey, error) |
| 91 } |
| 92 |
| 93 // RDSFactory is the function signature for factory methods compatible with |
| 94 // SetRDSFactory. |
| 95 type RDSFactory func(context.Context) RawDatastore |
| 96 |
| 97 // GetRDS gets the RawDatastore implementation from context. |
| 98 func GetRDS(c context.Context) RawDatastore { |
| 99 if f, ok := c.Value(rawDatastoreKey).(RDSFactory); ok && f != nil { |
| 100 return f(c) |
| 101 } |
| 102 return nil |
| 103 } |
| 104 |
| 105 // SetRDSFactory sets the function to produce Datastore instances, as returned b
y |
| 106 // the GetRDS method. |
| 107 func SetRDSFactory(c context.Context, rdsf RDSFactory) context.Context { |
| 108 return context.WithValue(c, rawDatastoreKey, rdsf) |
| 109 } |
| 110 |
| 111 // 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 // a factory which always returns the same object. |
| 114 func SetRDS(c context.Context, rds RawDatastore) context.Context { |
| 115 return SetRDSFactory(c, func(context.Context) RawDatastore { return rds
}) |
| 116 } |
OLD | NEW |