2011-09-19 55 views
0

我以爲我理解基於http://code.google.com/appengine/docs/python/datastore/queryclass.html的遊標和查詢的概念,但顯然它不是基於我在代碼中做什麼。幫助理解with_cursor和查詢

我有我的數據存儲310個項目,我想在它們之間迭代在100小批量:

query = Event.all() 
query.order("__key__") 

batch_size = 100; 

# expecting this to return the 1st 100 of the 310 items  
results = query.fetch(limit=batch_size) 

logging.info("count 1: %d, results: %d" % (query.count(), len(results))) 
# reports: count 1: 310, results: 100 

for item in results: 
    print item # this will print items 1-100, which is expected 

# Move to the next batch block 
cursor = query.cursor(); 
query.with_cursor(cursor);    

results = query.fetch(limit=batch_size) 
logging.info("count 2: %d, results: %d" % (query.count(), len(results))) 
# reports: count 2: 0, results: 0 
# but was expecting to move to item 101 

我怎麼能在100批次通過我所有的實體迭代? 'query.cursor()'是否返回第一個batch_size結尾處的光標或該塊的起始位置?

+1

我猜''.count()'搞亂了光標。在調用'.count()'之前嘗試獲取遊標。 (爲了免於尼克約翰遜抨擊我,我應該指出,你可能根本不想使用count())。 – geoffspear

+0

我看到 - 使用len(結果)而不是count來表示整個數據集的結束。 –

+0

我可以預測嗎? –

回答

2

.count()確實讓你失望。要查看原因,請創建第二個相同的查詢,將已保存的光標應用於該查詢,然後查看會發生什麼情況。

順便說一下,__key__順序是隱含的。