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

📄 testheapfile.java

📁 数据库实现的一个源码包
💻 JAVA
字号:
package test.heapfile;

import java.io.File;
import java.io.IOException;

import neustore.heapfile.*;
import neustore.base.DBBuffer;
import neustore.base.DBIndex;
import neustore.base.LRUBuffer;
import neustore.base.IntKey;
import neustore.base.StringData;

/**
 * A class that tests the HeapFile.
 * We could define our own Key and Data. But here we use the pre-defined
 * {@link IntKey} and {@link StringData}.
 * 
 * @author Donghui Zhang <donghui@ccs.neu.edu>
 */
public class TestHeapFile {
	final int maxStringLength = 10;

	HeapFile TestCreate( DBBuffer buffer, String filename ) throws IOException {
		File file = new File(filename);
		file.delete();
		IntKey sampleKey = new IntKey(0);
		StringData sampleData = new StringData(maxStringLength, ""); 
		HeapFile hp = new HeapFile(buffer, filename, DBIndex.CREATE, sampleKey, sampleData);
		return hp;
	}
	
	void TestInsert( HeapFile hp ) throws IOException{
		hp.insert( new IntKey(10), new StringData(maxStringLength, "John"));
		hp.insert( new IntKey(30), new StringData(maxStringLength, "Adams"));
		hp.insert( new IntKey(20), new StringData(maxStringLength, "Bill"));
		hp.insert( new IntKey(40), new StringData(maxStringLength, "George"));
		hp.insert( new IntKey(60), new StringData(maxStringLength, "Tom"));
		hp.insert( new IntKey(70), new StringData(maxStringLength, "Jack"));		
	}

	void TestSearch( HeapFile hp ) throws IOException {
		StringData sd = (StringData)hp.search( new IntKey(60) );
		if ( sd != null ) {
			System.out.println("Good, key 60 corresponds to " + sd.string );
		}
		else {
			System.out.println("Strange, key=60 was not found.");
		}
		sd = (StringData)hp.search( new IntKey(35) );
		if ( sd != null ) {
			System.out.println("Strange, key=35 was not inserted but found.");
		}
		else {
			System.out.println("Good, key=35 was not inserted and not found.");
		}
	}
	
	public static void main(String[]args) throws IOException {
		System.out.println( "***** Creating databases *****" );		
		String filename1 = "TESTHEAPFILE1";
		String filename2 = "TESTHEAPFILE2";	
		LRUBuffer buffer = new LRUBuffer( 2, 50 );
		TestHeapFile test = new TestHeapFile();		
		HeapFile hp1 = test.TestCreate( buffer, filename1 );
		HeapFile hp2 = test.TestCreate( buffer, filename2 );
		
		System.out.println( "***** Inserting keys *****");
		test.TestInsert(hp1);
		test.TestInsert(hp2);
		
		System.out.println( "***** Searching keys *****" );
		test.TestSearch(hp1);
		test.TestSearch(hp2);
		
		System.out.println( "***** Index information *****" );
		hp1.printInformation( );

		System.out.println( "***** Deleting key=60 from hp1 *****" );
		hp1.delete( new IntKey(60) );

		System.out.println( "***** Searching keys 60 *****" );
		StringData sd = (StringData)hp1.search( new IntKey(60) );
		if ( sd != null ) {
			System.out.println("Strange, key=60 was deleted but still found." );
		}
		else {
			System.out.println("Good, key=60 was deleted and thus not found.");
		}
		
		System.out.println( "***** Index information *****" );
		hp1.printInformation( );
		
		hp1.close();
		hp2.close();
	}
}

⌨️ 快捷键说明

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