File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\index\PageDelegateIndex.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.index;
8
9 import org.h2.engine.Session;
10 import org.h2.message.DbException;
11 import org.h2.result.Row;
12 import org.h2.result.SearchRow;
13 import org.h2.store.PageStore;
14 import org.h2.table.Column;
15 import org.h2.table.IndexColumn;
16 import org.h2.table.RegularTable;
17
18 /**
19 * An index that delegates indexing to the page data index.
20 */
21 public class PageDelegateIndex extends PageIndex {
22
23 private final PageDataIndex mainIndex;
24
25 public PageDelegateIndex(RegularTable table, int id, String name,
26 IndexType indexType, PageDataIndex mainIndex, boolean create, Session session) {
27 IndexColumn[] cols = IndexColumn.wrap(new Column[] { table.getColumn(mainIndex.getMainIndexColumn())});
28 this.initBaseIndex(table, id, name, cols, indexType);
29 this.mainIndex = mainIndex;
30 if (!database.isPersistent() || id < 0) {
31 throw DbException.throwInternalError("" + name);
32 }
33 PageStore store = database.getPageStore();
34 store.addIndex(this);
35 if (create) {
36 store.addMeta(this, session);
37 }
38 }
39
40 public void add(Session session, Row row) {
41 // nothing to do
42 }
43
44 public boolean canFindNext() {
45 return false;
46 }
47
48 public boolean canGetFirstOrLast() {
49 return true;
50 }
51
52 public void close(Session session) {
53 // nothing to do
54 }
55
56 public Cursor find(Session session, SearchRow first, SearchRow last) {
57 long min = mainIndex.getKey(first, Long.MIN_VALUE, Long.MIN_VALUE);
58 // ifNull is MIN_VALUE as well, because the column is never NULL
59 // so avoid returning all rows (returning one row is OK)
60 long max = mainIndex.getKey(last, Long.MAX_VALUE, Long.MIN_VALUE);
61 return mainIndex.find(session, min, max, false);
62 }
63
64 public Cursor findFirstOrLast(Session session, boolean first) {
65 Cursor cursor;
66 if (first) {
67 cursor = mainIndex.find(session, Long.MIN_VALUE, Long.MAX_VALUE, false);
68 } else {
69 long x = mainIndex.getLastKey();
70 cursor = mainIndex.find(session, x, x, false);
71 }
72 cursor.next();
73 return cursor;
74 }
75
76 public Cursor findNext(Session session, SearchRow higherThan, SearchRow last) {
77 throw DbException.throwInternalError();
78 }
79
80 public int getColumnIndex(Column col) {
81 if (col.getColumnId() == mainIndex.getMainIndexColumn()) {
82 return 0;
83 }
84 return -1;
85 }
86
87 public double getCost(Session session, int[] masks) {
88 return 10 * getCostRangeIndex(masks, mainIndex.getRowCount(session));
89 }
90
91 public boolean needRebuild() {
92 return false;
93 }
94
95 public void remove(Session session, Row row) {
96 // nothing to do
97 }
98
99 public void remove(Session session) {
100 mainIndex.setMainIndexColumn(-1);
101 session.getDatabase().getPageStore().removeMeta(this, session);
102 }
103
104 public void truncate(Session session) {
105 // nothing to do
106 }
107
108 public void checkRename() {
109 // ok
110 }
111
112 public long getRowCount(Session session) {
113 return mainIndex.getRowCount(session);
114 }
115
116 public long getRowCountApproximation() {
117 return mainIndex.getRowCountApproximation();
118 }
119
120 public void writeRowCount() {
121 // ignore
122 }
123
124 }
125