|
| 1 | +package io.github.dunwu.javadb.hbase.annotation; |
| 2 | + |
| 3 | +import cn.hutool.core.util.HashUtil; |
| 4 | +import cn.hutool.core.util.IdUtil; |
| 5 | +import cn.hutool.core.util.ReflectUtil; |
| 6 | +import cn.hutool.core.util.StrUtil; |
| 7 | +import io.github.dunwu.javadb.hbase.constant.RowType; |
| 8 | +import io.github.dunwu.javadb.hbase.entity.BaseHbaseEntity; |
| 9 | + |
| 10 | +import java.lang.reflect.Method; |
| 11 | + |
| 12 | +/** |
| 13 | + * {@link RowKeyRule} 解析器 |
| 14 | + * |
| 15 | + * @author <a href="mailto:forbreak@163.com">Zhang Peng</a> |
| 16 | + * @date 2023-11-20 |
| 17 | + */ |
| 18 | +public class RowKeyUtil { |
| 19 | + |
| 20 | + /** |
| 21 | + * 获取主键 |
| 22 | + */ |
| 23 | + public static <T extends BaseHbaseEntity> String getRowKey(T entity) throws IllegalArgumentException { |
| 24 | + |
| 25 | + String row = null; |
| 26 | + Class<? extends BaseHbaseEntity> clazz = entity.getClass(); |
| 27 | + RowKeyRule rule = getRowKeyRule(entity.getClass()); |
| 28 | + Method method = ReflectUtil.getMethodByName(clazz, rule.uk()); |
| 29 | + if (method == null) { |
| 30 | + String msg = StrUtil.format("{} 实体类定义错误!@RowKeyRule 指定的 uk:{} 方法未找到!", |
| 31 | + clazz.getCanonicalName(), rule.uk()); |
| 32 | + throw new IllegalArgumentException(msg); |
| 33 | + } |
| 34 | + switch (rule.type()) { |
| 35 | + case ORIGIN_ID: |
| 36 | + row = getRowKeyForOriginId(entity, method, rule.length()); |
| 37 | + break; |
| 38 | + case TIMESTAMP: |
| 39 | + row = getRowKeyForTimestamp(); |
| 40 | + break; |
| 41 | + case UUID: |
| 42 | + row = IdUtil.fastSimpleUUID(); |
| 43 | + break; |
| 44 | + case BUCKET: |
| 45 | + row = getRowKeyForBucket(entity, method, rule.length(), rule.bucket()); |
| 46 | + default: |
| 47 | + break; |
| 48 | + } |
| 49 | + |
| 50 | + if (StrUtil.isBlank(row)) { |
| 51 | + throw new IllegalArgumentException(StrUtil.format("实体定义错误!未定义 @RowKeyRule", entity.getClass(), |
| 52 | + BaseHbaseEntity.class.getCanonicalName())); |
| 53 | + } |
| 54 | + return row; |
| 55 | + } |
| 56 | + |
| 57 | + public static RowKeyRule getRowKeyRule(Class<? extends BaseHbaseEntity> clazz) { |
| 58 | + |
| 59 | + RowKeyRule rule = clazz.getAnnotation(RowKeyRule.class); |
| 60 | + |
| 61 | + if (rule == null) { |
| 62 | + String msg = StrUtil.format("{} 实体类定义错误!未定义 @RowKeyRule", clazz.getCanonicalName()); |
| 63 | + throw new IllegalArgumentException(msg); |
| 64 | + } |
| 65 | + |
| 66 | + if (rule.type() == RowType.ORIGIN_ID && rule.length() <= 0) { |
| 67 | + String msg = StrUtil.format("{} 实体类定义错误!@RowKeyRule type 为 ORIGIN_ID 时,length 必须大于 0!", |
| 68 | + clazz.getCanonicalName()); |
| 69 | + throw new IllegalArgumentException(msg); |
| 70 | + } |
| 71 | + |
| 72 | + if (rule.type() == RowType.BUCKET && (rule.length() <= 0 || rule.bucket() <= 0)) { |
| 73 | + String msg = StrUtil.format("{} 实体类定义错误!@RowKeyRule type 为 BUCKET 时,length 和 bucket 必须大于 0!", |
| 74 | + clazz.getCanonicalName()); |
| 75 | + throw new IllegalArgumentException(msg); |
| 76 | + } |
| 77 | + return rule; |
| 78 | + } |
| 79 | + |
| 80 | + public static <T extends BaseHbaseEntity> String getRowKeyForOriginId(T entity, Method method, int length) |
| 81 | + throws IllegalArgumentException { |
| 82 | + String originId; |
| 83 | + Object value = ReflectUtil.invoke(entity, method); |
| 84 | + if (value instanceof String) { |
| 85 | + originId = (String) value; |
| 86 | + } else { |
| 87 | + originId = String.valueOf(value); |
| 88 | + } |
| 89 | + if (length == 0) { |
| 90 | + throw new IllegalArgumentException("length 不能为 0"); |
| 91 | + } |
| 92 | + return getRowKeyForOriginId(originId, length); |
| 93 | + } |
| 94 | + |
| 95 | + public static String getRowKeyForOriginId(String bizId, int length) { |
| 96 | + return StrUtil.padPre(bizId, length, "0"); |
| 97 | + } |
| 98 | + |
| 99 | + public static String getRowKeyForTimestamp() { |
| 100 | + String timestamp = String.valueOf(System.currentTimeMillis() / 1000); |
| 101 | + return StrUtil.padPre(timestamp, 10, "0"); |
| 102 | + } |
| 103 | + |
| 104 | + public static <T extends BaseHbaseEntity> String getRowKeyForBucket(T entity, Method method, int length, int bucket) |
| 105 | + throws IllegalArgumentException { |
| 106 | + if (bucket == 0) { |
| 107 | + throw new IllegalArgumentException("bucket 不能为 0"); |
| 108 | + } |
| 109 | + |
| 110 | + String originId = getRowKeyForOriginId(entity, method, length); |
| 111 | + int bucketLength = getBucketIdLength(bucket); |
| 112 | + String bucketId = String.valueOf(HashUtil.fnvHash(originId) % bucket); |
| 113 | + return StrUtil.padPre(bucketId, bucketLength, "0") + originId; |
| 114 | + } |
| 115 | + |
| 116 | + public static <T extends BaseHbaseEntity> String getRowKeyForBucket(String contentId, Class<T> clazz) { |
| 117 | + RowKeyRule rule = RowKeyUtil.getRowKeyRule(clazz); |
| 118 | + return RowKeyUtil.getRowKeyForBucket(contentId, rule.length(), rule.bucket()); |
| 119 | + } |
| 120 | + |
| 121 | + public static String getRowKeyForBucket(String bizId, int length, int bucket) throws IllegalArgumentException { |
| 122 | + String originId = getRowKeyForOriginId(bizId, length); |
| 123 | + int bucketLength = getBucketIdLength(bucket); |
| 124 | + String bucketId = String.valueOf(HashUtil.fnvHash(originId) % bucket); |
| 125 | + return StrUtil.padPre(bucketId, bucketLength, "0") + originId; |
| 126 | + } |
| 127 | + |
| 128 | + private static int getBucketIdLength(int bucket) { |
| 129 | + bucket = bucket - 1; |
| 130 | + if (bucket <= 0) { |
| 131 | + return 1; |
| 132 | + } |
| 133 | + |
| 134 | + int length = 0; |
| 135 | + while (bucket > 0) { |
| 136 | + length++; |
| 137 | + bucket = bucket / 10; |
| 138 | + } |
| 139 | + return length; |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments