View Javadoc

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