@edu.umd.cs.findbugs.annotations.SuppressWarnings( value = "QBA_QUESTIONABLE_BOOLEAN_ASSIGNMENT", justification = "Assignment designed to work this way.") private ReturnCode navigate( final byte[] currentKey, int offset, int length, Terminate terminate) { int nSlots = slots.size(); // First check to see if we're in-range until we reach our end key if (endKeyLength > 0) { if (Bytes.compareTo(currentKey, offset, length, endKey, 0, endKeyLength) < 0) { return ReturnCode.INCLUDE; } // If key range of last slot is a single key, we can increment our position // since we know we'll be past the current row after including it. if (slots.get(nSlots - 1).get(position[nSlots - 1]).isSingleKey()) { if (nextPosition(nSlots - 1) < 0) { // Current row will be included, but we have no more isDone = true; return ReturnCode.NEXT_ROW; } } else { // Reset the positions to zero from the next slot after the earliest ranged slot, since the // next key could be bigger at this ranged slot, and smaller than the current position of // less significant slots. int earliestRangeIndex = nSlots - 1; for (int i = 0; i < nSlots; i++) { if (!slots.get(i).get(position[i]).isSingleKey()) { earliestRangeIndex = i; break; } } Arrays.fill(position, earliestRangeIndex + 1, position.length, 0); } } endKeyLength = 0; // We could have included the previous if (isDone) { return ReturnCode.NEXT_ROW; } int i = 0; boolean seek = false; int earliestRangeIndex = nSlots - 1; ptr.set(currentKey, offset, length); schema.first(ptr, i, ValueBitSet.EMPTY_VALUE_BITSET); while (true) { // Increment to the next range while the upper bound of our current slot is less than our // current key while (position[i] < slots.get(i).size() && slots.get(i).get(position[i]).compareUpperToLowerBound(ptr) < 0) { position[i]++; } Arrays.fill(position, i + 1, position.length, 0); if (position[i] >= slots.get(i).size()) { // Our current key is bigger than the last range of the current slot. // If navigating after current key, backtrack and increment the key of the previous slot // values. // If navigating to current key, just return if (terminate == Terminate.AT) { return ReturnCode.SEEK_NEXT_USING_HINT; } if (i == 0) { isDone = true; return ReturnCode.NEXT_ROW; } // Increment key and backtrack until in range. We know at this point that we'll be // issuing a seek next hint. seek = true; Arrays.fill(position, i, position.length, 0); i--; // If we're positioned at a single key, no need to copy the current key and get the next key // . // Instead, just increment to the next key and continue. boolean incremented = false; while (i >= 0 && slots.get(i).get(position[i]).isSingleKey() && (incremented = true) && (position[i] = (position[i] + 1) % slots.get(i).size()) == 0) { i--; incremented = false; } if (i < 0) { isDone = true; return ReturnCode.NEXT_ROW; } if (incremented) { // Continue the loop after setting the start key, because our start key maybe smaller than // the current key, so we'll end up incrementing the start key until it's bigger than the // current key. setStartKey(); ptr.set(ptr.get(), offset, length); // Reinitialize iterator to be positioned at previous slot position schema.setAccessor(ptr, i, ValueBitSet.EMPTY_VALUE_BITSET); } else { int currentLength = setStartKey(ptr, offset, i + 1); // From here on, we use startKey as our buffer with offset reset to 0 // We've copied the part of the current key above that we need into startKey ptr.set(startKey, offset = 0, length = startKeyLength); // Reinitialize iterator to be positioned at previous slot position // TODO: a schema.previous would potentially be more efficient schema.setAccessor(ptr, i, ValueBitSet.EMPTY_VALUE_BITSET); // Do nextKey after setting the accessor b/c otherwise the null byte may have // been incremented causing us not to find it ByteUtil.nextKey(startKey, currentLength); } } else if (slots.get(i).get(position[i]).compareLowerToUpperBound(ptr) > 0) { // Our current key is less than the lower range of the current position in the current slot. // Seek to the lower range, since it's bigger than the current key setStartKey(ptr, offset, i); return ReturnCode.SEEK_NEXT_USING_HINT; } else { // We're in range, check the next slot if (!slots.get(i).get(position[i]).isSingleKey() && i < earliestRangeIndex) { earliestRangeIndex = i; } i++; // If we're past the last slot or we know we're seeking to the next (in // which case the previously updated slot was verified to be within the // range, so we don't need to check the rest of the slots. If we were // to check the rest of the slots, we'd get into trouble because we may // have a null byte that was incremented which screws up our schema.next call) if (i >= nSlots || seek) { break; } // If we run out of slots in our key, it means we have a partial key. In this // case, we seek to the next full key after this one. // TODO: test for this if (schema.next(ptr, i, offset + length, ValueBitSet.EMPTY_VALUE_BITSET) == null) { setStartKey(ptr, offset, i); return ReturnCode.SEEK_NEXT_USING_HINT; } } } if (seek) { return ReturnCode.SEEK_NEXT_USING_HINT; } // Else, we're in range for all slots and can include this row plus all rows // up to the upper range of our last slot. We do this for ranges and single keys // since we potentially have multiple key values for the same row key. setEndKey(ptr, offset, slots.size() - 1); return ReturnCode.INCLUDE; }
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)); }