View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.partition.impl.btree;
18  
19  
20  import java.util.NoSuchElementException;
21  
22  import javax.naming.NamingEnumeration;
23  import javax.naming.NamingException;
24  
25  import org.apache.regexp.RE;
26  
27  
28  /***
29   * A NamingEnumeration over an Index which returns IndexRecords.
30   * 
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   * @version $Rev: 264732 $
33   */
34  public class IndexEnumeration
35      implements NamingEnumeration
36  {
37      /*** */
38      private final RE re;
39      /*** */
40      private final IndexRecord tmp = new IndexRecord();
41      /*** */
42      private final IndexRecord returned = new IndexRecord();
43      /*** */
44      private final IndexRecord prefetched = new IndexRecord();
45      /*** */
46      private final boolean swapKeyVal;
47      /*** */
48      private final NamingEnumeration underlying;
49  
50      /*** */
51      private boolean hasMore = true;
52  
53  
54      // ------------------------------------------------------------------------
55      // C O N S T R U C T O R S
56      // ------------------------------------------------------------------------
57  
58  
59      public IndexEnumeration( NamingEnumeration list ) throws NamingException
60      {
61          this( list, false, null );
62      }
63  
64  
65      public IndexEnumeration( NamingEnumeration list, boolean swapKeyVal )
66          throws NamingException
67      {
68          this( list, swapKeyVal, null );
69      }
70  
71  
72      public IndexEnumeration( NamingEnumeration list, boolean swapKeyVal,
73                               RE regex )
74          throws NamingException
75      {
76          re = regex;
77          underlying = list;
78          this.swapKeyVal = swapKeyVal;
79  
80          if ( ! underlying.hasMore() ) 
81          {
82              hasMore = false;
83              return;
84          }
85  
86          prefetch();
87      }
88  
89  
90      // ------------------------------------------------------------------------
91      // NamingEnumeration Interface Methods 
92      // ------------------------------------------------------------------------
93  
94  
95      /***
96       * @see javax.naming.NamingEnumeration#next()
97       */
98      public Object next()
99          throws NamingException
100     {
101         returned.copy( prefetched );
102         prefetch();
103         return returned;
104     }
105     
106     
107     /***
108      * @see java.util.Enumeration#nextElement()
109      */
110     public Object nextElement()
111     {
112         try
113         {
114             return next();
115         }
116         catch ( NamingException ne )
117         {
118             throw new NoSuchElementException();
119         }
120     }
121 
122 
123     /***
124      * @see javax.naming.NamingEnumeration#hasMore()
125      */
126     public boolean hasMore()
127     {
128         return hasMore;
129     }
130 
131 
132     /***
133      * @see javax.naming.NamingEnumeration#hasMoreElements()
134      */
135     public boolean hasMoreElements()
136     {
137         return hasMore;
138     }
139 
140 
141     /***
142      * @see javax.naming.NamingEnumeration#close()
143      */
144     public void close() throws NamingException
145     {
146         hasMore = false;
147         underlying.close();
148     }
149 
150 
151     // ------------------------------------------------------------------------
152     // Private Methods 
153     // ------------------------------------------------------------------------
154 
155 
156     private void prefetch() throws NamingException
157     {
158         while ( underlying.hasMore() ) 
159         {
160             Tuple tuple = ( Tuple ) underlying.next();
161 
162             if ( swapKeyVal ) 
163             {
164                 tmp.setSwapped( tuple, null );
165             } 
166             else 
167             {
168                 tmp.setTuple( tuple, null );
169             }
170 
171             // If regex is null just transfer into prefetched from tmp record
172             // but if it is not then use it to match.  Successful match shorts
173             // while loop.
174             if ( null == re || re.match( ( String ) tmp.getIndexKey() ) ) 
175             {
176                 prefetched.copy( tmp );
177                 return;
178             }
179         }
180 
181         // If we got down here then cursor has been consumed without a match!
182         hasMore = false;
183     }
184 }