2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 2000, 2012 Oracle and/or its affiliates. All rights reserved.
8 package com.sleepycat.collections;
10 import java.util.Comparator;
11 import java.util.SortedMap;
13 import com.sleepycat.bind.EntityBinding;
14 import com.sleepycat.bind.EntryBinding;
15 import com.sleepycat.db.Database;
16 import com.sleepycat.db.OperationStatus;
17 import com.sleepycat.util.RuntimeExceptionWrapper;
20 * A SortedMap view of a {@link Database}.
22 * <p>In addition to the standard SortedMap methods, this class provides the
23 * following methods for stored sorted maps only. Note that the use of these
24 * methods is not compatible with the standard Java collections interface.</p>
26 * <li>{@link #headMap(Object, boolean)}</li>
27 * <li>{@link #tailMap(Object, boolean)}</li>
28 * <li>{@link #subMap(Object, boolean, Object, boolean)}</li>
33 public class StoredSortedMap<K, V>
34 extends StoredMap<K, V>
35 implements SortedMap<K, V> {
38 * Creates a sorted map view of a {@link Database}.
40 * @param database is the Database underlying the new collection.
42 * @param keyBinding is the binding used to translate between key buffers
45 * @param valueBinding is the binding used to translate between value
46 * buffers and value objects.
48 * @param writeAllowed is true to create a read-write collection or false
49 * to create a read-only collection.
51 * @throws IllegalArgumentException if formats are not consistently
52 * defined or a parameter is invalid.
54 * @throws RuntimeExceptionWrapper if a checked exception is thrown,
55 * including a {@code DatabaseException} on BDB (C edition).
57 public StoredSortedMap(Database database,
58 EntryBinding<K> keyBinding,
59 EntryBinding<V> valueBinding,
60 boolean writeAllowed) {
62 super(new DataView(database, keyBinding, valueBinding, null,
67 * Creates a sorted map view of a {@link Database} with a {@link
68 * PrimaryKeyAssigner}. Writing is allowed for the created map.
70 * @param database is the Database underlying the new collection.
72 * @param keyBinding is the binding used to translate between key buffers
75 * @param valueBinding is the binding used to translate between value
76 * buffers and value objects.
78 * @param keyAssigner is used by the {@link #append} method to assign
81 * @throws IllegalArgumentException if formats are not consistently
82 * defined or a parameter is invalid.
84 * @throws RuntimeExceptionWrapper if a checked exception is thrown,
85 * including a {@code DatabaseException} on BDB (C edition).
87 public StoredSortedMap(Database database,
88 EntryBinding<K> keyBinding,
89 EntryBinding<V> valueBinding,
90 PrimaryKeyAssigner keyAssigner) {
92 super(new DataView(database, keyBinding, valueBinding, null,
97 * Creates a sorted map entity view of a {@link Database}.
99 * @param database is the Database underlying the new collection.
101 * @param keyBinding is the binding used to translate between key buffers
104 * @param valueEntityBinding is the binding used to translate between
105 * key/value buffers and entity value objects.
107 * @param writeAllowed is true to create a read-write collection or false
108 * to create a read-only collection.
110 * @throws IllegalArgumentException if formats are not consistently
111 * defined or a parameter is invalid.
113 * @throws RuntimeExceptionWrapper if a checked exception is thrown,
114 * including a {@code DatabaseException} on BDB (C edition).
116 public StoredSortedMap(Database database,
117 EntryBinding<K> keyBinding,
118 EntityBinding<V> valueEntityBinding,
119 boolean writeAllowed) {
121 super(new DataView(database, keyBinding, null, valueEntityBinding,
122 writeAllowed, null));
126 * Creates a sorted map entity view of a {@link Database} with a {@link
127 * PrimaryKeyAssigner}. Writing is allowed for the created map.
129 * @param database is the Database underlying the new collection.
131 * @param keyBinding is the binding used to translate between key buffers
134 * @param valueEntityBinding is the binding used to translate between
135 * key/value buffers and entity value objects.
137 * @param keyAssigner is used by the {@link #append} method to assign
140 * @throws IllegalArgumentException if formats are not consistently
141 * defined or a parameter is invalid.
143 * @throws RuntimeExceptionWrapper if a checked exception is thrown,
144 * including a {@code DatabaseException} on BDB (C edition).
146 public StoredSortedMap(Database database,
147 EntryBinding<K> keyBinding,
148 EntityBinding<V> valueEntityBinding,
149 PrimaryKeyAssigner keyAssigner) {
151 super(new DataView(database, keyBinding, null, valueEntityBinding,
155 StoredSortedMap(DataView mapView) {
161 * Returns null since comparators are not supported. The natural ordering
162 * of a stored collection is data byte order, whether the data classes
163 * implement the {@link java.lang.Comparable} interface or not.
164 * This method does not conform to the {@link SortedMap#comparator}
169 public Comparator<? super K> comparator() {
175 * Returns the first (lowest) key currently in this sorted map.
176 * This method conforms to the {@link SortedMap#firstKey} interface.
178 * @return the first key.
181 * @throws RuntimeExceptionWrapper if a checked exception is thrown,
182 * including a {@code DatabaseException} on BDB (C edition).
184 public K firstKey() {
186 return getFirstOrLastKey(true);
190 * Returns the last (highest) element currently in this sorted map.
191 * This method conforms to the {@link SortedMap#lastKey} interface.
193 * @return the last key.
196 * @throws RuntimeExceptionWrapper if a checked exception is thrown,
197 * including a {@code DatabaseException} on BDB (C edition).
201 return getFirstOrLastKey(false);
204 private K getFirstOrLastKey(boolean doGetFirst) {
206 DataCursor cursor = null;
208 cursor = new DataCursor(view, false);
209 OperationStatus status;
211 status = cursor.getFirst(false);
213 status = cursor.getLast(false);
215 return (K) ((status == OperationStatus.SUCCESS) ?
216 cursor.getCurrentKey() :
218 } catch (Exception e) {
219 throw StoredContainer.convertException(e);
226 * Returns a view of the portion of this sorted set whose keys are
227 * strictly less than toKey.
228 * This method conforms to the {@link SortedMap#headMap} interface.
230 * <p>Note that the return value is a StoredStoredMap and must be treated
231 * as such; for example, its iterators must be explicitly closed.</p>
233 * @param toKey is the upper bound.
235 * @return the submap.
237 * @throws RuntimeExceptionWrapper if a checked exception is thrown,
238 * including a {@code DatabaseException} on BDB (C edition).
240 public SortedMap<K, V> headMap(K toKey) {
242 return subMap(null, false, toKey, false);
246 * Returns a view of the portion of this sorted map whose elements are
247 * strictly less than toKey, optionally including toKey.
248 * This method does not exist in the standard {@link SortedMap} interface.
250 * <p>Note that the return value is a StoredStoredMap and must be treated
251 * as such; for example, its iterators must be explicitly closed.</p>
253 * @param toKey is the upper bound.
255 * @param toInclusive is true to include toKey.
257 * @return the submap.
259 * @throws RuntimeExceptionWrapper if a checked exception is thrown,
260 * including a {@code DatabaseException} on BDB (C edition).
262 public SortedMap<K, V> headMap(K toKey, boolean toInclusive) {
264 return subMap(null, false, toKey, toInclusive);
268 * Returns a view of the portion of this sorted map whose elements are
269 * greater than or equal to fromKey.
270 * This method conforms to the {@link SortedMap#tailMap} interface.
272 * <p>Note that the return value is a StoredStoredMap and must be treated
273 * as such; for example, its iterators must be explicitly closed.</p>
275 * @param fromKey is the lower bound.
277 * @return the submap.
279 * @throws RuntimeExceptionWrapper if a checked exception is thrown,
280 * including a {@code DatabaseException} on BDB (C edition).
282 public SortedMap<K, V> tailMap(K fromKey) {
284 return subMap(fromKey, true, null, false);
288 * Returns a view of the portion of this sorted map whose elements are
289 * strictly greater than fromKey, optionally including fromKey.
290 * This method does not exist in the standard {@link SortedMap} interface.
292 * <p>Note that the return value is a StoredStoredMap and must be treated
293 * as such; for example, its iterators must be explicitly closed.</p>
295 * @param fromKey is the lower bound.
297 * @param fromInclusive is true to include fromKey.
299 * @return the submap.
301 * @throws RuntimeExceptionWrapper if a checked exception is thrown,
302 * including a {@code DatabaseException} on BDB (C edition).
304 public SortedMap<K, V> tailMap(K fromKey, boolean fromInclusive) {
306 return subMap(fromKey, fromInclusive, null, false);
310 * Returns a view of the portion of this sorted map whose elements range
311 * from fromKey, inclusive, to toKey, exclusive.
312 * This method conforms to the {@link SortedMap#subMap} interface.
314 * <p>Note that the return value is a StoredStoredMap and must be treated
315 * as such; for example, its iterators must be explicitly closed.</p>
317 * @param fromKey is the lower bound.
319 * @param toKey is the upper bound.
321 * @return the submap.
323 * @throws RuntimeExceptionWrapper if a checked exception is thrown,
324 * including a {@code DatabaseException} on BDB (C edition).
326 public SortedMap<K, V> subMap(K fromKey, K toKey) {
328 return subMap(fromKey, true, toKey, false);
332 * Returns a view of the portion of this sorted map whose elements are
333 * strictly greater than fromKey and strictly less than toKey,
334 * optionally including fromKey and toKey.
335 * This method does not exist in the standard {@link SortedMap} interface.
337 * <p>Note that the return value is a StoredStoredMap and must be treated
338 * as such; for example, its iterators must be explicitly closed.</p>
340 * @param fromKey is the lower bound.
342 * @param fromInclusive is true to include fromKey.
344 * @param toKey is the upper bound.
346 * @param toInclusive is true to include toKey.
348 * @return the submap.
350 * @throws RuntimeExceptionWrapper if a checked exception is thrown,
351 * including a {@code DatabaseException} on BDB (C edition).
353 public SortedMap<K, V> subMap(K fromKey,
354 boolean fromInclusive,
356 boolean toInclusive) {
358 return new StoredSortedMap(
359 view.subView(fromKey, fromInclusive, toKey, toInclusive, null));
360 } catch (Exception e) {
361 throw StoredContainer.convertException(e);