1   /*
2    * Copyright 2009 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.regionserver.KeyValueScanner;
24  import org.apache.hadoop.hbase.KeyValue;
25  
26  import java.util.ArrayList;
27  import java.util.Iterator;
28  import java.util.Collections;
29  import java.util.List;
30  
31  /**
32   * A fixture that implements and presents a KeyValueScanner.
33   * It takes a list of key/values which is then sorted according
34   * to the provided comparator, and then the whole thing pretends
35   * to be a store file scanner.
36   */
37  public class KeyValueScanFixture implements KeyValueScanner {
38    ArrayList<KeyValue> data;
39    Iterator<KeyValue> iter = null;
40    KeyValue current = null;
41    KeyValue.KVComparator comparator;
42  
43    public KeyValueScanFixture(KeyValue.KVComparator comparator,
44                               KeyValue... incData) {
45      this.comparator = comparator;
46  
47      data = new ArrayList<KeyValue>(incData.length);
48      for( int i = 0; i < incData.length ; ++i) {
49        data.add(incData[i]);
50      }
51      Collections.sort(data, this.comparator);
52    }
53  
54    public static List<KeyValueScanner> scanFixture(KeyValue[] ... kvArrays) {
55      ArrayList<KeyValueScanner> scanners = new ArrayList<KeyValueScanner>();
56      for (KeyValue [] kvs : kvArrays) {
57        scanners.add(new KeyValueScanFixture(KeyValue.COMPARATOR, kvs));
58      }
59      return scanners;
60    }
61  
62  
63    @Override
64    public KeyValue peek() {
65      return this.current;
66    }
67  
68    @Override
69    public KeyValue next() {
70      KeyValue res = current;
71  
72      if (iter.hasNext())
73        current = iter.next();
74      else
75        current = null;
76      return res;
77    }
78  
79    @Override
80    public boolean seek(KeyValue key) {
81      // start at beginning.
82      iter = data.iterator();
83        int cmp;
84      KeyValue kv = null;
85      do {
86        if (!iter.hasNext()) {
87          current = null;
88          return false;
89        }
90        kv = iter.next();
91        cmp = comparator.compare(key, kv);
92      } while (cmp > 0);
93      current = kv;
94      return true;
95    }
96  
97    @Override
98    public boolean reseek(KeyValue key) {
99      return seek(key);
100   }
101 
102   @Override
103   public void close() {
104     // noop.
105   }
106 
107   @Override
108   public long getSequenceID() {
109     return 0;
110   }
111 }