HBASE-21843 RegionGroupingProvider breaks the meta wal file name pattern which may...
[hbase.git] / hbase-server / src / main / java / org / apache / hadoop / hbase / util / CollectionBackedScanner.java
blobd69a8c7483c79e19998b12e1e74212e76dd98602
1 /*
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org.apache.hadoop.hbase.util;
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;
27 import org.apache.yetus.audience.InterfaceAudience;
28 import org.apache.hadoop.hbase.Cell;
29 import org.apache.hadoop.hbase.CellComparator;
30 import org.apache.hadoop.hbase.regionserver.NonReversedNonLazyKeyValueScanner;
32 /**
33 * Utility scanner that wraps a sortable collection and serves as a KeyValueScanner.
35 @InterfaceAudience.Private
36 public class CollectionBackedScanner extends NonReversedNonLazyKeyValueScanner {
37 final private Iterable<Cell> data;
38 final CellComparator comparator;
39 private Iterator<Cell> iter;
40 private Cell current;
42 public CollectionBackedScanner(SortedSet<Cell> set) {
43 this(set, CellComparator.getInstance());
46 public CollectionBackedScanner(SortedSet<Cell> set,
47 CellComparator comparator) {
48 this.comparator = comparator;
49 data = set;
50 init();
53 public CollectionBackedScanner(List<Cell> list) {
54 this(list, CellComparator.getInstance());
57 public CollectionBackedScanner(List<Cell> list,
58 CellComparator comparator) {
59 Collections.sort(list, comparator);
60 this.comparator = comparator;
61 data = list;
62 init();
65 public CollectionBackedScanner(CellComparator comparator,
66 Cell... array) {
67 this.comparator = comparator;
69 List<Cell> tmp = new ArrayList<>(array.length);
70 Collections.addAll(tmp, array);
71 Collections.sort(tmp, comparator);
72 data = tmp;
73 init();
76 private void init() {
77 iter = data.iterator();
78 if(iter.hasNext()){
79 current = iter.next();
83 @Override
84 public Cell peek() {
85 return current;
88 @Override
89 public Cell next() {
90 Cell oldCurrent = current;
91 if(iter.hasNext()){
92 current = iter.next();
93 } else {
94 current = null;
96 return oldCurrent;
99 @Override
100 public boolean seek(Cell seekCell) {
101 // restart iterator
102 iter = data.iterator();
103 return reseek(seekCell);
106 @Override
107 public boolean reseek(Cell seekCell) {
108 while(iter.hasNext()){
109 Cell next = iter.next();
110 int ret = comparator.compare(next, seekCell);
111 if(ret >= 0){
112 current = next;
113 return true;
116 return false;
120 @Override
121 public void close() {
122 // do nothing