KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > index > BtreePage


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.index;
6
7 import java.sql.SQLException JavaDoc;
8
9 import org.h2.engine.Session;
10 import org.h2.result.Row;
11 import org.h2.result.SearchRow;
12 import org.h2.store.DataPage;
13 import org.h2.store.DiskFile;
14 import org.h2.store.Record;
15 import org.h2.table.Column;
16 import org.h2.util.ObjectArray;
17 import org.h2.value.Value;
18
19 /**
20  * @author Thomas
21  */

22 public abstract class BtreePage extends Record {
23     // TODO btree: make sure the indexed data is at most half this size! (and find a solution to work around this problem!)
24
// TODO memory: the btree page needs a lot of memory (in the cache) - probably better not use ObjectArray but array; not Row but ValueList / Value (for single key index), int array for row pos
25

26     protected static final int BLOCKS_PER_PAGE = 1024 / DiskFile.BLOCK_SIZE;
27
28     protected BtreeIndex index;
29     protected ObjectArray pageData;
30     protected boolean root;
31
32     BtreePage(BtreeIndex index) {
33         this.index = index;
34     }
35
36     abstract int add(Row row, Session session) throws SQLException JavaDoc;
37
38     // Returns the new first row in the list; null if no change; the deleted row if not empty
39
abstract SearchRow remove(Session session, Row row, int level) throws SQLException JavaDoc;
40     abstract BtreePage split(Session session, int splitPoint) throws SQLException JavaDoc;
41     abstract boolean findFirst(BtreeCursor cursor, SearchRow row) throws SQLException JavaDoc;
42     abstract SearchRow getFirst() throws SQLException JavaDoc;
43     abstract SearchRow getLast() throws SQLException JavaDoc;
44     abstract void next(BtreeCursor cursor, int i) throws SQLException JavaDoc;
45     abstract void first(BtreeCursor cursor) throws SQLException JavaDoc;
46     abstract int getRealByteCount() throws SQLException JavaDoc;
47
48     SearchRow getData(int i) throws SQLException JavaDoc {
49         return (SearchRow) pageData.get(i);
50     }
51
52     public int getByteCount(DataPage dummy) throws SQLException JavaDoc {
53         return DiskFile.BLOCK_SIZE*BLOCKS_PER_PAGE;
54     }
55
56     int getSplitPoint() throws SQLException JavaDoc {
57         if(pageData.size() == 1) {
58             return 0;
59         }
60         int size = getRealByteCount();
61         if(size >= DiskFile.BLOCK_SIZE*BLOCKS_PER_PAGE) {
62             return pageData.size() / 2;
63         }
64         return 0;
65     }
66
67     public boolean isEmpty() {
68         return false;
69     }
70
71     int getRowSize(DataPage dummy, SearchRow row) throws SQLException JavaDoc {
72         int rowsize = dummy.getIntLen();
73         Column[] columns = index.getColumns();
74         for (int j = 0; j < columns.length; j++) {
75             Value v = row.getValue(columns[j].getColumnId());
76             rowsize += dummy.getValueLen(v);
77         }
78         return rowsize;
79     }
80
81     void setRoot(boolean root) {
82         this.root = root;
83     }
84
85     public boolean isPinned() {
86         return root;
87     }
88
89     public abstract String JavaDoc print(String JavaDoc indent) throws SQLException JavaDoc;
90 }
91
Popular Tags