couchdb-pythons ViewField中map函数的含义

发布于 2024-11-05 04:31:48 字数 853 浏览 1 评论 0原文

我在我的一个项目中使用 couchdb.mapping 。我有一个名为 SupportCase 的类,派生自 Document,其中包含我想要的所有字段。

我的数据库(称为 admin)包含多种文档类型。我在所有文档中都有一个 type 字段,用于区分它们。我有许多 "case" 类型的文档,我想使用视图来获取它们。我有一个名为 support 的设计文档,其中有一个名为 cases 的视图。如果我使用 db.view("support/cases) 请求此视图的结果,我会得到一个包含我想要的内容的行列表。

但是,我想要以某种方式将其包装在 SupportCase 类中,以便我可以调用单个函数并获取我创建的系统中的所有 SupportCase 的列表。 code>ViewField 属性

@ViewField.define('cases')
def all(self, doc):
    if doc.get("type","") == "case":
        yield doc["_id"], doc

现在,如果我调用 SupportCase.all(db),我会返回所有案例,

我不明白的是该视图是否是预先计算并存储在中的 。数据库或类似于 db.query 的按需完成,如果是后者,速度会很慢,我想使用预先计算的视图,该怎么做?

I'm using the couchdb.mapping in one of my projects. I have a class called SupportCase derived from Document that contains all the fields I want.

My database (called admin) contains multiple document types. I have a type field in all the documents which I use to distinguish between them. I have many documents of type "case" which I want to get at using a view. I have design document called support with a view inside it called cases. If I request the results of this view using db.view("support/cases), I get back a list of Rows which have what I want.

However, I want to somehow have this wrapped by the SupportCase class so that I can call a single function and get back a list of all the SupportCases in the system. I created a ViewField property

@ViewField.define('cases')
def all(self, doc):
    if doc.get("type","") == "case":
        yield doc["_id"], doc

Now, if I call SupportCase.all(db), I get back all the cases.

What I don't understand is whether this view is precomputed and stored in the database or done on demand similar to db.query. If it's the latter, it's going to be slow and I want to use a precomputed view. How do I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼泪淡了忧伤 2024-11-12 04:31:48

我认为您需要的是:

@classmethod
def all(cls):
    result = cls.view(db, "support/all", include_docs=True)
    return result.rows

Document 类有一个类方法 view ,它按调用它的类包装行。因此,以下内容将返回一个 ViewResult,其中包含 SupportCase 类型的行,并获取其中的 .rows 给出支持案例列表。

SupportCase.view(db, viewname, include_docs=True)

我认为您不需要了解 ViewField 魔法。但让我解释一下它是如何工作的。请考虑 CouchDB-python 文档中的以下示例。

class Person(Document):
     @ViewField.define('people')
     def by_name(doc):
         yield doc['name'], doc

我认为这相当于:

class Person(Document):
    @classmethod
    def by_name(cls, db, **kw):
        return cls.view(db, **kw)

将原始函数附加到People.by_name.map_fun

I think what you need is:

@classmethod
def all(cls):
    result = cls.view(db, "support/all", include_docs=True)
    return result.rows

Document class has a classmethod view which wraps the rows by class on which it is called. So the following returns you a ViewResult with rows of type SupportCase and taking .rows of that gives a list of support cases.

SupportCase.view(db, viewname, include_docs=True)

And I don't think you need to get into the ViewField magic. But let me explain how it works. Consider the following example from the CouchDB-python documentation.

class Person(Document):
     @ViewField.define('people')
     def by_name(doc):
         yield doc['name'], doc

I think this is equivalent to:

class Person(Document):
    @classmethod
    def by_name(cls, db, **kw):
        return cls.view(db, **kw)

With the original function attached to People.by_name.map_fun.

稳稳的幸福 2024-11-12 04:31:48

映射函数在某些方面类似于关系数据库中的索引。并不是每次都重做,并且当添加新文档时更新的方式不需要重做所有内容(这是一种树结构)。

这有一个非常好的摘要

The map function is in some ways analogous to an index in a relational database. It is not done again every time, and when new documents are added the way it is updated does not require everything to be redone (it's a kind of tree structure).

This has a pretty good summary

似狗非友 2024-11-12 04:31:48

ViewField 使用预定义的视图,因此一旦构建,速度就会很快。它绝对不使用临时视图。

ViewField uses a pre-defined view so, once built, will be fast. It definitely doesn't use a temporary view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文