Search in sources :

Example 6 with CompareMode

use of org.h2.value.CompareMode in project h2database by h2database.

the class PageStore method addMeta.

/**
 * Add the meta data of an index.
 *
 * @param index the index to add
 * @param session the session
 */
public void addMeta(PageIndex index, Session session) {
    Table table = index.getTable();
    if (SysProperties.CHECK) {
        if (!table.isTemporary()) {
            // the Database lock before we take the PageStore lock
            synchronized (database) {
                synchronized (this) {
                    database.verifyMetaLocked(session);
                }
            }
        }
    }
    synchronized (this) {
        int type = index instanceof PageDataIndex ? META_TYPE_DATA_INDEX : META_TYPE_BTREE_INDEX;
        IndexColumn[] columns = index.getIndexColumns();
        StatementBuilder buff = new StatementBuilder();
        for (IndexColumn col : columns) {
            buff.appendExceptFirst(",");
            int id = col.column.getColumnId();
            buff.append(id);
            int sortType = col.sortType;
            if (sortType != 0) {
                buff.append('/');
                buff.append(sortType);
            }
        }
        String columnList = buff.toString();
        CompareMode mode = table.getCompareMode();
        String options = mode.getName() + "," + mode.getStrength() + ",";
        if (table.isTemporary()) {
            options += "temp";
        }
        options += ",";
        if (index instanceof PageDelegateIndex) {
            options += "d";
        }
        options += "," + mode.isBinaryUnsigned();
        Row row = metaTable.getTemplateRow();
        row.setValue(0, ValueInt.get(index.getId()));
        row.setValue(1, ValueInt.get(type));
        row.setValue(2, ValueInt.get(table.getId()));
        row.setValue(3, ValueInt.get(index.getRootPageId()));
        row.setValue(4, ValueString.get(options));
        row.setValue(5, ValueString.get(columnList));
        row.setKey(index.getId() + 1);
        metaIndex.add(session, row);
    }
}
Also used : RegularTable(org.h2.table.RegularTable) Table(org.h2.table.Table) PageDelegateIndex(org.h2.index.PageDelegateIndex) PageDataIndex(org.h2.index.PageDataIndex) StatementBuilder(org.h2.util.StatementBuilder) CompareMode(org.h2.value.CompareMode) ValueString(org.h2.value.ValueString) Row(org.h2.result.Row) IndexColumn(org.h2.table.IndexColumn)

Example 7 with CompareMode

use of org.h2.value.CompareMode in project h2database by h2database.

the class MVSecondaryIndex method addBufferedRows.

@Override
public void addBufferedRows(List<String> bufferNames) {
    ArrayList<String> mapNames = new ArrayList<>(bufferNames);
    CompareMode compareMode = database.getCompareMode();
    int buffersCount = bufferNames.size();
    Queue<Source> queue = new PriorityQueue<>(buffersCount, new Source.Comparator(compareMode));
    for (String bufferName : bufferNames) {
        Iterator<ValueArray> iter = openMap(bufferName).keyIterator(null);
        if (iter.hasNext()) {
            queue.add(new Source(iter));
        }
    }
    try {
        while (!queue.isEmpty()) {
            Source s = queue.remove();
            ValueArray rowData = s.next();
            if (indexType.isUnique()) {
                Value[] array = rowData.getList();
                // don't change the original value
                array = array.clone();
                array[keyColumns - 1] = ValueLong.MIN;
                ValueArray unique = ValueArray.get(array);
                SearchRow row = convertToSearchRow(rowData);
                if (!mayHaveNullDuplicates(row)) {
                    requireUnique(row, dataMap, unique);
                }
            }
            dataMap.putCommitted(rowData, ValueNull.INSTANCE);
            if (s.hasNext()) {
                queue.offer(s);
            }
        }
    } finally {
        for (String tempMapName : mapNames) {
            MVMap<ValueArray, Value> map = openMap(tempMapName);
            map.getStore().removeMap(map);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) PriorityQueue(java.util.PriorityQueue) Value(org.h2.value.Value) CompareMode(org.h2.value.CompareMode) ValueArray(org.h2.value.ValueArray) SearchRow(org.h2.result.SearchRow)

Example 8 with CompareMode

use of org.h2.value.CompareMode 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 9 with CompareMode

use of org.h2.value.CompareMode in project h2database by h2database.

the class TestDataPage method testValue.

private void testValue(Value v) {
    Data data = Data.create(null, 1024);
    data.checkCapacity((int) v.getPrecision());
    data.writeValue(v);
    data.writeInt(123);
    data.reset();
    Value v2 = data.readValue();
    assertEquals(v.getType(), v2.getType());
    assertEquals(0, v.compareTo(v2, compareMode));
    assertEquals(123, data.readInt());
}
Also used : Value(org.h2.value.Value) Data(org.h2.store.Data)

Example 10 with CompareMode

use of org.h2.value.CompareMode in project h2database by h2database.

the class JdbcResultSet method patchCurrentRow.

private void patchCurrentRow(Value[] row) {
    boolean changed = false;
    Value[] current = result.currentRow();
    CompareMode mode = conn.getCompareMode();
    for (int i = 0; i < row.length; i++) {
        if (row[i].compareTo(current[i], mode) != 0) {
            changed = true;
            break;
        }
    }
    if (patchedRows == null) {
        patchedRows = new HashMap<>();
    }
    Integer rowId = result.getRowId();
    if (!changed) {
        patchedRows.remove(rowId);
    } else {
        patchedRows.put(rowId, row);
    }
}
Also used : BigInteger(java.math.BigInteger) Value(org.h2.value.Value) CompareMode(org.h2.value.CompareMode)

Aggregations

CompareMode (org.h2.value.CompareMode)10 Value (org.h2.value.Value)7 ArrayList (java.util.ArrayList)2 Expression (org.h2.expression.Expression)2 PageDataIndex (org.h2.index.PageDataIndex)2 PageDelegateIndex (org.h2.index.PageDelegateIndex)2 IndexColumn (org.h2.table.IndexColumn)2 RegularTable (org.h2.table.RegularTable)2 Table (org.h2.table.Table)2 ValueArray (org.h2.value.ValueArray)2 ValueString (org.h2.value.ValueString)2 BigInteger (java.math.BigInteger)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 PriorityQueue (java.util.PriorityQueue)1 Random (java.util.Random)1 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)1 CreateTableData (org.h2.command.ddl.CreateTableData)1 Constraint (org.h2.constraint.Constraint)1