Assuming I have entries in a datastore that reference other datastore entities:
id link val_1 val_2
-----------------------------
1 None 10 None
2 1 None 4
3 2 None 5
4 3 None 2
etc... etc... etc... etc...
I need to recursively progress through the datastore doing the following:
1. Get entry 1 via get_by_id(1);
2. Get val_1 from entry 1
3. Query db for any entries that have '1' as the link.
4. With the results, add entry 1's val_1 to their val_2 and update their val_1
with the result
5. For each result, query db for other entries that have their 'link' set to the
results id, add the result's val_1 to their val_2 and update their val_1
6. Query db for any entries that have the result's id as their link
7. Repeat from step 5
I’ve tried to translate this into python, but I’m getting nowhere. Does anyone have some ideas on how I can accomplish this?
edit: I got the following code to do what I need. I feel that it could be refactored significantly. Any suggestions?
def updateLinked(cid):
c = Info.get_by_id(int(cid))
q = Info.all()
q.filter('link =',int(cid))
result = q.fetch(1000)
for item in result:
linked = 1
new_link_id = item.key().id()
val_2 = item.val_2
val_1 = c.val_1 + val_2
item.val_1 = val_1
item.put()
while linked == 1:
q2 = Info.all()
q2.filter('link=',new_link_lid)
result2 = q2.fetch(1000)
if len(result2):
linked = 1
for item2 in result2:
c = Info.get_by_id(new_link_id)
new_link_id = item2.key().id()
val_2 = item2.val_2
val_1 = c.val_1 + val_2
item2.val_1 = val_1
item2.put()
else:
linked = 0
Looks like you are essentially pushing an update out across a hierarchical dataset. Consider using either GAE’s built in parent/ancestor functionality or rolling your own by adding a db.ListProperty(db.Key) to your model that includes a list of all ‘link’ record keys. This would save you considerable time by allowing you to query all the data that needs to be updated in one go.