View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.io.IOException;
22  import java.util.Collection;
23  import java.util.List;
24  
25  import org.apache.hadoop.classification.InterfaceAudience;
26  
27  /**
28   * Utility functions for region server storage layer.
29   */
30  @InterfaceAudience.Private
31  public class StoreUtils {
32    /**
33     * Creates a deterministic hash code for store file collection.
34     */
35    public static Integer getDeterministicRandomSeed(final Collection<StoreFile> files) {
36      if (files != null && !files.isEmpty()) {
37        return files.iterator().next().getPath().getName().hashCode();
38      }
39      return null;
40    }
41  
42    /**
43     * Determines whether any files in the collection are references.
44     * @param files The files.
45     */
46    public static boolean hasReferences(final Collection<StoreFile> files) {
47      if (files != null) {
48        for (StoreFile hsf: files) {
49          if (hsf.isReference()) {
50            return true;
51          }
52        }
53      }
54      return false;
55    }
56  
57    /**
58     * Gets lowest timestamp from candidate StoreFiles
59     */
60    public static long getLowestTimestamp(final Collection<StoreFile> candidates)
61      throws IOException {
62      long minTs = Long.MAX_VALUE;
63      for (StoreFile storeFile : candidates) {
64        minTs = Math.min(minTs, storeFile.getModificationTimeStamp());
65      }
66      return minTs;
67    }
68  
69    /**
70     * Gets the largest file (with reader) out of the list of files.
71     * @param candidates The files to choose from.
72     * @return The largest file; null if no file has a reader.
73     */
74    static StoreFile getLargestFile(final Collection<StoreFile> candidates) {
75      long maxSize = -1L;
76      StoreFile largestSf = null;
77      for (StoreFile sf : candidates) {
78        StoreFile.Reader r = sf.getReader();
79        if (r == null) continue;
80        long size = r.length();
81        if (size > maxSize) {
82          maxSize = size;
83          largestSf = sf;
84        }
85      }
86      return largestSf;
87    }
88  }