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  
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  
26  /**
27   * Utility functions for region server storage layer.
28   */
29  @InterfaceAudience.Private
30  public class StoreUtils {
31    /**
32     * Creates a deterministic hash code for store file collection.
33     */
34    public static Integer getDeterministicRandomSeed(final Collection<StoreFile> files) {
35      if (files != null && !files.isEmpty()) {
36        return files.iterator().next().getPath().getName().hashCode();
37      }
38      return null;
39    }
40  
41    /**
42     * Determines whether any files in the collection are references.
43     * @param files The files.
44     */
45    public static boolean hasReferences(final Collection<StoreFile> files) {
46      if (files != null) {
47        for (StoreFile hsf: files) {
48          if (hsf.isReference()) {
49            return true;
50          }
51        }
52      }
53      return false;
54    }
55  
56    /**
57     * Gets lowest timestamp from candidate StoreFiles
58     */
59    public static long getLowestTimestamp(final Collection<StoreFile> candidates)
60      throws IOException {
61      long minTs = Long.MAX_VALUE;
62      for (StoreFile storeFile : candidates) {
63        minTs = Math.min(minTs, storeFile.getModificationTimeStamp());
64      }
65      return minTs;
66    }
67  
68    /**
69     * Gets the largest file (with reader) out of the list of files.
70     * @param candidates The files to choose from.
71     * @return The largest file; null if no file has a reader.
72     */
73    static StoreFile getLargestFile(final Collection<StoreFile> candidates) {
74      long maxSize = -1L;
75      StoreFile largestSf = null;
76      for (StoreFile sf : candidates) {
77        StoreFile.Reader r = sf.getReader();
78        if (r == null) continue;
79        long size = r.length();
80        if (size > maxSize) {
81          maxSize = size;
82          largestSf = sf;
83        }
84      }
85      return largestSf;
86    }
87  }