KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > btreeimpl > btreeindex > BtreeKeySet


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.persistence.btreeimpl.btreeindex;
20
21 import org.netbeans.mdr.persistence.*;
22 import java.util.*;
23 import java.io.*;
24
25 /**
26  * Read-only Set view of the distinct keys contained in a Btree.
27  *
28  * @author Dana Bergen
29  * @version 1.0
30  */

31 public class BtreeKeySet extends AbstractSet {
32
33     private Btree btree;
34
35     BtreeKeySet(Btree btree) {
36         this.btree = btree;
37     }
38
39     public int size() {
40     int count = 0;
41     BtreeKeyIterator i = new BtreeKeyIterator();
42     while (i.hasNext()) {
43         i.nextKey();
44         count++;
45     }
46     return count;
47     }
48     
49     public boolean isEmpty() {
50         return !iterator().hasNext();
51     }
52
53     public Iterator iterator() {
54         return new BtreeKeyIterator();
55     }
56
57     /**
58      * Iterator over a BtreeKeySet
59      */

60     public class BtreeKeyIterator extends Object JavaDoc implements Iterator {
61
62     protected SearchResult current;
63     protected BtreePageSource pageSource;
64     protected int modCount;
65
66     BtreeKeyIterator() {
67         try {
68             btree.beginRead();
69         modCount = btree.modCount;
70         pageSource = btree.pageSource;
71         current = btree.getFirst();
72         if (current.entryNum >= 0) {
73             current.entryNum--;
74         }
75         } catch (StorageException e) {
76             throw new RuntimeStorageException(e);
77         } finally {
78             btree.endRead();
79         }
80     }
81
82     /**
83      * Tests whether there are any more elements to return
84      *
85      * @return true if a call to next() would succeed
86      */

87     public boolean hasNext() {
88
89         boolean result;
90
91         try {
92             btree.beginRead();
93         checkModCount();
94         if (!BtreePage.hasNext(null, current)) {
95             pageSource.unpinPage(current.page);
96             current.page = null;
97             result = false;
98         } else {
99             result = true;
100         }
101         } catch (StorageException se) {
102         throw new RuntimeStorageException(se);
103         } finally {
104             btree.endRead();
105         }
106         return result;
107     }
108
109     /**
110      * Gets the next distinct key in the btree. If this is on a different
111      * page from the previous value, the old page will be unpinned.
112      *
113      * @return The next distinct key in the btree
114      *
115      * @exception NoSuchElementException If there was any error or if
116      * there are no more records
117      */

118     public Object JavaDoc next() throws NoSuchElementException {
119
120         byte[] key = nextKey();
121         return btree.keyInfo.fromBuffer(key);
122     }
123
124     private byte[] nextKey() throws NoSuchElementException {
125
126         BtreePage oldPage;
127         byte[] key;
128         
129         if (current.page == null) {
130         throw new NoSuchElementException();
131         }
132         try {
133             btree.beginRead();
134         checkModCount();
135         oldPage = current.page;
136         BtreePage.getNext(null, current);
137         if (current.page != oldPage) {
138             pageSource.unpinPage(oldPage);
139         }
140         if (current.matched) {
141             key = current.page.getKey(current.entryNum);
142         } else {
143             throw new NoSuchElementException();
144         }
145         /* position ourselves at the next distinct key */
146         while (BtreePage.hasNext(key, current)) {
147             oldPage = current.page;
148             BtreePage.getNext(null, current);
149             if (current.page != oldPage) {
150             pageSource.unpinPage(oldPage);
151             }
152         }
153         } catch (StorageException e) {
154         throw new RuntimeStorageException(e);
155         } finally {
156             btree.endRead();
157         }
158         return key;
159     }
160
161     private void checkModCount() {
162         if (btree.modCount > modCount) {
163             throw new ConcurrentModificationException("Index "
164                 + btree.getName()
165                 + " has been modified since iterator was created.");
166         }
167     }
168
169     /*
170      * Unpin the last page.
171      */

172     protected void finalize() {
173         if (current.page != null) {
174         pageSource.unpinPage(current.page);
175         current.page = null;
176         }
177     }
178
179     /**
180      * This is not supported.
181      *
182      * @exception UnsupportedOperationException Always thrown.
183      */

184     public void remove() throws UnsupportedOperationException JavaDoc {
185         throw new UnsupportedOperationException JavaDoc();
186     }
187     }
188 }
189
Popular Tags