/*
* Copyright (C) 2006 http://www.chaidb.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
package org.chaidb.db.index.btree.bufmgr;
import java.util.Iterator;
import java.util.LinkedHashMap;
/**
* This class manages a doubly linked list, with head and tail. It's
* actually implemented as a circular list, with a single head pointer,
* and the tail is <code>head.prev</code>.<p>
*/
class DList {
private LinkedHashMap map;
/**
* Construct an empty DList.
*/
DList() {
map = new LinkedHashMap(4096);
}
public synchronized Iterator getIterator() {
return map.values().iterator();
}
/**
* This method returns a string containing the display representation
* of this list.
*/
public synchronized String toString() {
return map.toString();
}
/* addFront */
public synchronized void addFront(BufferedPageInfo bpi) {
map.put(bpi, bpi);
}
/**
* Remove and return the item at the back of the list.
*
* @return the item at the tail of the list
*/
public synchronized BufferedPageInfo popBack() {
BufferedPageInfo bpi;
Iterator iter = map.values().iterator();
if (iter.hasNext()) {
bpi = (BufferedPageInfo) iter.next();
map.remove(bpi);
return bpi;
} else {
return null;
}
}
/**
* Remove item.
*
* @param pageInfo
*/
public synchronized void removeItem(BufferedPageInfo pageInfo) {
map.remove(pageInfo);
}
/**
* Gets the size of double linked list.
*
* @return The size of double linked list.
*/
public synchronized int getSize() {
return map.size();
}
/**
* Just for test.
* @param args
*/
/*
public static void main(String args[]){
DList cleanList = new DList();
DList dirtyList = new DList();
BufferedPageInfo bpi[] = new BufferedPageInfo[10];
for (int i = 0; i < 5; i ++){
bpi[i] = new BufferedPageInfo(0, i, new PageNumber(0,1, i));
dirtyList.addFront(bpi[i]);
}
// dirtyList.show(System.err);
System.err.println(dirtyList.toString());
// for (int i = 0; i < 5; i ++){
// bpi[i] = new BufferedPageInfo(0, i, new PageNumber(0,1, i));
// dirtyList.popBack();
// }
//end
// for (int j = 0; j < 5; j ++){
// bpi[j+5] = new BufferedPageInfo(0, j+5, new PageNumber(0,1, j+5));
// dirtyList.addFront(bpi[j+ 5]);
// }
dirtyList.removeItem(bpi[2]);
dirtyList.popBack();
dirtyList.removeItem(bpi[1]);
dirtyList.removeItem(bpi[3]);
// dirtyList.removeItem(bpi[4]);
dirtyList.popBack();
// dirtyList.popBack();
BufferedPageInfo tmpbpi = null;
for (int k = 0; k < 5; k ++){
tmpbpi = dirtyList.popBack();
cleanList.addFront(tmpbpi);
}
}
*/
}