Analysis of h2database source code and h2database
Recently I want to take a good look at the principles of the database, download the source code of h2database, and have a good look. Some ideas in this process can be written down for the time being. Use Permission as a code reading note!
Test cases prepared for debugging:
@Testpublic void testExternalDb() throws Exception{Class.forName("org.h2.Driver");Connection conn = DriverManager.getConnection("jdbc:h2:./testdb", "sa", "");// add application code hereStatement stmt = conn.createStatement();stmt.executeUpdate("DROP TABLE TEST IF EXISTS");stmt.executeUpdate("CREATE TABLE TEST(ID INT PRIMARY KEY,NAME VARCHAR(255));");stmt.executeUpdate("INSERT INTO TEST VALUES(100, 'Damn,World');");stmt.executeUpdate("INSERT INTO TEST VALUES(200, 'Hello,H2');");stmt.executeUpdate("INSERT INTO TEST VALUES(150, 'Hello,World');");ResultSet rs = stmt.executeQuery("SELECT * FROM TEST where ID>120 and NAME like 'Hello%'");while (rs.next()){System.out.println(rs.getInt("ID") + "," + rs.getString("NAME"));}conn.close();}
The following are some personal guesses, which will be verified later:
- H2 is a memory database. Its file version is only the persistence of the memory content, and persistence occurs when conn. close;
- The SQL statement is parsed into a Prepared object by h2, and then its update () method is called, where:
- DDL will be compiled into DefineCommand
- DML is compiled into Query, Update, Delete, and other objects.
- As an example, CreateTable is a derived class of DefineCommand:
- Query has two Derived classes: Select and SelectUnion.
- The Select. query () code is as follows:
while (topTableFilter.next()){setCurrentRowNumber(rowNumber + 1);if (condition == null || Boolean.TRUE.equals(condition.getBooleanValue(session))){Value[] row = new Value[columnCount];for (int i = 0; i < columnCount; i++){Expression expr = expressions.get(i);row[i] = expr.getValue(session);}//...result.addRow(row);rowNumber++;//...}}
- As you can see, Select contains a TableFilter, which indicates the table to be queried. It also contains an indexConditions to implement index Filtering:
- TableFilter. next () will call IndexCursor. next (), IndexCursor is the record iterator obtained by index. The find () method is equivalent to initialization. For example, for ID> 120, the firstRow pointing to {ID = 150} is calculated;
- TableFilter can be considered as a level-1 filter (filtering based on indexes), and condition can be considered as a level-2 filter;
- The condition type is Condition, and the query in the test case will be expressed as a ConditionAndOr object;
- LocalResult corresponds to the cache of a query result (isn't it delayed loading ???), Saves all the rows and provides the next () method;
- The Select. query () method writes the query result to LocalResult and returns the LocalResult;
- JdbcStatement encapsulates LocalResult into a JdbcResultSet and returns it to the JDBC client;
public class JdbcResultSet extends TraceObject implements ResultSet{//...public boolean next() throws SQLException{try{debugCodeCall("next");checkClosed();return nextRow();}catch (Exception e){throw logAndConvert(e);}}private boolean nextRow(){boolean next = result.next();if (!next && !scrollable){result.close();}return next;}//...}