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
.regionserver
;
21 import java
.util
.Collection
;
22 import java
.util
.Comparator
;
23 import java
.util
.Iterator
;
24 import java
.util
.NavigableSet
;
25 import java
.util
.NavigableMap
;
26 import java
.util
.SortedSet
;
27 import java
.util
.concurrent
.ConcurrentSkipListMap
;
29 import org
.apache
.hadoop
.hbase
.Cell
;
30 import org
.apache
.hadoop
.hbase
.CellComparator
;
31 import org
.apache
.hadoop
.hbase
.HConstants
;
32 import org
.apache
.yetus
.audience
.InterfaceAudience
;
35 * A {@link java.util.Set} of {@link Cell}s, where an add will overwrite the entry if already
36 * exists in the set. The call to add returns true if no value in the backing map or false if
37 * there was an entry with same key (though value may be different).
38 * implementation is tolerant of concurrent get and set and won't throw
39 * ConcurrentModificationException when iterating.
41 @InterfaceAudience.Private
42 public class CellSet
implements NavigableSet
<Cell
> {
44 public static final int UNKNOWN_NUM_UNIQUES
= -1;
45 // Implemented on top of a {@link java.util.concurrent.ConcurrentSkipListMap}
46 // Differ from CSLS in one respect, where CSLS does "Adds the specified element to this set if it
47 // is not already present.", this implementation "Adds the specified element to this set EVEN
48 // if it is already present overwriting what was there previous".
49 // Otherwise, has same attributes as ConcurrentSkipListSet
50 private final NavigableMap
<Cell
, Cell
> delegatee
; ///
52 private final int numUniqueKeys
;
54 public CellSet(final CellComparator c
) {
55 this.delegatee
= new ConcurrentSkipListMap
<>(c
.getSimpleComparator());
56 this.numUniqueKeys
= UNKNOWN_NUM_UNIQUES
;
59 CellSet(final NavigableMap
<Cell
, Cell
> m
, int numUniqueKeys
) {
61 this.numUniqueKeys
= numUniqueKeys
;
64 CellSet(final NavigableMap
<Cell
, Cell
> m
) {
66 this.numUniqueKeys
= UNKNOWN_NUM_UNIQUES
;
69 NavigableMap
<Cell
, Cell
> getDelegatee() {
74 public Cell
ceiling(Cell e
) {
75 throw new UnsupportedOperationException(HConstants
.NOT_IMPLEMENTED
);
79 public Iterator
<Cell
> descendingIterator() {
80 return this.delegatee
.descendingMap().values().iterator();
84 public NavigableSet
<Cell
> descendingSet() {
85 throw new UnsupportedOperationException(HConstants
.NOT_IMPLEMENTED
);
89 public Cell
floor(Cell e
) {
90 throw new UnsupportedOperationException(HConstants
.NOT_IMPLEMENTED
);
94 public SortedSet
<Cell
> headSet(final Cell toElement
) {
95 return headSet(toElement
, false);
99 public NavigableSet
<Cell
> headSet(final Cell toElement
,
101 return new CellSet(this.delegatee
.headMap(toElement
, inclusive
), UNKNOWN_NUM_UNIQUES
);
105 public Cell
higher(Cell e
) {
106 throw new UnsupportedOperationException(HConstants
.NOT_IMPLEMENTED
);
110 public Iterator
<Cell
> iterator() {
111 return this.delegatee
.values().iterator();
115 public Cell
lower(Cell e
) {
116 throw new UnsupportedOperationException(HConstants
.NOT_IMPLEMENTED
);
120 public Cell
pollFirst() {
121 throw new UnsupportedOperationException(HConstants
.NOT_IMPLEMENTED
);
125 public Cell
pollLast() {
126 throw new UnsupportedOperationException(HConstants
.NOT_IMPLEMENTED
);
130 public SortedSet
<Cell
> subSet(Cell fromElement
, Cell toElement
) {
131 throw new UnsupportedOperationException(HConstants
.NOT_IMPLEMENTED
);
135 public NavigableSet
<Cell
> subSet(Cell fromElement
,
136 boolean fromInclusive
, Cell toElement
, boolean toInclusive
) {
137 throw new UnsupportedOperationException(HConstants
.NOT_IMPLEMENTED
);
141 public SortedSet
<Cell
> tailSet(Cell fromElement
) {
142 return tailSet(fromElement
, true);
146 public NavigableSet
<Cell
> tailSet(Cell fromElement
, boolean inclusive
) {
147 return new CellSet(this.delegatee
.tailMap(fromElement
, inclusive
), UNKNOWN_NUM_UNIQUES
);
151 public Comparator
<?
super Cell
> comparator() {
152 throw new UnsupportedOperationException(HConstants
.NOT_IMPLEMENTED
);
156 public Cell
first() {
157 return this.delegatee
.firstEntry().getValue();
162 return this.delegatee
.lastEntry().getValue();
166 public boolean add(Cell e
) {
167 return this.delegatee
.put(e
, e
) == null;
171 public boolean addAll(Collection
<?
extends Cell
> c
) {
172 throw new UnsupportedOperationException(HConstants
.NOT_IMPLEMENTED
);
176 public void clear() {
177 this.delegatee
.clear();
181 public boolean contains(Object o
) {
182 //noinspection SuspiciousMethodCalls
183 return this.delegatee
.containsKey(o
);
187 public boolean containsAll(Collection
<?
> c
) {
188 throw new UnsupportedOperationException(HConstants
.NOT_IMPLEMENTED
);
192 public boolean isEmpty() {
193 return this.delegatee
.isEmpty();
197 public boolean remove(Object o
) {
198 return this.delegatee
.remove(o
) != null;
202 public boolean removeAll(Collection
<?
> c
) {
203 throw new UnsupportedOperationException(HConstants
.NOT_IMPLEMENTED
);
207 public boolean retainAll(Collection
<?
> c
) {
208 throw new UnsupportedOperationException(HConstants
.NOT_IMPLEMENTED
);
211 public Cell
get(Cell kv
) {
212 return this.delegatee
.get(kv
);
217 if (delegatee
instanceof ConcurrentSkipListMap
) {
218 throw new UnsupportedOperationException("ConcurrentSkipListMap.size() is time-consuming");
220 return this.delegatee
.size();
224 public Object
[] toArray() {
225 throw new UnsupportedOperationException(HConstants
.NOT_IMPLEMENTED
);
229 public <T
> T
[] toArray(T
[] a
) {
230 throw new UnsupportedOperationException(HConstants
.NOT_IMPLEMENTED
);
233 public int getNumUniqueKeys() {
234 return numUniqueKeys
;