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

Jean-Noel Rouvignac
27.50.2015 81d27a068e194169b632b5ec5f2f5249f232a049
opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/IndexInputBuffer.java
@@ -44,25 +44,26 @@
final class IndexInputBuffer implements Comparable<IndexInputBuffer>
{
  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
  private final IndexManager indexMgr;
  private final FileChannel channel;
  private final long begin;
  private final long end;
  private final int id;
  private long offset;
  private final ByteBuffer cache;
  private Integer indexID;
  private ByteBuffer keyBuf = ByteBuffer.allocate(128);
  /** Possible states while reading a record. */
  private static enum RecordState
  {
    START, NEED_INSERT_ID_SET, NEED_DELETE_ID_SET
  }
  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
  private final IndexManager indexMgr;
  private final FileChannel channel;
  private final long begin;
  private final long end;
  private final int bufferID;
  private long offset;
  private final ByteBuffer cache;
  // Next fields are the fetched record data
  private Integer indexID;
  private ByteBuffer keyBuf = ByteBuffer.allocate(128);
  private RecordState recordState = RecordState.START;
  /**
@@ -76,22 +77,22 @@
   *          The position of the start of the buffer in the scratch file.
   * @param end
   *          The position of the end of the buffer in the scratch file.
   * @param id
   *          The index ID.
   * @param bufferID
   *          The buffer ID.
   * @param cacheSize
   *          The cache size.
   * @throws IOException
   *           If an IO error occurred when priming the cache.
   */
  public IndexInputBuffer(IndexManager indexMgr, FileChannel channel,
      long begin, long end, int id, int cacheSize) throws IOException
      long begin, long end, int bufferID, int cacheSize) throws IOException
  {
    this.indexMgr = indexMgr;
    this.channel = channel;
    this.begin = begin;
    this.end = end;
    this.offset = 0;
    this.id = id;
    this.bufferID = bufferID;
    this.cache = ByteBuffer.allocate(Math.max(cacheSize - 384, 256));
    loadCache();
@@ -99,8 +100,6 @@
    keyBuf.flip();
  }
  private void loadCache() throws IOException
  {
    channel.position(begin + offset);
@@ -124,8 +123,6 @@
    indexMgr.addBytesRead(bytesRead);
  }
  /**
   * Returns {@code true} if this buffer has more data.
   *
@@ -139,8 +136,6 @@
    return cache.remaining() != 0 || hasMore;
  }
  /**
   * Returns the length of the next key.
   *
@@ -151,22 +146,18 @@
    return keyBuf.limit();
  }
  /**
   * Returns the next key.
   * Fetches the next key into the provided byte buffer.
   *
   * @param b
   *          A buffer into which the key should be added.
   *          A buffer where to fetch the key
   */
  public void getKey(ByteBuffer b)
  public void fetchKey(ByteBuffer b)
  {
    keyBuf.get(b.array(), 0, keyBuf.limit());
    b.limit(keyBuf.limit());
  }
  /**
   * Returns the index ID of the next record.
   *
@@ -178,20 +169,17 @@
    {
      try
      {
        getNextRecord();
        fetchNextRecord();
      }
      catch (IOException ex)
      {
        logger.error(ERR_JEB_IMPORT_BUFFER_IO_ERROR, indexMgr
            .getBufferFileName());
        logger.error(ERR_JEB_IMPORT_BUFFER_IO_ERROR, indexMgr.getBufferFileName());
        throw new RuntimeException(ex);
      }
    }
    return indexID;
  }
  /**
   * Reads the next record from the buffer, skipping any remaining data in the
   * current record.
@@ -199,7 +187,7 @@
   * @throws IOException
   *           If an IO error occurred.
   */
  public void getNextRecord() throws IOException
  public void fetchNextRecord() throws IOException
  {
    switch (recordState)
    {
@@ -237,16 +225,12 @@
    recordState = RecordState.NEED_INSERT_ID_SET;
  }
  private int getInt() throws IOException
  {
    ensureData(4);
    return cache.getInt();
  }
  /**
   * Reads the next ID set from the record and merges it with the provided ID
   * set.
@@ -301,30 +285,25 @@
    }
  }
  private boolean ensureData(int len) throws IOException
  {
    boolean ret = false;
    if (cache.remaining() == 0)
    {
      cache.clear();
      loadCache();
      cache.flip();
      ret = true;
      return true;
    }
    else if (cache.remaining() < len)
    {
      cache.compact();
      loadCache();
      cache.flip();
      ret = true;
      return true;
    }
    return ret;
    return false;
  }
  /**
   * Compares this buffer with the provided key and index ID.
   *
@@ -338,12 +317,9 @@
   */
  int compare(ByteBuffer cKey, Integer cIndexID)
  {
    if (keyBuf.limit() == 0)
    {
      getIndexID();
    }
    final int rc = Importer.indexComparator.compare(keyBuf.array(), 0, keyBuf.limit(), cKey.array(), cKey.limit());
    if (rc == 0)
    ensureRecordFetched();
    final int cmp = Importer.indexComparator.compare(keyBuf.array(), 0, keyBuf.limit(), cKey.array(), cKey.limit());
    if (cmp == 0)
    {
      return (indexID.intValue() == cIndexID.intValue()) ? 0 : 1;
    }
@@ -360,28 +336,36 @@
      return 0;
    }
    if (keyBuf.limit() == 0)
    {
      getIndexID();
    }
    if (o.keyBuf.limit() == 0)
    {
      o.getIndexID();
    }
    ensureRecordFetched();
    o.ensureRecordFetched();
    byte[] oKey = o.keyBuf.array();
    int oLen = o.keyBuf.limit();
    int returnCode = Importer.indexComparator.compare(keyBuf.array(), 0,
        keyBuf.limit(), oKey, oLen);
    if (returnCode == 0)
    int cmp = Importer.indexComparator.compare(keyBuf.array(), 0, keyBuf.limit(), oKey, oLen);
    if (cmp == 0)
    {
      returnCode = indexID.intValue() - o.getIndexID().intValue();
      if (returnCode == 0)
      cmp = indexID.intValue() - o.getIndexID().intValue();
      if (cmp == 0)
      {
        returnCode = id - o.id;
        return bufferID - o.bufferID;
      }
    }
    return returnCode;
    return cmp;
  }
  private void ensureRecordFetched()
  {
    if (keyBuf.limit() == 0)
    {
      try
      {
        fetchNextRecord();
      }
      catch (IOException ex)
      {
        logger.error(ERR_JEB_IMPORT_BUFFER_IO_ERROR, indexMgr.getBufferFileName());
        throw new RuntimeException(ex);
      }
    }
  }
}