View Javadoc

1   /**
2    *
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
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
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.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import com.google.common.annotations.VisibleForTesting;
22  
23  import java.util.Collection;
24  import java.util.Comparator;
25  import java.util.Iterator;
26  import java.util.NavigableSet;
27  import java.util.SortedSet;
28  import java.util.concurrent.ConcurrentNavigableMap;
29  import java.util.concurrent.ConcurrentSkipListMap;
30  
31  import org.apache.hadoop.hbase.Cell;
32  import org.apache.hadoop.hbase.KeyValue;
33  import org.apache.hadoop.hbase.classification.InterfaceAudience;
34  
35  /**
36   * A {@link java.util.Set} of {@link Cell}s implemented on top of a
37   * {@link java.util.concurrent.ConcurrentSkipListMap}.  Works like a
38   * {@link java.util.concurrent.ConcurrentSkipListSet} in all but one regard:
39   * An add will overwrite if already an entry for the added key.  In other words,
40   * where CSLS does "Adds the specified element to this set if it is not already
41   * present.", this implementation "Adds the specified element to this set EVEN
42   * if it is already present overwriting what was there previous".  The call to
43   * add returns true if no value in the backing map or false if there was an
44   * entry with same key (though value may be different).
45   * <p>Otherwise,
46   * has same attributes as ConcurrentSkipListSet: e.g. tolerant of concurrent
47   * get and set and won't throw ConcurrentModificationException when iterating.
48   */
49  @InterfaceAudience.Private
50  public class CellSkipListSet implements NavigableSet<Cell> {
51    private final ConcurrentNavigableMap<Cell, Cell> delegatee;
52  
53    CellSkipListSet(final KeyValue.KVComparator c) {
54      this.delegatee = new ConcurrentSkipListMap<Cell, Cell>(c);
55    }
56  
57    CellSkipListSet(final ConcurrentNavigableMap<Cell, Cell> m) {
58      this.delegatee = m;
59    }
60  
61    public Cell ceiling(Cell e) {
62      throw new UnsupportedOperationException("Not implemented");
63    }
64  
65    public Iterator<Cell> descendingIterator() {
66      return this.delegatee.descendingMap().values().iterator();
67    }
68  
69    public NavigableSet<Cell> descendingSet() {
70      throw new UnsupportedOperationException("Not implemented");
71    }
72  
73    public Cell floor(Cell e) {
74      throw new UnsupportedOperationException("Not implemented");
75    }
76  
77    public SortedSet<Cell> headSet(final Cell toElement) {
78      return headSet(toElement, false);
79    }
80  
81    public NavigableSet<Cell> headSet(final Cell toElement,
82        boolean inclusive) {
83      return new CellSkipListSet(this.delegatee.headMap(toElement, inclusive));
84    }
85  
86    public Cell higher(Cell e) {
87      throw new UnsupportedOperationException("Not implemented");
88    }
89  
90    public Iterator<Cell> iterator() {
91      return this.delegatee.values().iterator();
92    }
93  
94    public Cell lower(Cell e) {
95      throw new UnsupportedOperationException("Not implemented");
96    }
97  
98    public Cell pollFirst() {
99      throw new UnsupportedOperationException("Not implemented");
100   }
101 
102   public Cell pollLast() {
103     throw new UnsupportedOperationException("Not implemented");
104   }
105 
106   public SortedSet<Cell> subSet(Cell fromElement, Cell toElement) {
107     throw new UnsupportedOperationException("Not implemented");
108   }
109 
110   public NavigableSet<Cell> subSet(Cell fromElement,
111       boolean fromInclusive, Cell toElement, boolean toInclusive) {
112     throw new UnsupportedOperationException("Not implemented");
113   }
114 
115   public SortedSet<Cell> tailSet(Cell fromElement) {
116     return tailSet(fromElement, true);
117   }
118 
119   public NavigableSet<Cell> tailSet(Cell fromElement, boolean inclusive) {
120     return new CellSkipListSet(this.delegatee.tailMap(fromElement, inclusive));
121   }
122 
123   public Comparator<? super Cell> comparator() {
124     throw new UnsupportedOperationException("Not implemented");
125   }
126 
127   public Cell first() {
128     return this.delegatee.get(this.delegatee.firstKey());
129   }
130 
131   public Cell last() {
132     return this.delegatee.get(this.delegatee.lastKey());
133   }
134 
135   public boolean add(Cell e) {
136     return this.delegatee.put(e, e) == null;
137   }
138 
139   public boolean addAll(Collection<? extends Cell> c) {
140     throw new UnsupportedOperationException("Not implemented");
141   }
142 
143   public void clear() {
144     this.delegatee.clear();
145   }
146 
147   public boolean contains(Object o) {
148     //noinspection SuspiciousMethodCalls
149     return this.delegatee.containsKey(o);
150   }
151 
152   public boolean containsAll(Collection<?> c) {
153     throw new UnsupportedOperationException("Not implemented");
154   }
155 
156   public boolean isEmpty() {
157     return this.delegatee.isEmpty();
158   }
159 
160   public boolean remove(Object o) {
161     return this.delegatee.remove(o) != null;
162   }
163 
164   public boolean removeAll(Collection<?> c) {
165     throw new UnsupportedOperationException("Not implemented");
166   }
167 
168   public boolean retainAll(Collection<?> c) {
169     throw new UnsupportedOperationException("Not implemented");
170   }
171 
172   public Cell get(Cell kv) {
173     return this.delegatee.get(kv);
174   }
175 
176   public int size() {
177     if (delegatee instanceof ConcurrentSkipListMap) {
178       throw new UnsupportedOperationException("ConcurrentSkipListMap.size() is time-consuming");
179     }
180     return this.delegatee.size();
181   }
182 
183   public Object[] toArray() {
184     throw new UnsupportedOperationException("Not implemented");
185   }
186 
187   public <T> T[] toArray(T[] a) {
188     throw new UnsupportedOperationException("Not implemented");
189   }
190 
191   @VisibleForTesting
192   int sizeForTests() {
193     return this.delegatee.size();
194   }
195 }