分表路由实现伪代码

根据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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值