/** * @param row Row. * @return ID. */ private static ValueInt id(SearchRow row) { Value id = row.getValue(ID); assert id != null; return (ValueInt)id; }
@Override public int compareRows(SearchRow rowData, SearchRow compare) { if (rowData == compare) { return 0; } for (int i = 0, len = indexColumns.length; i < len; i++) { int index = columnIds[i]; Value v1 = rowData.getValue(index); Value v2 = compare.getValue(index); if (v1 == null || v2 == null) { // can't compare further return 0; } int c = compareValues(v1, v2, indexColumns[i].sortType); if (c != 0) { return c; } } return 0; }
/** * Check whether the given row contains data. * * @param row the row * @return true if it contains data */ boolean hasData(SearchRow row) { return row.getValue(columns[0].getColumnId()) != null; }
/** * Get the key from the row. * * @param row the row * @param ifEmpty the value to use if the row is empty * @param ifNull the value to use if the column is NULL * @return the key */ long getKey(SearchRow row, long ifEmpty, long ifNull) { if (row == null) { return ifEmpty; } Value v = row.getValue(mainIndexColumn); if (v == null) { throw DbException.throwInternalError(row.toString()); } else if (v == ValueNull.INSTANCE) { return ifNull; } return v.getLong(); }
/** * Get the key from the row. * * @param row the row * @param ifEmpty the value to use if the row is empty * @param ifNull the value to use if the column is NULL * @return the key */ ValueLong getKey(SearchRow row, ValueLong ifEmpty, ValueLong ifNull) { if (row == null) { return ifEmpty; } Value v = row.getValue(mainIndexColumn); if (v == null) { throw DbException.throwInternalError(row.toString()); } else if (v == ValueNull.INSTANCE) { return ifNull; } return (ValueLong) v.convertTo(Value.LONG); }
@Override public Cursor find(Session session, SearchRow first, SearchRow last) { long min = rangeTable.getMin(session), start = min; long max = rangeTable.getMax(session), end = max; long step = rangeTable.getStep(session); try { start = Math.max(min, first == null ? min : first.getValue(0).getLong()); } catch (Exception e) { // error when converting the value - ignore } try { end = Math.min(max, last == null ? max : last.getValue(0).getLong()); } catch (Exception e) { // error when converting the value - ignore } return new RangeCursor(session, start, end, step); }
private SearchRow getSpatialSearchRow(SearchRow row, int columnId, Value v) { if (row == null) { row = table.getTemplateRow(); } else if (row.getValue(columnId) != null) { // if an object needs to overlap with both a and b, // then it needs to overlap with the the union of a and b // (not the intersection) ValueGeometry vg = (ValueGeometry) row.getValue(columnId). convertTo(Value.GEOMETRY); v = ((ValueGeometry) v.convertTo(Value.GEOMETRY)). getEnvelopeUnion(vg); } if (columnId < 0) { row.setKey(v.getLong()); } else { row.setValue(columnId, v); } return row; }
/** * Get the size of a row (only the part that is stored in the index). * * @param dummy a dummy data page to calculate the size * @param row the row * @param onlyPosition whether only the position of the row is stored * @return the number of bytes */ int getRowSize(Data dummy, SearchRow row, boolean onlyPosition) { int rowsize = Data.getVarLongLen(row.getKey()); if (!onlyPosition) { for (Column col : columns) { Value v = row.getValue(col.getColumnId()); rowsize += dummy.getValueLen(v); } } return rowsize; }
/** * Clones provided row and copies values of alias key and val columns * into respective key and val positions. * * @param row Source row. * @return Result. */ public SearchRow prepareProxyIndexRow(SearchRow row) { if (row == null) return null; Value[] data = new Value[row.getColumnCount()]; for (int idx = 0; idx < data.length; idx++) data[idx] = row.getValue(idx); copyAliasColumnData(data, KEY_COL, keyAliasColId); copyAliasColumnData(data, VAL_COL, valAliasColId); return GridH2PlainRowFactory.create(data); }
private SearchRow getSearchRow(SearchRow row, int columnId, Value v, boolean max) { if (row == null) { row = table.getTemplateRow(); } else { v = getMax(row.getValue(columnId), v, max); } if (columnId < 0) { row.setKey(v.getLong()); } else { row.setValue(columnId, v); } return row; }
/** * Write a row to the data page at the given offset. * * @param data the data * @param offset the offset * @param onlyPosition whether only the position of the row is stored * @param row the row to write */ void writeRow(Data data, int offset, SearchRow row, boolean onlyPosition) { data.setPos(offset); data.writeVarLong(row.getKey()); if (!onlyPosition) { for (Column col : columns) { int idx = col.getColumnId(); data.writeValue(row.getValue(idx)); } } }
private ValueArray convertToKey(SearchRow r) { if (r == null) { return null; } Value[] array = new Value[keyColumns]; for (int i = 0; i < columns.length; i++) { Column c = columns[i]; int idx = c.getColumnId(); Value v = r.getValue(idx); if (v != null) { array[i] = v.convertTo(c.getType()); } } array[keyColumns - 1] = ValueLong.get(r.getKey()); return ValueArray.get(array); }
@Override public Cursor find(Session session, SearchRow first, SearchRow last) { if (first == null || last == null) { // TODO hash index: should additionally check if values are the same throw DbException.throwInternalError(first + " " + last); } Value v = first.getValue(indexColumn); /* * Sometimes the incoming search is a similar, but not the same type * e.g. the search value is INT, but the index column is LONG. In which * case we need to convert, otherwise the ValueHashMap will not find the * result. */ v = v.convertTo(tableData.getColumn(indexColumn).getType()); Row result; Long pos = rows.get(v); if (pos == null) { result = null; } else { result = tableData.getRow(session, pos.intValue()); } return new SingleRowCursor(result); }
/** * @param row Table row. * @return Segment ID for given row. */ @SuppressWarnings("IfMayBeConditional") protected int segmentForRow(SearchRow row) { assert row != null; if (segmentsCount() == 1 || ctx == null) return 0; CacheObject key; final Value keyColValue = row.getValue(KEY_COL); assert keyColValue != null; final Object o = keyColValue.getObject(); if (o instanceof CacheObject) key = (CacheObject)o; else key = ctx.toCacheKeyObject(o); return segmentForPartition(ctx.affinity().partition(key)); }
@Override public Value getValue(Column column) { if (joinBatch != null) { return joinBatch.getValue(joinFilterId, column); } if (currentSearchRow == null) { return null; } int columnId = column.getColumnId(); if (columnId == -1) { return ValueLong.get(currentSearchRow.getKey()); } if (current == null) { Value v = currentSearchRow.getValue(columnId); if (v != null) { return v; } current = cursor.get(); if (current == null) { return ValueNull.INSTANCE; } } return current.getValue(columnId); }
private SpatialKey getKey(SearchRow row) { if (row == null) { return null; } Value v = row.getValue(columnIds[0]); if (v == ValueNull.INSTANCE) { return null; } Geometry g = ((ValueGeometry) v.convertTo(Value.GEOMETRY)).getGeometryNoCopy(); Envelope env = g.getEnvelopeInternal(); return new SpatialKey(row.getKey(), (float) env.getMinX(), (float) env.getMaxX(), (float) env.getMinY(), (float) env.getMaxY()); }
@Override public Cursor find(Session session, SearchRow first, SearchRow last) { if (first == null || last == null) { throw DbException.throwInternalError(first + " " + last); } if (first != last) { if (compareKeys(first, last) != 0) { throw DbException.throwInternalError(); } } Value v = first.getValue(indexColumn); /* * Sometimes the incoming search is a similar, but not the same type * e.g. the search value is INT, but the index column is LONG. In which * case we need to convert, otherwise the ValueHashMap will not find the * result. */ v = v.convertTo(tableData.getColumn(indexColumn).getType()); ArrayList<Long> positions = rows.get(v); return new NonUniqueHashCursor(session, tableData, positions); }
private SpatialKey getKey(SearchRow row) { Value v = row.getValue(columnIds[0]); if (v == ValueNull.INSTANCE) { return new SpatialKey(row.getKey()); } Geometry g = ((ValueGeometry) v.convertTo(Value.GEOMETRY)).getGeometryNoCopy(); Envelope env = g.getEnvelopeInternal(); return new SpatialKey(row.getKey(), (float) env.getMinX(), (float) env.getMaxX(), (float) env.getMinY(), (float) env.getMaxY()); }
private Cursor find(Row row) { if (index == null) { // for the case "in(select ...)", the query might // use an optimization and not create the index // up front createIndex(); } Cursor cursor = index.find(session, row, row); while (cursor.next()) { SearchRow found = cursor.getSearchRow(); boolean ok = true; Database db = session.getDatabase(); for (int i = 0; i < row.getColumnCount(); i++) { if (!db.areEqual(row.getValue(i), found.getValue(i))) { ok = false; break; } } if (ok) { return cursor; } } return null; }