如何为所有NDB数据存储条目分配默认值? [英] How to assign default value to all NDB datastore entries?

查看:34
本文介绍了如何为所有NDB数据存储条目分配默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在现有的NDB类中添加一个新属性:

I have to add one new property to my existing NDB class:

class AppList(ndb.Model):
    ...
    ignore = ndb.BooleanProperty(default=False) # new property

然后我将按以下方式使用它:

Then I will use it like below:

entries = AppList.query()
entries = entries.filter(AppList.counter > 5)
entries = entries.filter(AppList.ignore == False)
entries = entries.fetch()

我不能使用AppList.ignore != True来捕获早期添加的记录(不具有ignore属性),因此我必须为AppList实体中的所有记录分配False.最有效的方法是什么?当前,该实体包含大约4000个条目.

I can not use AppList.ignore != True to catch early added records (which don't have ignore property), so I have to assign False for all records in my AppList entity. What is the most effective way to do it? Currently this entity contains about 4'000 entries.

更新.我决定使用以下丑陋的代码(无法应用cursors),它作为cron作业运行.但是我不是每次都更新相同的100条记录吗?

Upd. I've decided to use the following ugly code (didn't manage to apply cursors), it runs as a cron job. But don't I update the same 100 records each time?

entities = AppList.query()
# entities = entities.filter(AppList.ignore != False)
entities = entities.fetch(100)
while entities:
    for entity in entities:
        entity.ignore = False
        entity.put()
    entities = AppList.query()
    # entities = entities.filter(AppList.ignore != False)
    entities = entities.fetch(100)

推荐答案

请不要忘记在这些情况下使用了MapReduce库.但是我认为最好的方法是同时使用所有这些建议.

Don't forget that there is a MapReduce library that is used in these cases. But I think the best method is to use all these suggestions toghether.

现在,您需要get()和put()4000个实体,问题是如何减少此操作的成本".

Now, you need to get() and put() 4000 entities and the question is how to reduce the "costs" of this operation.

我只是想知道您的bool(entity.ignore)返回什么.如果missing属性返回False,则可以考虑False来调整代码并推迟操作.如果由于其他原因放置了put(),则由于默认参数,会将属性ignore写入False.因此,对于其余实体,可以运行这样的脚本(通过remote_api):

I'm just curious to know what your bool(entity.ignore) returns. If a missing property return False you can adjust the code considering it False and postponed the operation. If you put() for other reason the property ignore is written to False thanks to the default argument. So, for the rest of the entities can run a script like this (via remote_api):

def iter_entities(cursor=None):
    entries = AppList.query()
    res, cur, more = entries.fetch_page(100, start_cursor=cursor)
    put_queue = [ent for ent in res if not hasattr(ent, 'ignore')]
    # put_queue = []
    # for ent in res:
    #    if not hasattr(ent, 'ignore'):
    #        put_queue.append(ent)
    ndb.put_multi(put_queue)
    if more:
        iter_entities(cur) # a taskqueue is better

这篇关于如何为所有NDB数据存储条目分配默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆