Created
October 26, 2011 16:12
-
-
Save groks/1316849 to your computer and use it in GitHub Desktop.
Appengine: Comparing existing GetMulti() to proposed Query.ByKey()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Comparing existing GetMulti() to proposed Query.ByKey() | |
// | |
// https://groups.google.com/d/topic/google-appengine-go/R7jRPz0frlE/discussion | |
type Thingy struct { | |
A, B string | |
} | |
const N = 3 | |
func GetThingyMulti(w http.ResponseWriter, r *http.Request) { | |
c := appengine.NewContext(r) | |
var keys = make([]*ds.Key, N) | |
var entities = make([]interface{}, N) | |
for i := 0; i < N; i++ { | |
keys[i] = ds.NewKey(c, "Thingy", "", int64(i+1), nil) | |
thingies[i] = &Thingy{} | |
} | |
if err := ds.GetMulti(c, keys, entities); err != nil { | |
fmt.Fprintf(w, "error: %#v\n", err) | |
return | |
} | |
for _, e := range entities { | |
e, ok := e.(*Thingy) | |
if !ok { | |
fmt.Fprintf(w, "error: %#v\n") | |
return | |
} | |
fmt.Fprintf(w, "%#v: %#v\n", e.A, e.B) | |
} | |
} | |
func GetThingyQuery(w http.ResponseWriter, r *http.Request) { | |
c := appengine.NewContext(r) | |
q := ds.NewQuery("Thingy") | |
for i := 0; i< N; i++ { | |
q.ByKey(ds.NewKey(c, "Thingy", "", int64(i+1), nil)) | |
} | |
iter := q.Run(c) | |
var t Thingy | |
for _, err := iter.Next(&t); err != ds.Done; _, err = iter.Next() { | |
if err != nil { | |
fmt.Fprintf(w, "error: %#v\n", err) | |
return | |
} | |
fmt.Fprintf(w, "%#v: %#v\n", t.A, t.B) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment