Search in sources :

Example 1 with ValueHashMap

use of org.h2.util.ValueHashMap in project h2database by h2database.

the class HashIndex method find.

@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);
}
Also used : Value(org.h2.value.Value) Row(org.h2.result.Row) SearchRow(org.h2.result.SearchRow)

Example 2 with ValueHashMap

use of org.h2.util.ValueHashMap in project h2database by h2database.

the class NonUniqueHashIndex method find.

@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);
}
Also used : Value(org.h2.value.Value)

Example 3 with ValueHashMap

use of org.h2.util.ValueHashMap in project h2database by h2database.

the class TestValueHashMap method testRandomized.

private void testRandomized() {
    ValueHashMap<Value> map = ValueHashMap.newInstance();
    HashMap<Value, Value> hash = new HashMap<>();
    Random random = new Random(1);
    Comparator<Value> vc = new Comparator<Value>() {

        @Override
        public int compare(Value v1, Value v2) {
            return v1.compareTo(v2, compareMode);
        }
    };
    for (int i = 0; i < 10000; i++) {
        int op = random.nextInt(10);
        Value key = ValueInt.get(random.nextInt(100));
        Value value = ValueInt.get(random.nextInt(100));
        switch(op) {
            case 0:
                map.put(key, value);
                hash.put(key, value);
                break;
            case 1:
                map.remove(key);
                hash.remove(key);
                break;
            case 2:
                Value v1 = map.get(key);
                Value v2 = hash.get(key);
                assertTrue(v1 == null ? v2 == null : v1.equals(v2));
                break;
            case 3:
                {
                    ArrayList<Value> a1 = map.keys();
                    ArrayList<Value> a2 = new ArrayList<>(hash.keySet());
                    assertEquals(a1.size(), a2.size());
                    Collections.sort(a1, vc);
                    Collections.sort(a2, vc);
                    for (int j = 0; j < a1.size(); j++) {
                        assertTrue(a1.get(j).equals(a2.get(j)));
                    }
                    break;
                }
            case 4:
                ArrayList<Value> a1 = map.values();
                ArrayList<Value> a2 = new ArrayList<>(hash.values());
                assertEquals(a1.size(), a2.size());
                Collections.sort(a1, vc);
                Collections.sort(a2, vc);
                for (int j = 0; j < a1.size(); j++) {
                    assertTrue(a1.get(j).equals(a2.get(j)));
                }
                break;
            default:
        }
    }
}
Also used : Random(java.util.Random) HashMap(java.util.HashMap) ValueHashMap(org.h2.util.ValueHashMap) Value(org.h2.value.Value) ArrayList(java.util.ArrayList) Comparator(java.util.Comparator)

Example 4 with ValueHashMap

use of org.h2.util.ValueHashMap in project h2database by h2database.

the class Select method queryGroup.

private void queryGroup(int columnCount, LocalResult result) {
    ValueHashMap<HashMap<Expression, Object>> groups = ValueHashMap.newInstance();
    int rowNumber = 0;
    setCurrentRowNumber(0);
    currentGroup = null;
    ValueArray defaultGroup = ValueArray.get(new Value[0]);
    int sampleSize = getSampleSizeValue(session);
    while (topTableFilter.next()) {
        setCurrentRowNumber(rowNumber + 1);
        if (isConditionMet()) {
            Value key;
            rowNumber++;
            if (groupIndex == null) {
                key = defaultGroup;
            } else {
                Value[] keyValues = new Value[groupIndex.length];
                // update group
                for (int i = 0; i < groupIndex.length; i++) {
                    int idx = groupIndex[i];
                    Expression expr = expressions.get(idx);
                    keyValues[i] = expr.getValue(session);
                }
                key = ValueArray.get(keyValues);
            }
            HashMap<Expression, Object> values = groups.get(key);
            if (values == null) {
                values = new HashMap<>();
                groups.put(key, values);
            }
            currentGroup = values;
            currentGroupRowId++;
            for (int i = 0; i < columnCount; i++) {
                if (groupByExpression == null || !groupByExpression[i]) {
                    Expression expr = expressions.get(i);
                    expr.updateAggregate(session);
                }
            }
            if (sampleSize > 0 && rowNumber >= sampleSize) {
                break;
            }
        }
    }
    if (groupIndex == null && groups.size() == 0) {
        groups.put(defaultGroup, new HashMap<Expression, Object>());
    }
    ArrayList<Value> keys = groups.keys();
    for (Value v : keys) {
        ValueArray key = (ValueArray) v;
        currentGroup = groups.get(key);
        Value[] keyValues = key.getList();
        Value[] row = new Value[columnCount];
        for (int j = 0; groupIndex != null && j < groupIndex.length; j++) {
            row[groupIndex[j]] = keyValues[j];
        }
        for (int j = 0; j < columnCount; j++) {
            if (groupByExpression != null && groupByExpression[j]) {
                continue;
            }
            Expression expr = expressions.get(j);
            row[j] = expr.getValue(session);
        }
        if (isHavingNullOrFalse(row)) {
            continue;
        }
        row = keepOnlyDistinct(row, columnCount);
        result.addRow(row);
    }
}
Also used : HashMap(java.util.HashMap) Value(org.h2.value.Value) ValueArray(org.h2.value.ValueArray)

Example 5 with ValueHashMap

use of org.h2.util.ValueHashMap in project h2database by h2database.

the class TestValueHashMap method testNotANumber.

private void testNotANumber() {
    ValueHashMap<Integer> map = ValueHashMap.newInstance();
    for (int i = 1; i < 100; i++) {
        double d = Double.longBitsToDouble(0x7ff0000000000000L | i);
        ValueDouble v = ValueDouble.get(d);
        map.put(v, null);
        assertEquals(1, map.size());
    }
}
Also used : ValueDouble(org.h2.value.ValueDouble)

Aggregations

Value (org.h2.value.Value)4 HashMap (java.util.HashMap)2 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 Random (java.util.Random)1 Row (org.h2.result.Row)1 SearchRow (org.h2.result.SearchRow)1 ValueHashMap (org.h2.util.ValueHashMap)1 ValueArray (org.h2.value.ValueArray)1 ValueDouble (org.h2.value.ValueDouble)1