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