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