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; 022import static org.junit.jupiter.api.Assertions.assertEquals; 023 024import java.io.File; 025import java.io.IOException; 026import org.apache.hadoop.hbase.nio.ByteBuff; 027import org.apache.hadoop.hbase.testclassification.IOTests; 028import org.apache.hadoop.hbase.testclassification.SmallTests; 029import org.junit.jupiter.api.Tag; 030import org.junit.jupiter.api.Test; 031 032/** 033 * Basic test for {@link ExclusiveMemoryMmapIOEngine} 034 */ 035@Tag(IOTests.TAG) 036@Tag(SmallTests.TAG) 037public class TestExclusiveMemoryMmapEngine { 038 039 @Test 040 public void testFileMmapEngine() throws IOException { 041 int size = 2 * 1024 * 1024; // 2 MB 042 String filePath = "testFileMmapEngine"; 043 try { 044 ExclusiveMemoryMmapIOEngine fileMmapEngine = new ExclusiveMemoryMmapIOEngine(filePath, size); 045 for (int i = 0; i < 50; i++) { 046 int len = (int) Math.floor(Math.random() * 100); 047 long offset = (long) Math.floor(Math.random() * size % (size - len)); 048 int val = (int) (Math.random() * 255); 049 050 // write 051 ByteBuff src = TestByteBufferIOEngine.createByteBuffer(len, val, i % 2 == 0); 052 int pos = src.position(), lim = src.limit(); 053 fileMmapEngine.write(src, offset); 054 src.position(pos).limit(lim); 055 056 // read 057 BucketEntry be = createBucketEntry(offset, len); 058 fileMmapEngine.read(be); 059 ByteBuff dst = getByteBuff(be); 060 061 assertEquals(src.remaining(), len); 062 assertEquals(dst.remaining(), len); 063 assertEquals(0, ByteBuff.compareTo(src, pos, len, dst, dst.position(), dst.remaining())); 064 } 065 } finally { 066 File file = new File(filePath); 067 if (file.exists()) { 068 file.delete(); 069 } 070 } 071 } 072}