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.jdbm;
21
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.net.URI;
26
27 import jdbm.RecordManager;
28 import jdbm.helper.ByteArraySerializer;
29
30 import org.apache.directory.api.ldap.model.cursor.Cursor;
31 import org.apache.directory.api.ldap.model.cursor.CursorException;
32 import org.apache.directory.api.ldap.model.cursor.EmptyCursor;
33 import org.apache.directory.api.ldap.model.cursor.Tuple;
34 import org.apache.directory.api.ldap.model.exception.LdapException;
35 import org.apache.directory.api.ldap.model.exception.LdapOtherException;
36 import org.apache.directory.api.ldap.model.schema.AttributeType;
37 import org.apache.directory.api.ldap.model.schema.MatchingRule;
38 import org.apache.directory.api.ldap.model.schema.SchemaManager;
39 import org.apache.directory.api.ldap.model.schema.comparators.SerializableComparator;
40 import org.apache.directory.api.ldap.model.schema.comparators.UuidComparator;
41 import org.apache.directory.server.core.api.partition.PartitionTxn;
42 import org.apache.directory.server.core.partition.impl.btree.IndexCursorAdaptor;
43 import org.apache.directory.server.i18n.I18n;
44 import org.apache.directory.server.xdbm.AbstractIndex;
45 import org.apache.directory.server.xdbm.IndexEntry;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49
50
51
52
53
54
55 public class JdbmIndex<K> extends AbstractIndex<K, String>
56 {
57
58 private static final Logger LOG = LoggerFactory.getLogger( JdbmIndex.class );
59
60
61 public static final int DEFAULT_DUPLICATE_LIMIT = 512;
62
63
64 public static final String FORWARD_BTREE = "_forward";
65
66
67 public static final String REVERSE_BTREE = "_reverse";
68
69
70
71
72
73
74 protected JdbmTable<K, String> forward;
75
76
77
78
79
80
81 protected JdbmTable<String, K> reverse;
82
83
84
85
86 protected RecordManager recMan;
87
88
89
90
91 protected int numDupLimit = DEFAULT_DUPLICATE_LIMIT;
92
93
94 protected File wkDirPath;
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 public JdbmIndex( String attributeId, boolean withReverse )
125 {
126 super( attributeId, withReverse );
127
128 initialized = false;
129 }
130
131
132
133
134
135
136
137
138
139
140
141 public void init( RecordManager recMan, SchemaManager schemaManager, AttributeType attributeType )
142 throws LdapException, IOException
143 {
144 LOG.debug( "Initializing an Index for attribute '{}'", attributeType.getName() );
145
146 this.attributeType = attributeType;
147
148 if ( attributeId == null )
149 {
150 setAttributeId( attributeType.getName() );
151 }
152
153
154
155
156 String cacheSizeVal = System.getProperty( "jdbm.recman.cache.size", "100" );
157
158 int recCacheSize = Integer.parseInt( cacheSizeVal );
159
160 LOG.info( "Setting CacheRecondManager's cache size to {}", recCacheSize );
161
162 this.recMan = recMan;
163
164 try
165 {
166 initTables( schemaManager );
167 }
168 catch ( IOException e )
169 {
170
171 close( null );
172 throw e;
173 }
174
175 initialized = true;
176 }
177
178
179
180
181
182
183
184
185
186 private void initTables( SchemaManager schemaManager ) throws IOException
187 {
188 SerializableComparator<K> comp;
189
190 MatchingRule mr = attributeType.getEquality();
191
192 if ( mr == null )
193 {
194 throw new IOException( I18n.err( I18n.ERR_574, attributeType.getName() ) );
195 }
196
197 comp = new SerializableComparator<>( mr.getOid() );
198
199
200
201
202
203
204 UuidComparator.INSTANCE.setSchemaManager( schemaManager );
205 comp.setSchemaManager( schemaManager );
206
207 if ( mr.getSyntax().isHumanReadable() )
208 {
209 forward = new JdbmTable<>( schemaManager, attributeType.getOid() + FORWARD_BTREE, numDupLimit,
210 recMan,
211 comp, UuidComparator.INSTANCE, StringSerializer.INSTANCE, UuidSerializer.INSTANCE );
212 }
213 else
214 {
215 forward = new JdbmTable<>( schemaManager, attributeType.getOid() + FORWARD_BTREE, numDupLimit,
216 recMan,
217 comp, UuidComparator.INSTANCE, new ByteArraySerializer(), UuidSerializer.INSTANCE );
218 }
219
220
221
222
223
224
225
226 if ( withReverse )
227 {
228 if ( attributeType.isSingleValued() )
229 {
230 reverse = new JdbmTable<>( schemaManager, attributeType.getOid() + REVERSE_BTREE, recMan,
231 UuidComparator.INSTANCE, UuidSerializer.INSTANCE, null );
232 }
233 else
234 {
235 reverse = new JdbmTable<>( schemaManager, attributeType.getOid() + REVERSE_BTREE, numDupLimit,
236 recMan,
237 UuidComparator.INSTANCE, comp, UuidSerializer.INSTANCE, null );
238 }
239 }
240 }
241
242
243
244
245
246
247
248
249
250
251
252 public int getNumDupLimit()
253 {
254 return numDupLimit;
255 }
256
257
258
259
260
261
262
263
264 public void setNumDupLimit( int numDupLimit )
265 {
266 protect( "numDupLimit" );
267 this.numDupLimit = numDupLimit;
268 }
269
270
271
272
273
274
275
276
277 public void setWkDirPath( URI wkDirPath )
278 {
279 protect( "wkDirPath" );
280 this.wkDirPath = new File( wkDirPath );
281 }
282
283
284
285
286
287
288
289
290 public URI getWkDirPath()
291 {
292 return wkDirPath != null ? wkDirPath.toURI() : null;
293 }
294
295
296
297
298
299
300
301
302 public long count( PartitionTxn partitionTxn ) throws LdapException
303 {
304 return forward.count( partitionTxn );
305 }
306
307
308
309
310
311 public long count( PartitionTxn partitionTxn, K attrVal ) throws LdapException
312 {
313 return forward.count( partitionTxn, attrVal );
314 }
315
316
317
318
319
320 @Override
321 public long greaterThanCount( PartitionTxn partitionTxn, K attrVal ) throws LdapException
322 {
323 return forward.greaterThanCount( partitionTxn, attrVal );
324 }
325
326
327
328
329
330 @Override
331 public long lessThanCount( PartitionTxn partitionTxn, K attrVal ) throws LdapException
332 {
333 return forward.lessThanCount( partitionTxn, attrVal );
334 }
335
336
337
338
339
340
341
342
343
344 public String forwardLookup( PartitionTxn partitionTxn, K attrVal ) throws LdapException
345 {
346 return forward.get( partitionTxn, attrVal );
347 }
348
349
350
351
352
353 public K reverseLookup( PartitionTxn partitionTxn, String id ) throws LdapException
354 {
355 if ( withReverse )
356 {
357 return reverse.get( partitionTxn, id );
358 }
359 else
360 {
361 return null;
362 }
363 }
364
365
366
367
368
369
370
371
372
373 public synchronized void add( PartitionTxn partitionTxn, K attrVal, String id ) throws LdapException
374 {
375
376 forward.put( partitionTxn, attrVal, id );
377
378 if ( withReverse )
379 {
380 reverse.put( partitionTxn, id, attrVal );
381 }
382 }
383
384
385
386
387
388 public synchronized void drop( PartitionTxn partitionTxn, K attrVal, String id ) throws LdapException
389 {
390
391 if ( forward.has( partitionTxn, attrVal, id ) )
392 {
393 forward.remove( partitionTxn, attrVal, id );
394
395 if ( withReverse )
396 {
397 reverse.remove( partitionTxn, id, attrVal );
398 }
399 }
400 }
401
402
403
404
405
406 public void drop( PartitionTxn partitionTxn, String entryId ) throws LdapException
407 {
408 if ( withReverse )
409 {
410 if ( isDupsEnabled() )
411 {
412
413
414 Cursor<Tuple<String, K>> values = reverse.cursor( partitionTxn, entryId );
415
416 try
417 {
418 while ( values.next() )
419 {
420
421 forward.remove( partitionTxn, values.get().getValue(), entryId );
422 }
423
424 values.close();
425 }
426 catch ( CursorException | IOException e )
427 {
428 throw new LdapOtherException( e.getMessage(), e );
429 }
430 }
431 else
432 {
433 K key = reverse.get( partitionTxn, entryId );
434
435 forward.remove( partitionTxn, key );
436 }
437
438
439 reverse.remove( partitionTxn, entryId );
440 }
441 }
442
443
444
445
446
447 @SuppressWarnings("unchecked")
448 public Cursor<IndexEntry<K, String>> forwardCursor( PartitionTxn partitionTxn ) throws LdapException
449 {
450 return new IndexCursorAdaptor<>( partitionTxn, ( Cursor ) forward.cursor(), true );
451 }
452
453
454 public Cursor<IndexEntry<K, String>> forwardCursor( PartitionTxn partitionTxn, K key ) throws LdapException
455 {
456 return new IndexCursorAdaptor<>( partitionTxn, ( Cursor ) forward.cursor( partitionTxn, key ), true );
457 }
458
459
460
461
462
463 @Override
464 public Cursor<K> reverseValueCursor( PartitionTxn partitionTxn, String id ) throws LdapException
465 {
466 if ( withReverse )
467 {
468 return reverse.valueCursor( partitionTxn, id );
469 }
470 else
471 {
472 return new EmptyCursor<>();
473 }
474 }
475
476
477 public Cursor<String> forwardValueCursor( PartitionTxn partitionTxn, K key ) throws LdapException
478 {
479 return forward.valueCursor( partitionTxn, key );
480 }
481
482
483
484
485
486
487
488
489 public boolean forward( PartitionTxn partitionTxn, K attrVal ) throws LdapException
490 {
491 return forward.has( partitionTxn, attrVal );
492 }
493
494
495
496
497
498 public boolean forward( PartitionTxn partitionTxn, K attrVal, String id ) throws LdapException
499 {
500 return forward.has( partitionTxn, attrVal, id );
501 }
502
503
504
505
506
507 public boolean reverse( PartitionTxn partitionTxn, String id ) throws LdapException
508 {
509 if ( withReverse )
510 {
511 return reverse.has( partitionTxn, id );
512 }
513 else
514 {
515 return false;
516 }
517 }
518
519
520
521
522
523 public boolean reverse( PartitionTxn partitionTxn, String id, K attrVal ) throws LdapException
524 {
525 return forward.has( partitionTxn, attrVal, id );
526 }
527
528
529
530
531
532
533
534
535 @Override
536 public synchronized void close( PartitionTxn partitionTxn ) throws LdapException, IOException
537 {
538 if ( forward != null )
539 {
540 forward.close( partitionTxn );
541 }
542
543 if ( reverse != null )
544 {
545 reverse.close( partitionTxn );
546 }
547 }
548
549
550
551
552
553 @Override
554 public boolean isDupsEnabled()
555 {
556 if ( withReverse )
557 {
558 return reverse.isDupsEnabled();
559 }
560 else
561 {
562 return false;
563 }
564 }
565
566
567
568
569
570 public String toString()
571 {
572 return "Index<" + attributeId + ">";
573 }
574 }