【问题标题】:Couchbase view not updating after set from PHP SDK从 PHP SDK 设置后 Couchbase 视图未更新
【发布时间】:2013-11-20 18:15:38
【问题描述】:

我正在开发一个应用程序,我主要使用视图来获取数据。
我尝试做的是存储文档,重定向并检查存储的文档是否可用(这是通过视图完成的,因为我不是在寻找文档键,而是在寻找不同的值)。

我在用什么?

Debian Wheezy 上的 PHP(5.4.21)、Couchbase(2.1.1) 和 Couchbase(1.1) 的 PHP SDK。

那么发生了什么?

我使用 API 函数 set($id, $document) 存储一个文档,然后使用 API 函数 view($designDocument, $viewName, $options) 触发视图更新,其中 $options 至少包含 'stale'=>false,然后重定向到另一个页面,我检查新添加的文档/或只是想显示它。
但是新添加的文档并不总是显示出来,或者并不总是通过我的存在检查。

按照我正在使用的代码进行更详细的说明:

public function store(AbstractDocument $document)
{
    $result = $this->bucket->storeDocument($document->getId(), 
                                           json_encode($document));
    $this->afterSave();

    return $result;
}

public function storeDocument($id, $document)
{
    if (!is_string($document)) {
        $document = json_encode($document);
    }
    $res = $this->couchbase->set($id, $document);

    return $res;
}

public function afterSave()
{
    $this->bucket->triggerViewIndexing(
        $this->getDesignDocumentName(),
        Config::KEY_COUCHBASE_VIEW_USER_USERLIST
    );
}

public function triggerViewIndexing($designDocument, $viewName)
{
    $options = ['limit' => 1, 'stale' => false];
    $res     = $this->couchbase->view($designDocument, $viewName, $options);
}

如代码所示,我将 stale 参数设置为 false,以确保更新索引。
但它似乎不起作用。

在写这个问题之前,我查看了 Couchbase 论坛中的一些帖子、Stackoverflow 上的帖子、Couchbase 的 PHP SDK 文档和 Couchbase 的一般文档。
所以我想我了解stale 应该如何工作以及limitations 似乎是什么。
如果我认为陈旧的假设有效,但仅当文档不再在内存中但正在写入磁盘(或已经写入磁盘)时是错误的,请随时纠正我。

由于我试图做的事情没有奏效,我尝试了不同的方法,在一些解释和文档中也提到了这些方法,以帮助实现我想要的结果。

比如我想如果stale的默认行为是update_after,那么触发两次索引更新,就可以解决问题了。
好吧,它没有。

其他值得注意的事情是

$this->couchbase->keyDurability($id,$res);
$this->couchbase->observe($id, $res);

我在使用set 存储文档后直接使用了它们,分开并出于绝望组合。 也没有用。

我的假设是这里有什么问题,PHP SDK 要么不知何故没有通过stale=false 参数,而keyDurability 没有做它应该做的事情。当我在检查新创建的文档时传递stale=false 时,当然两者(触发器和检查)都适用于同一个存储桶和视图。

或者我在没有注意到的情况下做了一些可怕的错误。

如果有人能指出我正确的方向并希望能解释发生了什么问题,我会很高兴,因为我无法理解正在发生的事情。根据我的理解,一切都应该至少适用于keyDurabilitystale

【问题讨论】:

    标签: php couchbase couchbase-view


    【解决方案1】:

    更新视图的“问题”是,它只使用持久化到磁盘的数据。 您可以使用 keyDurability() 检查是否发生了这种情况,但是您需要刚刚编写的元素的 id 和 cas。

    我会将您的 triggerViewIndexing 函数更改为如下所示。

    /**
     * @param $designDocument
     * @param $viewName
     * @param null $id
     * @param null $cas
     */
    public function triggerViewIndexing($designDocument, $viewName, $id = null, $cas = null)
    {
        if (!empty($id) && !empty($cas)) {
            $details = [
                'persist_to' => 1, //The number of nodes the document should be persisted to
                'timeout'    => 2000, // The max time to wait for durability
                'interval'   => 100 //The interval between checking the state of the document
            ];
            $this->couchbase->keyDurability($id, $cas, $details);
        }
    
        $options = ['limit' => 1, 'stale' => false];
        $res = $this->couchbase->view($designDocument, $viewName, $options);
    }
    

    如果项目被写入磁盘,这将每 100 毫秒检查一次,最长 2000 毫秒。 之后,触发视图会刷新数据。

    【讨论】:

    • 有趣的是,我确实尝试将 id 和 cas 作为参数传递,但我没有传递任何其他参数($details),因为它们都应该具有默认值。根据php sdk文档persist_to默认为1,所以没必要,我想。但似乎这可以解决问题。对我来说,这看起来可能是 php sdk 中的另一个错误(或者如果你愿意,可以称之为 bug),因为我之前尝试使用 id 和 cas 但persist_to 有效。谢谢老哥,帮了大忙!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多