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.util;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.SortedSet;
26  
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.Cell;
29  import org.apache.hadoop.hbase.CellComparator;
30  import org.apache.hadoop.hbase.regionserver.NonReversedNonLazyKeyValueScanner;
31  
32  /**
33   * Utility scanner that wraps a sortable collection and serves as a KeyValueScanner.
34   */
35  @InterfaceAudience.Private
36  public class CollectionBackedScanner extends NonReversedNonLazyKeyValueScanner {
37    final private Iterable<Cell> data;
38    final CellComparator comparator;
39    private Iterator<Cell> iter;
40    private Cell current;
41  
42    public CollectionBackedScanner(SortedSet<Cell> set) {
43      this(set, CellComparator.COMPARATOR);
44    }
45  
46    public CollectionBackedScanner(SortedSet<Cell> set,
47        CellComparator comparator) {
48      this.comparator = comparator;
49      data = set;
50      init();
51    }
52  
53    public CollectionBackedScanner(List<Cell> list) {
54      this(list, CellComparator.COMPARATOR);
55    }
56  
57    public CollectionBackedScanner(List<Cell> list,
58        CellComparator comparator) {
59      Collections.sort(list, comparator);
60      this.comparator = comparator;
61      data = list;
62      init();
63    }
64  
65    public CollectionBackedScanner(CellComparator comparator,
66        Cell... array) {
67      this.comparator = comparator;
68  
69      List<Cell> tmp = new ArrayList<Cell>(array.length);
70      Collections.addAll(tmp, array);
71      Collections.sort(tmp, comparator);
72      data = tmp;
73      init();
74    }
75  
76    private void init() {
77      iter = data.iterator();
78      if(iter.hasNext()){
79        current = iter.next();
80      }
81    }
82  
83    @Override
84    public Cell peek() {
85      return current;
86    }
87  
88    @Override
89    public Cell next() {
90      Cell oldCurrent = current;
91      if(iter.hasNext()){
92        current = iter.next();
93      } else {
94        current = null;
95      }
96      return oldCurrent;
97    }
98  
99    @Override
100   public boolean seek(Cell seekCell) {
101     // restart iterator
102     iter = data.iterator();
103     return reseek(seekCell);
104   }
105 
106   @Override
107   public boolean reseek(Cell seekCell) {
108     while(iter.hasNext()){
109       Cell next = iter.next();
110       int ret = comparator.compare(next, seekCell);
111       if(ret >= 0){
112         current = next;
113         return true;
114       }
115     }
116     return false;
117   }
118 
119   /**
120    * @see org.apache.hadoop.hbase.regionserver.KeyValueScanner#getScannerOrder()
121    */
122   @Override
123   public long getScannerOrder() {
124     return 0;
125   }
126
127   @Override
128   public void close() {
129     // do nothing
130   }
131 }