<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>Android &amp; App Engine Tips</description><title>Tornblue</title><generator>Tumblr (3.0; @tornblue)</generator><link>http://tornblue.com/</link><item><title>App Engine: How to do an efficient OR query?</title><description>&lt;p&gt;Let’s say you want to find all of the current user’s facebook friends who are using your app. You’ve stored the facebook id of each connected user on a User entity and the Facebook API returns to you a large list of facebook ids of the friends of the current user. To look them up in your datastore you basically need a giant &lt;b&gt;OR&lt;/b&gt; or &lt;b&gt;IN&lt;/b&gt; query on the User entity. Here’s a traditional SQL example of how you might do this:&lt;/p&gt;

&lt;!-- HTML generated using hilite.me --&gt;&lt;div style="background: #f8f8f8; overflow:auto;width:auto;font-size:12px;color:black;background:white;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"&gt;&lt;pre style="margin: 0; line-height: 125%"&gt;&lt;span style="color: #204a87; font-weight: bold"&gt;SELECT&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;*&lt;/span&gt; &lt;span style="color: #204a87; font-weight: bold"&gt;FROM&lt;/span&gt; &lt;span style="color: #204a87; font-weight: bold"&gt;User&lt;/span&gt; &lt;span style="color: #204a87; font-weight: bold"&gt;WHERE&lt;/span&gt; &lt;span style="color: #000000"&gt;facebook_id&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #4e9a06"&gt;'100213'&lt;/span&gt; &lt;span style="color: #204a87; font-weight: bold"&gt;OR&lt;/span&gt; &lt;span style="color: #000000"&gt;facebook_id&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #4e9a06"&gt;'104413'&lt;/span&gt; 
     &lt;span style="color: #204a87; font-weight: bold"&gt;OR&lt;/span&gt; &lt;span style="color: #000000"&gt;facebook_id&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #4e9a06"&gt;'150273'&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold"&gt;...&lt;/span&gt; &lt;span style="color: #000000"&gt;etc&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;.&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br/&gt;&lt;p&gt;The way the App Engine documentation says to query for multiple possible values is to use an &lt;b&gt;IN&lt;/b&gt; query. Example:&lt;/p&gt;

&lt;!-- HTML generated using hilite.me --&gt;&lt;div style="background: #f8f8f8; overflow:auto;width:auto;font-size:12px;color:black;background:white;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"&gt;&lt;pre style="margin: 0; line-height: 125%"&gt;&lt;span style="color: #000000"&gt;employees&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #000000"&gt;User&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;gql&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #4e9a06"&gt;'WHERE facebook_id IN :1'&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;,&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold"&gt;[&lt;/span&gt;&lt;span style="color: #4e9a06"&gt;'100213'&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;,&lt;/span&gt; &lt;span style="color: #4e9a06"&gt;'104413'&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;,&lt;/span&gt; &lt;span style="color: #4e9a06"&gt;'150273'&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;])&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br/&gt;&lt;p&gt;The problem with this is that it quickly becomes slow and maxes out at 30 values. The reason is that behind the scenes it’s actually running an equals query for each possible value. This of course won’t work with thousands of facebook ids you might need to look up.&lt;/p&gt;

&lt;p&gt;One way to handle this efficiently in App Engine is to create a separate entity in your datastore for just facebook id lookups. You can call it something like &lt;b&gt;FacebookIdIndex&lt;/b&gt;. You will need a &lt;b&gt;FacebookIdIndex&lt;/b&gt; entity for each User that has an assigned facebook_id. Here’s what the model might look like:&lt;/p&gt;

&lt;!-- HTML generated using hilite.me --&gt;&lt;div style="background: #f8f8f8; overflow:auto;width:auto;font-size:12px;color:black;background:white;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"&gt;&lt;pre style="margin: 0; line-height: 125%"&gt;&lt;span style="color: #204a87; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #000000"&gt;FacebookIdIndex&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;db&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;Model&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;)&lt;/span&gt;
    &lt;span style="color: #000000"&gt;user&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #000000"&gt;db&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;ReferenceProperty&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;User&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;,&lt;/span&gt; &lt;span style="color: #000000"&gt;required&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt;&lt;span style="color: #3465a4"&gt;True&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br/&gt;&lt;p&gt;Then, when you create each &lt;b&gt;FacebookIdIndex&lt;/b&gt; you set the key_name to the facebook id and set the reference property of the corresponding app User entity. Like so:&lt;/p&gt;

&lt;!-- HTML generated using hilite.me --&gt;&lt;div style="background: #f8f8f8; overflow:auto;width:auto;font-size:12px;color:black;background:white;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"&gt;&lt;pre style="margin: 0; line-height: 125%"&gt;&lt;span style="color: #000000"&gt;facebook_index&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #000000"&gt;FacebookIdIndex&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;key_name&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt;&lt;span style="color: #204a87"&gt;str&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;facebook_id&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;),&lt;/span&gt; &lt;span style="color: #000000"&gt;user&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt;user&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;)&lt;/span&gt;
&lt;span style="color: #000000"&gt;facebook_index&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;put&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br/&gt;&lt;p&gt;This creates an an entity with the key name of the facebook id and one property with a reference back to the User entity we care about. (NOTE: Make sure you convert the &lt;b&gt;facebook_id&lt;/b&gt; to a string just in case you get it as an integer. Key names can not be integer values.) &lt;/p&gt;

&lt;p&gt;Now, to look up all Users who are friends with the current user in your app, you can do a bulk key name lookup. This is extremely fast compared to separate queries and there is no limit on the number of key names you can lookup. Given a list of facebook ids, here’s how it’s done:&lt;/p&gt;

&lt;!-- HTML generated using hilite.me --&gt;&lt;div style="background: #f8f8f8; overflow:auto;width:auto;font-size:12px;color:black;background:white;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"&gt;&lt;pre style="margin: 0; line-height: 125%"&gt;&lt;span style="color: #8f5902; font-style: italic"&gt;# Get all matching FacebookIdIndex's in one quick bulk key name lookup.&lt;/span&gt;
&lt;span style="color: #000000"&gt;indexes&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #000000"&gt;FacebookIdIndex&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;get_by_key_name&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;facebook_ids&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;)&lt;/span&gt;

&lt;span style="color: #8f5902; font-style: italic"&gt;# Now get a list of all of the User entity keys that match the facebook ids.&lt;/span&gt;
&lt;span style="color: #000000"&gt;user_keys&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold"&gt;[]&lt;/span&gt;
&lt;span style="color: #204a87; font-weight: bold"&gt;for&lt;/span&gt; &lt;span style="color: #000000"&gt;index&lt;/span&gt; &lt;span style="color: #204a87; font-weight: bold"&gt;in&lt;/span&gt; &lt;span style="color: #000000"&gt;indexes&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;:&lt;/span&gt;
    &lt;span style="color: #8f5902; font-style: italic"&gt;# Check if a match. The bulk get by key name will return None for non matches.&lt;/span&gt;
    &lt;span style="color: #204a87; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #000000"&gt;index&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;:&lt;/span&gt;
        &lt;span style="color: #8f5902; font-style: italic"&gt;# Get the key of the User entity we want.&lt;/span&gt;
        &lt;span style="color: #000000"&gt;user_key&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #000000"&gt;FacebookIdIndex&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;user&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;get_value_for_datastore&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;index&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;)&lt;/span&gt;
        &lt;span style="color: #8f5902; font-style: italic"&gt;# Check if entity exists just to make sure User entity still exists.&lt;/span&gt;
        &lt;span style="color: #204a87; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #000000"&gt;user_key&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;:&lt;/span&gt;
            &lt;span style="color: #000000"&gt;user_keys&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;append&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;user_key&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;)&lt;/span&gt;

&lt;span style="color: #8f5902; font-style: italic"&gt;# If any users matched, bulk get all of them.&lt;/span&gt;
&lt;span style="color: #204a87; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #000000"&gt;user_keys&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;:&lt;/span&gt;
    &lt;span style="color: #000000"&gt;users&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #000000"&gt;User&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;get&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;user_keys&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;)&lt;/span&gt;

&lt;span style="color: #8f5902; font-style: italic"&gt;# Found all friends of the current user using the app!&lt;/span&gt;
&lt;span style="color: #204a87; font-weight: bold"&gt;return&lt;/span&gt; &lt;span style="color: #000000"&gt;users&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;br/&gt;&lt;p&gt;The &lt;b&gt;get_value_for_datastore()&lt;/b&gt; call is used to check for non matches and also to prevent App Engine from making individual get calls for each User entity. We want to delay the get for each User entity for the bulk &lt;b&gt;get()&lt;/b&gt; call, which is much faster than doing individual gets for each User entity.&lt;/p&gt;

&lt;p&gt;I hope that all makes sense. We’ve now gone from multiple slow GQL queries to two quick bulk get calls. &lt;/p&gt;</description><link>http://tornblue.com/post/11310830291</link><guid>http://tornblue.com/post/11310830291</guid><pubDate>Tue, 11 Oct 2011 00:58:00 -0700</pubDate><category>google app engine</category><category>python</category><category>gql</category></item><item><title>The Most Important Page In All App Engine Land</title><description>&lt;p&gt;&lt;a href="http://code.google.com/appengine/docs/python/datastore/functions.html"&gt;&lt;a href="http://code.google.com/appengine/docs/python/datastore/functions.html"&gt;http://code.google.com/appengine/docs/python/datastore/functions.html&lt;/a&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For my first post I’ll talk about what I think is a very useful docs page that is sort of buried in the Python App Engine docs. It’s a page that describes all of the functions available in the &lt;b&gt;google.appengine.ext.db&lt;/b&gt; package. These are extremely useful functions that I find most App Engine beginners never discover or never use. I’m only going to talk about some of the functions, but all of them can be really useful. I’m not going to talk about &lt;b&gt;run_in_transaction()&lt;/b&gt; since that function is covered in the Transactions section of the App Engine docs.&lt;/p&gt;

&lt;p&gt;Functions like &lt;b&gt;delete()&lt;/b&gt;, &lt;b&gt;delete_async()&lt;/b&gt;, &lt;b&gt;get()&lt;/b&gt;, &lt;b&gt;get_async()&lt;/b&gt;, &lt;b&gt;put()&lt;/b&gt;, and &lt;b&gt;put_async()&lt;/b&gt; can really help improve the latency in your app. These are functions that let you edit entities in bulk, which can really speed up your datastore requests by sending only one request (RPC) to the datastore instead of many.&lt;/p&gt;

&lt;p&gt;Typical case:&lt;/p&gt;

&lt;!-- HTML generated using hilite.me --&gt;&lt;div style="background: #f8f8f8; overflow:auto;width:auto;font-size:12px;color:black;background:white;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"&gt;&lt;pre style="margin: 0; line-height: 125%"&gt;&lt;span style="color: #000000"&gt;employees_trained&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #000000"&gt;Employee&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;gql&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #4e9a06"&gt;"WHERE account IN :1"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;,&lt;/span&gt;
                                 &lt;span style="color: #000000"&gt;training_registration_list&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;)&lt;/span&gt;
&lt;span style="color: #204a87; font-weight: bold"&gt;for&lt;/span&gt; &lt;span style="color: #000000"&gt;e&lt;/span&gt; &lt;span style="color: #204a87; font-weight: bold"&gt;in&lt;/span&gt; &lt;span style="color: #000000"&gt;employees_trained&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;:&lt;/span&gt;
    &lt;span style="color: #000000"&gt;e&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;new_hire_training_completed&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #3465a4"&gt;True&lt;/span&gt;
    &lt;span style="color: #000000"&gt;e&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;put&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br/&gt;&lt;p&gt;
Now, instead of doing an individual put() for each employee, we can create create a list of entities to put later all at once:
&lt;/p&gt;

&lt;!-- HTML generated using hilite.me --&gt;&lt;div style="background: #f8f8f8; overflow:auto;width:auto;font-size:12px;color:black;background:white;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"&gt;&lt;pre style="margin: 0; line-height: 125%"&gt;&lt;span style="color: #000000"&gt;employees_trained&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #000000"&gt;Employee&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;gql&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #4e9a06"&gt;"WHERE account IN :1"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;,&lt;/span&gt;
                                 &lt;span style="color: #000000"&gt;training_registration_list&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;)&lt;/span&gt;
&lt;span style="color: #000000"&gt;to_save&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold"&gt;[]&lt;/span&gt;
&lt;span style="color: #204a87; font-weight: bold"&gt;for&lt;/span&gt; &lt;span style="color: #000000"&gt;e&lt;/span&gt; &lt;span style="color: #204a87; font-weight: bold"&gt;in&lt;/span&gt; &lt;span style="color: #000000"&gt;employees_trained&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;:&lt;/span&gt;
    &lt;span style="color: #000000"&gt;e&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;new_hire_training_completed&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #3465a4"&gt;True&lt;/span&gt;
    &lt;span style="color: #000000"&gt;to_save&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;append&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;e&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;)&lt;/span&gt;

&lt;span style="color: #000000"&gt;db&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;put&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;to_save&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br/&gt;&lt;p&gt;If the number of employees to modify is high and you’re saving them individually then it can really slow down your page loads. Editing them in bulk removes a lot of the overhead of many trips back and forth with the datastore. You can even save entities of different model types. Just append any to be updated model instances to the to_save list and call db.put() all at once.&lt;/p&gt;

&lt;p&gt;To save even more time you can use the async functions. These will asynchronously start reading/writing to the datastore while the code after it gets to execute. This is so key when you’re doing a lot of processing in one page request. &lt;/p&gt;

&lt;p&gt;Quick example:&lt;/p&gt;

&lt;!-- HTML generated using hilite.me --&gt;&lt;div style="background: #f8f8f8; overflow:auto;width:auto;font-size:12px;color:black;background:white;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"&gt;&lt;pre style="margin: 0; line-height: 125%"&gt;&lt;span style="color: #000000"&gt;employees_trained&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #000000"&gt;Employee&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;gql&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #4e9a06"&gt;"WHERE account IN :1"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;,&lt;/span&gt;
                                 &lt;span style="color: #000000"&gt;training_registration_list&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;)&lt;/span&gt;
&lt;span style="color: #000000"&gt;to_save&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold"&gt;[]&lt;/span&gt;
&lt;span style="color: #204a87; font-weight: bold"&gt;for&lt;/span&gt; &lt;span style="color: #000000"&gt;e&lt;/span&gt; &lt;span style="color: #204a87; font-weight: bold"&gt;in&lt;/span&gt; &lt;span style="color: #000000"&gt;employees_trained&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;:&lt;/span&gt;
    &lt;span style="color: #000000"&gt;e&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;new_hire_training_completed&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #3465a4"&gt;True&lt;/span&gt;
    &lt;span style="color: #000000"&gt;to_save&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;append&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;e&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;)&lt;/span&gt;

&lt;span style="color: #000000"&gt;employee_async&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #000000"&gt;db&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;put_async&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;to_save&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;)&lt;/span&gt;

&lt;span style="color: #ce5c00; font-weight: bold"&gt;...&lt;/span&gt;
&lt;span style="color: #8f5902; font-style: italic"&gt;# Do a bunch of stuff.&lt;/span&gt;
&lt;span style="color: #ce5c00; font-weight: bold"&gt;...&lt;/span&gt;

&lt;span style="color: #8f5902; font-style: italic"&gt;# Now check for async result at end of code to guarantee it was written.&lt;/span&gt;
&lt;span style="color: #000000"&gt;employee_async&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;check_success&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br/&gt;&lt;p&gt; Next up, &lt;b&gt;model_to_protobuf()&lt;/b&gt; and &lt;b&gt;model_from_protobuf()&lt;/b&gt; can be really useful when trying to serialize an entity to save either in memcache or some other datastore. We specifically use it to store model instances in memcache.&lt;/p&gt;

&lt;p&gt;Here’s an example of how to use these methods to set and get model instances from memcache for an Employee model instance:&lt;/p&gt;

&lt;!-- HTML generated using hilite.me --&gt;&lt;div style="background: #f8f8f8; overflow:auto;width:auto;font-size:12px;color:black;background:white;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"&gt;&lt;pre style="margin: 0; line-height: 125%"&gt;&lt;span style="color: #000000"&gt;employee&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #3465a4"&gt;None&lt;/span&gt;
&lt;span style="color: #000000"&gt;pb_employee&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #000000"&gt;memcache&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;get&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;memcache_key&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;)&lt;/span&gt;
&lt;span style="color: #204a87; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #000000"&gt;pb_employee&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;:&lt;/span&gt;
  &lt;span style="color: #000000"&gt;employee&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #000000"&gt;db&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;model_from_protobuf&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;entity_pb&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;EntityProto&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;pb_employee&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;))&lt;/span&gt;

&lt;span style="color: #204a87; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #204a87; font-weight: bold"&gt;not&lt;/span&gt; &lt;span style="color: #000000"&gt;employee&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;:&lt;/span&gt;
  &lt;span style="color: #000000"&gt;employee&lt;/span&gt; &lt;span style="color: #ce5c00; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #000000"&gt;Employee&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;get&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #4e9a06"&gt;'&lt;key&gt;'&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;)&lt;/span&gt;
  &lt;span style="color: #000000"&gt;memcache&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;set&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;memcache_key&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;,&lt;/span&gt; &lt;span style="color: #000000"&gt;db&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;model_to_protobuf&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;employee&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;)&lt;/span&gt;&lt;span style="color: #ce5c00; font-weight: bold"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;Encode&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;())&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br/&gt;&lt;p&gt;
Finally, &lt;b&gt;allocate_ids()&lt;/b&gt;, &lt;b&gt;allocate_ids_async()&lt;/b&gt;, and &lt;b&gt;allocate_id_range()&lt;/b&gt; can be useful for when you need to know what the datastore IDs of entities will be before you create them or to avoid conflicts when transferring data across datastores. Bill Katz gives a good explanation of how those functions are useful for datastore backup and restore (&lt;a href="http://www.billkatz.com/2009/9/Pre-allocating-IDs-for-App-Engine-datastore"&gt;link&lt;/a&gt;)
&lt;/p&gt;

&lt;p&gt;That’s it! You should always check back on that page. Google keeps adding more and more useful functions to the db package.&lt;/p&gt;&lt;p&gt; For my next App Engine post I’ll talk about how to use custom models to handle large OR conditional queries like how to bulk lookup every entity that matches each of a list of different values.&lt;/p&gt;</description><link>http://tornblue.com/post/11014923517</link><guid>http://tornblue.com/post/11014923517</guid><pubDate>Tue, 04 Oct 2011 00:25:15 -0700</pubDate><category>app engine</category><category>python</category><category>db package</category></item><item><title>Dropping Knowledge, Whether it's accurate or not.</title><description>&lt;p&gt;I’ve been working on Android and App Engine development for a while now and I feel like a lot of the answers I was looking for on the web weren’t there. So I’m going to start writing Android and Google App Engine tips here. Hopefully it will save at least someone a lot of time. I’m a terrible writer so hopefully that doesn’t get in the way. &lt;/p&gt;
&lt;p&gt;I’m not claiming to be an expert so I might be wrong from time to time. If so let me know and it will make me better.&lt;/p&gt;
&lt;p&gt;On with it!&lt;/p&gt;</description><link>http://tornblue.com/post/10841844395</link><guid>http://tornblue.com/post/10841844395</guid><pubDate>Fri, 30 Sep 2011 01:08:00 -0700</pubDate></item></channel></rss>

