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 "fmt" | 8 "fmt" |
9 | 9 |
10 "golang.org/x/net/context" | 10 "golang.org/x/net/context" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 // * It may be nil if some of the keys/vals to the PutMulti were bad, since | 74 // * It may be nil if some of the keys/vals to the PutMulti were bad, since |
75 // all keys are validated before the RPC occurs! | 75 // all keys are validated before the RPC occurs! |
76 // - err is an error associated with putting this entity. | 76 // - err is an error associated with putting this entity. |
77 type PutMultiCB func(key Key, err error) | 77 type PutMultiCB func(key Key, err error) |
78 | 78 |
79 // DeleteMultiCB is the callback signature provided to RawInterface.DeleteMulti | 79 // DeleteMultiCB is the callback signature provided to RawInterface.DeleteMulti |
80 // | 80 // |
81 // - err is an error associated with deleting this entity. | 81 // - err is an error associated with deleting this entity. |
82 type DeleteMultiCB func(err error) | 82 type DeleteMultiCB func(err error) |
83 | 83 |
| 84 type nullMetaGetterType struct{} |
| 85 |
| 86 func (nullMetaGetterType) GetMeta(string) (interface{}, error)
{ return nil, ErrMetaFieldUnset } |
| 87 func (nullMetaGetterType) GetMetaDefault(_ string, dflt interface{}) interface{}
{ return dflt } |
| 88 |
| 89 var nullMetaGetter MetaGetter = nullMetaGetterType{} |
| 90 |
| 91 // MultiMetaGetter is a carrier for metadata, used with RawInterface.GetMulti |
| 92 // |
| 93 // It's OK to default-construct this. GetMeta will just return |
| 94 // (nil, ErrMetaFieldUnset) for every index. |
| 95 type MultiMetaGetter []MetaGetter |
| 96 |
| 97 // NewMultiMetaGetter returns a new MultiMetaGetter object. data may be nil. |
| 98 func NewMultiMetaGetter(data []PropertyMap) MultiMetaGetter { |
| 99 if len(data) == 0 { |
| 100 return nil |
| 101 } |
| 102 inner := make(MultiMetaGetter, len(data)) |
| 103 for i, pm := range data { |
| 104 inner[i] = pm |
| 105 } |
| 106 return inner |
| 107 } |
| 108 |
| 109 // GetMeta is like PropertyLoadSaver.GetMeta, but it also takes an index |
| 110 // indicating which slot you want metadata for. If idx isn't there, this |
| 111 // returns (nil, ErrMetaFieldUnset). |
| 112 func (m MultiMetaGetter) GetMeta(idx int, key string) (interface{}, error) { |
| 113 return m.GetSingle(idx).GetMeta(key) |
| 114 } |
| 115 |
| 116 // GetMetaDefault is like PropertyLoadSaver.GetMetaDefault, but it also takes an |
| 117 // index indicating which slot you want metadata for. If idx isn't there, this |
| 118 // returns dflt. |
| 119 func (m MultiMetaGetter) GetMetaDefault(idx int, key string, dflt interface{}) i
nterface{} { |
| 120 return m.GetSingle(idx).GetMetaDefault(key, dflt) |
| 121 } |
| 122 |
| 123 func (m MultiMetaGetter) GetSingle(idx int) MetaGetter { |
| 124 if idx >= len(m) || m[idx] == nil { |
| 125 return nullMetaGetter |
| 126 } |
| 127 return m[idx] |
| 128 } |
| 129 |
84 // RawInterface implements the datastore functionality without any of the fancy | 130 // RawInterface implements the datastore functionality without any of the fancy |
85 // reflection stuff. This is so that Filters can avoid doing lots of redundant | 131 // reflection stuff. This is so that Filters can avoid doing lots of redundant |
86 // reflection work. See datastore.RawInterface for a more user-friendly interfac
e. | 132 // reflection work. See datastore.RawInterface for a more user-friendly interfac
e. |
87 type RawInterface interface { | 133 type RawInterface interface { |
88 NewKey(kind, stringID string, intID int64, parent Key) Key | 134 NewKey(kind, stringID string, intID int64, parent Key) Key |
89 DecodeKey(encoded string) (Key, error) | 135 DecodeKey(encoded string) (Key, error) |
90 NewQuery(kind string) Query | 136 NewQuery(kind string) Query |
91 | 137 |
92 // RunInTransaction runs f in a transaction. | 138 // RunInTransaction runs f in a transaction. |
93 // | 139 // |
94 // opts may be nil. | 140 // opts may be nil. |
95 // | 141 // |
96 // NOTE: Implementations and filters are guaranteed that: | 142 // NOTE: Implementations and filters are guaranteed that: |
97 // - f is not nil | 143 // - f is not nil |
98 RunInTransaction(f func(c context.Context) error, opts *TransactionOptio
ns) error | 144 RunInTransaction(f func(c context.Context) error, opts *TransactionOptio
ns) error |
99 | 145 |
100 // Run executes the given query, and calls `cb` for each successfully it
em. | 146 // Run executes the given query, and calls `cb` for each successfully it
em. |
101 // | 147 // |
102 // NOTE: Implementations and filters are guaranteed that: | 148 // NOTE: Implementations and filters are guaranteed that: |
103 // - query is not nil | 149 // - query is not nil |
104 // - cb is not nil | 150 // - cb is not nil |
105 Run(q Query, cb RawRunCB) error | 151 Run(q Query, cb RawRunCB) error |
106 | 152 |
107 // GetMulti retrieves items from the datastore. | 153 // GetMulti retrieves items from the datastore. |
108 // | 154 // |
109 // Callback execues once per key, in the order of keys. Callback may not | 155 // Callback execues once per key, in the order of keys. Callback may not |
110 // execute at all if there's a server error. If callback is nil, this | 156 // execute at all if there's a server error. If callback is nil, this |
111 // method does nothing. | 157 // method does nothing. |
112 // | 158 // |
| 159 // meta is used to propagate metadata from higher levels. |
| 160 // |
113 // NOTE: Implementations and filters are guaranteed that: | 161 // NOTE: Implementations and filters are guaranteed that: |
114 // - len(keys) > 0 | 162 // - len(keys) > 0 |
115 // - all keys are Valid, !Incomplete, and in the current namespace | 163 // - all keys are Valid, !Incomplete, and in the current namespace |
116 // - cb is not nil | 164 // - cb is not nil |
117 » GetMulti(keys []Key, cb GetMultiCB) error | 165 » GetMulti(keys []Key, meta MultiMetaGetter, cb GetMultiCB) error |
118 | 166 |
119 // PutMulti writes items to the datastore. | 167 // PutMulti writes items to the datastore. |
120 // | 168 // |
121 // Callback execues once per key/value pair, in the passed-in order. Cal
lback | 169 // Callback execues once per key/value pair, in the passed-in order. Cal
lback |
122 // may not execute at all if there was a server error. | 170 // may not execute at all if there was a server error. |
123 // | 171 // |
124 // NOTE: Implementations and filters are guaranteed that: | 172 // NOTE: Implementations and filters are guaranteed that: |
125 // - len(keys) > 0 | 173 // - len(keys) > 0 |
126 // - len(keys) == len(vals) | 174 // - len(keys) == len(vals) |
127 // - all keys are Valid and in the current namespace | 175 // - all keys are Valid and in the current namespace |
128 // - cb is not nil | 176 // - cb is not nil |
129 PutMulti(keys []Key, vals []PropertyMap, cb PutMultiCB) error | 177 PutMulti(keys []Key, vals []PropertyMap, cb PutMultiCB) error |
130 | 178 |
131 // DeleteMulti removes items from the datastore. | 179 // DeleteMulti removes items from the datastore. |
132 // | 180 // |
133 // Callback execues once per key, in the order of keys. Callback may not | 181 // Callback execues once per key, in the order of keys. Callback may not |
134 // execute at all if there's a server error. | 182 // execute at all if there's a server error. |
135 // | 183 // |
136 // NOTE: Implementations and filters are guaranteed that | 184 // NOTE: Implementations and filters are guaranteed that |
137 // - len(keys) > 0 | 185 // - len(keys) > 0 |
138 // - all keys are Valid, !Incomplete, and in the current namespace | 186 // - all keys are Valid, !Incomplete, and in the current namespace |
139 // - none keys of the keys are 'special' (use a kind prefixed with '__
') | 187 // - none keys of the keys are 'special' (use a kind prefixed with '__
') |
140 // - cb is not nil | 188 // - cb is not nil |
141 DeleteMulti(keys []Key, cb DeleteMultiCB) error | 189 DeleteMulti(keys []Key, cb DeleteMultiCB) error |
142 } | 190 } |
OLD | NEW |