1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
33
34
35
36
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
48
49
50
51
52
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 }