h2/src/main/org/h2/mvstore/Page.java
/**
     * Search the key in this page using a binary search. Instead of always
     * starting the search in the middle, the last found index is cached.
     * <p>
     * If the key was found, the returned value is the index in the key array.
     * If not found, the returned value is negative, where -1 means the provided
     * key is smaller than any keys in this page. See also Arrays.binarySearch.
     *
     * @param key the key
     * @return the value or null
     */
Search the key in this page using a binary search. Instead of always
starting the search in the middle, the last found index is cached.

If the key was found, the returned value is the index in the key array.
If not found, the returned value is negative, where -1 means the provided
key is smaller than any keys in this page.

@param key the key
@return the value or null
Search the key in this page using a binary search . Instead of always starting the search in the middle the last found index is cached . If the key was found the returned value is the index in the key array . If not found the returned value is negative where - 1 means the provided key is smaller than any keys in this page . @param key the key @return the value or null
public int binarySearch(Object key) {
        int low = 0, high = keyCount - 1;
        int x = cachedCompare - 1;
        if (x < 0 || x > high) {
            x = (low + high) >>> 1;
        }
        Object[] k = keys;
        while (low <= high) {
            int compare = map.compare(key, k[x]);
            if (compare > 0) {
                low = x + 1;
            } else if (compare < 0) {
                high = x - 1;
            } else {
                cachedCompare = x + 1;
                return x;
            }
            x = (low + high) >>> 1;
        }
        cachedCompare = low;
        return -(low + 1);
    }
public int binarySearch ( Object key ) { int low = 0 , high = keyCount - 1 ; int x = cachedCompare - 1 ; if ( x < 0 || x > high ) { x = ( low + high ) >>> 1 ; } Object [ ] k = keys ; while ( low <= high ) { int compare = map . compare ( key , k [ x ] ) ; if ( compare > 0 ) { low = x + 1 ; } else if ( compare < 0 ) { high = x - 1 ; } else { cachedCompare = x + 1 ; return x ; } x = ( low + high ) >>> 1 ; } cachedCompare = low ; return - ( low + 1 ) ; }
Search the key in this page using a binary search.
Search the key in this page using a binary search .
// the cached index minus one, so that // for the first time (when cachedCompare is 0), // the default value is used // regular binary search (without caching) // int low = 0, high = keyCount - 1; // while (low <= high) { //     int x = (low + high) >>> 1; //     int compare = map.compare(key, keys[x]); //     if (compare > 0) { //         low = x + 1; //     } else if (compare < 0) { //         high = x - 1; //     } else { //         return x; //     } // } // return -(low + 1);