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

查看:23
本文介绍了如何为所有 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 属性),所以我必须分配 False 对于我的 AppList 实体中的所有记录.最有效的方法是什么?目前该实体包含大约 4,000 个条目.

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天全站免登陆