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.mob.compactions;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.fs.FileStatus;
28  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
29  
30  /**
31   * An implementation of {@link MobCompactionRequest} that is used in
32   * {@link PartitionedMobCompactor}.
33   * The mob files that have the same start key and date in their names belong to
34   * the same partition.
35   */
36  @InterfaceAudience.Private
37  public class PartitionedMobCompactionRequest extends MobCompactionRequest {
38  
39    protected Collection<FileStatus> delFiles;
40    protected Collection<CompactionPartition> compactionPartitions;
41  
42    public PartitionedMobCompactionRequest(Collection<CompactionPartition> compactionPartitions,
43      Collection<FileStatus> delFiles) {
44      this.selectionTime = EnvironmentEdgeManager.currentTime();
45      this.compactionPartitions = compactionPartitions;
46      this.delFiles = delFiles;
47    }
48  
49    /**
50     * Gets the compaction partitions.
51     * @return The compaction partitions.
52     */
53    public Collection<CompactionPartition> getCompactionPartitions() {
54      return this.compactionPartitions;
55    }
56  
57    /**
58     * Gets the del files.
59     * @return The del files.
60     */
61    public Collection<FileStatus> getDelFiles() {
62      return this.delFiles;
63    }
64  
65    /**
66     * The partition in the mob compaction.
67     * The mob files that have the same start key and date in their names belong to
68     * the same partition.
69     */
70    protected static class CompactionPartition {
71      private List<FileStatus> files = new ArrayList<FileStatus>();
72      private CompactionPartitionId partitionId;
73  
74      public CompactionPartition(CompactionPartitionId partitionId) {
75        this.partitionId = partitionId;
76      }
77  
78      public CompactionPartitionId getPartitionId() {
79        return this.partitionId;
80      }
81  
82      public void addFile(FileStatus file) {
83        files.add(file);
84      }
85  
86      public List<FileStatus> listFiles() {
87        return Collections.unmodifiableList(files);
88      }
89      public int getFileCount () {
90        return files.size();
91      }
92    }
93  
94    /**
95     * The partition id that consists of start key and date of the mob file name.
96     */
97    public static class CompactionPartitionId {
98      private String startKey;
99      private String date;
100 
101     public CompactionPartitionId() {
102       // initialize these fields to empty string
103       this.startKey = "";
104       this.date = "";
105     }
106 
107     public CompactionPartitionId(String startKey, String date) {
108       if (startKey == null || date == null) {
109         throw new IllegalArgumentException("Neither of start key and date could be null");
110       }
111       this.startKey = startKey;
112       this.date = date;
113     }
114 
115     public String getStartKey() {
116       return this.startKey;
117     }
118 
119     public void setStartKey(final String startKey) {
120       this.startKey = startKey;
121     }
122 
123     public String getDate() {
124       return this.date;
125     }
126 
127     public void setDate(final String date) {
128       this.date = date;
129     }
130 
131     @Override
132     public int hashCode() {
133       int result = 17;
134       result = 31 * result + startKey.hashCode();
135       result = 31 * result + date.hashCode();
136       return result;
137     }
138 
139     @Override
140     public boolean equals(Object obj) {
141       if (this == obj) {
142         return true;
143       }
144       if (!(obj instanceof CompactionPartitionId)) {
145         return false;
146       }
147       CompactionPartitionId another = (CompactionPartitionId) obj;
148       if (!this.startKey.equals(another.startKey)) {
149         return false;
150       }
151       if (!this.date.equals(another.date)) {
152         return false;
153       }
154       return true;
155     }
156 
157     @Override
158     public String toString() {
159       return new StringBuilder(startKey).append(date).toString();
160     }
161   }
162 }