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.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<KeyValue> data;
39    final KeyValue.KVComparator comparator;
40    private Iterator<KeyValue> iter;
41    private KeyValue current;
42  
43    public CollectionBackedScanner(SortedSet<KeyValue> set) {
44      this(set, KeyValue.COMPARATOR);
45    }
46  
47    public CollectionBackedScanner(SortedSet<KeyValue> set,
48        KeyValue.KVComparator comparator) {
49      this.comparator = comparator;
50      data = set;
51      init();
52    }
53  
54    public CollectionBackedScanner(List<KeyValue> list) {
55      this(list, KeyValue.COMPARATOR);
56    }
57  
58    public CollectionBackedScanner(List<KeyValue> 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        KeyValue... array) {
68      this.comparator = comparator;
69  
70      List<KeyValue> tmp = new ArrayList<KeyValue>(array.length);
71      for( int i = 0; i < array.length ; ++i) {
72        tmp.add(array[i]);
73      }
74      Collections.sort(tmp, comparator);
75      data = tmp;
76      init();
77    }
78  
79    private void init() {
80      iter = data.iterator();
81      if(iter.hasNext()){
82        current = iter.next();
83      }
84    }
85  
86    @Override
87    public KeyValue peek() {
88      return current;
89    }
90  
91    @Override
92    public KeyValue next() {
93      KeyValue oldCurrent = current;
94      if(iter.hasNext()){
95        current = iter.next();
96      } else {
97        current = null;
98      }
99      return oldCurrent;
100   }
101 
102   @Override
103   public boolean seek(Cell seekKv) {
104     // restart iterator
105     iter = data.iterator();
106     return reseek(seekKv);
107   }
108 
109   @Override
110   public boolean reseek(Cell seekKv) {
111     while(iter.hasNext()){
112       KeyValue next = iter.next();
113       int ret = comparator.compare(next, seekKv);
114       if(ret >= 0){
115         current = next;
116         return true;
117       }
118     }
119     return false;
120   }
121 
122   @Override
123   public long getSequenceID() {
124     return 0;
125   }
126 
127   @Override
128   public void close() {
129     // do nothing
130   }
131 }