Trying to Determine next available document id failing

I have the following PHP methods:

function get_latest_doc_id()
{
    $myCluster = new CouchbaseCluster('couchbase://localhost');
    $myBucket = $myCluster->openBucket();
    $myBucket->enableN1ql(array('http://1.1.1.1:8093/','http://1.1.1.2:8093/'));
    $query = CouchbaseN1qlQuery::fromString('select meta(`lazycouch`).id from `lazycouch` order by meta(`lazycouch`).id desc limit 1');
    $res = $myBucket->query($query);
    if ( count($res) > 0 ) {
            $id = (int)$res[0]->id;
            return $id + 1;
    
    } else {
            return 1;
    }

}

And

function create_new_audit_record($data)
{
        $next_id = (string)get_latest_doc_id();
echo "creating audit record using id " .$next_id."<BR>";
        $myCluster = new CouchbaseCluster('couchbase://localhost');
        $myBucket = $myCluster->openBucket('lazycouch');
        $res = $myBucket->insert($next_id, $data);
        return $res;
}

Problem:

The logic to increment the document counter in get_latest_doc_id() is not showing the right value after I do an insert. Here’s the code that drives these two functions, as well as the output:

create_new_audit_record(array('1'=>'test'));
$id = get_latest_doc_id();

echo " after insert, this is next id: $id<BR>";

output:

manually setting count
creating audit record using id 1
manually setting count
after insert, this is next id: 1

I was expecting that after the insert, another call to the get_latest_doc_id would return 2. But it’s as though the insert statement is not committed to the database or something like that because when I refresh my web page, the count does increment properly like so:

creating audit record using id 2
after insert, this is next id: 2

It’s just for the first insert that this is failing…

You need to set scan_consistency to REQUEST_PLUS. Please consult the PHP SDK documentation.

can you show me how to use the REQUEST_PLUS in the context of my question? I’ve been searching for examples but haven’t really found anything.

http://docs.couchbase.com/sdk-api/couchbase-php-client-2.1.0/classes/CouchbaseN1qlQuery.html#constant_REQUEST_PLUS

That’s not too useful. I’m continuing to look …

Thanks.

Apologies I posted to the wrong thread here - please disregard. request_plus is supported in the PHP SDK - geralds is correct. we are working on an enhancement in this area. There is a different capability that will be coming up shortly in beta.
-cihan

boy o boy. i wish someone would have said that sooner. i just spent a bunch of time trying to get it working …
any other suggestions then on how i can get my code working without the REQUEST_PLUS setting?

@wooj, REQUEST_PLUS is the correct solution and is already available in Couchbase Server since 4.0. We are looking into the PHP bindings and will update you. Thanks for your patience.

ok. i’ll wait to hear back. thanks

@wooj: apologies for the delay in reply on this-- I’ve asked my colleague @avsej to get you the specifics. It’s after the end-of-the-day in his TZ right now, but I expect he’ll look into it tomorrow morning.

Hi @wooj

Here is your slightly modified example which demonstrates scan consistency behaviour during query:

<?php
function get_latest_doc_id($myBucket)
{
    global $consistency;
    $query = CouchbaseN1qlQuery::fromString('select meta(`default`).id from `default` order by meta(`default`).id desc limit 1');
    $query->consistency($consistency);
    $res = $myBucket->query($query);
    if ( count($res) > 0 ) {
        $id = (int)$res[0]->id;
        return $id + 1;
    } else {
        return 1;
    }
}

function create_new_audit_record($myBucket, $data)
{
    global $current_id;
    $next_id = (string)get_latest_doc_id($myBucket);
    if ($current_id == $next_id) {
        printf("STALE ID: %d\n", $next_id);
    }
    $key = sprintf("%08d", $next_id);
    $res = $myBucket->upsert($key, $data);
    $current_id = $next_id;
    return $res;
}


$myCluster = new CouchbaseCluster('couchbase://localhost');
$myBucket = $myCluster->openBucket('default');

printf("using NOT_BOUNDED consistency\n");
$consistency = CouchbaseN1qlQuery::NOT_BOUNDED;
for ($i = 0; $i < 10; $i++) {
    printf(".");
    create_new_audit_record($myBucket, array('hello' => 'world'));
}

printf("using REQUEST_PLUS consistency\n");
$consistency = CouchbaseN1qlQuery::REQUEST_PLUS;
for ($i = 0; $i < 10; $i++) {
    printf(".");
    create_new_audit_record($myBucket, array('hello' => 'world'));
}

In the first run with unbound consistency, it will complain about stale ID almost for every iteration, like in your example, but in the second case with request_plus everything will be okay.

1 Like