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.regionserver.compactions;
019
020import static org.mockito.Mockito.mock;
021import static org.mockito.Mockito.when;
022
023import java.util.LinkedList;
024import java.util.List;
025import java.util.concurrent.ThreadLocalRandom;
026import org.apache.commons.lang3.RandomStringUtils;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.regionserver.HStoreFile;
029import org.apache.hadoop.hbase.regionserver.StoreFileReader;
030import org.apache.hadoop.util.StringUtils;
031
032import org.apache.hbase.thirdparty.com.google.common.base.MoreObjects;
033
034/**
035 * Base class of objects that can create mock store files with a given size.
036 */
037class MockStoreFileGenerator {
038  /** How many chars long the store file name will be. */
039  private static final int FILENAME_LENGTH = 10;
040
041  protected List<HStoreFile> createStoreFileList(final int[] fs) {
042    List<HStoreFile> storeFiles = new LinkedList<>();
043    for (int fileSize : fs) {
044      storeFiles.add(createMockStoreFile(fileSize));
045    }
046    return storeFiles;
047  }
048
049  protected HStoreFile createMockStoreFile(final long size) {
050    return createMockStoreFile(size * 1024 * 1024, -1L);
051  }
052
053  protected HStoreFile createMockStoreFileBytes(final long size) {
054    return createMockStoreFile(size, -1L);
055  }
056
057  protected HStoreFile createMockStoreFile(final long sizeInBytes, final long seqId) {
058    HStoreFile mockSf = mock(HStoreFile.class);
059    StoreFileReader reader = mock(StoreFileReader.class);
060    String stringPath = "/hbase/testTable/regionA/" + RandomStringUtils.random(FILENAME_LENGTH, 0,
061      0, true, true, null, ThreadLocalRandom.current());
062    Path path = new Path(stringPath);
063
064    when(reader.getSequenceID()).thenReturn(seqId);
065    when(reader.getTotalUncompressedBytes()).thenReturn(sizeInBytes);
066    when(reader.length()).thenReturn(sizeInBytes);
067
068    when(mockSf.getPath()).thenReturn(path);
069    when(mockSf.excludeFromMinorCompaction()).thenReturn(false);
070    when(mockSf.isReference()).thenReturn(false); // TODO come back to
071    // this when selection takes this into account
072    when(mockSf.getReader()).thenReturn(reader);
073    String toString = MoreObjects.toStringHelper("MockStoreFile").add("isReference", false)
074      .add("fileSize", StringUtils.humanReadableInt(sizeInBytes)).add("seqId", seqId)
075      .add("path", stringPath).toString();
076    when(mockSf.toString()).thenReturn(toString);
077
078    return mockSf;
079  }
080}