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