| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.mvstore.db; |
| 7 |
|
| 8 |
import org.h2.engine.Database; |
| 9 |
import org.h2.expression.Expression; |
| 10 |
import org.h2.message.DbException; |
| 11 |
import org.h2.mvstore.Cursor; |
| 12 |
import org.h2.mvstore.MVMap; |
| 13 |
import org.h2.mvstore.MVMap.Builder; |
| 14 |
import org.h2.mvstore.type.LongDataType; |
| 15 |
import org.h2.result.ResultExternal; |
| 16 |
import org.h2.result.RowFactory.DefaultRowFactory; |
| 17 |
import org.h2.value.Value; |
| 18 |
import org.h2.value.ValueRow; |
| 19 |
|
| 20 |
/** |
| 21 |
* Plain temporary result. |
| 22 |
*/ |
| 23 |
class MVPlainTempResult extends MVTempResult { |
| 24 |
|
| 25 |
/** |
| 26 |
* Map with identities of rows as keys rows as values. |
| 27 |
*/ |
| 28 |
private final MVMap<Long, ValueRow> map; |
| 29 |
|
| 30 |
/** |
| 31 |
* Counter for the identities of rows. A separate counter is used instead of |
| 32 |
* {@link #rowCount} because rows due to presence of {@link #removeRow(Value[])} |
| 33 |
* method to ensure that each row will have an own identity. |
| 34 |
*/ |
| 35 |
private long counter; |
| 36 |
|
| 37 |
/** |
| 38 |
* Cursor for the {@link #next()} method. |
| 39 |
*/ |
| 40 |
private Cursor<Long, ValueRow> cursor; |
| 41 |
|
| 42 |
/** |
| 43 |
* Creates a shallow copy of the result. |
| 44 |
* |
| 45 |
* @param parent |
| 46 |
* parent result |
| 47 |
*/ |
| 48 |
private MVPlainTempResult(MVPlainTempResult parent) { |
| 49 |
super(parent); |
| 50 |
this.map = parent.map; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Creates a new plain temporary result. This result does not sort its rows, |
| 55 |
* but it can be used in index-sorted queries and it can preserve additional |
| 56 |
* columns for WITH TIES processing. |
| 57 |
* |
| 58 |
* @param database |
| 59 |
* database |
| 60 |
* @param expressions |
| 61 |
* column expressions |
| 62 |
* @param visibleColumnCount |
| 63 |
* count of visible columns |
| 64 |
* @param resultColumnCount |
| 65 |
* the number of columns including visible columns and additional |
| 66 |
* virtual columns for ORDER BY clause |
| 67 |
*/ |
| 68 |
MVPlainTempResult(Database database, Expression[] expressions, int visibleColumnCount, int resultColumnCount) { |
| 69 |
super(database, expressions, visibleColumnCount, resultColumnCount); |
| 70 |
ValueDataType valueType = new ValueDataType(database, new int[resultColumnCount]); |
| 71 |
valueType.setRowFactory(DefaultRowFactory.INSTANCE.createRowFactory(database, database.getCompareMode(), |
| 72 |
database, expressions, null, false)); |
| 73 |
Builder<Long, ValueRow> builder = new MVMap.Builder<Long, ValueRow>().keyType(LongDataType.INSTANCE) |
| 74 |
.valueType(valueType).singleWriter(); |
| 75 |
map = store.openMap("tmp", builder); |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public int addRow(Value[] values) { |
| 80 |
assert parent == null; |
| 81 |
map.append(counter++, ValueRow.get(values)); |
| 82 |
return ++rowCount; |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public boolean contains(Value[] values) { |
| 87 |
throw DbException.getUnsupportedException("contains()"); |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public synchronized ResultExternal createShallowCopy() { |
| 92 |
if (parent != null) { |
| 93 |
return parent.createShallowCopy(); |
| 94 |
} |
| 95 |
if (closed) { |
| 96 |
return null; |
| 97 |
} |
| 98 |
childCount++; |
| 99 |
return new MVPlainTempResult(this); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public Value[] next() { |
| 104 |
if (cursor == null) { |
| 105 |
cursor = map.cursor(null); |
| 106 |
} |
| 107 |
if (!cursor.hasNext()) { |
| 108 |
return null; |
| 109 |
} |
| 110 |
cursor.next(); |
| 111 |
return cursor.getValue().getList(); |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public int removeRow(Value[] values) { |
| 116 |
throw DbException.getUnsupportedException("removeRow()"); |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public void reset() { |
| 121 |
cursor = null; |
| 122 |
} |
| 123 |
|
| 124 |
} |