private void map() throws StoreException {
      try (RandomAccessFile raf = new RandomAccessFile(indexFile, "r")) {
        switch (config.storeIndexMemState) {
          case IN_DIRECT_MEM:
            serEntries = readFileIntoBuffer(raf, true);
            break;
          case IN_HEAP_MEM:
            serEntries = readFileIntoBuffer(raf, false);
            break;
          default:
            MappedByteBuffer buf = raf.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, indexFile.length());
            if (config.storeIndexMemState == IndexMemState.MMAP_WITH_FORCE_LOAD) {
              buf.load();
            }
            serEntries = buf;
            break;
        }
        checkDataIntegrity();
        // We've checked the CRC and it matches, which means this index segment file is intact. All the errors from here
        // on are logical errors, not format errors.
        serEntries.position(0);
        setVersion(serEntries.getShort());
        StoreKey storeKey;
        int keySize;
        PersistentIndex.IndexEntryType resetKeyType;
        short resetKeyLifeVersion = UNINITIALIZED_RESET_KEY_VERSION;
        switch (getVersion()) {
          case PersistentIndex.VERSION_0:
            indexSizeExcludingEntries = VERSION_FIELD_LENGTH + KEY_OR_ENTRY_SIZE_FIELD_LENGTH + VALUE_SIZE_FIELD_LENGTH
                + LOG_END_OFFSET_FIELD_LENGTH + CRC_FIELD_LENGTH;
            keySize = serEntries.getInt();
            persistedValueSize = serEntries.getInt();
            persistedEntrySize = keySize + persistedValueSize;
            endOffset.set(new Offset(startOffset.getName(), serEntries.getLong()));
            lastModifiedTimeSec.set(indexFile.lastModified() / 1000);
            firstKeyRelativeOffset = indexSizeExcludingEntries - CRC_FIELD_LENGTH;
            break;
          case PersistentIndex.VERSION_1:
            keySize = serEntries.getInt();
            persistedValueSize = serEntries.getInt();
            persistedEntrySize = keySize + persistedValueSize;
            endOffset.set(new Offset(startOffset.getName(), serEntries.getLong()));
            lastModifiedTimeSec.set(serEntries.getLong());
            storeKey = factory.getStoreKey(new DataInputStream(new ByteBufferInputStream(serEntries)));
            resetKeyType = PersistentIndex.IndexEntryType.values()[serEntries.getShort()];
            resetKeyInfo = new ResetKeyInfo(storeKey, resetKeyType, resetKeyLifeVersion);
            indexSizeExcludingEntries = VERSION_FIELD_LENGTH + KEY_OR_ENTRY_SIZE_FIELD_LENGTH + VALUE_SIZE_FIELD_LENGTH
                + LOG_END_OFFSET_FIELD_LENGTH + CRC_FIELD_LENGTH + LAST_MODIFIED_TIME_FIELD_LENGTH
                + storeKey.sizeInBytes() + RESET_KEY_TYPE_FIELD_LENGTH;
            firstKeyRelativeOffset = indexSizeExcludingEntries - CRC_FIELD_LENGTH;
            break;
          case PersistentIndex.VERSION_2:
          case PersistentIndex.VERSION_3:
            persistedEntrySize = serEntries.getInt();
            persistedValueSize = serEntries.getInt();
            endOffset.set(new Offset(startOffset.getName(), serEntries.getLong()));
            lastModifiedTimeSec.set(serEntries.getLong());
            storeKey = factory.getStoreKey(new DataInputStream(new ByteBufferInputStream(serEntries)));
            resetKeyType = PersistentIndex.IndexEntryType.values()[serEntries.getShort()];
            resetKeyInfo = new ResetKeyInfo(storeKey, resetKeyType, resetKeyLifeVersion);
            indexSizeExcludingEntries = VERSION_FIELD_LENGTH + KEY_OR_ENTRY_SIZE_FIELD_LENGTH + VALUE_SIZE_FIELD_LENGTH
                + LOG_END_OFFSET_FIELD_LENGTH + CRC_FIELD_LENGTH + LAST_MODIFIED_TIME_FIELD_LENGTH
                + storeKey.sizeInBytes() + RESET_KEY_TYPE_FIELD_LENGTH;
            firstKeyRelativeOffset = indexSizeExcludingEntries - CRC_FIELD_LENGTH;
            break;
          case PersistentIndex.VERSION_4:
            persistedEntrySize = serEntries.getInt();
            persistedValueSize = serEntries.getInt();
            endOffset.set(new Offset(startOffset.getName(), serEntries.getLong()));
            lastModifiedTimeSec.set(serEntries.getLong());
            storeKey = factory.getStoreKey(new DataInputStream(new ByteBufferInputStream(serEntries)));
            resetKeyType = PersistentIndex.IndexEntryType.values()[serEntries.getShort()];
            resetKeyLifeVersion = serEntries.getShort();
            resetKeyInfo = new ResetKeyInfo(storeKey, resetKeyType, resetKeyLifeVersion);
            indexSizeExcludingEntries = VERSION_FIELD_LENGTH + KEY_OR_ENTRY_SIZE_FIELD_LENGTH + VALUE_SIZE_FIELD_LENGTH
                + LOG_END_OFFSET_FIELD_LENGTH + CRC_FIELD_LENGTH + LAST_MODIFIED_TIME_FIELD_LENGTH
                + storeKey.sizeInBytes() + RESET_KEY_TYPE_FIELD_LENGTH + RESET_KEY_VERSION_FIELD_LENGTH;
            firstKeyRelativeOffset = indexSizeExcludingEntries - CRC_FIELD_LENGTH;
            break;
          default:
            throw new StoreException("IndexSegment : " + indexFile.getAbsolutePath() + " unknown version in index file",
                StoreErrorCodes.IndexVersionError);
        }
        index = null;
      } catch (FileNotFoundException e) {
        throw new StoreException("File not found while mapping the segment of index", e, StoreErrorCodes.FileNotFound);
      } catch (IOException e) {
        StoreErrorCodes errorCode = StoreException.resolveErrorCode(e);
        throw new StoreException(errorCode.toString() + " while mapping the segment of index", e, errorCode);
      }
    }
Maps the segment of index either as a memory map or a in memory buffer depending on config.
@throws StoreException if there are problems with the index
ambry-store/src/main/java/com/github/ambry/store/IndexSegment.java