duoshuo / php-cassandra

php Cassandra driver which support Protocol v3 (Cassandra 2.1) and asynchronous requests
MIT License
153 stars 53 forks source link

Accessing page_state in result metadata for token based paging #85

Open NathanBaulch opened 5 years ago

NathanBaulch commented 5 years ago

I'm trying to implement token based paging in my project's API but it doesn't look like the page_state result metadata is exposed by Cassandra\Response\Result. Is there a recommended way to capture this value so my clients can request the next page at a later time?

For now I'm using the following hack to work around this:

$pageState = null;
while (true) {
    /** @var Result $res */
    $res = $cass->querySync('SELECT "key" FROM message', [], null, ['page_size' => 10, 'paging_state' => $pageState]);
    yield from $res->fetchCol();
    $pageState = CassandraResultMetadataAccessor::getPageState($res);
    if (!$pageState) break;
    echo 'DEBUG: paging token for client: ' . base64_encode($pageState) . "\n";
}

class CassandraResultMetadataAccessor extends Result
{
    public static function getPageState(Result $res) {
        return $res->_metadata['page_state'] ?? null;
    }
}
NathanBaulch commented 5 years ago

OK, it looks like this has already been fixed in master, it's just that the most recent release v0.5.2 is 84 commits behind despite this project having no activity in the last 3.5 years. So I guess I'll try my chances with the latest unreleased code (I also need the unreleased Tuple support). My workaround above still applies if you're using the official release.