};
}
try {
return new SIPParallelIterator<java.util.Map.Entry<K, V>, K>() {
private InputStream in =
new PageInputStream(DBBPTree.this.firstLeafPage,
DBBPTree.this.pageManager);
{
this.innerNodes = new LinkedList<Tuple<K, InputStream>>();
InputHelper.readLuposBoolean(this.in);
}
private List<Tuple<K, InputStream>> innerNodes;
private int entrynumber = 0;
private K lastKey = null;
private V lastValue = null;
@Override
public boolean hasNext() {
return (this.entrynumber < DBBPTree.this.size());
}
private java.util.Map.Entry<K, V> getFirst(
final int filename, final K k) {
if (filename < 0) {
return null;
}
try {
final InputStream in_local = new PageInputStream(filename, DBBPTree.this.pageManager);
final boolean leaf = InputHelper.readLuposBoolean(in_local);
if (leaf) { // leaf node reached!
this.lastKey = null;
this.lastValue = null;
while (true) {
final DBBPTreeEntry<K, V> e = DBBPTree.this.getNextLeafEntry(in_local, this.lastKey, this.lastValue);
if (e == null || e.key == null) {
in_local.close();
this.close();
return null;
}
final K key = e.key;
this.lastKey = key;
this.lastValue = e.value;
final int compare = DBBPTree.this.comparator
.compare(key, k);
if (compare == 0) {
this.in = in_local;
return new MapEntry<K, V>(
e.key, e.value);
} else if (compare > 0) {
this.in = in_local;
return new MapEntry<K, V>(
e.key, e.value);
}
}
} else {
K lastKey = null;
while (true) {
final Tuple<K, Integer> nextEntry = DBBPTree.this.getNextInnerNodeEntry(lastKey, in_local);
if (nextEntry == null || nextEntry.getSecond() <= 0) {
in_local.close();
this.close();
return null;
}
lastKey = nextEntry.getFirst();
if (nextEntry.getFirst() == null) {
this.innerNodes
.add(new Tuple<K, InputStream>(null, in_local));
return this.getFirst(nextEntry
.getSecond(), k);
}
final int compare = DBBPTree.this.comparator
.compare(nextEntry
.getFirst(), k);
if (compare >= 0) {
this.innerNodes
.add(new Tuple<K, InputStream>(nextEntry.getFirst(), in_local));
return this.getFirst(nextEntry.getSecond(), k);
}
}
}
} catch (final FileNotFoundException e) {
e.printStackTrace();
System.err.println(e);
} catch (final IOException e) {
System.err.println("filename:"+filename);
e.printStackTrace();
System.err.println(e);
}
return null;
}
private java.util.Map.Entry<K, V> getFirstUsingCache(
final int index, final K kkey) {
if (index < 0) {
return null;
}
try {
if (this.innerNodes.size() <= index) {
this.close();
return null;
// close();
// innerNodes.clear();
// return getFirst(rootFilename,
// triplekey);
}
final Tuple<K, InputStream> current = this.innerNodes.get(index);
final InputStream in = current
.getSecond();
K lastKey = current.getFirst();
if (lastKey == null
|| DBBPTree.this.comparator
.compare(lastKey, kkey) >= 0) {
return this.getFirstUsingCache(index + 1,
kkey);
}
while (this.innerNodes.size() > index + 1) {
final Tuple<K, InputStream> toBeDeleted = this.innerNodes.remove(this.innerNodes.size() - 1);
try {
toBeDeleted.getSecond().close();
} catch (final IOException e) {
e.printStackTrace();
System.err.println(e);
}
}
while (true) {
final Tuple<K, Integer> nextEntry = DBBPTree.this.getNextInnerNodeEntry(lastKey, in);
if (nextEntry == null || nextEntry.getSecond() <= 0) {
in.close();
this.close();
return null;
}
lastKey = nextEntry.getFirst();
if (nextEntry.getFirst() == null) {
current.setFirst(null);
return this.getFirst(nextEntry
.getSecond(), kkey);
}
final int compare = DBBPTree.this.comparator.compare(
nextEntry.getFirst(), kkey);
if (compare >= 0) {
current.setFirst(nextEntry
.getFirst());
return this.getFirst(nextEntry
.getSecond(), kkey);
}
}
} catch (final FileNotFoundException e) {
e.printStackTrace();
System.err.println(e);
} catch (final IOException e) {
e.printStackTrace();
System.err.println(e);
}
return null;
}
@Override
public java.util.Map.Entry<K, V> next() {
if(!this.hasNext()) {
return null;
}
try {
final DBBPTreeEntry<K, V> e = DBBPTree.this.getNextLeafEntry(this.in, this.lastKey, this.lastValue);
if (e != null) {
if (e.key == null) {
if (e.filenameOfNextLeafNode >= 0) {
this.in.close();
try{
this.in =
new PageInputStream(
e.filenameOfNextLeafNode,
DBBPTree.this.pageManager);
InputHelper.readLuposBoolean(this.in);
this.lastKey = null;
this.lastValue = null;
return this.next();
} catch(final Exception e1){
System.err.println(e1);
e1.printStackTrace();
return null;
}
}
} else {
this.lastKey = e.key;
this.lastValue = e.value;
this.entrynumber++;
return new MapEntry<K, V>(e.key,
e.value);
}
}
} catch (final FileNotFoundException e1) {
System.err.println(e1);
e1.printStackTrace();
} catch (final IOException e1) {
System.err.println(e1);
e1.printStackTrace();
}
return null;
}
@Override
public void remove() {
throw (new UnsupportedOperationException(
"This iterator is ReadOnly."));
}
@Override
protected void finalize() throws Throwable {
try {
this.in.close();
} finally {
super.finalize();
}
}
@Override
public void close() {
for (final Tuple<K, InputStream> tuple : this.innerNodes) {
try {
tuple.getSecond().close();
} catch (final IOException e) {
}
}
try {
this.in.close();
} catch (final IOException e) {
System.err.println(e);
e.printStackTrace();
}
}
private java.util.Map.Entry<K, V> getNext(final K k) {
try {
final DBBPTreeEntry<K, V> e = DBBPTree.this.getNextLeafEntry(this.in, this.lastKey, this.lastValue);
if (e != null) {
if (e.key == null) {
if (e.filenameOfNextLeafNode >= 0) {
this.in.close();
this.in =
new PageInputStream(
e.filenameOfNextLeafNode,
DBBPTree.this.pageManager);
InputHelper.readLuposBoolean(this.in);
this.lastKey = null;
this.lastValue = null;