inline int indri::file::BulkBlock::_find( const char* key, int keyLength, bool& exact ) { int left = 0; int right = count() - 1; while( right - left > 1 ) { int middle = left + (right - left) / 2; int middleKeyStart = _keyStart( middle ); int middleKeyEnd = _keyEnd( middle ); const char* middleKey = _buffer + middleKeyStart; int result = _compare( key, keyLength, middleKey, middleKeyEnd - middleKeyStart ); if( result < 0 ) { right = middle; } else if( result > 0 ) { left = middle; } else { exact = true; return middle; } } const char* leftKey = _keyStart( left ) + _buffer; int leftLength = _keyEnd( left ) - _keyStart( left ); int leftResult = _compare( key, keyLength, leftKey, leftLength ); const char* rightKey = _keyStart( right ) + _buffer; int rightLength = _keyEnd( right ) - _keyStart( right ); int rightResult = _compare( key, keyLength, rightKey, rightLength ); // matches the left key if( leftResult == 0 ) { exact = true; return left; } // matches the right key if( rightResult == 0 ) { exact = true; return right; } // bigger than the right key; choose right if( rightResult > 0 ) { exact = false; return right; } // smaller than the left key; invalid! if( leftResult < 0 ) { exact = false; return -1; } exact = false; return left; }
bool indri::file::BulkBlock::getIndex( int index, char* key, int& keyActual, int keyLength, char* value, int& valueActual, int valueLength ) { int count = *(UINT16*) _buffer; keyActual = 0; valueActual = 0; if( index < 0 || index >= count ) return false; if( key ) { int keyStart = _keyStart( index ); int keyEnd = _keyEnd( index ); keyActual = lemur_compat::min( keyEnd - keyStart, keyLength ); memcpy( key, _buffer + keyStart, keyActual ); } if( value ) { int valueStart = _valueStart( index ); int valueEnd = _valueEnd( index ); valueActual = lemur_compat::min( valueEnd - valueStart, valueLength ); memcpy( value, _buffer + valueStart, valueActual ); } return true; }
inline int indri::file::BulkBlock::_valueStart( int index ) { return _keyEnd( index ); }
inline int indri::file::BulkBlock::_valueStart(int index) { return _keyEnd(index); // value start of index equals with key end of index }