kallaspriit / Cassandra-PHP-Client-Library

Cassandra PHP-based client library for managing and querying your Cassandra cluster
http://cassandra-php-client-library.com
103 stars 25 forks source link

CassandraDataIterator error when lots of empty rows #25

Open LordStephen opened 11 years ago

LordStephen commented 11 years ago

In the function CassandraDataIterator::next(), the next() function is recursivly called until it find a valid value.

Depending on the data this function can pass the maximum nesting level throwing this error:

Fatal error: Maximum function nesting level of '100' reached, aborting!

I coded this fix :

public function next() {
    $value = null;
    $beyondLastRow = false;

    if (!empty($this->buffer)) {
        $this->nextStartKey = key($this->buffer);

        $value = next($this->buffer);

        if (count(current($this->buffer)) == 0) {
            $value = $this->findNextValidValue();
        } 

        $key = key($this->buffer);
        if (isset($key)) {
            $this->rowsSeen++;

            if (
                $this->rowCountLimit !== null
                && $this->rowsSeen > $this->rowCountLimit
            ) {
                $this->isValid = false;

                return null;
            }
        } else {
            $beyondLastRow = true;
        }           
    } else {
        $beyondLastRow = true;
    }

    if ($beyondLastRow) {
        if ($this->currentPageSize < $this->expectedPageSize) {
            $this->isValid = false;
        } else {
            $this->updateBuffer();

            if (count($this->buffer) == 1) {
                $this->isValid = false;
            } else {
                $this->next();
            }
        }
    }

    return $value;
}

private function findNextValidValue(){
    $value = next($this->buffer);
    while (count(current($this->buffer)) == 0) {
        $value = next($this->buffer);
    }
    return $value;
}