View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.regionserver.compactions;
20  
21  import java.util.LinkedList;
22  import java.util.List;
23  import java.util.Random;
24  
25  import com.google.common.base.Objects;
26  import com.google.common.io.Files;
27  import org.apache.commons.lang.RandomStringUtils;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.hbase.regionserver.StoreFile;
30  import org.apache.hadoop.util.StringUtils;
31  
32  import static org.mockito.Mockito.mock;
33  import static org.mockito.Mockito.when;
34  
35  /**
36   * Base class of objects that can create mock store files with a given size.
37   */
38  class MockStoreFileGenerator {
39    /** How many chars long the store file name will be. */
40    private static final int FILENAME_LENGTH = 10;
41    /** The random number generator. */
42    protected Random random;
43  
44    MockStoreFileGenerator(Class klass) {
45      random = new Random(klass.getSimpleName().hashCode());
46    }
47  
48    protected List<StoreFile> createStoreFileList(final int[] fs) {
49      List<StoreFile> storeFiles = new LinkedList<StoreFile>();
50      for (int fileSize : fs) {
51        storeFiles.add(createMockStoreFile(fileSize));
52      }
53      return storeFiles;
54    }
55  
56    protected StoreFile createMockStoreFile(final long size) {
57      return createMockStoreFile(size * 1024 * 1024, -1L);
58    }
59  
60    protected StoreFile createMockStoreFileBytes(final long size) {
61      return createMockStoreFile(size, -1L);
62    }
63  
64    protected StoreFile createMockStoreFile(final long sizeInBytes, final long seqId) {
65      StoreFile mockSf = mock(StoreFile.class);
66      StoreFile.Reader reader = mock(StoreFile.Reader.class);
67      String stringPath = "/hbase/testTable/regionA/"
68          + RandomStringUtils.random(FILENAME_LENGTH, 0, 0, true, true, null, random);
69      Path path = new Path(stringPath);
70  
71  
72      when(reader.getSequenceID()).thenReturn(seqId);
73      when(reader.getTotalUncompressedBytes()).thenReturn(sizeInBytes);
74      when(reader.length()).thenReturn(sizeInBytes);
75  
76      when(mockSf.getPath()).thenReturn(path);
77      when(mockSf.excludeFromMinorCompaction()).thenReturn(false);
78      when(mockSf.isReference()).thenReturn(false); // TODO come back to
79      // this when selection takes this into account
80      when(mockSf.getReader()).thenReturn(reader);
81      String toString = Objects.toStringHelper("MockStoreFile")
82          .add("isReference", false)
83          .add("fileSize", StringUtils.humanReadableInt(sizeInBytes))
84          .add("seqId", seqId)
85          .add("path", stringPath).toString();
86      when(mockSf.toString()).thenReturn(toString);
87  
88      return mockSf;
89    }
90  }