1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
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
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
128 }
129 }