Phoenix创建索引源码过程
org.apache.phoenix.index.IndexMaintainer
1 | public byte[] buildRowKey(ValueGetter valueGetter, ImmutableBytesWritable rowKeyPtr, byte[] regionStartKey, byte[] regionEndKey, long ts) { |
如果是本地索引,则在rowkey前加入startrowkey索引
1 | // For local indexes, we must prepend the row key with the start region key |
判断是否有加盐,如果有,则增加一个标志位,后面再更改这个标志位
1 | if (isIndexSalted) { |
如果在索引视图id不为null,会在索引rowkey中加入视图id
1 | if (viewIndexId != null) { |
dataRowKeySchema是数据表的信息,忽略在视图变量的中常量值,并标记出原表pk的rowkey的offset 和 length,方便后面定位数据表rowkey插入。
1 | for (int i = dataPosOffset; i < dataRowKeySchema.getFieldCount(); i++) { |
考虑索引的数据的顺序
1 | // 获取表达式索引,表达式索引默认值都为1,未开启的时候isNullAble为true |
填充开始的加盐部分的字节位,规则是根据数据做hash,然后再对nIndexSaltBuckets取余
1 | if (isIndexSalted) { |
返回所有的生成的rowkey
1 | return indexRowKey.length == length ? indexRowKey : Arrays.copyOf(indexRowKey, length); |
根据数据列返回不同的datatype,判断该列是否可比较。不可比较的列有decimal,varchar,boolean,Binary等
1 | // Since we cannot have nullable fixed length in a row key |
让数据有可比性
1 | protected static int toBytes(BigDecimal v, byte[] result, final int offset, int length) { |