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

Jean-Noel Rouvignac
23.38.2015 b6cdb03de75763199a60a37891b8b0632974ae55
Code cleanup


Suffix.java:
In setIndexesTrusted() + setTrusted(), passed in the WriteableStorage.
In importPhaseOne(), used ExecutorService.submit() for single tasks rather than ExecutorService.invokeAll().
In TmpEnv.insert(), removed the AtomicBoolean, superseded by WriteableStorage.update() return value.
Renamed TmpEnv.isDNMatched() to contains() + handled null parameter.

Importer.java
Renamed idContainerMap and idECMap fields to indexIDToIndexMap and indexIDToECMap.
2 files modified
173 ■■■■■ changed files
opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Importer.java 127 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Suffix.java 46 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Importer.java
@@ -28,11 +28,11 @@
import static org.opends.messages.JebMessages.*;
import static org.opends.server.admin.std.meta.BackendIndexCfgDefn.IndexType.*;
import static org.opends.server.backends.pluggable.EntryIDSet.*;
import static org.opends.server.backends.pluggable.IndexOutputBuffer.*;
import static org.opends.server.util.DynamicConstants.*;
import static org.opends.server.util.ServerConstants.*;
import static org.opends.server.util.StaticUtils.*;
import static org.opends.server.backends.pluggable.EntryIDSet.*;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
@@ -79,7 +79,6 @@
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
@@ -241,10 +240,10 @@
  /** Map of DNs to Suffix objects. */
  private final Map<DN, Suffix> dnSuffixMap = new LinkedHashMap<DN, Suffix>();
  /** Map of container ids to database containers. */
  private final ConcurrentHashMap<Integer, Index> idContainerMap = new ConcurrentHashMap<Integer, Index>();
  /** Map of container ids to entry containers. */
  private final ConcurrentHashMap<Integer, EntryContainer> idECMap =
  /** Map of indexIDs to database containers. */
  private final ConcurrentHashMap<Integer, Index> indexIDToIndexMap = new ConcurrentHashMap<Integer, Index>();
  /** Map of indexIDs to entry containers. */
  private final ConcurrentHashMap<Integer, EntryContainer> indexIDToECMap =
      new ConcurrentHashMap<Integer, EntryContainer>();
  /** Used to synchronize when a scratch file index writer is first setup. */
@@ -287,8 +286,8 @@
   * @throws ConfigException
   *           If a problem occurs during initialization.
   */
  public Importer(RebuildConfig rebuildConfig, PersistitBackendCfg cfg) throws InitializationException,
      StorageRuntimeException, ConfigException
  Importer(RebuildConfig rebuildConfig, PersistitBackendCfg cfg)
      throws InitializationException, StorageRuntimeException, ConfigException
  {
    this.importConfiguration = null;
    this.backendConfiguration = cfg;
@@ -325,7 +324,7 @@
   * @throws StorageRuntimeException
   *           If an error occurred when opening the DB.
   */
  public Importer(LDIFImportConfig importConfiguration, PersistitBackendCfg backendCfg)
  Importer(LDIFImportConfig importConfiguration, PersistitBackendCfg backendCfg)
      throws InitializationException, ConfigException, StorageRuntimeException
  {
    this.rebuildManager = null;
@@ -699,7 +698,7 @@
  {
    if (index != null)
    {
      idContainerMap.putIfAbsent(getIndexID(index), index);
      indexIDToIndexMap.putIfAbsent(getIndexID(index), index);
    }
  }
@@ -934,20 +933,10 @@
   * @param txn
   *          The database transaction
   * @return A LDIF result.
   * @throws ConfigException
   *           If the import failed because of an configuration error.
   * @throws InitializationException
   *           If the import failed because of an initialization error.
   * @throws StorageRuntimeException
   *           If the import failed due to a database error.
   * @throws InterruptedException
   *           If the import failed due to an interrupted error.
   * @throws ExecutionException
   *           If the import failed due to an execution error.
   * @throws Exception
   *           If the import failed
   */
  public LDIFImportResult processImport(RootContainer rootContainer, WriteableStorage txn)
      throws ConfigException, InitializationException, StorageRuntimeException,
      InterruptedException, ExecutionException
  public LDIFImportResult processImport(RootContainer rootContainer, WriteableStorage txn) throws Exception
  {
    this.rootContainer = rootContainer;
    try {
@@ -965,7 +954,7 @@
              BUILD_ID, REVISION_NUMBER);
      logger.info(NOTE_JEB_IMPORT_THREAD_COUNT, threadCount);
      initializeSuffixes(txn);
      setIndexesTrusted(false);
      setIndexesTrusted(txn, false);
      final long startTime = System.currentTimeMillis();
      importPhaseOne(txn);
@@ -989,7 +978,7 @@
      }
      final long phaseTwoFinishTime = System.currentTimeMillis();
      setIndexesTrusted(true);
      setIndexesTrusted(txn, true);
      switchEntryContainers(txn);
      recursiveDelete(tempDir);
      final long finishTime = System.currentTimeMillis();
@@ -1064,13 +1053,13 @@
    }
  }
  private void setIndexesTrusted(boolean trusted) throws StorageRuntimeException
  private void setIndexesTrusted(WriteableStorage txn, boolean trusted) throws StorageRuntimeException
  {
    try
    {
      for (Suffix s : dnSuffixMap.values())
      {
        s.setIndexesTrusted(trusted);
        s.setIndexesTrusted(txn, trusted);
      }
    }
    catch (StorageRuntimeException ex)
@@ -1098,11 +1087,9 @@
    bufferSortService = Executors.newFixedThreadPool(threadCount);
    final ExecutorService execService = Executors.newFixedThreadPool(threadCount);
    final List<Callable<Void>> tasks = new ArrayList<Callable<Void>>(threadCount);
    tasks.add(new MigrateExistingTask(txn));
    getAll(execService.invokeAll(tasks));
    tasks.clear();
    execService.submit(new MigrateExistingTask(txn)).get();
    final List<Callable<Void>> tasks = new ArrayList<Callable<Void>>(threadCount);
    if (importConfiguration.appendToExistingData()
        && importConfiguration.replaceExistingEntries())
    {
@@ -1121,8 +1108,7 @@
    getAll(execService.invokeAll(tasks));
    tasks.clear();
    tasks.add(new MigrateExcludedTask(txn));
    getAll(execService.invokeAll(tasks));
    execService.submit(new MigrateExcludedTask(txn)).get();
    stopScratchFileWriters();
    getAll(scratchFileWriterFutures);
@@ -1693,16 +1679,6 @@
      }
    }
    private void processAttribute(Index index, ImportIndexType presence, Entry entry,
        AttributeType attributeType, EntryID entryID, IndexingOptions options) throws InterruptedException
    {
      if (index != null)
      {
        IndexKey indexKey = new IndexKey(attributeType, presence, index.getIndexEntryLimit());
        processAttribute(index, entry, entryID, options, indexKey);
      }
    }
    private void processAttributes(Collection<Index> indexes, ImportIndexType indexType, Entry entry,
        AttributeType attributeType, EntryID entryID, IndexingOptions options) throws InterruptedException
    {
@@ -1710,11 +1686,20 @@
      {
        for (Index index : indexes)
        {
          processAttribute(index, indexType, entry, attributeType, entryID, options);
        }
      }
    }
    private void processAttribute(Index index, ImportIndexType indexType, Entry entry,
        AttributeType attributeType, EntryID entryID, IndexingOptions options) throws InterruptedException
    {
      if (index != null)
      {
          IndexKey indexKey = new IndexKey(attributeType, indexType, index.getIndexEntryLimit());
          processAttribute(index, entry, entryID, options, indexKey);
        }
      }
    }
    void processAttribute(Index index, Entry entry, EntryID entryID, IndexingOptions options,
        IndexKey indexKey) throws StorageRuntimeException, InterruptedException
@@ -1791,8 +1776,8 @@
    {
      DN2ID dn2id = suffix.getDN2ID();
      ByteString dnBytes = JebFormat.dnToDNKey(dn, suffix.getBaseDN().size());
      int id = processKey(dn2id, dnBytes, entryID, dnIndexKey, true);
      idECMap.putIfAbsent(id, suffix.getEntryContainer());
      int indexID = processKey(dn2id, dnBytes, entryID, dnIndexKey, true);
      indexIDToECMap.putIfAbsent(indexID, suffix.getEntryContainer());
    }
    void processDN2URI(Suffix suffix, Entry oldEntry, Entry newEntry) throws StorageRuntimeException
@@ -2084,7 +2069,7 @@
        return new ImportIDSet(key, newDefinedSet(), 1, false);
      }
      final Index index = idContainerMap.get(indexID);
      final Index index = indexIDToIndexMap.get(indexID);
      return new ImportIDSet(key, newDefinedSet(), index.getIndexEntryLimit(), index.getMaintainCount());
    }
@@ -2099,12 +2084,12 @@
      {
        if (deleteSet.size() > 0 || !deleteSet.isDefined())
        {
          final Index index = idContainerMap.get(indexID);
          final Index index = indexIDToIndexMap.get(indexID);
          index.delete(txn, deleteSet);
        }
        if (insertSet.size() > 0 || !insertSet.isDefined())
        {
          final Index index = idContainerMap.get(indexID);
          final Index index = indexIDToIndexMap.get(indexID);
          index.insert(txn, insertSet);
        }
      }
@@ -2115,7 +2100,7 @@
      DNState dnState;
      if (!dnStateMap.containsKey(indexID))
      {
        dnState = new DNState(idECMap.get(indexID));
        dnState = new DNState(indexIDToECMap.get(indexID));
        dnStateMap.put(indexID, dnState);
      }
      else
@@ -3954,11 +3939,11 @@
   */
  private final class TmpEnv implements DNCache
  {
    private Storage storage;
    private final Storage storage;
    private WriteableStorage txn;
    private org.opends.server.backends.pluggable.spi.Importer importer;
    private static final String DB_NAME = "dn_cache";
    private TreeName dnCache = new TreeName("", DB_NAME);
    private final TreeName dnCache = new TreeName("", DB_NAME);
    /**
     * Create a temporary DB environment and database to be used as a cache of
@@ -4056,34 +4041,22 @@
    private boolean insert(ByteString key, final ByteStringBuilder dn) throws StorageRuntimeException
    {
      Cursor cursor = null;
      try
      {
        final AtomicBoolean result = new AtomicBoolean();
        txn.update(dnCache, key, new UpdateFunction()
      return txn.update(dnCache, key, new UpdateFunction()
        {
          @Override
          public ByteSequence computeNewValue(ByteSequence existingDns)
          {
            if (existingDns != null)
          if (containsDN(existingDns, dn))
            {
              if (isDNMatched(existingDns, dn))
              {
                // dn is already present, no change
                result.set(false);
            // no change
                return existingDns;
              }
              else
          else if (existingDns != null)
              {
                // dn is not present in the list, add it
                result.set(true);
                return addDN(existingDns, dn);
              }
            }
            else
            {
              // no previous data, create a new list
              result.set(true);
              return singletonList(dn);
            }
          }
@@ -4107,32 +4080,28 @@
            return singleton;
          }
        });
        return result.get();
      }
      finally
      {
        close(cursor);
      }
    }
    /** Return true if the specified DN is in the DNs saved as a result of hash collisions. */
    private boolean isDNMatched(ByteSequence existingDns, ByteStringBuilder dn)
    private boolean containsDN(ByteSequence existingDns, ByteStringBuilder dn)
    {
      if (existingDns != null && existingDns.length() > 0)
      {
        // TODO JNR remove call to toByteArray() on next line?
        final byte[] existingDnsBytes = existingDns.toByteArray();
      final ByteSequenceReader reader = existingDns.asReader();
      int previousPos = 0;
      while (reader.remaining() != 0)
      {
        int pLen = INT_SIZE;
        int len = reader.getInt();
        // TODO JNR remove call to toByteArray() on next line
        final byte[] existingDnsBytes = existingDns.toByteArray();
        if (indexComparator.compare(existingDnsBytes, previousPos + pLen, len, dn.getBackingArray(), dn.length()) == 0)
        {
          return true;
        }
        previousPos = reader.position();
      }
      }
      return false;
    }
@@ -4152,7 +4121,7 @@
      if (existingDns != null)
      {
        final ByteStringBuilder dnBytes = new ByteStringBuilder().append(dn.toString());
        return isDNMatched(existingDns, dnBytes);
        return containsDN(existingDns, dnBytes);
      }
      return false;
    }
opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/Suffix.java
@@ -37,6 +37,7 @@
import org.opends.server.backends.pluggable.Importer.DNCache;
import org.opends.server.backends.pluggable.spi.ReadableStorage;
import org.opends.server.backends.pluggable.spi.StorageRuntimeException;
import org.opends.server.backends.pluggable.spi.WriteableStorage;
import org.opends.server.types.AttributeType;
import org.opends.server.types.DN;
@@ -253,50 +254,48 @@
   * Sets the trusted status of all of the indexes, vlvIndexes, id2children
   * and id2subtree indexes.
   *
   * @param trusted True if the indexes should be trusted or false
   *                otherwise.
   *
   * @throws StorageRuntimeException If an error occurred setting the indexes to
   *                           trusted.
   * @param txn a non null database transaction
   * @param trusted True if the indexes should be trusted or false otherwise.
   * @throws StorageRuntimeException If an error occurred setting the indexes to trusted.
   */
  public void setIndexesTrusted(boolean trusted) throws StorageRuntimeException
  public void setIndexesTrusted(WriteableStorage txn, boolean trusted) throws StorageRuntimeException
  {
    entryContainer.getID2Children().setTrusted(null,trusted);
    entryContainer.getID2Subtree().setTrusted(null, trusted);
    entryContainer.getID2Children().setTrusted(txn, trusted);
    entryContainer.getID2Subtree().setTrusted(txn, trusted);
    for (AttributeIndex attributeIndex : entryContainer.getAttributeIndexes())
    {
      setTrusted(attributeIndex.getEqualityIndex(), trusted);
      setTrusted(attributeIndex.getPresenceIndex(), trusted);
      setTrusted(attributeIndex.getSubstringIndex(), trusted);
      setTrusted(attributeIndex.getOrderingIndex(), trusted);
      setTrusted(attributeIndex.getApproximateIndex(), trusted);
      setTrusted(txn, attributeIndex.getEqualityIndex(), trusted);
      setTrusted(txn, attributeIndex.getPresenceIndex(), trusted);
      setTrusted(txn, attributeIndex.getSubstringIndex(), trusted);
      setTrusted(txn, attributeIndex.getOrderingIndex(), trusted);
      setTrusted(txn, attributeIndex.getApproximateIndex(), trusted);
      Map<String, Collection<Index>> exIndexes = attributeIndex.getExtensibleIndexes();
      if(!exIndexes.isEmpty())
      {
        setTrusted(exIndexes.get(EXTENSIBLE_INDEXER_ID_SUBSTRING), trusted);
        setTrusted(exIndexes.get(EXTENSIBLE_INDEXER_ID_SHARED), trusted);
        setTrusted(txn, exIndexes.get(EXTENSIBLE_INDEXER_ID_SUBSTRING), trusted);
        setTrusted(txn, exIndexes.get(EXTENSIBLE_INDEXER_ID_SHARED), trusted);
      }
    }
    for(VLVIndex vlvIdx : entryContainer.getVLVIndexes()) {
      vlvIdx.setTrusted(null, trusted);
      vlvIdx.setTrusted(txn, trusted);
    }
  }
  private void setTrusted(Index index, boolean trusted)
  private void setTrusted(WriteableStorage txn, Index index, boolean trusted)
  {
    if (index != null)
    {
      index.setTrusted(null, trusted);
      index.setTrusted(txn, trusted);
    }
  }
  private void setTrusted(Collection<Index> subIndexes, boolean trusted)
  private void setTrusted(WriteableStorage txn, Collection<Index> indexes, boolean trusted)
  {
    if (subIndexes != null)
    if (indexes != null)
    {
      for (Index subIndex : subIndexes)
      for (Index subIndex : indexes)
      {
        subIndex.setTrusted(null, trusted);
        subIndex.setTrusted(txn, trusted);
      }
    }
  }
@@ -311,7 +310,6 @@
    return this.srcEntryContainer;
  }
  /**
   * Return include branches.
   *
@@ -322,7 +320,6 @@
    return this.includeBranches;
  }
  /**
   * Return exclude branches.
   *
@@ -333,7 +330,6 @@
    return this.excludeBranches;
  }
  /**
   * Return base DN.
   *