mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

coulbeck
31.56.2006 8ebde236cfc479258e491a4e94796ccbf05b1129
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Portions Copyright 2006 Sun Microsystems, Inc.
 */
package org.opends.server.backends.jeb;
 
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.Transaction;
 
import org.opends.server.api.SubstringMatchingRule;
import org.opends.server.api.OrderingMatchingRule;
import org.opends.server.core.DirectoryException;
import org.opends.server.protocols.asn1.ASN1OctetString;
import org.opends.server.types.AttributeType;
import org.opends.server.types.AttributeValue;
import org.opends.server.types.ByteString;
import org.opends.server.types.Entry;
import org.opends.server.types.Modification;
import org.opends.server.types.SearchFilter;
 
import static org.opends.server.loggers.Debug.debugException;
 
/**
 * Class representing an attribute index.
 * We have a separate database for each type of indexing, which makes it easy
 * to tell which attribute indexes are configured.  The different types of
 * indexing are equality, presence, substrings and ordering.  The keys in the
 * ordering index are ordered by setting the btree comparator to the ordering
 * matching rule comparator.
 * Note that the values in the equality index are normalized by the equality
 * matching rule, whereas the values in the ordering index are normalized
 * by the ordering matching rule.  If these could be guaranteed to be identical
 * then we would not need a separate ordering index.
 */
public class AttributeIndex
{
  /**
   * The fully-qualified name of this class for debugging purposes.
   */
  private static final String CLASS_NAME =
       "org.opends.server.backends.jeb.AttributeIndex";
 
 
  /**
   * A database key for the presence index.
   */
  public static final DatabaseEntry presenceKey =
       new DatabaseEntry("+".getBytes());
 
  /**
   * The container in which this attribute index resides.
   */
  Container container;
 
  /**
   * The attribute index configuration.
   */
  IndexConfig indexConfig;
 
  /**
   * The index database for attribute equality.
   */
  Index equalityIndex = null;
 
  /**
   * The index database for attribute presence.
   */
  Index presenceIndex = null;
 
  /**
   * The index database for attribute substrings.
   */
  Index substringIndex = null;
 
  /**
   * The index database for attribute ordering.
   */
  Index orderingIndex = null;
 
  /**
   * Create a new attribute index object.
   * @param indexConfig The attribute index configuration.
   * @param container The container of this attribute index.
   */
  public AttributeIndex(IndexConfig indexConfig, Container container)
  {
    this.indexConfig = indexConfig;
    this.container = container;
 
    AttributeType attrType = indexConfig.getAttributeType();
    String name = attrType.getNameOrOID();
 
    if (indexConfig.isEqualityIndex())
    {
      Indexer equalityIndexer = new EqualityIndexer(indexConfig);
      this.equalityIndex = new Index(container, name + ".equality",
                                     equalityIndexer,
                                     indexConfig.getEqualityEntryLimit(),
                                     indexConfig.getCursorEntryLimit());
    }
 
    if (indexConfig.isPresenceIndex())
    {
      Indexer presenceIndexer = new PresenceIndexer(indexConfig);
      this.presenceIndex = new Index(container, name + ".presence",
                                     presenceIndexer,
                                     indexConfig.getPresenceEntryLimit(),
                                     indexConfig.getCursorEntryLimit());
    }
 
    if (indexConfig.isSubstringIndex())
    {
      Indexer substringIndexer = new SubstringIndexer(indexConfig);
      this.substringIndex = new Index(container, name + ".substring",
                                     substringIndexer,
                                     indexConfig.getSubstringEntryLimit(),
                                     indexConfig.getCursorEntryLimit());
    }
 
    if (indexConfig.isOrderingIndex())
    {
      Indexer orderingIndexer = new OrderingIndexer(indexConfig);
      this.orderingIndex = new Index(container, name + ".ordering",
                                     orderingIndexer,
                                     indexConfig.getEqualityEntryLimit(),
                                     indexConfig.getCursorEntryLimit());
    }
  }
 
  /**
   * Open the attribute index.
   *
   * @param dbConfig The JE Database Config that will be used to open
   *                 underlying JE databases.
   *
   * @throws DatabaseException If an error occurs opening the underlying
   *                           databases.
   */
  public void open(DatabaseConfig dbConfig) throws DatabaseException
  {
    if (equalityIndex != null)
    {
      equalityIndex.open(dbConfig);
    }
 
    if (presenceIndex != null)
    {
      presenceIndex.open(dbConfig);
    }
 
    if (substringIndex != null)
    {
      substringIndex.open(dbConfig);
    }
 
    if (orderingIndex != null)
    {
      orderingIndex.open(dbConfig);
    }
  }
 
  /**
   * Close the attribute index.
   */
  public void close()
  {
    // The container is responsible for closing the JE databases.
  }
 
  /**
   * Get the attribute type of this attribute index.
   * @return The attribute type of this attribute index.
   */
  public AttributeType getAttributeType()
  {
    return indexConfig.getAttributeType();
  }
 
  /**
   * Update the attribute index for a new entry.
   *
   * @param txn         The database transaction to be used for the insertions.
   * @param entryID     The entry ID.
   * @param entry       The contents of the new entry.
   * @throws DatabaseException If an error occurs in the JE database.
   * @throws DirectoryException If a Directory Server error occurs.
   * @throws JebException If an error occurs in the JE backend.
   */
  public void addEntry(Transaction txn, EntryID entryID, Entry entry)
       throws DatabaseException, DirectoryException, JebException
  {
    if (equalityIndex != null)
    {
      equalityIndex.addEntry(txn, entryID, entry);
    }
 
    if (presenceIndex != null)
    {
      presenceIndex.addEntry(txn, entryID, entry);
    }
 
    if (substringIndex != null)
    {
      substringIndex.addEntry(txn, entryID, entry);
    }
 
    if (orderingIndex != null)
    {
      orderingIndex.addEntry(txn, entryID, entry);
    }
  }
 
  /**
   * Update the attribute index for a deleted entry.
   *
   * @param txn         The database transaction to be used for the deletions
   * @param entryID     The entry ID
   * @param entry       The contents of the deleted entry.
   * @throws DatabaseException If an error occurs in the JE database.
   * @throws DirectoryException If a Directory Server error occurs.
   * @throws JebException If an error occurs in the JE backend.
   */
  public void removeEntry(Transaction txn, EntryID entryID, Entry entry)
       throws DatabaseException, DirectoryException, JebException
  {
    if (equalityIndex != null)
    {
      equalityIndex.removeEntry(txn, entryID, entry);
    }
 
    if (presenceIndex != null)
    {
      presenceIndex.removeEntry(txn, entryID, entry);
    }
 
    if (substringIndex != null)
    {
      substringIndex.removeEntry(txn, entryID, entry);
    }
 
    if (orderingIndex != null)
    {
      orderingIndex.removeEntry(txn, entryID, entry);
    }
  }
 
  /**
   * Update the index to reflect a sequence of modifications in a Modify
   * operation.
   *
   * @param txn The JE transaction to use for database updates.
   * @param entryID The ID of the entry that was modified.
   * @param oldEntry The entry before the modifications were applied.
   * @param newEntry The entry after the modifications were applied.
   * @param mods The sequence of modifications in the Modify operation.
   * @throws DatabaseException If an error occurs during an operation on a
   * JE database.
   */
  public void modifyEntry(Transaction txn,
                          EntryID entryID,
                          Entry oldEntry,
                          Entry newEntry,
                          List<Modification> mods)
       throws DatabaseException
  {
    if (equalityIndex != null)
    {
      equalityIndex.modifyEntry(txn, entryID, oldEntry, newEntry, mods);
    }
 
    if (presenceIndex != null)
    {
      presenceIndex.modifyEntry(txn, entryID, oldEntry, newEntry, mods);
    }
 
    if (substringIndex != null)
    {
      substringIndex.modifyEntry(txn, entryID, oldEntry, newEntry, mods);
    }
 
    if (orderingIndex != null)
    {
      orderingIndex.modifyEntry(txn, entryID, oldEntry, newEntry, mods);
    }
  }
 
  /**
   * Makes a byte array representing an equality index key from
   * a byte array containing the normalized value.
   * The key is '=' followed by the normalized value.
   *
   * @param normalizedBytes The normalized value.
   * @return A byte array containing the equality key.
   */
  byte[] makeEqualityKey(byte[] normalizedBytes)
  {
    byte[] keyBytes = new byte[1 + normalizedBytes.length];
    keyBytes[0] = '=';
    System.arraycopy(normalizedBytes, 0, keyBytes, 1,
                     normalizedBytes.length);
    return keyBytes;
  }
 
  /**
   * Makes a byte array representing a substring index key for
   * one substring of a value.
   *
   * @param bytes The byte array containing the value.
   * @param pos The starting position of the substring.
   * @param len The length of the substring.
   * @return A byte array containing a substring key.
   */
  private byte[] makeSubstringKey(byte[] bytes, int pos, int len)
  {
    byte[] keyBytes = new byte[len + 1];
    keyBytes[0] = '*';
    System.arraycopy(bytes, pos, keyBytes, 1, len);
    return keyBytes;
  }
 
  /**
   * Decompose an attribute value into a set of substring index keys.
   * The ID of the entry containing this value should be inserted
   * into the list of each of these keys.
   *
   * @param value A byte array containing the normalized attribute value.
   * @return A set of index keys.
   */
  Set<ByteString> substringKeys(byte[] value)
  {
    // Eliminate duplicates by putting the keys into a set.
    // Sorting the keys will ensure database record locks are acquired
    // in a consistent order and help prevent transaction deadlocks between
    // concurrent writers.
    Set<ByteString> set = new HashSet<ByteString>();
 
    int substrLength = indexConfig.getSubstringLength();
    byte[] keyBytes;
 
    // Example: The value is ABCDE and the substring length is 3.
    // We produce the keys ABC BCD CDE DE E
    // To find values containing a short substring such as DE,
    // iterate through keys with prefix DE. To find values
    // containing a longer substring such as BCDE, read keys
    // BCD and CDE.
    for (int i = 0, remain = value.length; remain > 0; i++, remain--)
    {
      int len = Math.min(substrLength, remain);
      keyBytes = makeSubstringKey(value, i, len);
      set.add(new ASN1OctetString(keyBytes));
    }
 
    return set;
  }
 
  /**
   * Retrieve the entry IDs that might contain a given substring.
   * @param bytes A normalized substring of an attribute value.
   * @return The candidate entry IDs.
   */
  private EntryIDSet matchSubstring(byte[] bytes)
  {
    int substrLength = indexConfig.getSubstringLength();
 
    // There are two cases, depending on whether the user-provided
    // substring is smaller than the configured index substring length or not.
    if (bytes.length < substrLength)
    {
      // Iterate through all the keys that have this value as the prefix.
 
      // Set the lower bound for a range search.
      byte[] lower = makeSubstringKey(bytes, 0, bytes.length);
 
      // Set the upper bound for a range search.
      // We need a key for the upper bound that is of equal length
      // but slightly greater than the lower bound.
      byte[] upper = makeSubstringKey(bytes, 0, bytes.length);
      for (int i = upper.length-1; i >= 0; i--)
      {
        if (upper[i] == 0xFF)
        {
          // We have to carry the overflow to the more significant byte.
          upper[i] = 0;
        }
        else
        {
          // No overflow, we can stop.
          upper[i] = (byte) (upper[i] + 1);
          break;
        }
      }
 
      // Read the range: lower <= keys < upper.
      return substringIndex.readRange(lower, upper, true, false);
    }
    else
    {
      // Break the value up into fragments of length equal to the
      // index substring length, and read those keys.
 
      // Eliminate duplicates by putting the keys into a set.
      Set<byte[]> set = new HashSet<byte[]>();
 
      // Example: The value is ABCDE and the substring length is 3.
      // We produce the keys ABC BCD CDE.
      for (int first = 0, last = substrLength;
           last <= bytes.length; first++, last++)
      {
        byte[] keyBytes;
        keyBytes = makeSubstringKey(bytes, first, substrLength);
        set.add(keyBytes);
      }
 
      EntryIDSet results = new EntryIDSet();
      DatabaseEntry key = new DatabaseEntry();
      for (byte[] keyBytes : set)
      {
        // Read the key.
        key.setData(keyBytes);
        EntryIDSet list = substringIndex.readKey(key, null, LockMode.DEFAULT);
 
        // Incorporate them into the results.
        results.retainAll(list);
 
        // We may have reached the point of diminishing returns where
        // it is quicker to stop now and process the current small number of
        // candidates.
        if (results.isDefined() &&
             results.size() <= IndexFilter.FILTER_CANDIDATE_THRESHOLD)
        {
          break;
        }
      }
 
      return results;
    }
  }
 
  /**
   * Uses an equality index to retrieve the entry IDs that might contain a
   * given initial substring.
   * @param bytes A normalized initial substring of an attribute value.
   * @return The candidate entry IDs.
   */
  private EntryIDSet matchInitialSubstring(byte[] bytes)
  {
    // Iterate through all the keys that have this value as the prefix.
 
    // Set the lower bound for a range search.
    byte[] lower = makeEqualityKey(bytes);
 
    // Set the upper bound for a range search.
    // We need a key for the upper bound that is of equal length
    // but slightly greater than the lower bound.
    byte[] upper = makeEqualityKey(bytes);
    for (int i = upper.length-1; i >= 0; i--)
    {
      if (upper[i] == 0xFF)
      {
        // We have to carry the overflow to the more significant byte.
        upper[i] = 0;
      }
      else
      {
        // No overflow, we can stop.
        upper[i] = (byte) (upper[i] + 1);
        break;
      }
    }
 
    // Read the range: lower <= keys < upper.
    return equalityIndex.readRange(lower, upper, true, false);
  }
 
  /**
   * Retrieve the entry IDs that might match an equality filter.
   *
   * @param equalityFilter The equality filter.
   * @return The candidate entry IDs that might contain the filter
   *         assertion value.
   */
  public EntryIDSet evaluateEqualityFilter(SearchFilter equalityFilter)
  {
    if (!indexConfig.isEqualityIndex())
    {
      return new EntryIDSet();
    }
 
    try
    {
      // Make a key from the normalized assertion value.
      byte[] normBytes =
           equalityFilter.getAssertionValue().getNormalizedValue().value();
      byte[] keyBytes = makeEqualityKey(normBytes);
      DatabaseEntry key = new DatabaseEntry(keyBytes);
 
      // Read the key.
      return equalityIndex.readKey(key, null, LockMode.DEFAULT);
    }
    catch (DirectoryException e)
    {
      assert debugException(CLASS_NAME, "evaluateEqualityFilter", e);
      return new EntryIDSet();
    }
  }
 
  /**
   * Retrieve the entry IDs that might match a presence filter.
   *
   * @param filter The presence filter.
   * @return The candidate entry IDs that might contain one or more
   *         values of the attribute type in the filter.
   */
  public EntryIDSet evaluatePresenceFilter(SearchFilter filter)
  {
    if (!indexConfig.isPresenceIndex())
    {
      return new EntryIDSet();
    }
 
    // Read the presence key
    return presenceIndex.readKey(presenceKey, null, LockMode.DEFAULT);
  }
 
  /**
   * Retrieve the entry IDs that might match a greater-or-equal filter.
   *
   * @param filter The greater-or-equal filter.
   * @return The candidate entry IDs that might contain a value
   *         greater than or equal to the filter assertion value.
   */
  public EntryIDSet evaluateGreaterOrEqualFilter(SearchFilter filter)
  {
    if (!indexConfig.isOrderingIndex() || orderingIndex == null)
    {
      return new EntryIDSet();
    }
 
    try
    {
      // Set the lower bound for a range search.
      // Use the ordering matching rule to normalize the value.
      OrderingMatchingRule orderingRule =
           filter.getAttributeType().getOrderingMatchingRule();
      byte[] normBytes = orderingRule.normalizeValue(
           filter.getAssertionValue().getValue()).value();
      byte[] lower = makeEqualityKey(normBytes);
 
      // Set the upper bound for a range search.
      // We need a key for the upper bound that is slightly greater than
      // the equality key prefix.
      byte[] upper = makeEqualityKey(new byte[0]);
      upper[0] = (byte) (upper[0] + 1);
 
      // Read the range: lower <= keys < upper.
      return orderingIndex.readRange(lower, upper, true, false);
    }
    catch (DirectoryException e)
    {
      assert debugException(CLASS_NAME, "evaluateGreaterOrEqualFilter", e);
      return new EntryIDSet();
    }
  }
 
  /**
   * Retrieve the entry IDs that might match a less-or-equal filter.
   *
   * @param filter The less-or-equal filter.
   * @return The candidate entry IDs that might contain a value
   *         less than or equal to the filter assertion value.
   */
  public EntryIDSet evaluateLessOrEqualFilter(SearchFilter filter)
  {
    if (!indexConfig.isOrderingIndex() || orderingIndex == null)
    {
      return new EntryIDSet();
    }
 
    try
    {
      // Set the lower bound for a range search.
      byte[] lower = makeEqualityKey(new byte[0]);
 
      // Set the upper bound for a range search.
      // Use the ordering matching rule to normalize the value.
      OrderingMatchingRule orderingRule =
           filter.getAttributeType().getOrderingMatchingRule();
      byte[] normBytes = orderingRule.normalizeValue(
           filter.getAssertionValue().getValue()).value();
      byte[] upper = makeEqualityKey(normBytes);
 
      // Read the range: lower < keys <= upper.
      return orderingIndex.readRange(lower, upper, false, true);
    }
    catch (DirectoryException e)
    {
      assert debugException(CLASS_NAME, "evaluateLessOrEqualFilter", e);
      return new EntryIDSet();
    }
  }
 
  /**
   * Retrieve the entry IDs that might match a substring filter.
   *
   * @param filter The substring filter.
   * @return The candidate entry IDs that might contain a value
   *         that matches the filter substrings.
   */
  public EntryIDSet evaluateSubstringFilter(SearchFilter filter)
  {
    SubstringMatchingRule matchRule =
         filter.getAttributeType().getSubstringMatchingRule();
 
    try
    {
      ArrayList<ByteString> elements = new ArrayList<ByteString>();
      EntryIDSet results = new EntryIDSet();
 
      if (filter.getSubInitialElement() != null)
      {
        // Use the equality index for initial substrings if possible.
        if (indexConfig.isEqualityIndex())
        {
          ByteString normValue =
               matchRule.normalizeSubstring(filter.getSubInitialElement());
          byte[] normBytes = normValue.value();
 
          EntryIDSet list = matchInitialSubstring(normBytes);
          results.retainAll(list);
 
          if (results.isDefined() &&
               results.size() <= IndexFilter.FILTER_CANDIDATE_THRESHOLD)
          {
            return results;
          }
        }
        else
        {
          elements.add(filter.getSubInitialElement());
        }
      }
 
      if (!indexConfig.isSubstringIndex())
      {
        return results;
      }
 
      // We do not distinguish between sub and final elements
      // in the substring index. Put all the elements into a single list.
      elements.addAll(filter.getSubAnyElements());
      if (filter.getSubFinalElement() != null)
      {
        elements.add(filter.getSubFinalElement());
      }
 
      // Iterate through each substring element.
      for (ByteString element : elements)
      {
        // Normalize the substring according to the substring matching rule.
        ByteString normValue = matchRule.normalizeSubstring(element);
        byte[] normBytes = normValue.value();
 
        // Get the candidate entry IDs from the index.
        EntryIDSet list = matchSubstring(normBytes);
 
        // Incorporate them into the results.
        results.retainAll(list);
 
        // We may have reached the point of diminishing returns where
        // it is quicker to stop now and process the current small number of
        // candidates.
        if (results.isDefined() &&
             results.size() <= IndexFilter.FILTER_CANDIDATE_THRESHOLD)
        {
          break;
        }
      }
 
      return results;
    }
    catch (DirectoryException e)
    {
      assert debugException(CLASS_NAME, "evaluateSubstringFilter", e);
      return new EntryIDSet();
    }
  }
 
  /**
   * Retrieve the entry IDs that might have a value greater than or
   * equal to the lower bound value, and less than or equal to the
   * upper bound value.
   *
   * @param lowerValue The lower bound value
   * @param upperValue The upper bound value
   * @return The candidate entry IDs.
   */
  public EntryIDSet evaluateBoundedRange(AttributeValue lowerValue,
                                          AttributeValue upperValue)
  {
    if (orderingIndex == null)
    {
      return new EntryIDSet();
    }
 
    try
    {
      // Use the ordering matching rule to normalize the values.
      OrderingMatchingRule orderingRule =
           getAttributeType().getOrderingMatchingRule();
 
      byte[] normBytes;
 
      // Set the lower bound for a range search.
      normBytes = orderingRule.normalizeValue(lowerValue.getValue()).value();
      byte[] lower = makeEqualityKey(normBytes);
 
      // Set the upper bound for a range search.
      normBytes = orderingRule.normalizeValue(upperValue.getValue()).value();
      byte[] upper = makeEqualityKey(normBytes);
 
      // Read the range: lower <= keys <= upper.
      return orderingIndex.readRange(lower, upper, true, true);
    }
    catch (DirectoryException e)
    {
      assert debugException(CLASS_NAME, "evaluateBoundedRange", e);
      return new EntryIDSet();
    }
  }
 
  /**
   * The default lexicographic byte array comparator.
   * Is there one available in the Java platform?
   */
  static public class KeyComparator implements Comparator<byte[]>
  {
    /**
     * Compares its two arguments for order.  Returns a negative integer,
     * zero, or a positive integer as the first argument is less than, equal
     * to, or greater than the second.
     *
     * @param a the first object to be compared.
     * @param b the second object to be compared.
     * @return a negative integer, zero, or a positive integer as the
     *         first argument is less than, equal to, or greater than the
     *         second.
     */
    public int compare(byte[] a, byte[] b)
    {
      int i;
      for (i = 0; i < a.length && i < b.length; i++)
      {
        if (a[i] > b[i])
        {
          return 1;
        }
        else if (a[i] < b[i])
        {
          return -1;
        }
      }
      if (a.length == b.length)
      {
        return 0;
      }
      if (a.length > b.length)
      {
        return 1;
      }
      else
      {
        return -1;
      }
    }
  }
 
  /**
   * Remove the index from disk. The index must not be open.
   * @throws DatabaseException If an error occurs in the JE database.
   */
  public void removeIndex() throws DatabaseException
  {
    AttributeType attrType = indexConfig.getAttributeType();
    String name = attrType.getNameOrOID();
    if (indexConfig.isEqualityIndex())
    {
      container.removeDatabase(name + ".equality");
    }
    if (indexConfig.isPresenceIndex())
    {
      container.removeDatabase(name + ".presence");
    }
    if (indexConfig.isSubstringIndex())
    {
      container.removeDatabase(name + ".substring");
    }
    if (indexConfig.isOrderingIndex())
    {
      container.removeDatabase(name + ".ordering");
    }
  }
 
  /**
   * Return the number of values that have exceeded the entry limit since this
   * object was created.
   *
   * @return The number of values that have exceeded the entry limit.
   */
  public long getEntryLimitExceededCount()
  {
    long entryLimitExceededCount = 0;
 
    if (equalityIndex != null)
    {
      entryLimitExceededCount += equalityIndex.getEntryLimitExceededCount();
    }
 
    if (presenceIndex != null)
    {
      entryLimitExceededCount += presenceIndex.getEntryLimitExceededCount();
    }
 
    if (substringIndex != null)
    {
      entryLimitExceededCount += substringIndex.getEntryLimitExceededCount();
    }
 
    if (orderingIndex != null)
    {
      entryLimitExceededCount += orderingIndex.getEntryLimitExceededCount();
    }
 
    return entryLimitExceededCount;
  }
}