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

boli
02.48.2007 061e0172510debb6cd8920caa717c29a10e1b233
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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
/*
 * 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-2007 Sun Microsystems, Inc.
 */
package org.opends.server.backends.jeb;
 
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.EnvironmentStats;
import com.sleepycat.je.StatsConfig;
import com.sleepycat.je.Transaction;
 
import org.opends.server.api.Backend;
import org.opends.server.loggers.debug.DebugLogger;
import org.opends.server.types.DebugLogLevel;
import org.opends.server.messages.JebMessages;
import org.opends.server.types.AttributeType;
import org.opends.server.types.DirectoryException;
import org.opends.server.types.DN;
import org.opends.server.types.Entry;
import org.opends.server.types.ErrorLogCategory;
import org.opends.server.types.ErrorLogSeverity;
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.types.ResultCode;
import org.opends.server.util.LDIFException;
import org.opends.server.util.LDIFReader;
 
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.LinkedBlockingQueue;
 
import static org.opends.server.loggers.debug.DebugLogger.debugCaught;
import static org.opends.server.loggers.debug.DebugLogger.debugEnabled;
import static org.opends.server.messages.JebMessages.
     MSGID_JEB_IMPORT_ENTRY_EXISTS;
import static org.opends.server.messages.MessageHandler.getMessage;
import static org.opends.server.messages.JebMessages.
     MSGID_JEB_IMPORT_PARENT_NOT_FOUND;
import static org.opends.server.loggers.Error.logError;
import static org.opends.server.messages.JebMessages.*;
 
/**
 * Import from LDIF to a JE backend.
 */
public class ImportJob implements Thread.UncaughtExceptionHandler
{
 
  /**
   * The backend instance we are importing into.
   */
  private Backend backend;
 
  /**
   * The JE backend configuration.
   */
  private Config config;
 
  /**
   * The root container used for this import job.
   */
  private RootContainer rootContainer;
 
  /**
   * The LDIF import configuration.
   */
  private LDIFImportConfig ldifImportConfig;
 
  /**
   * The LDIF reader.
   */
  private LDIFReader reader;
 
  /**
   * Map of base DNs to their import context.
   */
  private HashMap<DN,ImportContext> importMap =
       new HashMap<DN, ImportContext>();
 
  /**
   * The import threads created to process the entry queue.
   */
  private ArrayList<ImportThread> threads;
 
  /**
   * The current number of entries rejected.
   */
  private int rejectedCount = 0;
 
  /**
   * The number of milliseconds between job progress reports.
   */
  private long progressInterval = 10000;
 
  /**
   * Create a new import job.
   *
   * @param backend The backend performing the import.
   * @param config The backend configuration.
   * @param ldifImportConfig The LDIF import configuration.
   */
  public ImportJob(Backend backend, Config config,
                   LDIFImportConfig ldifImportConfig)
  {
    this.backend = backend;
    this.config = config;
    this.ldifImportConfig = ldifImportConfig;
  }
 
  /**
   * Import from LDIF file to one or more base DNs. Opens the database
   * environment and deletes existing data for each base DN unless we are
   * appending to existing data. Creates a temporary working directory,
   * processes the LDIF file, then merges the resulting intermediate
   * files to load the index databases.
   *
   * @throws DatabaseException If an error occurs in the JE database.
   * @throws IOException  If a problem occurs while opening the LDIF file for
   *                      reading, or while reading from the LDIF file.
   * @throws JebException If an error occurs in the JE backend.
   */
  public void importLDIF()
      throws DatabaseException, IOException, JebException
  {
/*
    envConfig.setConfigParam("je.env.runCleaner", "false");
    envConfig.setConfigParam("je.log.numBuffers", "2");
    envConfig.setConfigParam("je.log.bufferSize", "15000000");
    envConfig.setConfigParam("je.log.totalBufferBytes", "30000000");
    envConfig.setConfigParam("je.log.fileMax", "100000000");
*/
    rootContainer = new RootContainer(config, backend);
    if (ldifImportConfig.appendToExistingData())
    {
      rootContainer.open(config.getBackendDirectory(),
                         config.getBackendPermission(),
                         false, true, true, true, true, false);
    }
    else
    {
      rootContainer.open(config.getBackendDirectory(),
                         config.getBackendPermission(),
                         false, true, false, false, false, false);
    }
 
    if (!ldifImportConfig.appendToExistingData())
    {
      // We have the writer lock on the environment, now delete the
      // environment and re-open it. Only do this when we are
      // importing to all the base DNs in the backend.
      rootContainer.close();
      EnvManager.removeFiles(config.getBackendDirectory().getPath());
      rootContainer.open(config.getBackendDirectory(),
                         config.getBackendPermission(),
                         false, true, false, false, false, false);
    }
 
    // Divide the total buffer size by the number of threads
    // and give that much to each thread.
    int importThreadCount = config.getImportThreadCount();
    long bufferSize = config.getImportBufferSize() /
         (importThreadCount*config.getBaseDNs().length);
 
    int msgID = MSGID_JEB_IMPORT_THREAD_COUNT;
    String message = getMessage(msgID, importThreadCount);
    logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.NOTICE,
             message, msgID);
 
    msgID = MSGID_JEB_IMPORT_BUFFER_SIZE;
    message = getMessage(msgID, bufferSize);
    logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.NOTICE,
             message, msgID);
 
    msgID = MSGID_JEB_IMPORT_ENVIRONMENT_CONFIG;
    message = getMessage(msgID,
                         rootContainer.getEnvironmentConfig().toString());
    logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.NOTICE,
             message, msgID);
 
    DebugLogger.debugInfo(
        rootContainer.getEnvironmentConfig().toString());
 
 
    rootContainer.openEntryContainers(config.getBaseDNs());
 
    // Create the import contexts for each base DN.
    DN baseDN;
 
    for (EntryContainer entryContainer : rootContainer.getEntryContainers())
    {
      baseDN = entryContainer.getBaseDN();
 
      // Create an import context.
      ImportContext importContext = new ImportContext();
      importContext.setBufferSize(bufferSize);
      importContext.setConfig(config);
      importContext.setLDIFImportConfig(this.ldifImportConfig);
 
      importContext.setBaseDN(baseDN);
      importContext.setContainerName(entryContainer.getContainerName());
      importContext.setEntryContainer(entryContainer);
      importContext.setBufferSize(bufferSize);
 
      // Create an entry queue.
      LinkedBlockingQueue<Entry> queue =
           new LinkedBlockingQueue<Entry>(config.getImportQueueSize());
      importContext.setQueue(queue);
 
      importMap.put(baseDN, importContext);
    }
 
    // Make a note of the time we started.
    long startTime = System.currentTimeMillis();
 
    try
    {
      // Create a temporary work directory.
      File tempDir = new File(config.getImportTempDirectory());
      tempDir.mkdir();
      if (tempDir.listFiles() != null)
      {
        for (File f : tempDir.listFiles())
        {
          f.delete();
        }
      }
 
      try
      {
        int     passNumber = 1;
        boolean moreData   = true;
        while (moreData)
        {
          moreData = processLDIF();
          if (moreData)
          {
            msgID = MSGID_JEB_IMPORT_BEGINNING_INTERMEDIATE_MERGE;
            message = getMessage(msgID, passNumber++);
            logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.NOTICE,
                     message, msgID);
          }
          else
          {
            msgID = MSGID_JEB_IMPORT_BEGINNING_FINAL_MERGE;
            message = getMessage(msgID);
            logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.NOTICE,
                     message, msgID);
          }
 
 
          long mergeStartTime = System.currentTimeMillis();
          merge();
          long mergeEndTime = System.currentTimeMillis();
 
          if (moreData)
          {
            msgID = MSGID_JEB_IMPORT_RESUMING_LDIF_PROCESSING;
            message = getMessage(msgID, ((mergeEndTime-mergeStartTime)/1000));
            logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.NOTICE,
                     message, msgID);
          }
          else
          {
            msgID = MSGID_JEB_IMPORT_FINAL_MERGE_COMPLETED;
            message = getMessage(msgID, ((mergeEndTime-mergeStartTime)/1000));
            logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.NOTICE,
                     message, msgID);
          }
        }
      }
      finally
      {
        tempDir.delete();
      }
    }
    finally
    {
      rootContainer.close();
 
      // Sync the environment to disk.
      msgID = MSGID_JEB_IMPORT_CLOSING_DATABASE;
      message = getMessage(msgID);
      logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.NOTICE,
               message, msgID);
    }
 
    long finishTime = System.currentTimeMillis();
    long importTime = (finishTime - startTime);
 
    float rate = 0;
    if (importTime > 0)
    {
      rate = 1000f*getImportedCount() / importTime;
    }
 
    msgID = MSGID_JEB_IMPORT_FINAL_STATUS;
    message = getMessage(msgID, reader.getEntriesRead(), getImportedCount(),
                         reader.getEntriesIgnored(),
                         reader.getEntriesRejected(), importTime/1000, rate);
    logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.NOTICE,
             message, msgID);
 
    msgID = MSGID_JEB_IMPORT_ENTRY_LIMIT_EXCEEDED_COUNT;
    message = getMessage(msgID, getEntryLimitExceededCount());
    logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.NOTICE,
             message, msgID);
  }
 
  /**
   * Merge the intermediate files to load the index databases.
   */
  public void merge()
  {
    Map<AttributeType,IndexConfig>
         indexConfigs = config.getIndexConfigMap();
    ArrayList<IndexMergeThread> mergers = new ArrayList<IndexMergeThread>();
 
    // Create merge threads for each base DN.
    for (ImportContext importContext : importMap.values())
    {
      String containerName = importContext.getContainerName();
      EntryContainer entryContainer = importContext.getEntryContainer();
 
      // For each configured attribute index.
      for (IndexConfig indexConfig : indexConfigs.values())
      {
        AttributeIndex attrIndex =
             entryContainer.getAttributeIndex(indexConfig.getAttributeType());
        if (indexConfig.isEqualityIndex())
        {
          Index index = attrIndex.equalityIndex;
          String name = containerName + "_" + index.toString();
          IndexMergeThread indexMergeThread =
               new IndexMergeThread(name, config, ldifImportConfig, index,
                                    indexConfig.getEqualityEntryLimit());
          mergers.add(indexMergeThread);
        }
        if (indexConfig.isPresenceIndex())
        {
          Index index = attrIndex.presenceIndex;
          String name = containerName + "_" + index.toString();
          IndexMergeThread indexMergeThread =
               new IndexMergeThread(name, config, ldifImportConfig, index,
                                    indexConfig.getPresenceEntryLimit());
          mergers.add(indexMergeThread);
        }
        if (indexConfig.isSubstringIndex())
        {
          Index index = attrIndex.substringIndex;
          String name = containerName + "_" + index.toString();
          IndexMergeThread indexMergeThread =
               new IndexMergeThread(name, config, ldifImportConfig, index,
                                    indexConfig.getSubstringEntryLimit());
          mergers.add(indexMergeThread);
        }
        if (indexConfig.isOrderingIndex())
        {
          Index index = attrIndex.orderingIndex;
          String name = containerName + "_" + index.toString();
          IndexMergeThread indexMergeThread =
               new IndexMergeThread(name, config, ldifImportConfig, index,
                                    indexConfig.getEqualityEntryLimit());
          mergers.add(indexMergeThread);
        }
      }
 
      // Id2Children index.
      Index id2Children = entryContainer.getID2Children();
      String name = containerName + "_" + id2Children.toString();
      IndexMergeThread indexMergeThread =
           new IndexMergeThread(name, config, ldifImportConfig,
                                id2Children,
                                config.getBackendIndexEntryLimit());
      mergers.add(indexMergeThread);
 
      // Id2Subtree index.
      Index id2Subtree = entryContainer.getID2Subtree();
      name = containerName + "_" + id2Subtree.toString();
      indexMergeThread =
           new IndexMergeThread(name, config, ldifImportConfig,
                           id2Subtree,
                           config.getBackendIndexEntryLimit());
      mergers.add(indexMergeThread);
    }
 
    // Run all the merge threads.
    for (IndexMergeThread imt : mergers)
    {
      imt.start();
    }
 
    // Wait for the threads to finish.
    for (IndexMergeThread imt : mergers)
    {
      try
      {
        imt.join();
      }
      catch (InterruptedException e)
      {
        if (debugEnabled())
        {
          debugCaught(DebugLogLevel.ERROR, e);
        }
      }
    }
  }
 
  /**
   * Open a reader for the LDIF file and create a set of worker threads, one
   * set for each base DN. Read each entry from the LDIF and determine which
   * base DN the entry belongs to. Write the dn2id database, then put the
   * entry on the appropriate queue for the worker threads to consume.
   * Record the entry count for each base DN when all entries have been
   * processed.
   *
   * @return true if is more data to be read from the LDIF file (the import
   * pass size was reached), false if the entire LDIF file has been read.
   *
   * @throws JebException If an error occurs in the JE backend.
   * @throws DatabaseException If an error occurs in the JE database.
   * @throws  IOException  If a problem occurs while opening the LDIF file for
   *                       reading, or while reading from the LDIF file.
   */
  private boolean processLDIF()
          throws JebException, DatabaseException, IOException
  {
    boolean moreData = false;
 
    // Create an LDIF reader if necessary.
    if (reader == null)
    {
      reader = new LDIFReader(ldifImportConfig);
      for (ImportContext ic : importMap.values())
      {
        ic.setLDIFReader(reader);
      }
    }
 
    try
    {
      // Create one set of worker threads for each base DN.
      int importThreadCount = config.getImportThreadCount();
      threads = new ArrayList<ImportThread>(importThreadCount*importMap.size());
      for (ImportContext ic : importMap.values())
      {
        for (int i = 0; i < importThreadCount; i++)
        {
          ImportThread t = new ImportThread(ic, i);
          t.setUncaughtExceptionHandler(this);
          threads.add(t);
 
          t.start();
        }
      }
 
      try
      {
        // Create a counter to use to determine whether we've hit the import
        // pass size.
        int entriesProcessed = 0;
        int importPassSize   = config.getImportPassSize();
        if (importPassSize <= 0)
        {
          importPassSize = Integer.MAX_VALUE;
        }
 
        // Start a timer for the progress report.
        Timer timer = new Timer();
        TimerTask progressTask = new ImportJob.ProgressTask();
        timer.scheduleAtFixedRate(progressTask, progressInterval,
                                  progressInterval);
 
        try
        {
          do
          {
            try
            {
              // Read the next entry.
              Entry entry = reader.readEntry();
 
              // Check for end of file.
              if (entry == null)
              {
                break;
              }
 
              // Route it according to base DN.
              ImportContext importContext = getImportConfig(entry.getDN());
 
              processEntry(importContext, entry);
 
              entriesProcessed++;
              if (entriesProcessed >= importPassSize)
              {
                moreData = true;
                break;
              }
            }
            catch (LDIFException e)
            {
              if (debugEnabled())
              {
                debugCaught(DebugLogLevel.ERROR, e);
              }
              // Update stats.
              rejectedCount++;
            }
            catch (DirectoryException e)
            {
              if (debugEnabled())
              {
                debugCaught(DebugLogLevel.ERROR, e);
              }
              // Update stats.
              rejectedCount++;
            }
          } while (true);
 
          // Wait for the queues to be drained.
          for (ImportContext ic : importMap.values())
          {
            while (ic.getQueue().size() > 0)
            {
              try
              {
                Thread.sleep(100);
              } catch (Exception e)
              {
                // No action needed.
              }
            }
          }
 
        }
        finally
        {
          timer.cancel();
        }
      }
      finally
      {
        // Order the threads to stop.
        for (ImportThread t : threads)
        {
          t.stopProcessing();
        }
 
        // Wait for each thread to stop.
        joinThreads();
      }
    }
    finally
    {
      if (! moreData)
      {
        reader.close();
      }
    }
 
 
    return moreData;
  }
 
  /**
   * Process an entry to be imported. Read dn2id to check if the entry already
   * exists, and write dn2id if it does not. Put the entry on the worker
   * thread queue.
   *
   * @param importContext The import context for this entry.
   * @param entry The entry to be imported.
   * @throws DatabaseException If an error occurs in the JE database.
   * @throws JebException If an error occurs in the JE backend.
   */
  public void processEntry(ImportContext importContext, Entry entry)
       throws JebException, DatabaseException
  {
    DN entryDN = entry.getDN();
    LDIFImportConfig ldifImportConfig = importContext.getLDIFImportConfig();
 
    Transaction txn = null;
    if (ldifImportConfig.appendToExistingData())
    {
      txn = importContext.getEntryContainer().beginTransaction();
    }
 
    DN2ID dn2id = importContext.getEntryContainer().getDN2ID();
    ID2Entry id2entry = importContext.getEntryContainer().getID2Entry();
 
    try
    {
      // See if the entry already exists.
      EntryID entryID = dn2id.get(txn, entryDN);
      if (entryID != null)
      {
        // See if we are allowed to replace the entry that exists.
        if (ldifImportConfig.appendToExistingData() &&
             ldifImportConfig.replaceExistingEntries())
        {
          // Read the existing entry contents.
          Entry oldEntry = id2entry.get(txn, entryID);
 
          // Attach the ID to the old entry.
          oldEntry.setAttachment(entryID);
 
          // Attach the old entry to the new entry.
          entry.setAttachment(oldEntry);
 
          // Put the entry on the queue.
          try
          {
            importContext.getQueue().put(entry);
          }
          catch (InterruptedException e)
          {
            if (debugEnabled())
            {
              debugCaught(DebugLogLevel.ERROR, e);
            }
          }
        }
        else
        {
          // Reject the entry.
          int msgID = MSGID_JEB_IMPORT_ENTRY_EXISTS;
          String msg = getMessage(msgID);
          importContext.getLDIFReader().rejectLastEntry(msg);
 
          // Update stats.
          rejectedCount++;
          return;
        }
      }
      else
      {
        // Make sure the parent entry exists, unless this entry is a base DN.
        EntryID parentID = null;
        DN parentDN = importContext.getEntryContainer().
             getParentWithinBase(entryDN);
        if (parentDN != null)
        {
          parentID = dn2id.get(txn, parentDN);
          if (parentID == null)
          {
            // Reject the entry.
            int msgID = MSGID_JEB_IMPORT_PARENT_NOT_FOUND;
            String msg = getMessage(msgID, parentDN.toString());
            importContext.getLDIFReader().rejectLastEntry(msg);
 
            // Update stats.
            rejectedCount++;
            return;
          }
        }
 
        // Assign a new entry identifier and write the new DN.
        entryID = rootContainer.getNextEntryID();
        dn2id.insert(txn, entryDN, entryID);
 
        // Construct a list of IDs up the DIT.
        ArrayList<EntryID> IDs;
        if (parentDN != null && importContext.getParentDN() != null &&
             parentDN.equals(importContext.getParentDN()))
        {
          // Reuse the previous values.
          IDs = new ArrayList<EntryID>(importContext.getIDs());
          IDs.set(0, entryID);
        }
        else
        {
          IDs = new ArrayList<EntryID>(entryDN.getNumComponents());
          IDs.add(entryID);
          if (parentID != null)
          {
            IDs.add(parentID);
            EntryContainer ec = importContext.getEntryContainer();
            for (DN dn = ec.getParentWithinBase(parentDN); dn != null;
                 dn = ec.getParentWithinBase(dn))
            {
              // Read the ID from dn2id.
              EntryID nodeID = dn2id.get(txn, dn);
              IDs.add(nodeID);
            }
          }
        }
        importContext.setParentDN(parentDN);
        importContext.setIDs(IDs);
 
        // Attach the list of IDs to the entry.
        entry.setAttachment(IDs);
 
        // Put the entry on the queue.
        try
        {
          importContext.getQueue().put(entry);
        }
        catch (InterruptedException e)
        {
          if (debugEnabled())
          {
            debugCaught(DebugLogLevel.ERROR, e);
          }
        }
      }
 
      if (txn != null)
      {
        importContext.getEntryContainer().transactionCommit(txn);
        txn = null;
      }
    }
    finally
    {
      if (txn != null)
      {
        importContext.getEntryContainer().transactionAbort(txn);
      }
    }
  }
 
  /**
   * Get a statistic of the number of keys that reached the entry limit.
   * @return The number of keys that reached the entry limit.
   */
  private int getEntryLimitExceededCount()
  {
    int count = 0;
    for (ImportContext ic : importMap.values())
    {
      count += ic.getEntryContainer().getEntryLimitExceededCount();
    }
    return count;
  }
 
  /**
   * Wait for all worker threads to exit.
   */
  private void joinThreads()
  {
    for (ImportThread t : threads)
    {
      try
      {
        t.join();
      }
      catch (InterruptedException ie)
      {
        // No action needed?
      }
    }
  }
 
  /**
   * Get the number of entries imported so far.
   * @return The number of entries imported.
   */
  private int getImportedCount()
  {
    int count = 0;
    for (ImportThread t : threads)
    {
      count += t.getImportedCount();
    }
    return count;
  }
 
  /**
   * Get the number of entries rejected so far.
   * @return The number of entries rejected.
   */
  private int getRejectedCount()
  {
    return rejectedCount;
  }
 
  /**
   * Method invoked when the given thread terminates due to the given uncaught
   * exception. <p>Any exception thrown by this method will be ignored by the
   * Java Virtual Machine.
   *
   * @param t the thread
   * @param e the exception
   */
  public void uncaughtException(Thread t, Throwable e)
  {
    e.printStackTrace();
  }
 
  /**
   * Determine the appropriate import context for an entry.
   *
   * @param dn The DN of an entry
   * @return The import context.
   * @throws DirectoryException If the entry DN does not match any
   *                            of the base DNs.
   */
  private ImportContext getImportConfig(DN dn) throws DirectoryException
  {
    ImportContext importContext = null;
    DN nodeDN = dn;
 
    while (importContext == null && nodeDN != null)
    {
      importContext = importMap.get(nodeDN);
      if (importContext == null)
      {
        nodeDN = nodeDN.getParentDNInSuffix();
      }
    }
 
    if (nodeDN == null)
    {
      // The entry should not have been given to this backend.
      String message = getMessage(JebMessages.MSGID_JEB_INCORRECT_ROUTING,
                                  String.valueOf(dn));
      throw new DirectoryException(ResultCode.NO_SUCH_OBJECT, message,
                                   JebMessages.MSGID_JEB_INCORRECT_ROUTING);
    }
 
    return importContext;
  }
 
  /**
   * This class reports progress of the import job at fixed intervals.
   */
  class ProgressTask extends TimerTask
  {
    /**
     * The number of entries that had been imported at the time of the
     * previous progress report.
     */
    private int previousCount = 0;
 
    /**
     * The time in milliseconds of the previous progress report.
     */
    private long previousTime;
 
    /**
     * The environment statistics at the time of the previous report.
     */
    private EnvironmentStats prevEnvStats;
 
    /**
     * The number of bytes in a megabyte.
     * Note that 1024*1024 bytes may eventually become known as a mebibyte(MiB).
     */
    private static final int bytesPerMegabyte = 1024*1024;
 
    /**
     * Create a new import progress task.
     * @throws DatabaseException If an error occurs in the JE database.
     */
    public ProgressTask() throws DatabaseException
    {
      previousTime = System.currentTimeMillis();
      prevEnvStats = rootContainer.getEnvironmentStats(new StatsConfig());
    }
 
    /**
     * The action to be performed by this timer task.
     */
    public void run()
    {
      int latestCount = getImportedCount();
      int deltaCount = (latestCount - previousCount);
      long latestTime = System.currentTimeMillis();
      long deltaTime = latestTime - previousTime;
 
      if (deltaTime == 0)
      {
        return;
      }
 
      long numRead     = reader.getEntriesRead();
      long numIgnored  = reader.getEntriesIgnored();
      long numRejected = reader.getEntriesRejected();
      float rate = 1000f*deltaCount / deltaTime;
 
      int msgID = MSGID_JEB_IMPORT_PROGRESS_REPORT;
      String message = getMessage(msgID, numRead, numIgnored, numRejected,
                                  rate);
      logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.NOTICE,
               message, msgID);
 
      try
      {
        Runtime runtime = Runtime.getRuntime();
        long freeMemory = runtime.freeMemory() / bytesPerMegabyte;
 
        EnvironmentStats envStats =
            rootContainer.getEnvironmentStats(new StatsConfig());
        long nCacheMiss =
             envStats.getNCacheMiss() - prevEnvStats.getNCacheMiss();
 
        float cacheMissRate = 0;
        if (deltaCount > 0)
        {
          cacheMissRate = nCacheMiss/(float)deltaCount;
        }
 
        msgID = MSGID_JEB_IMPORT_CACHE_AND_MEMORY_REPORT;
        message = getMessage(msgID, freeMemory, cacheMissRate);
        logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.NOTICE,
                 message, msgID);
 
        prevEnvStats = envStats;
      }
      catch (DatabaseException e)
      {
        // Unlikely to happen and not critical.
      }
 
 
      previousCount = latestCount;
      previousTime = latestTime;
    }
  }
 
}