更好的解释couchbase lite map功能规则和常见错误
better explain couchbase lite map function rules and common mistakes
我是 couchbase 的新手,我正在尝试在我的 Android 应用程序之一中实施 couchbase lite。我特别纠结的是文档中所述的视图概念和地图功能规则。
应用程序在数据库中存储各种文档类型的文档。在一个查询中,我需要按文档类型获取整个文档 ("payments")
并根据文档属性的值 (doc["approved"] = true)
因此我会像这样创建一个视图:
com.couchbase.lite.View view = database.getView("payments");
if (view.getMap() == null) {
Mapper map = new Mapper() {
@Override
public void map(Map<String, Object> doc, Emitter emitter) {
if (doc.get("type").equals("payments") && doc.get("approved") == true) {
emitter.emit(doc.get("name"), doc);
}
}
};
view.setMap(map, "1");
}
请注意,doc["approved"] 值可以随时间更新。在文档中关于地图功能的规则之一中说:
It must be a "pure" function: ... That means any time it's called with the
same input, it must produce exactly the same output.
如上所示的地图功能的实现会违反该规则吗?
在文档中它进一步说:
In particular, avoid these common mistakes: ... Don't make any assumptions
about when the map function is called. That's an implementation detail
of the indexer. (For example, it's not called every time a document
changes.).
这是否意味着当其中一个文档的批准状态从 false 更新为 true 时,以下查询不一定包含更新后的文档?如果是这样,我需要做什么才能实现这一目标?我很不确定这到底意味着什么规则?谁能帮我睁开眼睛?
"pure" 的意思是您不能在地图函数中使用外部状态。您的所有决定都必须完全基于传递给它的参数。您的地图功能没有违反这一点。
我认为您的理解中缺少的部分是存储和索引之间的区别。您可以将文档的修订存储到数据库中,对吗?这本身不会导致更新视图的索引。这就是文档中 "not called every time a document changes." 的意思 下一个查询是 运行 时默认会更新索引,所以会输出文档的最新状态。自上次查询 运行 以来,它实际上可能已经更改了很多次。
我是 couchbase 的新手,我正在尝试在我的 Android 应用程序之一中实施 couchbase lite。我特别纠结的是文档中所述的视图概念和地图功能规则。
应用程序在数据库中存储各种文档类型的文档。在一个查询中,我需要按文档类型获取整个文档 ("payments") 并根据文档属性的值 (doc["approved"] = true)
因此我会像这样创建一个视图:
com.couchbase.lite.View view = database.getView("payments");
if (view.getMap() == null) {
Mapper map = new Mapper() {
@Override
public void map(Map<String, Object> doc, Emitter emitter) {
if (doc.get("type").equals("payments") && doc.get("approved") == true) {
emitter.emit(doc.get("name"), doc);
}
}
};
view.setMap(map, "1");
}
请注意,doc["approved"] 值可以随时间更新。在文档中关于地图功能的规则之一中说:
It must be a "pure" function: ... That means any time it's called with the same input, it must produce exactly the same output.
如上所示的地图功能的实现会违反该规则吗? 在文档中它进一步说:
In particular, avoid these common mistakes: ... Don't make any assumptions about when the map function is called. That's an implementation detail of the indexer. (For example, it's not called every time a document changes.).
这是否意味着当其中一个文档的批准状态从 false 更新为 true 时,以下查询不一定包含更新后的文档?如果是这样,我需要做什么才能实现这一目标?我很不确定这到底意味着什么规则?谁能帮我睁开眼睛?
"pure" 的意思是您不能在地图函数中使用外部状态。您的所有决定都必须完全基于传递给它的参数。您的地图功能没有违反这一点。
我认为您的理解中缺少的部分是存储和索引之间的区别。您可以将文档的修订存储到数据库中,对吗?这本身不会导致更新视图的索引。这就是文档中 "not called every time a document changes." 的意思 下一个查询是 运行 时默认会更新索引,所以会输出文档的最新状态。自上次查询 运行 以来,它实际上可能已经更改了很多次。