Search in sources :

Example 21 with Row

use of org.h2.result.Row in project ignite by apache.

the class H2Tree method compareRows.

/**
 * Compares two H2 rows.
 *
 * @param r1 Row 1.
 * @param r2 Row 2.
 * @return Compare result: see {@link Comparator#compare(Object, Object)} for values.
 */
public int compareRows(SearchRow r1, SearchRow r2) {
    if (r1 == r2)
        return 0;
    for (int i = 0, len = cols.length; i < len; i++) {
        int idx = columnIds[i];
        Value v1 = r1.getValue(idx);
        Value v2 = r2.getValue(idx);
        if (v1 == null || v2 == null) {
            // Can't compare further.
            return 0;
        }
        int c = compareValues(v1, v2);
        if (c != 0)
            return InlineIndexHelper.fixSort(c, cols[i].sortType);
    }
    return 0;
}
Also used : Value(org.h2.value.Value)

Example 22 with Row

use of org.h2.result.Row in project ignite by apache.

the class H2Tree method compare.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("ForLoopReplaceableByForEach")
@Override
protected int compare(BPlusIO<SearchRow> io, long pageAddr, int idx, SearchRow row) throws IgniteCheckedException {
    if (inlineSize() == 0)
        return compareRows(getRow(io, pageAddr, idx), row);
    else {
        int off = io.offset(idx);
        int fieldOff = 0;
        int lastIdxUsed = 0;
        for (int i = 0; i < inlineIdxs.size(); i++) {
            InlineIndexHelper inlineIdx = inlineIdxs.get(i);
            Value v2 = row.getValue(inlineIdx.columnIndex());
            if (v2 == null)
                return 0;
            int c = inlineIdx.compare(pageAddr, off + fieldOff, inlineSize() - fieldOff, v2, comp);
            if (c == -2)
                break;
            lastIdxUsed++;
            if (c != 0)
                return c;
            fieldOff += inlineIdx.fullSize(pageAddr, off + fieldOff);
            if (fieldOff > inlineSize())
                break;
        }
        if (lastIdxUsed == cols.length)
            return 0;
        SearchRow rowData = getRow(io, pageAddr, idx);
        for (int i = lastIdxUsed, len = cols.length; i < len; i++) {
            IndexColumn col = cols[i];
            int idx0 = col.column.getColumnId();
            Value v2 = row.getValue(idx0);
            if (v2 == null) {
                // Can't compare further.
                return 0;
            }
            Value v1 = rowData.getValue(idx0);
            int c = compareValues(v1, v2);
            if (c != 0)
                return InlineIndexHelper.fixSort(c, col.sortType);
        }
        return 0;
    }
}
Also used : Value(org.h2.value.Value) SearchRow(org.h2.result.SearchRow) IndexColumn(org.h2.table.IndexColumn)

Example 23 with Row

use of org.h2.result.Row in project ignite by apache.

the class UpdatePlan method processRowForUpdate.

/**
 * Convert a row into value.
 *
 * @param row Row to process.
 * @throws IgniteCheckedException if failed.
 * @return Tuple contains: [key, old value, new value]
 */
public T3<Object, Object, Object> processRowForUpdate(List<?> row) throws IgniteCheckedException {
    GridH2RowDescriptor rowDesc = tbl.rowDescriptor();
    GridQueryTypeDescriptor desc = rowDesc.type();
    GridCacheContext cctx = rowDesc.context();
    boolean hasNewVal = (valColIdx != -1);
    boolean hasProps = !hasNewVal || colNames.length > 1;
    Object key = row.get(0);
    Object oldVal = row.get(1);
    if (cctx.binaryMarshaller() && !(oldVal instanceof BinaryObject))
        oldVal = cctx.grid().binary().toBinary(oldVal);
    Object newVal;
    Map<String, Object> newColVals = new HashMap<>();
    for (int i = 0; i < colNames.length; i++) {
        if (hasNewVal && i == valColIdx - 2)
            continue;
        GridQueryProperty prop = tbl.rowDescriptor().type().property(colNames[i]);
        assert prop != null : "Unknown property: " + colNames[i];
        newColVals.put(colNames[i], DmlUtils.convert(row.get(i + 2), rowDesc, prop.type(), colTypes[i]));
    }
    newVal = valSupplier.apply(row);
    if (newVal == null)
        throw new IgniteSQLException("New value for UPDATE must not be null", IgniteQueryErrorCode.NULL_VALUE);
    // Skip key and value - that's why we start off with 3rd column
    for (int i = 0; i < tbl.getColumns().length - DEFAULT_COLUMNS_COUNT; i++) {
        Column c = tbl.getColumn(i + DEFAULT_COLUMNS_COUNT);
        if (rowDesc.isKeyValueOrVersionColumn(c.getColumnId()))
            continue;
        GridQueryProperty prop = desc.property(c.getName());
        if (prop.key())
            // Don't get values of key's columns - we won't use them anyway
            continue;
        boolean hasNewColVal = newColVals.containsKey(c.getName());
        if (!hasNewColVal)
            continue;
        Object colVal = newColVals.get(c.getName());
        // UPDATE currently does not allow to modify key or its fields, so we must be safe to pass null as key.
        rowDesc.setColumnValue(null, newVal, colVal, i);
    }
    if (cctx.binaryMarshaller() && hasProps) {
        assert newVal instanceof BinaryObjectBuilder;
        newVal = ((BinaryObjectBuilder) newVal).build();
    }
    desc.validateKeyAndValue(key, newVal);
    return new T3<>(key, oldVal, newVal);
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) HashMap(java.util.HashMap) GridQueryTypeDescriptor(org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor) GridQueryProperty(org.apache.ignite.internal.processors.query.GridQueryProperty) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) BinaryObject(org.apache.ignite.binary.BinaryObject) Column(org.h2.table.Column) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) T3(org.apache.ignite.internal.util.typedef.T3)

Example 24 with Row

use of org.h2.result.Row in project ignite by apache.

the class UpdatePlan method processRow.

/**
 * Convert a row into key-value pair.
 *
 * @param row Row to process.
 * @throws IgniteCheckedException if failed.
 */
public IgniteBiTuple<?, ?> processRow(List<?> row) throws IgniteCheckedException {
    if (mode != BULK_LOAD && row.size() != colNames.length)
        throw new IgniteSQLException("Not enough values in a row: " + row.size() + " instead of " + colNames.length, IgniteQueryErrorCode.ENTRY_PROCESSING);
    GridH2RowDescriptor rowDesc = tbl.rowDescriptor();
    GridQueryTypeDescriptor desc = rowDesc.type();
    GridCacheContext cctx = rowDesc.context();
    Object key = keySupplier.apply(row);
    if (QueryUtils.isSqlType(desc.keyClass())) {
        assert keyColIdx != -1;
        key = DmlUtils.convert(key, rowDesc, desc.keyClass(), colTypes[keyColIdx]);
    }
    Object val = valSupplier.apply(row);
    if (QueryUtils.isSqlType(desc.valueClass())) {
        assert valColIdx != -1;
        val = DmlUtils.convert(val, rowDesc, desc.valueClass(), colTypes[valColIdx]);
    }
    if (key == null) {
        if (F.isEmpty(desc.keyFieldName()))
            throw new IgniteSQLException("Key for INSERT, COPY, or MERGE must not be null", IgniteQueryErrorCode.NULL_KEY);
        else
            throw new IgniteSQLException("Null value is not allowed for column '" + desc.keyFieldName() + "'", IgniteQueryErrorCode.NULL_KEY);
    }
    if (val == null) {
        if (F.isEmpty(desc.valueFieldName()))
            throw new IgniteSQLException("Value for INSERT, COPY, MERGE, or UPDATE must not be null", IgniteQueryErrorCode.NULL_VALUE);
        else
            throw new IgniteSQLException("Null value is not allowed for column '" + desc.valueFieldName() + "'", IgniteQueryErrorCode.NULL_VALUE);
    }
    int actualColCnt = Math.min(colNames.length, row.size());
    Map<String, Object> newColVals = new HashMap<>();
    for (int i = 0; i < actualColCnt; i++) {
        if (i == keyColIdx || i == valColIdx)
            continue;
        String colName = colNames[i];
        GridQueryProperty prop = desc.property(colName);
        assert prop != null;
        Class<?> expCls = prop.type();
        newColVals.put(colName, DmlUtils.convert(row.get(i), rowDesc, expCls, colTypes[i]));
    }
    desc.setDefaults(key, val);
    // We update columns in the order specified by the table for a reason - table's
    // column order preserves their precedence for correct update of nested properties.
    Column[] tblCols = tbl.getColumns();
    // First 3 columns are _key, _val and _ver. Skip 'em.
    for (int i = DEFAULT_COLUMNS_COUNT; i < tblCols.length; i++) {
        if (tbl.rowDescriptor().isKeyValueOrVersionColumn(i))
            continue;
        String colName = tblCols[i].getName();
        if (!newColVals.containsKey(colName))
            continue;
        Object colVal = newColVals.get(colName);
        desc.setValue(colName, key, val, colVal);
    }
    if (cctx.binaryMarshaller()) {
        if (key instanceof BinaryObjectBuilder)
            key = ((BinaryObjectBuilder) key).build();
        if (val instanceof BinaryObjectBuilder)
            val = ((BinaryObjectBuilder) val).build();
    }
    desc.validateKeyAndValue(key, val);
    return new IgniteBiTuple<>(key, val);
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) HashMap(java.util.HashMap) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridQueryTypeDescriptor(org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor) GridQueryProperty(org.apache.ignite.internal.processors.query.GridQueryProperty) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) Column(org.h2.table.Column) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder)

Example 25 with Row

use of org.h2.result.Row in project ignite by apache.

the class GridH2SpatialIndex method put.

/**
 * {@inheritDoc}
 */
@Override
public GridH2Row put(GridH2Row row) {
    assert row instanceof GridH2KeyValueRowOnheap : "requires key to be at 0";
    Lock l = lock.writeLock();
    l.lock();
    try {
        checkClosed();
        Value key = row.getValue(KEY_COL);
        assert key != null;
        final int seg = segmentForRow(row);
        Long rowId = keyToId.get(key);
        if (rowId != null) {
            Long oldRowId = segments[seg].remove(getEnvelope(idToRow.get(rowId), rowId));
            assert rowId.equals(oldRowId);
        } else {
            rowId = ++rowIds;
            keyToId.put(key, rowId);
        }
        GridH2Row old = idToRow.put(rowId, row);
        segments[seg].put(getEnvelope(row, rowId), rowId);
        if (old == null)
            // No replace.
            rowCnt++;
        return old;
    } finally {
        l.unlock();
    }
}
Also used : Value(org.h2.value.Value) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Aggregations

Value (org.h2.value.Value)16 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)11 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)10 Column (org.h2.table.Column)8 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)7 ArrayList (java.util.ArrayList)6 Index (org.h2.index.Index)6 BinaryObject (org.apache.ignite.binary.BinaryObject)5 GridQueryProperty (org.apache.ignite.internal.processors.query.GridQueryProperty)5 SimpleResultSet (org.h2.tools.SimpleResultSet)5 ResultSet (java.sql.ResultSet)4 LinkedHashMap (java.util.LinkedHashMap)4 H2PkHashIndex (org.apache.ignite.internal.processors.query.h2.database.H2PkHashIndex)4 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)4 SQLException (java.sql.SQLException)3 Statement (java.sql.Statement)3 HashMap (java.util.HashMap)3 CacheException (javax.cache.CacheException)3 GridKernalContext (org.apache.ignite.internal.GridKernalContext)3 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)3