View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.regionserver;
22  
23  import org.apache.hadoop.hbase.HConstants;
24  import org.apache.hadoop.hbase.KeyValue;
25  
26  import java.io.IOException;
27  import java.util.List;
28  
29  /**
30   * A scanner that does a minor compaction at the same time.  Doesn't need to
31   * implement ChangedReadersObserver, since it doesn't scan memstore, only store files
32   * and optionally the memstore-snapshot.
33   */
34  public class MinorCompactingStoreScanner implements KeyValueScanner, InternalScanner {
35    private KeyValueHeap heap;
36    private KeyValue.KVComparator comparator;
37  
38    MinorCompactingStoreScanner(Store store, List<? extends KeyValueScanner> scanners)
39        throws IOException {
40      comparator = store.comparator;
41      KeyValue firstKv = KeyValue.createFirstOnRow(HConstants.EMPTY_START_ROW);
42      for (KeyValueScanner scanner : scanners ) {
43        scanner.seek(firstKv);
44      }
45      heap = new KeyValueHeap(scanners, store.comparator);
46    }
47  
48    MinorCompactingStoreScanner(String cfName, KeyValue.KVComparator comparator,
49                                List<? extends KeyValueScanner> scanners)
50        throws IOException {
51      this.comparator = comparator;
52  
53      KeyValue firstKv = KeyValue.createFirstOnRow(HConstants.EMPTY_START_ROW);
54      for (KeyValueScanner scanner : scanners ) {
55        scanner.seek(firstKv);
56      }
57  
58      heap = new KeyValueHeap(scanners, comparator);
59    }
60  
61    public KeyValue peek() {
62      return heap.peek();
63    }
64  
65    public KeyValue next() throws IOException {
66      return heap.next();
67    }
68  
69    @Override
70    public boolean seek(KeyValue key) {
71      // cant seek.
72      throw new UnsupportedOperationException("Can't seek a MinorCompactingStoreScanner");
73    }
74  
75    public boolean reseek(KeyValue key) {
76      return seek(key);
77    }
78  
79    /**
80     * High performance merge scan.
81     * @param writer
82     * @return True if more.
83     * @throws IOException
84     */
85    public boolean next(StoreFile.Writer writer) throws IOException {
86      KeyValue row = heap.peek();
87      if (row == null) {
88        close();
89        return false;
90      }
91      KeyValue kv;
92      while ((kv = heap.peek()) != null) {
93        // check to see if this is a different row
94        if (comparator.compareRows(row, kv) != 0) {
95          // reached next row
96          return true;
97        }
98        writer.append(heap.next());
99      }
100     close();
101     return false;
102   }
103 
104   @Override
105   public boolean next(List<KeyValue> results) throws IOException {
106     KeyValue row = heap.peek();
107     if (row == null) {
108       close();
109       return false;
110     }
111     KeyValue kv;
112     while ((kv = heap.peek()) != null) {
113       // check to see if this is a different row
114       if (comparator.compareRows(row, kv) != 0) {
115         // reached next row
116         return true;
117       }
118       results.add(heap.next());
119     }
120     close();
121     return false;
122   }
123 
124   @Override
125   public boolean next(List<KeyValue> results, int limit) throws IOException {
126     // should not use limits with minor compacting store scanner
127     return next(results);
128   }
129 
130   public void close() {
131     heap.close();
132   }
133 }