Java Code Examples for org.apache.phoenix.schema.PTable#getRowTimestampColPos()

The following examples show how to use org.apache.phoenix.schema.PTable#getRowTimestampColPos() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MutationState.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static ImmutableBytesPtr getNewRowKeyWithRowTimestamp(ImmutableBytesPtr ptr, long rowTimestamp, PTable table) {
    RowKeySchema schema = table.getRowKeySchema();
    int rowTimestampColPos = table.getRowTimestampColPos();
    Field rowTimestampField = schema.getField(rowTimestampColPos);
    byte[] rowTimestampBytes = rowTimestampField.getDataType() == PTimestamp.INSTANCE ?
        PTimestamp.INSTANCE.toBytes(new Timestamp(rowTimestamp), rowTimestampField.getSortOrder()) :
        PLong.INSTANCE.toBytes(rowTimestamp, rowTimestampField.getSortOrder());
    int oldOffset = ptr.getOffset();
    int oldLength = ptr.getLength();
    // Move the pointer to the start byte of the row timestamp pk
    schema.position(ptr, 0, rowTimestampColPos);
    byte[] b = ptr.get();
    int newOffset = ptr.getOffset();
    int length = ptr.getLength();
    for (int i = newOffset; i < newOffset + length; i++) {
        // modify the underlying bytes array with the bytes of the row timestamp
        b[i] = rowTimestampBytes[i - newOffset];
    }
    // move the pointer back to where it was before.
    ptr.set(ptr.get(), oldOffset, oldLength);
    return ptr;
}
 
Example 2
Source File: UpsertCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static boolean isRowTimestampSet(int[] pkSlotIndexes, PTable table) {
    checkArgument(table.getRowTimestampColPos() != -1, "Call this method only for tables with row timestamp column");
    int rowTimestampColPKSlot = table.getRowTimestampColPos();
    for (int pkSlot : pkSlotIndexes) {
        if (pkSlot == rowTimestampColPKSlot) {
            return true;
        }
    }
    return false;
}
 
Example 3
Source File: UpsertCompiler.java    From phoenix with Apache License 2.0 4 votes vote down vote up
private static void setValues(byte[][] values, int[] pkSlotIndex, int[] columnIndexes,
        PTable table, MultiRowMutationState mutation,
        PhoenixStatement statement, boolean useServerTimestamp, IndexMaintainer maintainer,
        byte[][] viewConstants, byte[] onDupKeyBytes, int numSplColumns) throws SQLException {
    long columnValueSize = 0;
    Map<PColumn,byte[]> columnValues = Maps.newHashMapWithExpectedSize(columnIndexes.length);
    byte[][] pkValues = new byte[table.getPKColumns().size()][];
    // If the table uses salting, the first byte is the salting byte, set to an empty array
    // here and we will fill in the byte later in PRowImpl.
    if (table.getBucketNum() != null) {
        pkValues[0] = new byte[] {0};
    }
    for(int i = 0; i < numSplColumns; i++) {
        pkValues[i + (table.getBucketNum() != null ? 1 : 0)] = values[i];
    }
    Long rowTimestamp = null; // case when the table doesn't have a row timestamp column
    RowTimestampColInfo rowTsColInfo = new RowTimestampColInfo(useServerTimestamp, rowTimestamp);
    for (int i = 0, j = numSplColumns; j < values.length; j++, i++) {
        byte[] value = values[j];
        PColumn column = table.getColumns().get(columnIndexes[i]);
        if (SchemaUtil.isPKColumn(column)) {
            pkValues[pkSlotIndex[i]] = value;
            if (SchemaUtil.getPKPosition(table, column) == table.getRowTimestampColPos()) {
                if (!useServerTimestamp) {
                    PColumn rowTimestampCol = table.getPKColumns().get(table.getRowTimestampColPos());
                    rowTimestamp = PLong.INSTANCE.getCodec().decodeLong(value, 0, rowTimestampCol.getSortOrder());
                    if (rowTimestamp < 0) {
                        throw new IllegalDataException("Value of a column designated as ROW_TIMESTAMP cannot be less than zero");
                    }
                    rowTsColInfo = new RowTimestampColInfo(useServerTimestamp, rowTimestamp);
                } 
            }
        } else {
            columnValues.put(column, value);
            columnValueSize += (column.getEstimatedSize() + value.length);
        }
    }
    ImmutableBytesPtr ptr = new ImmutableBytesPtr();
    table.newKey(ptr, pkValues);
    if (table.getIndexType() == IndexType.LOCAL && maintainer != null) {
        byte[] rowKey = maintainer.buildDataRowKey(ptr, viewConstants);
        HRegionLocation region =
                statement.getConnection().getQueryServices()
                        .getTableRegionLocation(table.getParentName().getBytes(), rowKey);
        byte[] regionPrefix =
                region.getRegion().getStartKey().length == 0 ? new byte[region
                        .getRegion().getEndKey().length] : region.getRegion()
                        .getStartKey();
        if (regionPrefix.length != 0) {
            ptr.set(ScanRanges.prefixKey(ptr.get(), 0, ptr.getLength(), regionPrefix,
                regionPrefix.length));
        }
    } 
    mutation.put(ptr, new RowMutationState(columnValues, columnValueSize, statement.getConnection().getStatementExecutionCounter(), rowTsColInfo, onDupKeyBytes));
}
 
Example 4
Source File: MutationState.java    From phoenix with Apache License 2.0 4 votes vote down vote up
private void generateMutations(final TableRef tableRef, final long mutationTimestamp, final long serverTimestamp,
        final MultiRowMutationState values, final List<Mutation> mutationList,
        final List<Mutation> mutationsPertainingToIndex) {
    final PTable table = tableRef.getTable();
    boolean tableWithRowTimestampCol = table.getRowTimestampColPos() != -1;
    Iterator<Map.Entry<ImmutableBytesPtr, RowMutationState>> iterator = values.entrySet().iterator();
    long timestampToUse = mutationTimestamp;
    MultiRowMutationState modifiedValues = new MultiRowMutationState(16);
    boolean wildcardIncludesDynamicCols = connection.getQueryServices().getProps().getBoolean(
            WILDCARD_QUERY_DYNAMIC_COLS_ATTRIB, DEFAULT_WILDCARD_QUERY_DYNAMIC_COLS_ATTRIB);
    while (iterator.hasNext()) {
        Map.Entry<ImmutableBytesPtr, RowMutationState> rowEntry = iterator.next();
        byte[] onDupKeyBytes = rowEntry.getValue().getOnDupKeyBytes();
        boolean hasOnDupKey = onDupKeyBytes != null;
        ImmutableBytesPtr key = rowEntry.getKey();
        RowMutationState state = rowEntry.getValue();
        if (tableWithRowTimestampCol) {
            RowTimestampColInfo rowTsColInfo = state.getRowTimestampColInfo();
            if (rowTsColInfo.useServerTimestamp()) {
                // regenerate the key with this timestamp.
                key = getNewRowKeyWithRowTimestamp(key, serverTimestamp, table);
                // since we are about to modify the byte[] stored in key (which changes its hashcode)
                // we need to remove the entry from the values map and add a new entry with the modified byte[]
                modifiedValues.put(key, state);
                iterator.remove();
                timestampToUse = serverTimestamp;
            } else {
                if (rowTsColInfo.getTimestamp() != null) {
                    timestampToUse = rowTsColInfo.getTimestamp();
                }
            }
        }
        PRow row = table.newRow(connection.getKeyValueBuilder(), timestampToUse, key, hasOnDupKey);
        List<Mutation> rowMutations, rowMutationsPertainingToIndex;
        if (rowEntry.getValue().getColumnValues() == PRow.DELETE_MARKER) { // means delete
            row.delete();
            rowMutations = row.toRowMutations();
            // The DeleteCompiler already generates the deletes for indexes, so no need to do it again
            rowMutationsPertainingToIndex = Collections.emptyList();
        } else {
            for (Map.Entry<PColumn, byte[]> valueEntry : rowEntry.getValue().getColumnValues().entrySet()) {
                row.setValue(valueEntry.getKey(), valueEntry.getValue());
            }
            if (wildcardIncludesDynamicCols && row.setAttributesForDynamicColumnsIfReqd()) {
                row.setAttributeToProcessDynamicColumnsMetadata();
            }
            rowMutations = row.toRowMutations();
            // Pass through ON DUPLICATE KEY info through mutations
            // In the case of the same clause being used on many statements, this will be
            // inefficient because we're transmitting the same information for each mutation.
            // TODO: use our ServerCache
            for (Mutation mutation : rowMutations) {
                if (onDupKeyBytes != null) {
                    mutation.setAttribute(PhoenixIndexBuilder.ATOMIC_OP_ATTRIB, onDupKeyBytes);
                }
            }
            rowMutationsPertainingToIndex = rowMutations;
        }
        mutationList.addAll(rowMutations);
        if (mutationsPertainingToIndex != null) mutationsPertainingToIndex.addAll(rowMutationsPertainingToIndex);
    }
    values.putAll(modifiedValues);
}
 
Example 5
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public static boolean hasRowTimestampColumn(PTable table) {
	return table.getRowTimestampColPos()>0;
}