/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.client.file.cache.store;

import alluxio.client.file.cache.PageId;
import alluxio.client.file.cache.PageStore;
import alluxio.exception.PageNotFoundException;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.concurrent.NotThreadSafe;

/**
 * The {@link MemoryPageStore} is an implementation of {@link PageStore} which
 * stores all pages in memory.
 */
@NotThreadSafe
public class MemoryPageStore implements PageStore {
  private static final Logger LOG = LoggerFactory.getLogger(MemoryPageStore.class);

  private ConcurrentHashMap<PageId, byte[]> mPageStoreMap = new ConcurrentHashMap<>();

  @Override
  public void put(PageId pageId, ByteBuffer page, boolean isTemporary) throws IOException {
    //TODO(beinan): support temp page for memory page store
    PageId pageKey = getKeyFromPageId(pageId);
    try {
      byte[] pageCopy = new byte[page.remaining()];
      page.get(pageCopy);
      mPageStoreMap.put(pageKey, pageCopy);
    } catch (Exception e) {
      throw new IOException("Failed to put cached data in memory for page " + pageId);
    }
  }

  @Override
  public int get(PageId pageId, int pageOffset, int bytesToRead, PageReadTargetBuffer target,
      boolean isTemporary) throws IOException, PageNotFoundException {
    Preconditions.checkArgument(target != null, "buffer is null");
    Preconditions.checkArgument(pageOffset >= 0, "page offset should be non-negative");
    PageId pageKey = getKeyFromPageId(pageId);
    if (!mPageStoreMap.containsKey(pageKey)) {
      throw new PageNotFoundException(pageId.getFileId() + "_" + pageId.getPageIndex());
    }
    byte[] page = mPageStoreMap.get(pageKey);
    Preconditions.checkArgument(pageOffset <= page.length, "page offset %s exceeded page size %s",
        pageOffset, page.length);
    int bytesLeft = (int) Math.min(page.length - pageOffset, target.remaining());
    bytesLeft = Math.min(bytesLeft, bytesToRead);
    //todo(beinan) implement zero-copy logic
    System.arraycopy(page, pageOffset, target.byteArray(), (int) target.offset(), bytesLeft);
    return bytesLeft;
  }

  @Override
  public void delete(PageId pageId) throws IOException, PageNotFoundException {
    PageId pageKey = getKeyFromPageId(pageId);
    if (!mPageStoreMap.containsKey(pageKey)) {
      throw new PageNotFoundException(pageId.getFileId() + "_" + pageId.getPageIndex());
    }
    mPageStoreMap.remove(pageKey);
    LOG.info("Remove cached page, size: {}", mPageStoreMap.size());
  }

  /**
   * @param pageId page Id
   * @return the key to this page
   */
  @VisibleForTesting
  public PageId getKeyFromPageId(PageId pageId) {
    // TODO(feng): encode fileId with URLEncoder to escape invalid characters for file name
    // Key is : PageId
    return pageId;
  }

  @Override
  public void close() {
    mPageStoreMap.clear();
    mPageStoreMap = null;
  }

  /**
   *
   */
  public void reset() {
    mPageStoreMap.clear();
  }
}

@@ -787,65 +787,8 @@
 ons;
-%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aim
@@ -1128,93 +1128,8 @@
 e %7B%0A
-  private static final Logger LOG = LoggerFactory.getLogger(MemoryPageStore.class);%0A%0A
   pr
@@ -2999,76 +2999,8 @@
 y);%0A
-    LOG.info(%22Remove cached page, size: %7B%7D%22, mPageStoreMap.size());%0A
   %7D%0A

Remove unnecessary log in memory page store