classes/binarySearch/BinarySearch.php
public function search($key)
    {
        // split the range
        $splitOffset = $this->getSplitOffset();
        
        // search right side
        $keys = $this->index->getKeyReader()->readKeys($splitOffset, self::DIRECTION_FORWARD);
        $foundKey = $this->findKey($key, $keys);
        // found
        if (! is_null($foundKey)) {
            return $foundKey;
            
        }
        // check if search should terminate
        if ($this->isKeyRange($key, $keys)) {
            return \reset($keys);
            
        }
        
        // If found keys are smaller continue in the right side
        if (! empty($keys) && \end($keys)->getKey() < $key) {
            $newOffset = $splitOffset + $this->index->getKeyReader()->getReadLength();
            // Stop if beyond index
            if ($newOffset >= $this->index->getFile()->getFileSize()) {
                return \end($keys);
                
            }
            $newLength =
                $this->range->getLength() - ($newOffset - $this->range->getOffset());
            $this->range->setOffset($newOffset);
            $this->range->setLength($newLength);
            return $this->search($key);
            
        }
        
        // Look at the key, which lies in both sides
        $centerKeyOffset = empty($keys)
            ? $this->range->getNextByteOffset()
            : \reset($keys)->getOffset();
        $keys = $this->index->getKeyReader()->readKeys($centerKeyOffset, self::DIRECTION_BACKWARD);
        $foundKey = $this->findKey($key, $keys);
        // found
        if (! is_null($foundKey)) {
            return $foundKey;
            
        }
        // terminate if no more keys in the index
        if (empty($keys)) {
            return null;
            
        }
        // check if search should terminate
        if ($this->isKeyRange($key, $keys)) {
            return \reset($keys);
            
        }
        
        // Finally continue searching in the left side
        $newLength = \reset($keys)->getOffset() - $this->range->getOffset() - 1;
        if ($newLength >= $this->range->getLength()) {
            return \reset($keys);
            
        }
        $this->range->setLength($newLength);
        return $this->search($key);
    }
public function search($key)
    {
        // split the range
        $splitOffset = $this->getSplitOffset();
        
        // search right side
        $keys = $this->index->getKeyReader()->readKeys($splitOffset, self::DIRECTION_FORWARD);
        $foundKey = $this->findKey($key, $keys);
        // found
        if (! is_null($foundKey)) {
            return $foundKey;
            
        }
        // check if search should terminate
        if ($this->isKeyRange($key, $keys)) {
            return \reset($keys);
            
        }
        
        // If found keys are smaller continue in the right side
        if (! empty($keys) && \end($keys)->getKey() < $key) {
            $newOffset = $splitOffset + $this->index->getKeyReader()->getReadLength();
            // Stop if beyond index
            if ($newOffset >= $this->index->getFile()->getFileSize()) {
                return \end($keys);
                
            }
            $newLength =
                $this->range->getLength() - ($newOffset - $this->range->getOffset());
            $this->range->setOffset($newOffset);
            $this->range->setLength($newLength);
            return $this->search($key);
            
        }
        
        // Look at the key, which lies in both sides
        $centerKeyOffset = empty($keys)
            ? $this->range->getNextByteOffset()
            : \reset($keys)->getOffset();
        $keys = $this->index->getKeyReader()->readKeys($centerKeyOffset, self::DIRECTION_BACKWARD);
        $foundKey = $this->findKey($key, $keys);
        // found
        if (! is_null($foundKey)) {
            return $foundKey;
            
        }
        // terminate if no more keys in the index
        if (empty($keys)) {
            return null;
            
        }
        // check if search should terminate
        if ($this->isKeyRange($key, $keys)) {
            return \reset($keys);
            
        }
        
        // Finally continue searching in the left side
        $newLength = \reset($keys)->getOffset() - $this->range->getOffset() - 1;
        if ($newLength >= $this->range->getLength()) {
            return \reset($keys);
            
        }
        $this->range->setLength($newLength);
        return $this->search($key);
    }
public function search ( $ key ) { // split the range $ splitOffset = $ this -> getSplitOffset ( ) ; // search right side $ keys = $ this -> index -> getKeyReader ( ) -> readKeys ( $ splitOffset , self :: DIRECTION_FORWARD ) ; $ foundKey = $ this -> findKey ( $ key , $ keys ) ; // found if ( ! is_null ( $ foundKey ) ) { return $ foundKey ; } // check if search should terminate if ( $ this -> isKeyRange ( $ key , $ keys ) ) { return \ reset ( $ keys ) ; } // If found keys are smaller continue in the right side if ( ! empty ( $ keys ) && \ end ( $ keys ) -> getKey ( ) < $ key ) { $ newOffset = $ splitOffset + $ this -> index -> getKeyReader ( ) -> getReadLength ( ) ; // Stop if beyond index if ( $ newOffset >= $ this -> index -> getFile ( ) -> getFileSize ( ) ) { return \ end ( $ keys ) ; } $ newLength = $ this -> range -> getLength ( ) - ( $ newOffset - $ this -> range -> getOffset ( ) ) ; $ this -> range -> setOffset ( $ newOffset ) ; $ this -> range -> setLength ( $ newLength ) ; return $ this -> search ( $ key ) ; } // Look at the key, which lies in both sides $ centerKeyOffset = empty ( $ keys ) ? $ this -> range -> getNextByteOffset ( ) : \ reset ( $ keys ) -> getOffset ( ) ; $ keys = $ this -> index -> getKeyReader ( ) -> readKeys ( $ centerKeyOffset , self :: DIRECTION_BACKWARD ) ; $ foundKey = $ this -> findKey ( $ key , $ keys ) ; // found if ( ! is_null ( $ foundKey ) ) { return $ foundKey ; } // terminate if no more keys in the index if ( empty ( $ keys ) ) { return null ; } // check if search should terminate if ( $ this -> isKeyRange ( $ key , $ keys ) ) { return \ reset ( $ keys ) ; } // Finally continue searching in the left side $ newLength = \ reset ( $ keys ) -> getOffset ( ) - $ this -> range -> getOffset ( ) - 1 ; if ( $ newLength >= $ this -> range -> getLength ( ) ) { return \ reset ( $ keys ) ; } $ this -> range -> setLength ( $ newLength ) ; return $ this -> search ( $ key ) ; }
Searches for a key or some neighbour

If it doesn't find the key. A neighbour will be returned. The
neighbour mustn't be the closest neighbour. It's just a good hint
where the key should be expected.

Returns null if no key could be found at all.

@param string $key Key

@return Result
@throws IOIndexException
Searches for a key or some neighbour