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;
20  
21  import java.io.IOException;
22  import java.util.Map;
23  import java.util.TreeMap;
24  
25  import org.apache.hadoop.fs.Path;
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.HDFSBlocksDistribution;
28  import org.apache.hadoop.hbase.KeyValue.KVComparator;
29  import org.apache.hadoop.hbase.io.hfile.CacheConfig;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
32  
33  /** A mock used so our tests don't deal with actual StoreFiles */
34  public class MockStoreFile extends StoreFile {
35    long length = 0;
36    boolean isRef = false;
37    long ageInDisk;
38    long sequenceid;
39    private Map<byte[], byte[]> metadata = new TreeMap<byte[], byte[]>(Bytes.BYTES_COMPARATOR);
40    byte[] splitPoint = null;
41    TimeRangeTracker timeRangeTracker;
42    long entryCount;
43    boolean isMajor;
44    HDFSBlocksDistribution hdfsBlocksDistribution;
45    long modificationTime;
46    
47    MockStoreFile(HBaseTestingUtility testUtil, Path testPath,
48        long length, long ageInDisk, boolean isRef, long sequenceid) throws IOException {
49      super(testUtil.getTestFileSystem(), testPath, testUtil.getConfiguration(),
50        new CacheConfig(testUtil.getConfiguration()), BloomType.NONE);
51      this.length = length;
52      this.isRef = isRef;
53      this.ageInDisk = ageInDisk;
54      this.sequenceid = sequenceid;
55      this.isMajor = false;
56      hdfsBlocksDistribution = new HDFSBlocksDistribution();
57      hdfsBlocksDistribution.addHostsAndBlockWeight(
58        new String[] { HRegionServer.getHostname(testUtil.getConfiguration()) }, 1);
59      modificationTime = EnvironmentEdgeManager.currentTimeMillis();
60    }
61  
62    void setLength(long newLen) {
63      this.length = newLen;
64    }
65  
66    @Override
67    byte[] getFileSplitPoint(KVComparator comparator) throws IOException {
68      return this.splitPoint;
69    }
70  
71    @Override
72    public long getMaxSequenceId() {
73      return sequenceid;
74    }
75  
76    @Override
77    public boolean isMajorCompaction() {
78      return isMajor;
79    }
80  
81    public void setIsMajor(boolean isMajor) {
82      this.isMajor = isMajor;
83    }
84    @Override
85    public boolean isReference() {
86      return this.isRef;
87    }
88  
89    @Override
90    public boolean isBulkLoadResult() {
91      return false;
92    }
93  
94    @Override
95    public byte[] getMetadataValue(byte[] key) {
96      return this.metadata.get(key);
97    }
98  
99    public void setMetadataValue(byte[] key, byte[] value) {
100     this.metadata.put(key, value);
101   }
102 
103   void setTimeRangeTracker(TimeRangeTracker timeRangeTracker) {
104     this.timeRangeTracker = timeRangeTracker;
105   }
106 
107   void setEntries(long entryCount) {
108     this.entryCount = entryCount;
109   }
110 
111   public Long getMinimumTimestamp() {
112     return (timeRangeTracker == null) ? null : timeRangeTracker.getMin();
113   }
114 
115   public Long getMaximumTimestamp() {
116     return (timeRangeTracker == null) ? null : timeRangeTracker.getMax();
117   }
118 
119   @Override
120   public HDFSBlocksDistribution getHDFSBlockDistribution() {
121     return hdfsBlocksDistribution;
122   }
123 
124   @Override
125   public long getModificationTimeStamp() {
126     return modificationTime;
127   }
128   
129   @Override
130   public StoreFile.Reader getReader() {
131     final long len = this.length;
132     final long entries = this.entryCount;
133     return new StoreFile.Reader() {
134       @Override
135       public long length() {
136         return len;
137       }
138 
139       @Override
140       public long getMaxTimestamp() {
141         return timeRange == null ? Long.MAX_VALUE : timeRange.getMax();
142       }
143 
144       @Override
145       public long getEntries() {
146         return entries;
147       }
148     };
149   }
150 }