|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.mdr.persistence.btreeimpl.btreeindex; import org.netbeans.mdr.persistence.*; import java.util.*; import java.io.*; /** * Read-only Set view of the distinct keys contained in a Btree. * * @author Dana Bergen * @version 1.0 */ public class BtreeKeySet extends AbstractSet { private Btree btree; BtreeKeySet(Btree btree) { this.btree = btree; } public int size() { int count = 0; BtreeKeyIterator i = new BtreeKeyIterator(); while (i.hasNext()) { i.nextKey(); count++; } return count; } public boolean isEmpty() { return !iterator().hasNext(); } public Iterator iterator() { return new BtreeKeyIterator(); } /** * Iterator over a BtreeKeySet */ public class BtreeKeyIterator extends Object implements Iterator { protected SearchResult current; protected BtreePageSource pageSource; protected int modCount; BtreeKeyIterator() { try { btree.beginRead(); modCount = btree.modCount; pageSource = btree.pageSource; current = btree.getFirst(); if (current.entryNum >= 0) { current.entryNum--; } } catch (StorageException e) { throw new RuntimeStorageException(e); } finally { btree.endRead(); } } /** * Tests whether there are any more elements to return * * @return true if a call to next() would succeed */ public boolean hasNext() { boolean result; try { btree.beginRead(); checkModCount(); if (!BtreePage.hasNext(null, current)) { pageSource.unpinPage(current.page); current.page = null; result = false; } else { result = true; } } catch (StorageException se) { throw new RuntimeStorageException(se); } finally { btree.endRead(); } return result; } /** * Gets the next distinct key in the btree. If this is on a different * page from the previous value, the old page will be unpinned. * * @return The next distinct key in the btree * * @exception NoSuchElementException If there was any error or if * there are no more records */ public Object next() throws NoSuchElementException { byte[] key = nextKey(); return btree.keyInfo.fromBuffer(key); } private byte[] nextKey() throws NoSuchElementException { BtreePage oldPage; byte[] key; if (current.page == null) { throw new NoSuchElementException(); } try { btree.beginRead(); checkModCount(); oldPage = current.page; BtreePage.getNext(null, current); if (current.page != oldPage) { pageSource.unpinPage(oldPage); } if (current.matched) { key = current.page.getKey(current.entryNum); } else { throw new NoSuchElementException(); } /* position ourselves at the next distinct key */ while (BtreePage.hasNext(key, current)) { oldPage = current.page; BtreePage.getNext(null, current); if (current.page != oldPage) { pageSource.unpinPage(oldPage); } } } catch (StorageException e) { throw new RuntimeStorageException(e); } finally { btree.endRead(); } return key; } private void checkModCount() { if (btree.modCount > modCount) { throw new ConcurrentModificationException("Index " + btree.getName() + " has been modified since iterator was created."); } } /* * Unpin the last page. */ protected void finalize() { if (current.page != null) { pageSource.unpinPage(current.page); current.page = null; } } /** * This is not supported. * * @exception UnsupportedOperationException Always thrown. */ public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.