h2database源码浅析:MVTable与MVIndex
- Database包含一个Store:
MVTableEngine.Store getMvStore() - MVTableEngine.Store可以获取各tables:
java.util.HashMap<java.lang.String,MVTable> getTables() - MVTable代表存在MVTableEngine.Store的一张表,MVTable.addRow()用以插入一条记录(该方法会被Insert语句调用):
public void addRow(Session session, Row row) { lastModificationId = database.getNextModificationDataId(); Transaction t = getTransaction(session); long savepoint = t.setSavepoint(); try { for (int i = 0, size = indexes.size(); i < size; i++) { Index index = indexes.get(i); index.add(session, row); } } catch (Throwable e) { t.rollbackToSavepoint(savepoint); //... } }
其中会向每个Index中插入Row。
void init(Session session) { primaryIndex = new MVPrimaryIndex(session.getDatabase(), this, getId(), IndexColumn.wrap(getColumns()), IndexType.createScan(true)); indexes.add(primaryIndex); }
public void add(Session session, Row row) { if (mainIndexColumn == -1) { if (row.getKey() == 0) { row.setKey(++lastKey); } } else { long c = row.getValue(mainIndexColumn).getLong(); row.setKey(c); } //... TransactionMap<Value, Value> map = getMap(session); Value key = ValueLong.get(row.getKey()); //... try { map.put(key, ValueArray.get(row.getValueList())); } catch (IllegalStateException e) { throw DbException.get(ErrorCode.CONCURRENT_UPDATE_1, e, table.getName()); } lastKey = Math.max(lastKey, row.getKey()); }该MVPrimaryIndex保存了一个Map,以<key, row>保存了所有记录。在遍历的时候,这个map有会被用起来:
Cursor find(Session session, ValueLong first, ValueLong last) { TransactionMap<Value, Value> map = getMap(session); return new MVStoreCursor(map.entryIterator(first), last); }MVStoreCursor差不多是Iterator的一个包装类,主要加了一个上限的范围判断。
public Index addIndex(Session session, String indexName, int indexId, IndexColumn[] cols, IndexType indexType, boolean create, String indexComment) { //... MVIndex index; int mainIndexColumn; //... if (mainIndexColumn != -1) { primaryIndex.setMainIndexColumn(mainIndexColumn); index = new MVDelegateIndex(this, indexId, indexName, primaryIndex, indexType); } else if (indexType.isSpatial()) { index = new MVSpatialIndex(session.getDatabase(), this, indexId, indexName, cols, indexType); } else { index = new MVSecondaryIndex(session.getDatabase(), this, indexId, indexName, cols, indexType); } //... return index; }可以看出,针对主键,会创建一个MVDelegateIndex,空间的创建MVSpatialIndex,其他情况创建MVSecondaryIndex。
+++++++++++++++++++++++++++++++++++++++++++
如本文存在任何侵权部分,请及时告知,我会第一时间删除!
转载本博客原创文章,请附上原文@cnblogs的网址!
QQ: 5854165 我的开源项目 欢迎大家一起交流编程架构技术&大数据技术! +++++++++++++++++++++++++++++++++++++++++++
如本文存在任何侵权部分,请及时告知,我会第一时间删除!
转载本博客原创文章,请附上原文@cnblogs的网址!
QQ: 5854165 我的开源项目 欢迎大家一起交流编程架构技术&大数据技术! +++++++++++++++++++++++++++++++++++++++++++