View Javadoc

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.server.xdbm.*;
24  import org.apache.directory.server.core.cursor.ClosureMonitor;
25  import org.apache.directory.server.core.cursor.Cursor;
26  import org.apache.directory.server.core.cursor.CursorIterator;
27  
28  import java.util.Iterator;
29  
30  
31  /**
32   * A Cursor which adapts an underlying Tuple based Cursor to one which returns
33   * IndexEntry objects rather than tuples.
34   *
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   * @version $Rev$
37   */
38  public class IndexCursorAdaptor<K,O> implements IndexCursor<K,O>
39  {
40      @SuppressWarnings("unchecked")
41      final Cursor<Tuple> wrappedCursor;
42      final ForwardIndexEntry<K, O> forwardEntry;
43      final ReverseIndexEntry<K, O> reverseEntry;
44  
45  
46      /**
47       * Creates an IndexCursorAdaptor which wraps and adapts a Cursor from a table to
48       * one which returns an IndexEntry.
49       *
50       * @param wrappedCursor the Cursor being adapted
51       * @param forwardIndex true for a cursor over a forward index, false for
52       * one over a reverse index
53       */
54      @SuppressWarnings("unchecked")
55      public IndexCursorAdaptor( Cursor<Tuple> wrappedCursor, boolean forwardIndex )
56      {
57          this.wrappedCursor = wrappedCursor;
58          if ( forwardIndex )
59          {
60              forwardEntry = new ForwardIndexEntry<K,O>();
61              reverseEntry = null;
62          }
63          else
64          {
65              forwardEntry = null;
66              reverseEntry = new ReverseIndexEntry<K, O>();
67          }
68      }
69  
70  
71      public boolean available()
72      {
73          return wrappedCursor.available();
74      }
75  
76  
77      @SuppressWarnings("unchecked")
78      public void beforeValue( Long id, K key ) throws Exception
79      {
80          if ( wrappedCursor instanceof TupleCursor )
81          {
82              ( ( TupleCursor ) wrappedCursor ).beforeValue( key, id );
83          }
84      }
85  
86  
87      @SuppressWarnings("unchecked")
88      public void afterValue( Long id, K key ) throws Exception
89      {
90          if ( wrappedCursor instanceof TupleCursor )
91          {
92              ( ( TupleCursor ) wrappedCursor ).afterValue( key, id );
93          }
94      }
95  
96  
97      public void before( IndexEntry<K, O> element ) throws Exception
98      {
99          wrappedCursor.before( element.getTuple() );
100     }
101 
102 
103     public void after( IndexEntry<K, O> element ) throws Exception
104     {
105         wrappedCursor.after( element.getTuple() );
106     }
107 
108 
109     public void beforeFirst() throws Exception
110     {
111         wrappedCursor.beforeFirst();
112     }
113 
114 
115     public void afterLast() throws Exception
116     {
117         wrappedCursor.afterLast();
118     }
119 
120 
121     public boolean first() throws Exception
122     {
123         return wrappedCursor.first();
124     }
125 
126 
127     public boolean last() throws Exception
128     {
129         return wrappedCursor.last();
130     }
131 
132 
133     public boolean isClosed() throws Exception
134     {
135         return wrappedCursor.isClosed();
136     }
137 
138 
139     public boolean previous() throws Exception
140     {
141         return wrappedCursor.previous();
142     }
143 
144 
145     public boolean next() throws Exception
146     {
147         return wrappedCursor.next();
148     }
149 
150 
151     @SuppressWarnings("unchecked")
152     public IndexEntry<K, O> get() throws Exception
153     {
154         if ( forwardEntry != null )
155         {
156             Tuple<K,Long> tuple = wrappedCursor.get();
157             forwardEntry.setTuple( tuple, null );
158             return forwardEntry;
159         }
160         else
161         {
162             Tuple<Long,K> tuple = wrappedCursor.get();
163             reverseEntry.setTuple( tuple, null );
164             return reverseEntry;
165         }
166     }
167 
168 
169     public boolean isElementReused()
170     {
171         return true;
172     }
173     
174     
175     public final void setClosureMonitor( ClosureMonitor monitor )
176     {
177         wrappedCursor.setClosureMonitor( monitor );
178     }
179 
180 
181     public void close() throws Exception
182     {
183         wrappedCursor.close();
184     }
185 
186 
187     public void close( Exception reason ) throws Exception
188     {
189         wrappedCursor.close( reason );
190     }
191 
192 
193     public Iterator<IndexEntry<K, O>> iterator()
194     {
195         return new CursorIterator<IndexEntry<K,O>>( this );
196     }
197 }