Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(256)

Side by Side Diff: service/datastore/serialize/serialize.go

Issue 1336443003: Implement projection queries correctly. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: fix comments Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « service/datastore/reflect.go ('k') | service/datastore/serialize/serialize_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 serialize 5 package serialize
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "errors" 9 "errors"
10 "fmt" 10 "fmt"
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 // way that it does for WriteKey, but only has an effect if `p` contains a 215 // way that it does for WriteKey, but only has an effect if `p` contains a
216 // Key as its Value. 216 // Key as its Value.
217 func WriteProperty(buf Buffer, context KeyContext, p ds.Property) (err error) { 217 func WriteProperty(buf Buffer, context KeyContext, p ds.Property) (err error) {
218 defer recoverTo(&err) 218 defer recoverTo(&err)
219 typb := byte(p.Type()) 219 typb := byte(p.Type())
220 if p.IndexSetting() != ds.NoIndex { 220 if p.IndexSetting() != ds.NoIndex {
221 typb |= 0x80 221 typb |= 0x80
222 } 222 }
223 panicIf(buf.WriteByte(typb)) 223 panicIf(buf.WriteByte(typb))
224 switch p.Type() { 224 switch p.Type() {
225 » case ds.PTNull, ds.PTBoolTrue, ds.PTBoolFalse: 225 » case ds.PTNull:
226 » case ds.PTBool:
227 » » b := p.Value().(bool)
228 » » if b {
229 » » » err = buf.WriteByte(1)
230 » » } else {
231 » » » err = buf.WriteByte(0)
232 » » }
226 case ds.PTInt: 233 case ds.PTInt:
227 _, err = cmpbin.WriteInt(buf, p.Value().(int64)) 234 _, err = cmpbin.WriteInt(buf, p.Value().(int64))
228 case ds.PTFloat: 235 case ds.PTFloat:
229 _, err = cmpbin.WriteFloat64(buf, p.Value().(float64)) 236 _, err = cmpbin.WriteFloat64(buf, p.Value().(float64))
230 case ds.PTString: 237 case ds.PTString:
231 _, err = cmpbin.WriteString(buf, p.Value().(string)) 238 _, err = cmpbin.WriteString(buf, p.Value().(string))
232 case ds.PTBytes: 239 case ds.PTBytes:
233 » » if p.IndexSetting() == ds.NoIndex { 240 » » _, err = cmpbin.WriteBytes(buf, p.Value().([]byte))
234 » » » _, err = cmpbin.WriteBytes(buf, p.Value().([]byte))
235 » » } else {
236 » » » _, err = cmpbin.WriteBytes(buf, p.Value().(ds.ByteString ))
237 » » }
238 case ds.PTTime: 241 case ds.PTTime:
239 err = WriteTime(buf, p.Value().(time.Time)) 242 err = WriteTime(buf, p.Value().(time.Time))
240 case ds.PTGeoPoint: 243 case ds.PTGeoPoint:
241 err = WriteGeoPoint(buf, p.Value().(ds.GeoPoint)) 244 err = WriteGeoPoint(buf, p.Value().(ds.GeoPoint))
242 case ds.PTKey: 245 case ds.PTKey:
243 err = WriteKey(buf, context, p.Value().(ds.Key)) 246 err = WriteKey(buf, context, p.Value().(ds.Key))
244 case ds.PTBlobKey: 247 case ds.PTBlobKey:
245 _, err = cmpbin.WriteString(buf, string(p.Value().(blobstore.Key ))) 248 _, err = cmpbin.WriteString(buf, string(p.Value().(blobstore.Key )))
246 } 249 }
247 return 250 return
248 } 251 }
249 252
250 // ReadProperty reads a Property from the buffer. `context`, `appid`, and 253 // ReadProperty reads a Property from the buffer. `context`, `appid`, and
251 // `namespace` behave the same way they do for ReadKey, but only have an 254 // `namespace` behave the same way they do for ReadKey, but only have an
252 // effect if the decoded property has a Key value. 255 // effect if the decoded property has a Key value.
253 func ReadProperty(buf Buffer, context KeyContext, appid, namespace string) (p ds .Property, err error) { 256 func ReadProperty(buf Buffer, context KeyContext, appid, namespace string) (p ds .Property, err error) {
254 val := interface{}(nil) 257 val := interface{}(nil)
255 » typb, err := buf.ReadByte() 258 » b, err := buf.ReadByte()
256 if err != nil { 259 if err != nil {
257 return 260 return
258 } 261 }
259 is := ds.ShouldIndex 262 is := ds.ShouldIndex
260 » if (typb & 0x80) == 0 { 263 » if (b & 0x80) == 0 {
261 is = ds.NoIndex 264 is = ds.NoIndex
262 } 265 }
263 » switch ds.PropertyType(typb & 0x7f) { 266 » switch ds.PropertyType(b & 0x7f) {
264 case ds.PTNull: 267 case ds.PTNull:
265 » case ds.PTBoolTrue: 268 » case ds.PTBool:
266 » » val = true 269 » » b, err = buf.ReadByte()
267 » case ds.PTBoolFalse: 270 » » val = (b != 0)
268 » » val = false
269 case ds.PTInt: 271 case ds.PTInt:
270 val, _, err = cmpbin.ReadInt(buf) 272 val, _, err = cmpbin.ReadInt(buf)
271 case ds.PTFloat: 273 case ds.PTFloat:
272 val, _, err = cmpbin.ReadFloat64(buf) 274 val, _, err = cmpbin.ReadFloat64(buf)
273 case ds.PTString: 275 case ds.PTString:
274 val, _, err = cmpbin.ReadString(buf) 276 val, _, err = cmpbin.ReadString(buf)
275 case ds.PTBytes: 277 case ds.PTBytes:
276 » » b := []byte(nil) 278 » » val, _, err = cmpbin.ReadBytes(buf)
277 » » if b, _, err = cmpbin.ReadBytes(buf); err != nil {
278 » » » break
279 » » }
280 » » if is == ds.NoIndex {
281 » » » val = b
282 » » } else {
283 » » » val = ds.ByteString(b)
284 » » }
285 case ds.PTTime: 279 case ds.PTTime:
286 val, err = ReadTime(buf) 280 val, err = ReadTime(buf)
287 case ds.PTGeoPoint: 281 case ds.PTGeoPoint:
288 val, err = ReadGeoPoint(buf) 282 val, err = ReadGeoPoint(buf)
289 case ds.PTKey: 283 case ds.PTKey:
290 val, err = ReadKey(buf, context, appid, namespace) 284 val, err = ReadKey(buf, context, appid, namespace)
291 case ds.PTBlobKey: 285 case ds.PTBlobKey:
292 s := "" 286 s := ""
293 if s, _, err = cmpbin.ReadString(buf); err != nil { 287 if s, _, err = cmpbin.ReadString(buf); err != nil {
294 break 288 break
295 } 289 }
296 val = blobstore.Key(s) 290 val = blobstore.Key(s)
297 default: 291 default:
298 » » err = fmt.Errorf("read: unknown type! %v", typb) 292 » » err = fmt.Errorf("read: unknown type! %v", b)
299 } 293 }
300 if err == nil { 294 if err == nil {
301 err = p.SetValue(val, is) 295 err = p.SetValue(val, is)
302 } 296 }
303 return 297 return
304 } 298 }
305 299
306 // WritePropertyMap writes an entire PropertyMap to the buffer. `context` behave s the same 300 // WritePropertyMap writes an entire PropertyMap to the buffer. `context` behave s the same
307 // way that it does for WriteKey. If WritePropertyMapDeterministic is true, then 301 // way that it does for WriteKey. If WritePropertyMapDeterministic is true, then
308 // the rows will be sorted by property name before they're serialized to buf 302 // the rows will be sorted by property name before they're serialized to buf
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
548 } 542 }
549 } 543 }
550 544
551 func recoverTo(err *error) { 545 func recoverTo(err *error) {
552 if r := recover(); r != nil { 546 if r := recover(); r != nil {
553 if rerr := r.(parseError); rerr != nil { 547 if rerr := r.(parseError); rerr != nil {
554 *err = error(rerr) 548 *err = error(rerr)
555 } 549 }
556 } 550 }
557 } 551 }
OLDNEW
« no previous file with comments | « service/datastore/reflect.go ('k') | service/datastore/serialize/serialize_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698