001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.hfile.bucket;
019
020import static org.apache.hadoop.hbase.io.hfile.bucket.TestByteBufferIOEngine.createBucketEntry;
021import static org.apache.hadoop.hbase.io.hfile.bucket.TestByteBufferIOEngine.getByteBuff;
022
023import java.io.File;
024import java.io.IOException;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.nio.ByteBuff;
027import org.apache.hadoop.hbase.testclassification.IOTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.junit.Assert;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034/**
035 * Basic test for {@link ExclusiveMemoryMmapIOEngine}
036 */
037@Category({ IOTests.class, SmallTests.class })
038public class TestExclusiveMemoryMmapEngine {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestExclusiveMemoryMmapEngine.class);
043
044  @Test
045  public void testFileMmapEngine() throws IOException {
046    int size = 2 * 1024 * 1024; // 2 MB
047    String filePath = "testFileMmapEngine";
048    try {
049      ExclusiveMemoryMmapIOEngine fileMmapEngine = new ExclusiveMemoryMmapIOEngine(filePath, size);
050      for (int i = 0; i < 50; i++) {
051        int len = (int) Math.floor(Math.random() * 100);
052        long offset = (long) Math.floor(Math.random() * size % (size - len));
053        int val = (int) (Math.random() * 255);
054
055        // write
056        ByteBuff src = TestByteBufferIOEngine.createByteBuffer(len, val, i % 2 == 0);
057        int pos = src.position(), lim = src.limit();
058        fileMmapEngine.write(src, offset);
059        src.position(pos).limit(lim);
060
061        // read
062        BucketEntry be = createBucketEntry(offset, len);
063        fileMmapEngine.read(be);
064        ByteBuff dst = getByteBuff(be);
065
066        Assert.assertEquals(src.remaining(), len);
067        Assert.assertEquals(dst.remaining(), len);
068        Assert.assertEquals(0,
069          ByteBuff.compareTo(src, pos, len, dst, dst.position(), dst.remaining()));
070      }
071    } finally {
072      File file = new File(filePath);
073      if (file.exists()) {
074        file.delete();
075      }
076    }
077  }
078}