private int getTerminatorCount(RowKeySchema schema) { int nTerminators = 0; for (int i = 0; i < schema.getFieldCount(); i++) { Field field = schema.getField(i); // We won't have a terminator on the last PK column // unless it is variable length and exclusive, but // having the extra byte irregardless won't hurt anything if (!field.getType().isFixedWidth()) { nTerminators++; } } return nTerminators; }
private int setStartKey(ImmutableBytesWritable ptr, int offset, int i) { int length = ptr.getOffset() - offset; startKey = copyKey(startKey, length + this.maxKeyLength, ptr.get(), offset, length); startKeyLength = length; // Add separator byte if we're at the end of the buffer, since trailing separator bytes are // stripped if (ptr.getOffset() + ptr.getLength() == offset + length && i - 1 > 0 && !schema.getField(i - 1).getType().isFixedWidth()) { startKey[startKeyLength++] = QueryConstants.SEPARATOR_BYTE; } startKeyLength += setKey(Bound.LOWER, startKey, startKeyLength, i); return length; }
private void assertIteration(String dataColumns, String pk, Object[] values, String dataProps) throws Exception { String schemaName = ""; String tableName = "T"; Connection conn = DriverManager.getConnection(getUrl()); String fullTableName = SchemaUtil.getTableName( SchemaUtil.normalizeIdentifier(schemaName), SchemaUtil.normalizeIdentifier(tableName)); conn.createStatement() .execute( "CREATE TABLE " + fullTableName + "(" + dataColumns + " CONSTRAINT pk PRIMARY KEY (" + pk + ")) " + (dataProps.isEmpty() ? "" : dataProps)); PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class); PTable table = pconn.getTable(new PTableKey(pconn.getTenantId(), fullTableName)); conn.close(); StringBuilder buf = new StringBuilder("UPSERT INTO " + fullTableName + " VALUES("); for (int i = 0; i < values.length; i++) { buf.append("?,"); } buf.setCharAt(buf.length() - 1, ')'); PreparedStatement stmt = conn.prepareStatement(buf.toString()); for (int i = 0; i < values.length; i++) { stmt.setObject(i + 1, values[i]); } stmt.execute(); Iterator<Pair<byte[], List<KeyValue>>> iterator = PhoenixRuntime.getUncommittedDataIterator(conn); List<KeyValue> dataKeyValues = iterator.next().getSecond(); KeyValue keyValue = dataKeyValues.get(0); List<SortOrder> sortOrders = Lists.newArrayListWithExpectedSize(table.getPKColumns().size()); for (PColumn col : table.getPKColumns()) { sortOrders.add(col.getSortOrder()); } RowKeySchema schema = table.getRowKeySchema(); int minOffset = keyValue.getRowOffset(); ImmutableBytesWritable ptr = new ImmutableBytesWritable(); int nExpectedValues = values.length; for (int i = values.length - 1; i >= 0; i--) { if (values[i] == null) { nExpectedValues--; } else { break; } } int i = 0; int maxOffset = schema.iterator(keyValue.getRowArray(), minOffset, keyValue.getRowLength(), ptr); for (i = 0; i < schema.getFieldCount(); i++) { Boolean hasValue = schema.next(ptr, i, maxOffset); if (hasValue == null) { break; } assertTrue(hasValue); PDataType type = PDataType.fromLiteral(values[i]); SortOrder sortOrder = sortOrders.get(i); Object value = type.toObject(ptr, schema.getField(i).getDataType(), sortOrder); assertEquals(values[i], value); } assertEquals(nExpectedValues, i); assertNull(schema.next(ptr, i, maxOffset)); for (i--; i >= 0; i--) { Boolean hasValue = schema.previous(ptr, i, minOffset); if (hasValue == null) { break; } assertTrue(hasValue); PDataType type = PDataType.fromLiteral(values[i]); SortOrder sortOrder = sortOrders.get(i); Object value = type.toObject(ptr, schema.getField(i).getDataType(), sortOrder); assertEquals(values[i], value); } assertEquals(-1, i); assertNull(schema.previous(ptr, i, minOffset)); }