// 构建数据流对象,对数据进行put DataOutput output = new DataOutputStream(stream);
如果是本地索引,则在rowkey前加入startrowkey索引
1 2 3 4 5 6 7 8 9
// For local indexes, we must prepend the row key with the start region key if (prependRegionStartKey) { if (regionStartKey.length == 0) { // 如果startRK为null,则其实使用的endRK output.write(new byte[prefixKeyLength]); } else { output.write(regionStartKey); } }
判断是否有加盐,如果有,则增加一个标志位,后面再更改这个标志位
1 2 3
if (isIndexSalted) { output.write(0); // will be set at end to index salt byte }
如果在索引视图id不为null,会在索引rowkey中加入视图id
1 2 3
if (viewIndexId != null) { output.write(viewIndexId); }
for (int i = dataPosOffset; i < dataRowKeySchema.getFieldCount(); i++) { Boolean hasValue=dataRowKeySchema.next(ptr, i, maxRowKeyOffset); // Ignore view constants from the data table, as these // don't need to appear in the index (as they're the // same for all rows in this index) if (!viewConstantColumnBitSet.get(i)) { int pos = rowKeyMetaData.getIndexPkPosition(i-dataPosOffset); if (Boolean.TRUE.equals(hasValue)) { dataRowKeyLocator[0][pos] = ptr.getOffset(); dataRowKeyLocator[1][pos] = ptr.getLength(); } else { dataRowKeyLocator[0][pos] = 0; dataRowKeyLocator[1][pos] = 0; } } }
// Since we cannot have nullable fixed length in a row key // we need to translate to variable length. The verification that we have a valid index // row key was already done, so here we just need to convert from one built-in type to // another. public static PDataType getIndexColumnDataType(boolean isNullable, PDataType dataType) { if (dataType == null || !isNullable || !dataType.isFixedWidth()) { return dataType; } // for fixed length numeric types and boolean if (dataType.isCastableTo(PDecimal.INSTANCE)) { return PDecimal.INSTANCE; } // for CHAR if (dataType.isCoercibleTo(PVarchar.INSTANCE)) { return PVarchar.INSTANCE; }
if (PBinary.INSTANCE.equals(dataType)) { return PVarbinary.INSTANCE; } throw new IllegalArgumentException("Unsupported non nullable type " + dataType); }
让数据有可比性
1 2 3 4 5 6 7 8
protected static int toBytes(BigDecimal v, byte[] result, final int offset, int length) { // From scale to exponent byte (if BigDecimal is positive): (-(scale+(scale % 2 == 0 : 0 : 1)) / 2 + 65) | 0x80 // If scale % 2 is 1 (i.e. it's odd), then multiple last base-100 digit by 10 // For example: new BigDecimal(BigInteger.valueOf(1), -4); // (byte)((-(-4+0) / 2 + 65) | 0x80) = -61 // From scale to exponent byte (if BigDecimal is negative): ~(-(scale+1)/2 + 65 + 128) & 0x7F // For example: new BigDecimal(BigInteger.valueOf(1), 2); // ~(-2/2 + 65 + 128) & 0x7F = 63