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.ArrayList;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.classification.InterfaceAudience;
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.hbase.KeyValue;
33  import org.apache.hadoop.hbase.KeyValue.KVComparator;
34  
35  import com.google.common.collect.ImmutableCollection;
36  import com.google.common.collect.ImmutableList;
37  import com.google.common.collect.Lists;
38  
39  /**
40   * Default implementation of StoreFileManager. Not thread-safe.
41   */
42  @InterfaceAudience.Private
43  class DefaultStoreFileManager implements StoreFileManager {
44    static final Log LOG = LogFactory.getLog(DefaultStoreFileManager.class);
45  
46    private final KVComparator kvComparator;
47    private final Configuration conf;
48  
49    /**
50     * List of store files inside this store. This is an immutable list that
51     * is atomically replaced when its contents change.
52     */
53    private volatile ImmutableList<StoreFile> storefiles = null;
54  
55    public DefaultStoreFileManager(KVComparator kvComparator, Configuration conf) {
56      this.kvComparator = kvComparator;
57      this.conf = conf;
58    }
59  
60    @Override
61    public void loadFiles(List<StoreFile> storeFiles) {
62      sortAndSetStoreFiles(storeFiles);
63    }
64  
65    @Override
66    public final Collection<StoreFile> getStorefiles() {
67      return storefiles;
68    }
69  
70    @Override
71    public void insertNewFiles(Collection<StoreFile> sfs) throws IOException {
72      ArrayList<StoreFile> newFiles = new ArrayList<StoreFile>(storefiles);
73      newFiles.addAll(sfs);
74      sortAndSetStoreFiles(newFiles);
75    }
76  
77    @Override
78    public ImmutableCollection<StoreFile> clearFiles() {
79      ImmutableList<StoreFile> result = storefiles;
80      storefiles = ImmutableList.of();
81      return result;
82    }
83  
84    @Override
85    public final int getStorefileCount() {
86      return storefiles.size();
87    }
88  
89    @Override
90    public void addCompactionResults(
91      Collection<StoreFile> compactedFiles, Collection<StoreFile> results) {
92      ArrayList<StoreFile> newStoreFiles = Lists.newArrayList(storefiles);
93      newStoreFiles.removeAll(compactedFiles);
94      if (!results.isEmpty()) {
95        newStoreFiles.addAll(results);
96      }
97      sortAndSetStoreFiles(newStoreFiles);
98    }
99  
100   @Override
101   public final Iterator<StoreFile> getCandidateFilesForRowKeyBefore(final KeyValue targetKey) {
102     return new ArrayList<StoreFile>(Lists.reverse(this.storefiles)).iterator();
103   }
104 
105   @Override
106   public Iterator<StoreFile> updateCandidateFilesForRowKeyBefore(
107       Iterator<StoreFile> candidateFiles, final KeyValue targetKey, final KeyValue candidate) {
108     // Default store has nothing useful to do here.
109     // TODO: move this comment when implementing Level:
110     // Level store can trim the list by range, removing all the files which cannot have
111     // any useful candidates less than "candidate".
112     return candidateFiles;
113   }
114 
115   @Override
116   public final byte[] getSplitPoint() throws IOException {
117     if (this.storefiles.isEmpty()) {
118       return null;
119     }
120     return StoreUtils.getLargestFile(this.storefiles).getFileSplitPoint(this.kvComparator);
121   }
122 
123   @Override
124   public final Collection<StoreFile> getFilesForScanOrGet(boolean isGet,
125       byte[] startRow, byte[] stopRow) {
126     // We cannot provide any useful input and already have the files sorted by seqNum.
127     return getStorefiles();
128   }
129 
130   @Override
131   public int getStoreCompactionPriority() {
132     int blockingFileCount = conf.getInt(
133         HStore.BLOCKING_STOREFILES_KEY, HStore.DEFAULT_BLOCKING_STOREFILE_COUNT);
134     int priority = blockingFileCount - storefiles.size();
135     return (priority == HStore.PRIORITY_USER) ? priority + 1 : priority;
136   }
137 
138   private void sortAndSetStoreFiles(List<StoreFile> storeFiles) {
139     Collections.sort(storeFiles, StoreFile.Comparators.SEQ_ID);
140     storefiles = ImmutableList.copyOf(storeFiles);
141   }
142 
143 }
144