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 "golang.org/x/net/context" | 8 "golang.org/x/net/context" |
9 ) | 9 ) |
10 | 10 |
11 // Interface is the 'user-friendly' interface to access the current filtered | 11 // Interface is the 'user-friendly' interface to access the current filtered |
12 // datastore service implementation. | 12 // datastore service implementation. |
13 // | 13 // |
14 // Note that in exchange for userfriendliness, this interface ends up doing | 14 // Note that in exchange for userfriendliness, this interface ends up doing |
15 // a lot of reflection. | 15 // a lot of reflection. |
16 // | 16 // |
17 // Methods taking 'interface{}' objects describe what a valid type for that | 17 // Methods taking 'interface{}' objects describe what a valid type for that |
18 // interface are in the comments. | 18 // interface are in the comments. |
19 // | 19 // |
20 // Struct objects passed in will be converted to PropertyLoadSaver interfaces | 20 // Struct objects passed in will be converted to PropertyLoadSaver interfaces |
21 // using this package's GetPLS function. | 21 // using this package's GetPLS function. |
22 type Interface interface { | 22 type Interface interface { |
23 // NewKey produces a new Key with the current appid and namespace. | |
24 NewKey(kind, stringID string, intID int64, parent Key) Key | |
25 | |
26 // KeyForObj extracts a key from src. | 23 // KeyForObj extracts a key from src. |
27 // | 24 // |
28 // It is the same as KeyForObjErr, except that if KeyForObjErr would hav e | 25 // It is the same as KeyForObjErr, except that if KeyForObjErr would hav e |
29 // returned an error, this method panics. It's safe to use if you know t hat | 26 // returned an error, this method panics. It's safe to use if you know t hat |
30 // src statically meets the metadata constraints described by KeyForObjE rr. | 27 // src statically meets the metadata constraints described by KeyForObjE rr. |
31 » KeyForObj(src interface{}) Key | 28 » KeyForObj(src interface{}) *Key |
29 | |
30 » // MakeKey is a convenience method for manufacturing a *Key. It should o nly be | |
31 » // used when elems... is known statically (e.g. in the code) to be corre ct. | |
32 » // | |
33 » // elems is pairs of (string, string|int|int32|int64) pairs, which corre spond | |
34 » // to Kind/id pairs. Example: | |
35 » // dstore.MakeKey("Parent", 1, "Child", "id") | |
36 » // | |
37 » // Would create the key: | |
38 » // <current appID>:<current Namespace>:/Parent,1/Child,id | |
39 » // | |
40 » // If elems is not parsable (e.g. wrong length, wrong types, etc.) this method | |
41 » // will panic. | |
42 » MakeKey(elems ...interface{}) *Key | |
dnj
2015/09/18 16:47:57
WDYT about making these helper methods instead of
iannucci
2015/09/18 22:25:48
discussed offline; Interface /is/ the convenience
| |
43 | |
44 » // NewKey constructs a new key in the current appID/Namespace, using the | |
45 » // specified parameters. | |
46 » NewKey(kind, stringID string, intID int64, parent *Key) *Key | |
47 | |
48 » // NewKeyToks constructs a new key in the current appID/Namespace, using the | |
49 » // specified key tokens. | |
50 » NewKeyToks([]KeyTok) *Key | |
32 | 51 |
33 // KeyForObjErr extracts a key from src. | 52 // KeyForObjErr extracts a key from src. |
34 // | 53 // |
35 // src must be one of: | 54 // src must be one of: |
36 // - *S where S is a struct | 55 // - *S where S is a struct |
37 // - a PropertyLoadSaver | 56 // - a PropertyLoadSaver |
38 // | 57 // |
39 // It is expected that the struct or PropertyLoadSaver exposes the | 58 // It is expected that the struct or PropertyLoadSaver exposes the |
40 // following metadata (as retrieved by PropertyLoadSaver.GetMeta): | 59 // following metadata (as retrieved by PropertyLoadSaver.GetMeta): |
41 // - "key" (type: Key) - The full datastore key to use. Must not be ni l. | 60 // - "key" (type: Key) - The full datastore key to use. Must not be ni l. |
42 // OR | 61 // OR |
43 // - "id" (type: int64 or string) - The id of the Key to create | 62 // - "id" (type: int64 or string) - The id of the Key to create |
44 // - "kind" (optional, type: string) - The kind of the Key to create. If | 63 // - "kind" (optional, type: string) - The kind of the Key to create. If |
45 // blank or not present, KeyForObjErr will extract the name of the s rc | 64 // blank or not present, KeyForObjErr will extract the name of the s rc |
46 // object's type. | 65 // object's type. |
47 // - "parent" (optional, type: Key) - The parent key to use. | 66 // - "parent" (optional, type: Key) - The parent key to use. |
48 // | 67 // |
49 // If a required metadata item is missing or of the wrong type, then thi s will | 68 // If a required metadata item is missing or of the wrong type, then thi s will |
50 // return an error. | 69 // return an error. |
51 » KeyForObjErr(src interface{}) (Key, error) | 70 » KeyForObjErr(src interface{}) (*Key, error) |
52 | |
53 » // DecodeKey decodes a proto-encoded key. | |
54 » // | |
55 » // The encoding is defined by the appengine SDK's implementation. In | |
56 » // particular, it is a no-pad-base64-encoded protobuf. If there's an err or | |
57 » // during the decoding process, it will be returned. | |
58 » DecodeKey(encoded string) (Key, error) | |
59 | |
60 » // NewQuery creates a new Query object. No server communication occurs. | |
61 » NewQuery(kind string) Query | |
62 | 71 |
63 // RunInTransaction runs f inside of a transaction. See the appengine SD K's | 72 // RunInTransaction runs f inside of a transaction. See the appengine SD K's |
64 // documentation for full details on the behavior of transactions in the | 73 // documentation for full details on the behavior of transactions in the |
65 // datastore. | 74 // datastore. |
66 // | 75 // |
67 // Note that the behavior of transactions may change depending on what f ilters | 76 // Note that the behavior of transactions may change depending on what f ilters |
68 // have been installed. It's possible that we'll end up implementing thi ngs | 77 // have been installed. It's possible that we'll end up implementing thi ngs |
69 // like nested/buffered transactions as filters. | 78 // like nested/buffered transactions as filters. |
70 RunInTransaction(f func(c context.Context) error, opts *TransactionOptio ns) error | 79 RunInTransaction(f func(c context.Context) error, opts *TransactionOptio ns) error |
71 | 80 |
72 // Run executes the given query, and calls `cb` for each successfully | 81 // Run executes the given query, and calls `cb` for each successfully |
73 // retrieved item. | 82 // retrieved item. |
74 // | 83 // |
75 // cb is a callback function whose signature is | 84 // cb is a callback function whose signature is |
76 // func(obj TYPE, getCursor CursorCB) bool | 85 // func(obj TYPE, getCursor CursorCB) bool |
77 // | 86 // |
78 // Where TYPE is one of: | 87 // Where TYPE is one of: |
79 // - S or *S where S is a struct | 88 // - S or *S where S is a struct |
80 // - P or *P where *P is a concrete type implementing PropertyLoadSave r | 89 // - P or *P where *P is a concrete type implementing PropertyLoadSave r |
81 » // - Key (implies a keys-only query) | 90 » // - *Key (implies a keys-only query) |
82 // | 91 // |
83 // Run stops on the first error encountered. | 92 // Run stops on the first error encountered. |
84 » Run(q Query, cb interface{}) error | 93 » Run(q *Query, cb interface{}) error |
85 | 94 |
86 // DecodeCursor converts a string returned by a Cursor into a Cursor ins tance. | 95 // DecodeCursor converts a string returned by a Cursor into a Cursor ins tance. |
87 // It will return an error if the supplied string is not valid, or could not | 96 // It will return an error if the supplied string is not valid, or could not |
88 // be decoded by the implementation. | 97 // be decoded by the implementation. |
89 DecodeCursor(string) (Cursor, error) | 98 DecodeCursor(string) (Cursor, error) |
90 | 99 |
91 // GetAll retrieves all of the Query results into dst. | 100 // GetAll retrieves all of the Query results into dst. |
92 // | 101 // |
93 // dst must be one of: | 102 // dst must be one of: |
94 // - *[]S or *[]*S where S is a struct | 103 // - *[]S or *[]*S where S is a struct |
95 // - *[]P or *[]*P where *P is a concrete type implementing | 104 // - *[]P or *[]*P where *P is a concrete type implementing |
96 // PropertyLoadSaver | 105 // PropertyLoadSaver |
97 » // - *[]Key implies a keys-only query. | 106 » // - *[]*Key implies a keys-only query. |
98 » GetAll(q Query, dst interface{}) error | 107 » GetAll(q *Query, dst interface{}) error |
99 | 108 |
100 // Get retrieves a single object from the datastore | 109 // Get retrieves a single object from the datastore |
101 // | 110 // |
102 // dst must be one of: | 111 // dst must be one of: |
103 // - *S where S is a struct | 112 // - *S where S is a struct |
104 // - *P where *P is a concrete type implementing PropertyLoadSaver | 113 // - *P where *P is a concrete type implementing PropertyLoadSaver |
105 Get(dst interface{}) error | 114 Get(dst interface{}) error |
106 | 115 |
107 // Put inserts a single object into the datastore | 116 // Put inserts a single object into the datastore |
108 // | 117 // |
109 // src must be one of: | 118 // src must be one of: |
110 // - *S where S is a struct | 119 // - *S where S is a struct |
111 // - *P where *P is a concrete type implementing PropertyLoadSaver | 120 // - *P where *P is a concrete type implementing PropertyLoadSaver |
112 // | 121 // |
113 » // A Key will be extracted from src via KeyForObj. If | 122 » // A *Key will be extracted from src via KeyForObj. If |
114 » // KeyIncomplete(extractedKey) is true, then Put will write the resolved (i.e. | 123 » // extractedKey.Incomplete() is true, then Put will write the resolved ( i.e. |
115 » // automatic datastore-populated) Key back to src. | 124 » // automatic datastore-populated) *Key back to src. |
116 Put(src interface{}) error | 125 Put(src interface{}) error |
117 | 126 |
118 // Delete removes an item from the datastore. | 127 // Delete removes an item from the datastore. |
119 » Delete(key Key) error | 128 » Delete(key *Key) error |
120 | 129 |
121 // GetMulti retrieves items from the datastore. | 130 // GetMulti retrieves items from the datastore. |
122 // | 131 // |
123 // dst must be one of: | 132 // dst must be one of: |
124 // - []S or []*S where S is a struct | 133 // - []S or []*S where S is a struct |
125 // - []P or []*P where *P is a concrete type implementing PropertyLoad Saver | 134 // - []P or []*P where *P is a concrete type implementing PropertyLoad Saver |
126 // - []I where I is some interface type. Each element of the slice mus t | 135 // - []I where I is some interface type. Each element of the slice mus t |
127 // be non-nil, and its underlying type must be either *S or *P. | 136 // be non-nil, and its underlying type must be either *S or *P. |
128 GetMulti(dst interface{}) error | 137 GetMulti(dst interface{}) error |
129 | 138 |
130 // PutMulti writes items to the datastore. | 139 // PutMulti writes items to the datastore. |
131 // | 140 // |
132 // src must be one of: | 141 // src must be one of: |
133 // - []S or []*S where S is a struct | 142 // - []S or []*S where S is a struct |
134 // - []P or []*P where *P is a concrete type implementing PropertyLoad Saver | 143 // - []P or []*P where *P is a concrete type implementing PropertyLoad Saver |
135 // - []I where i is some interface type. Each elemet of the slice must | 144 // - []I where i is some interface type. Each elemet of the slice must |
136 // be non-nil, and its underlying type must be either *S or *P. | 145 // be non-nil, and its underlying type must be either *S or *P. |
137 // | 146 // |
138 // If items in src resolve to Incomplete keys, PutMulti will write the | 147 // If items in src resolve to Incomplete keys, PutMulti will write the |
139 // resolved keys back to the items in src. | 148 // resolved keys back to the items in src. |
140 PutMulti(src interface{}) error | 149 PutMulti(src interface{}) error |
141 | 150 |
142 // DeleteMulti removes items from the datastore. | 151 // DeleteMulti removes items from the datastore. |
143 » DeleteMulti(keys []Key) error | 152 » DeleteMulti(keys []*Key) error |
144 | 153 |
145 // Testable returns the Testable interface for the implementation, or ni l if | 154 // Testable returns the Testable interface for the implementation, or ni l if |
146 // there is none. | 155 // there is none. |
147 Testable() Testable | 156 Testable() Testable |
148 | 157 |
149 // Raw returns the underlying RawInterface. The Interface and RawInterfa ce may | 158 // Raw returns the underlying RawInterface. The Interface and RawInterfa ce may |
150 // be used interchangably; there's no danger of interleaving access to t he | 159 // be used interchangably; there's no danger of interleaving access to t he |
151 // datastore via the two. | 160 // datastore via the two. |
152 Raw() RawInterface | 161 Raw() RawInterface |
153 } | 162 } |
OLD | NEW |