这次感觉比前几次都难,尤其卡在了iterator上,需要反复阅读上面的说明,说明在DbfileIterator上,。详细代码如下HeapFile.java
在文本编译器上删的sout测试语句,可能会带来一些错误,欢迎指正
public class HeapFile implements DbFile {
/**
* Constructs a heap file backed by the specified file.
*
* @param f
* the file that stores the on-disk backing store for this heap
* file.
*/
private File f;
private TupleDesc tupleDesc ;
private BufferPool bufferPool ;
private HeapPage heapPage;
private int Id;
List<HeapPage> pages= new ArrayList<>();
public HeapFile(File f, TupleDesc td) {
// some code goes here
this.f = f;
this.tupleDesc = td;
int i=0;
File file = getFile();
try {
FileInputStream fileInputStream = new FileInputStream(file);
byte[] data = HeapPage.createEmptyPageData();
int tableid = Database.getCatalog().getTableId(td,0);
if (tableid==-1){
DbFile dbFile ;
Database.getCatalog().addTable(tupleDesc);
}
//System.out.println(tableid);
int size = 0;
while ((size=fileInputStream.read(data))!=-1) {
HeapPageId heapPageId= new HeapPageId(tableid,i) ;
pages.add(new HeapPage(heapPageId,data,tupleDesc));
i++;
data = HeapPage.createEmptyPageData();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns the File backing this HeapFile on disk.
*
* @return the File backing this HeapFile on disk.
*/
public File getFile() {
return f;
// some code goes here
}
/**
* Returns an ID uniquely identifying this HeapFile. Implementation note:
* you will need to generate this tableid somewhere ensure that each
* HeapFile has a "unique id," and that you always return the same value for
* a particular HeapFile. We suggest hashing the absolute file name of the
* file underlying the heapfile, i.e. f.getAbsoluteFile().hashCode().
*
* @return an ID uniquely identifying this HeapFile.
*/
public int getId() {
// some code goes here
Id = f.getAbsoluteFile().hashCode();
return f.getAbsoluteFile().hashCode();
}
/**
* Returns the TupleDesc of the table stored in this DbFile.
*
* @return TupleDesc of this DbFile.
*/
public TupleDesc getTupleDesc() {
return tupleDesc;
// some code goes here
}
// see DbFile.java for javadocs
public Page readPage(PageId pid) {
// some code goes here
HeapPageId heapPageId= (HeapPageId) pid;
File file = getFile();
try {
FileInputStream fileInputStream = new FileInputStream(file);
byte[] data = new byte[fileInputStream.available()];
int size = 0;
while ((size=fileInputStream.read(data))!=-1) {
heapPage = new HeapPage(heapPageId,data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
return heapPage;
}
}
// see DbFile.java for javadocs
public void writePage(Page page) throws IOException {
// some code goes here
// not necessary for proj1
}
/**
* Returns the number of pages in this HeapFile.
*/
public int numPages() {
// some code goes here
return pages.size();
}
// see DbFile.java for javadocs
public ArrayList<Page> insertTuple(TransactionId tid, Tuple t)
throws DbException, IOException, TransactionAbortedException {
// some code goes here
bufferPool = new BufferPool(pages.size());
bufferPool.insertTuple(tid,Id,t);
return null;
// not necessary for proj1
}
// see DbFile.java for javadocs
public Page deleteTuple(TransactionId tid, Tuple t) throws DbException,
TransactionAbortedException {
// some code goes here
return null;
// not necessary for proj1
}
// see DbFile.java for javadocs
public DbFileIterator iterator(TransactionId tid) {
return new DbFileIterator() {
private Field field;
private int index=-1;//我没标准答案,自己想的
@Override
public void open() throws DbException, TransactionAbortedException {
index = 0;//这就是初始化
}
@Override
public boolean hasNext() throws DbException, TransactionAbortedException {
int count =0;
Tuple tuple = new Tuple(tupleDesc);
IntField intField = new IntField(0);
for (int i=0;i<numPages();i++){
if (pages.get(i)==null)break;
for (int j=0;j<pages.get(i).tuples.length;j++){
Tuple newT = new Tuple(tupleDesc);
if (!pages.get(i).tuples[j].tuple.equals(newT.tuple)){
count++;
}
}
}
return index<count&&index!=-1;
}
@Override
public Tuple next() throws DbException, TransactionAbortedException, NoSuchElementException {
int k=0;
int index1 = index;
index++;
for (int i=0;i<pages.size();i++){
for(int j=0;j<pages.get(i).tuples.length;j++){
if (k==index1) {
// System.out.println( pages.get(i).tuples[j]);
//System.out.println(i);
return pages.get(i).tuples[j];
}
k++;
}
}
throw new NoSuchElementException();
}
@Override
public void rewind() throws DbException, TransactionAbortedException {
}
@Override
public void close() {
index = -1;
}
};
}
}