View Javadoc

1   /*
2    * Copyright 2011 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  package org.apache.hadoop.hbase.util;
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.SortedSet;
27  
28  import org.apache.hadoop.hbase.KeyValue;
29  import org.apache.hadoop.hbase.regionserver.NonLazyKeyValueScanner;
30  
31  /**
32   * Utility scanner that wraps a sortable collection and serves
33   * as a KeyValueScanner.
34   */
35  public class CollectionBackedScanner extends NonLazyKeyValueScanner {
36    final private Iterable<KeyValue> data;
37    final KeyValue.KVComparator comparator;
38    private Iterator<KeyValue> iter;
39    private KeyValue current;
40  
41    public CollectionBackedScanner(SortedSet<KeyValue> set) {
42      this(set, KeyValue.COMPARATOR);
43    }
44  
45    public CollectionBackedScanner(SortedSet<KeyValue> set,
46        KeyValue.KVComparator comparator) {
47      this.comparator = comparator;
48      data = set;
49      init();
50    }
51  
52    public CollectionBackedScanner(List<KeyValue> list) {
53      this(list, KeyValue.COMPARATOR);
54    }
55  
56    public CollectionBackedScanner(List<KeyValue> list,
57        KeyValue.KVComparator comparator) {
58      Collections.sort(list, comparator);
59      this.comparator = comparator;
60      data = list;
61      init();
62    }
63  
64    public CollectionBackedScanner(KeyValue.KVComparator comparator,
65        KeyValue... array) {
66      this.comparator = comparator;
67  
68      List<KeyValue> tmp = new ArrayList<KeyValue>(array.length);
69      for( int i = 0; i < array.length ; ++i) {
70        tmp.add(array[i]);
71      }
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 KeyValue peek() {
86      return current;
87    }
88  
89    @Override
90    public KeyValue next() {
91      KeyValue 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(KeyValue seekKv) {
102     // restart iterator
103     iter = data.iterator();
104     return reseek(seekKv);
105   }
106 
107   @Override
108   public boolean reseek(KeyValue seekKv) {
109     while(iter.hasNext()){
110       KeyValue next = iter.next();
111       int ret = comparator.compare(next, seekKv);
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 }