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