btpageformatter.java

来自「用java语言简单实现数据库的初步功能」· Java 代码 · 共 56 行

JAVA
56
字号
package simpledb.index.btree;

import static simpledb.file.Page.*;
import static simpledb.sql.Types.INTEGER;
import simpledb.file.Page;
import simpledb.buffer.PageFormatter;
import simpledb.record.TableInfo;

/**
 * An object that can format a page to look like an
 * empty B-tree block.
 * @author Edward Sciore
 */
public class BTPageFormatter implements PageFormatter {
	private TableInfo md;
	private int flag;

	/**
	 * Creates a formatter for a new page of the
	 * specified B-tree index.
	 * @param md the index's metadata
	 * @param flag the page's initial flag value
	 */
	public BTPageFormatter(TableInfo md, int flag) {
		this.md = md;
		this.flag = flag;
	}

	/** 
	 * Formats the page by initializing as many index-record slots
	 * as possible to have default values.
	 * Each integer field is given a value of 0, and
	 * each string field is given a value of "".
	 * The location that indicates the number of records
	 * in the page is also set to 0.
	 * @see simpledb.buffer.PageFormatter#format(simpledb.file.Page)
	 */
	public void format(Page page) {
		page.setInt(0, flag);
		page.setInt(INT_SIZE, 0);  // #records = 0
		int recsize = md.recordLength();
		for (int pos=2*INT_SIZE; pos+recsize<BLOCK_SIZE; pos += recsize)
			makeDefaultRecord(page, pos);
	}

	private void makeDefaultRecord(Page page, int pos) {
		for (String fldname : md.schema().fields()) {
			int offset = md.offset(fldname);
			if (md.schema().type(fldname) == INTEGER)
				page.setInt(pos + INT_SIZE + offset, 0);
			else
				page.setString(pos + INT_SIZE + offset, "");
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?