⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 btreeleaf.java

📁 用java语言简单实现数据库的初步功能
💻 JAVA
字号:
package simpledb.index.btree;import simpledb.file.Block;import simpledb.tx.Transaction;import simpledb.record.*;import simpledb.query.Constant;/** * An object that holds the contents of a B-tree leaf block. * @author Edward Sciore */public class BTreeLeaf {	private TableInfo md;	private Transaction tx;	private String filename;	private Constant searchkey;	private BTreePage contents;	private int currentslot;	/**	 * Opens a page to hold the specified leaf block.	 * The page is positioned immediately before the first record	 * having the specified search key (if any).	 * @param blk a reference to the disk block	 * @param md the metadata of the B-tree leaf file	 * @param searchkey the search key value	 * @param tx the calling transaction	 */	public BTreeLeaf(Block blk, TableInfo md, Constant searchkey, Transaction tx) {		this.md = md;		this.tx = tx;		this.searchkey = searchkey;		filename = blk.fileName();		contents = new BTreePage(blk, md, tx);		currentslot = contents.findSlotBefore(searchkey);	}	/**	 * Closes the leaf page.	 */	public void close() {		contents.close();	}	/**	 * Moves to the next leaf record having the 	 * previously-specified search key.	 * Returns false if there is no more such records.	 * @return false if there are no more leaf records for the search key	 */	public boolean next() {		currentslot++;		while (currentslot >= contents.getNumRecs())			if (!moveToOverflow())				return false;		return contents.getDataVal(currentslot).equals(searchkey);	}	/**	 * Returns the dataRID value of the current leaf record.	 * @return the dataRID of the current record	 */	public RID getDataRid() {		return contents.getDataRid(currentslot);	}	/**	 * Deletes the leaf record having the specified dataRID	 * @param datarid the dataRId whose record is to be deleted	 */	public void delete(RID datarid) {		while(next())			if(getDataRid().equals(datarid)) {				contents.delete(currentslot);				return;			}	}	/**	 * Inserts a new leaf record having the specified dataRID	 * and the previously-specified search key.	 * If the record does not fit in the page, then 	 * the page splits and the method returns the	 * directory entry for the new page;	 * otherwise, the method returns null.  	 * @param datarid the dataRID value of the new record	 * @return the directory entry of the newly-split page, if one exists.	 */	public DirEntry insert(RID datarid) {		currentslot++;		contents.insertLeaf(currentslot, searchkey, datarid);		if (!contents.isFull())			return null;		// else page is full, so split it		Block newblk = contents.appendNew(filename, -1);		BTreePage newpage = new BTreePage(newblk, md, tx);		Constant splitval = contents.split(newpage);		if (contents.getDataVal(0).equals(splitval)) {			// new page contains overflow; link them			newpage.setFlag(contents.getFlag());			contents.setFlag(newblk.number());		}		newpage.close();		return new DirEntry(splitval, newblk.number());	}	private boolean moveToOverflow() {		int blknum = contents.getFlag();		if (blknum < 0)			return false;		contents.close();		Block nextblk = new Block(filename, blknum);		contents = new BTreePage(nextblk, md, tx);		currentslot = 0;		return true;	}}

⌨️ 快捷键说明

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