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。
  • Index具有多个子类:

  • 一个MVTable会包含多个index:

  • MVTable会默认创建一个MVPrimaryIndex,它包含所有列:
    	void init(Session session)
    	{
    		primaryIndex = new MVPrimaryIndex(session.getDatabase(), this, getId(), IndexColumn.wrap(getColumns()),
    				IndexType.createScan(true));
    		indexes.add(primaryIndex);
    	}
  • MVPrimaryIndex变相的保存了table的所有row:
    	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的一个包装类,主要加了一个上限的范围判断。

  • 手动创建Index的方法为MVTable.addIndex():
  • 	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。
  • 将table的rows处理成MVPrimaryIndex,将主键封装成MVDelegateIndex<====这点确实让人不解。。。以后有了新的答案再来解惑!

你可能感兴趣的:(h2database源码浅析:MVTable与MVIndex)