| 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.index; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
|
| 10 |
import org.h2.command.query.AllColumnsForPlan; |
| 11 |
import org.h2.engine.SessionLocal; |
| 12 |
import org.h2.message.DbException; |
| 13 |
import org.h2.result.Row; |
| 14 |
import org.h2.result.SearchRow; |
| 15 |
import org.h2.result.SortOrder; |
| 16 |
import org.h2.table.Column; |
| 17 |
import org.h2.table.IndexColumn; |
| 18 |
import org.h2.table.MetaTable; |
| 19 |
import org.h2.table.TableFilter; |
| 20 |
|
| 21 |
/** |
| 22 |
* The index implementation for meta data tables. |
| 23 |
*/ |
| 24 |
public class MetaIndex extends Index { |
| 25 |
|
| 26 |
private final MetaTable meta; |
| 27 |
private final boolean scan; |
| 28 |
|
| 29 |
public MetaIndex(MetaTable meta, IndexColumn[] columns, boolean scan) { |
| 30 |
super(meta, 0, null, columns, 0, IndexType.createNonUnique(true)); |
| 31 |
this.meta = meta; |
| 32 |
this.scan = scan; |
| 33 |
} |
| 34 |
|
| 35 |
@Override |
| 36 |
public void close(SessionLocal session) { |
| 37 |
// nothing to do |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public void add(SessionLocal session, Row row) { |
| 42 |
throw DbException.getUnsupportedException("META"); |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
public void remove(SessionLocal session, Row row) { |
| 47 |
throw DbException.getUnsupportedException("META"); |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public Cursor find(SessionLocal session, SearchRow first, SearchRow last) { |
| 52 |
ArrayList<Row> rows = meta.generateRows(session, first, last); |
| 53 |
return new MetaCursor(rows); |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public double getCost(SessionLocal session, int[] masks, |
| 58 |
TableFilter[] filters, int filter, SortOrder sortOrder, |
| 59 |
AllColumnsForPlan allColumnsSet) { |
| 60 |
if (scan) { |
| 61 |
return 10 * MetaTable.ROW_COUNT_APPROXIMATION; |
| 62 |
} |
| 63 |
return getCostRangeIndex(masks, MetaTable.ROW_COUNT_APPROXIMATION, |
| 64 |
filters, filter, sortOrder, false, allColumnsSet); |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public void truncate(SessionLocal session) { |
| 69 |
throw DbException.getUnsupportedException("META"); |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public void remove(SessionLocal session) { |
| 74 |
throw DbException.getUnsupportedException("META"); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public int getColumnIndex(Column col) { |
| 79 |
if (scan) { |
| 80 |
// the scan index cannot use any columns |
| 81 |
return -1; |
| 82 |
} |
| 83 |
return super.getColumnIndex(col); |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public boolean isFirstColumn(Column column) { |
| 88 |
if (scan) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
return super.isFirstColumn(column); |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public void checkRename() { |
| 96 |
throw DbException.getUnsupportedException("META"); |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public boolean needRebuild() { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public String getCreateSQL() { |
| 106 |
return null; |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public long getRowCount(SessionLocal session) { |
| 111 |
return MetaTable.ROW_COUNT_APPROXIMATION; |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public long getRowCountApproximation(SessionLocal session) { |
| 116 |
return MetaTable.ROW_COUNT_APPROXIMATION; |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public long getDiskSpaceUsed() { |
| 121 |
return meta.getDiskSpaceUsed(); |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public String getPlanSQL() { |
| 126 |
return "meta"; |
| 127 |
} |
| 128 |
|
| 129 |
} |