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.db;
18  
19  
20  import org.apache.regexp.RE;
21  
22  import javax.naming.NamingEnumeration;
23  import javax.naming.NamingException;
24  import java.util.NoSuchElementException;
25  
26  
27  /***
28   * A NamingEnumeration over an Index which returns IndexRecords.
29   * 
30   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31   * @version $Rev: 159259 $
32   */
33  public class IndexEnumeration
34      implements NamingEnumeration
35  {
36      /*** */
37      private final RE re;
38      /*** */
39      private final IndexRecord tmp = new IndexRecord();
40      /*** */
41      private final IndexRecord returned = new IndexRecord();
42      /*** */
43      private final IndexRecord prefetched = new IndexRecord();
44      /*** */
45      private final boolean swapKeyVal;
46      /*** */
47      private final NamingEnumeration underlying;
48  
49      /*** */
50      private boolean hasMore = true;
51  
52  
53      // ------------------------------------------------------------------------
54      // C O N S T R U C T O R S
55      // ------------------------------------------------------------------------
56  
57  
58      /***
59       * TODO Domument me!
60       *
61       * @param list TODO
62       * @throws NamingException TODO
63       */
64      public IndexEnumeration( NamingEnumeration list ) throws NamingException
65      {
66          this( list, false, null );
67      }
68  
69  
70      /***
71       * TODO Domument me!
72       *
73       * @param list TODO
74       * @param swapKeyVal TODO
75       * @throws NamingException TODO
76       */
77      public IndexEnumeration( NamingEnumeration list, boolean swapKeyVal )
78          throws NamingException
79      {
80          this( list, swapKeyVal, null );
81      }
82  
83  
84      /***
85       * TODO Domument me!
86       *
87       * @param list TODO
88       * @param swapKeyVal TODO
89       * @param regex TODO
90       * @throws NamingException TODO
91       */
92      public IndexEnumeration( NamingEnumeration list, boolean swapKeyVal,
93                               RE regex )
94          throws NamingException
95      {
96          re = regex;
97          underlying = list;
98          this.swapKeyVal = swapKeyVal;
99  
100         if ( ! underlying.hasMore() ) 
101         {
102             hasMore = false;
103             return;
104         }
105 
106         prefetch();
107     }
108 
109 
110     // ------------------------------------------------------------------------
111     // NamingEnumeration Interface Methods 
112     // ------------------------------------------------------------------------
113 
114 
115     /***
116      * @see javax.naming.NamingEnumeration#next()
117      */
118     public Object next()
119         throws NamingException
120     {
121         returned.copy( prefetched );
122         prefetch();
123         return returned;
124     }
125     
126     
127     /***
128      * @see java.util.Enumeration#nextElement()
129      */
130     public Object nextElement()
131     {
132         try
133         {
134             return next();
135         }
136         catch ( NamingException ne )
137         {
138             throw new NoSuchElementException();
139         }
140     }
141 
142 
143     /***
144      * @see javax.naming.NamingEnumeration#hasMore()
145      */
146     public boolean hasMore()
147     {
148         return hasMore;
149     }
150 
151 
152     /***
153      * @see javax.naming.NamingEnumeration#hasMoreElements()
154      */
155     public boolean hasMoreElements()
156     {
157         return hasMore;
158     }
159 
160 
161     /***
162      * @see javax.naming.NamingEnumeration#close()
163      */
164     public void close() throws NamingException
165     {
166         hasMore = false;
167         underlying.close();
168     }
169 
170 
171     // ------------------------------------------------------------------------
172     // Private Methods 
173     // ------------------------------------------------------------------------
174 
175 
176     /***
177      * TODO Document me!
178      *
179      * @throws NamingException TODO
180      */
181     private void prefetch() throws NamingException
182     {
183         while ( underlying.hasMore() ) 
184         {
185             Tuple tuple = ( Tuple ) underlying.next();
186 
187             if ( swapKeyVal ) 
188             {
189                 tmp.setSwapped( tuple, null );
190             } 
191             else 
192             {
193                 tmp.setTuple( tuple, null );
194             }
195 
196             // If regex is null just transfer into prefetched from tmp record
197             // but if it is not then use it to match.  Successful match shorts
198             // while loop.
199             if ( null == re || re.match( ( String ) tmp.getIndexKey() ) ) 
200             {
201                 prefetched.copy( tmp );
202                 return;
203             }
204         }
205 
206         // If we got down here then cursor has been consumed without a match!
207         hasMore = false;
208     }
209 }