分表路由实现伪代码

来源:互联网 发布:java web开发环境搭建 编辑:程序博客网 时间:2024/05/29 16:09

根据Id分段实现分表路由的核心伪代码实现

这里写图片描述

核心伪代码如下

1、二分查找算法:

    public static int binarySearch(byte[] a, byte key) {        return binarySearch0(a, 0, a.length, key);    }    // Like public version, but without range checks.    private static int binarySearch0(byte[] a, int fromIndex, int toIndex,                                     byte key) {        int low = fromIndex;        int high = toIndex - 1;        while (low <= high) {            int mid = (low + high) >>> 1;            byte midVal = a[mid];            if (midVal < key)                low = mid + 1;            else if (midVal > key)                high = mid - 1;            else                return mid; // key found        }        return -(low + 1);  // key not found.    }

2、检索路由key

    //根据期Id 查找路由区间 拼接成字符串返回    private String searchIssueIdLoction(long[] arr, long issueId) {        // 获取期Id索引位置        int index = getIndex(arr, issueId);        // 小于0的情况出现频次高        if (index < 0) {            return arr[Math.abs(index) - 2] + SEPARATOR + arr[Math.abs(index) - 1];        }        if (isEven(index)) {            return arr[Math.abs(index)] + SEPARATOR + arr[Math.abs(index + 1)];        }        return arr[Math.abs(index) - 1] + SEPARATOR + arr[Math.abs(index)];    }    //是偶数    private boolean isEven(int index) {        return index % 2 == 0;    }
原创粉丝点击