001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2000,2008 Oracle. All rights reserved.
005: *
006: * $Id: StoredSortedMap.java,v 1.31.2.3 2008/01/07 15:14:06 cwl Exp $
007: */
008:
009: package com.sleepycat.collections;
010:
011: import java.util.Comparator;
012: import java.util.SortedMap;
013:
014: import com.sleepycat.bind.EntityBinding;
015: import com.sleepycat.bind.EntryBinding;
016: import com.sleepycat.je.Database;
017: import com.sleepycat.je.OperationStatus;
018:
019: /**
020: * A SortedMap view of a {@link Database}.
021: *
022: * <p>In addition to the standard SortedMap methods, this class provides the
023: * following methods for stored sorted maps only. Note that the use of these
024: * methods is not compatible with the standard Java collections interface.</p>
025: * <ul>
026: * <li>{@link #headMap(Object, boolean)}</li>
027: * <li>{@link #tailMap(Object, boolean)}</li>
028: * <li>{@link #subMap(Object, boolean, Object, boolean)}</li>
029: * </ul>
030: *
031: * @author Mark Hayes
032: */
033: public class StoredSortedMap extends StoredMap implements SortedMap {
034:
035: /**
036: * Creates a sorted map view of a {@link Database}.
037: *
038: * @param database is the Database underlying the new collection.
039: *
040: * @param keyBinding is the binding used to translate between key buffers
041: * and key objects.
042: *
043: * @param valueBinding is the binding used to translate between value
044: * buffers and value objects.
045: *
046: * @param writeAllowed is true to create a read-write collection or false
047: * to create a read-only collection.
048: *
049: * @throws IllegalArgumentException if formats are not consistently
050: * defined or a parameter is invalid.
051: *
052: * @throws RuntimeExceptionWrapper if a {@link
053: * com.sleepycat.je.DatabaseException} is thrown.
054: */
055: public StoredSortedMap(Database database, EntryBinding keyBinding,
056: EntryBinding valueBinding, boolean writeAllowed) {
057:
058: super (new DataView(database, keyBinding, valueBinding, null,
059: writeAllowed, null));
060: }
061:
062: /**
063: * Creates a sorted map view of a {@link Database} with a {@link
064: * PrimaryKeyAssigner}. Writing is allowed for the created map.
065: *
066: * @param database is the Database underlying the new collection.
067: *
068: * @param keyBinding is the binding used to translate between key buffers
069: * and key objects.
070: *
071: * @param valueBinding is the binding used to translate between value
072: * buffers and value objects.
073: *
074: * @param keyAssigner is used by the {@link #append} method to assign
075: * primary keys.
076: *
077: * @throws IllegalArgumentException if formats are not consistently
078: * defined or a parameter is invalid.
079: *
080: * @throws RuntimeExceptionWrapper if a {@link
081: * com.sleepycat.je.DatabaseException} is thrown.
082: */
083: public StoredSortedMap(Database database, EntryBinding keyBinding,
084: EntryBinding valueBinding, PrimaryKeyAssigner keyAssigner) {
085:
086: super (new DataView(database, keyBinding, valueBinding, null,
087: true, keyAssigner));
088: }
089:
090: /**
091: * Creates a sorted map entity view of a {@link Database}.
092: *
093: * @param database is the Database underlying the new collection.
094: *
095: * @param keyBinding is the binding used to translate between key buffers
096: * and key objects.
097: *
098: * @param valueEntityBinding is the binding used to translate between
099: * key/value buffers and entity value objects.
100: *
101: * @param writeAllowed is true to create a read-write collection or false
102: * to create a read-only collection.
103: *
104: * @throws IllegalArgumentException if formats are not consistently
105: * defined or a parameter is invalid.
106: *
107: * @throws RuntimeExceptionWrapper if a {@link
108: * com.sleepycat.je.DatabaseException} is thrown.
109: */
110: public StoredSortedMap(Database database, EntryBinding keyBinding,
111: EntityBinding valueEntityBinding, boolean writeAllowed) {
112:
113: super (new DataView(database, keyBinding, null,
114: valueEntityBinding, writeAllowed, null));
115: }
116:
117: /**
118: * Creates a sorted map entity view of a {@link Database} with a {@link
119: * PrimaryKeyAssigner}. Writing is allowed for the created map.
120: *
121: * @param database is the Database underlying the new collection.
122: *
123: * @param keyBinding is the binding used to translate between key buffers
124: * and key objects.
125: *
126: * @param valueEntityBinding is the binding used to translate between
127: * key/value buffers and entity value objects.
128: *
129: * @param keyAssigner is used by the {@link #append} method to assign
130: * primary keys.
131: *
132: * @throws IllegalArgumentException if formats are not consistently
133: * defined or a parameter is invalid.
134: *
135: * @throws RuntimeExceptionWrapper if a {@link
136: * com.sleepycat.je.DatabaseException} is thrown.
137: */
138: public StoredSortedMap(Database database, EntryBinding keyBinding,
139: EntityBinding valueEntityBinding,
140: PrimaryKeyAssigner keyAssigner) {
141:
142: super (new DataView(database, keyBinding, null,
143: valueEntityBinding, true, keyAssigner));
144: }
145:
146: StoredSortedMap(DataView mapView) {
147:
148: super (mapView);
149: }
150:
151: /**
152: * Returns null since comparators are not supported. The natural ordering
153: * of a stored collection is data byte order, whether the data classes
154: * implement the {@link java.lang.Comparable} interface or not.
155: * This method does not conform to the {@link SortedMap#comparator}
156: * interface.
157: *
158: * @return null.
159: */
160: public Comparator comparator() {
161:
162: return null;
163: }
164:
165: /**
166: * Returns the first (lowest) key currently in this sorted map.
167: * This method conforms to the {@link SortedMap#firstKey} interface.
168: *
169: * @return the first key.
170: *
171: * @throws RuntimeExceptionWrapper if a {@link
172: * com.sleepycat.je.DatabaseException} is thrown.
173: */
174: public Object firstKey() {
175:
176: return getFirstOrLastKey(true);
177: }
178:
179: /**
180: * Returns the last (highest) element currently in this sorted map.
181: * This method conforms to the {@link SortedMap#lastKey} interface.
182: *
183: * @return the last key.
184: *
185: * @throws RuntimeExceptionWrapper if a {@link
186: * com.sleepycat.je.DatabaseException} is thrown.
187: */
188: public Object lastKey() {
189:
190: return getFirstOrLastKey(false);
191: }
192:
193: private Object getFirstOrLastKey(boolean doGetFirst) {
194:
195: DataCursor cursor = null;
196: try {
197: cursor = new DataCursor(view, false);
198: OperationStatus status;
199: if (doGetFirst) {
200: status = cursor.getFirst(false);
201: } else {
202: status = cursor.getLast(false);
203: }
204: return (status == OperationStatus.SUCCESS) ? cursor
205: .getCurrentKey() : null;
206: } catch (Exception e) {
207: throw StoredContainer.convertException(e);
208: } finally {
209: closeCursor(cursor);
210: }
211: }
212:
213: /**
214: * Returns a view of the portion of this sorted set whose keys are
215: * strictly less than toKey.
216: * This method conforms to the {@link SortedMap#headMap} interface.
217: *
218: * <p>Note that the return value is a StoredStoredMap and must be treated
219: * as such; for example, its iterators must be explicitly closed.</p>
220: *
221: * @param toKey is the upper bound.
222: *
223: * @return the submap.
224: *
225: * @throws RuntimeExceptionWrapper if a {@link
226: * com.sleepycat.je.DatabaseException} is thrown.
227: */
228: public SortedMap headMap(Object toKey) {
229:
230: return subMap(null, false, toKey, false);
231: }
232:
233: /**
234: * Returns a view of the portion of this sorted map whose elements are
235: * strictly less than toKey, optionally including toKey.
236: * This method does not exist in the standard {@link SortedMap} interface.
237: *
238: * <p>Note that the return value is a StoredStoredMap and must be treated
239: * as such; for example, its iterators must be explicitly closed.</p>
240: *
241: * @param toKey is the upper bound.
242: *
243: * @param toInclusive is true to include toKey.
244: *
245: * @return the submap.
246: *
247: * @throws RuntimeExceptionWrapper if a {@link
248: * com.sleepycat.je.DatabaseException} is thrown.
249: */
250: public SortedMap headMap(Object toKey, boolean toInclusive) {
251:
252: return subMap(null, false, toKey, toInclusive);
253: }
254:
255: /**
256: * Returns a view of the portion of this sorted map whose elements are
257: * greater than or equal to fromKey.
258: * This method conforms to the {@link SortedMap#tailMap} interface.
259: *
260: * <p>Note that the return value is a StoredStoredMap and must be treated
261: * as such; for example, its iterators must be explicitly closed.</p>
262: *
263: * @param fromKey is the lower bound.
264: *
265: * @return the submap.
266: *
267: * @throws RuntimeExceptionWrapper if a {@link
268: * com.sleepycat.je.DatabaseException} is thrown.
269: */
270: public SortedMap tailMap(Object fromKey) {
271:
272: return subMap(fromKey, true, null, false);
273: }
274:
275: /**
276: * Returns a view of the portion of this sorted map whose elements are
277: * strictly greater than fromKey, optionally including fromKey.
278: * This method does not exist in the standard {@link SortedMap} interface.
279: *
280: * <p>Note that the return value is a StoredStoredMap and must be treated
281: * as such; for example, its iterators must be explicitly closed.</p>
282: *
283: * @param fromKey is the lower bound.
284: *
285: * @param fromInclusive is true to include fromKey.
286: *
287: * @return the submap.
288: *
289: * @throws RuntimeExceptionWrapper if a {@link
290: * com.sleepycat.je.DatabaseException} is thrown.
291: */
292: public SortedMap tailMap(Object fromKey, boolean fromInclusive) {
293:
294: return subMap(fromKey, fromInclusive, null, false);
295: }
296:
297: /**
298: * Returns a view of the portion of this sorted map whose elements range
299: * from fromKey, inclusive, to toKey, exclusive.
300: * This method conforms to the {@link SortedMap#subMap} interface.
301: *
302: * <p>Note that the return value is a StoredStoredMap and must be treated
303: * as such; for example, its iterators must be explicitly closed.</p>
304: *
305: * @param fromKey is the lower bound.
306: *
307: * @param toKey is the upper bound.
308: *
309: * @return the submap.
310: *
311: * @throws RuntimeExceptionWrapper if a {@link
312: * com.sleepycat.je.DatabaseException} is thrown.
313: */
314: public SortedMap subMap(Object fromKey, Object toKey) {
315:
316: return subMap(fromKey, true, toKey, false);
317: }
318:
319: /**
320: * Returns a view of the portion of this sorted map whose elements are
321: * strictly greater than fromKey and strictly less than toKey,
322: * optionally including fromKey and toKey.
323: * This method does not exist in the standard {@link SortedMap} interface.
324: *
325: * <p>Note that the return value is a StoredStoredMap and must be treated
326: * as such; for example, its iterators must be explicitly closed.</p>
327: *
328: * @param fromKey is the lower bound.
329: *
330: * @param fromInclusive is true to include fromKey.
331: *
332: * @param toKey is the upper bound.
333: *
334: * @param toInclusive is true to include toKey.
335: *
336: * @return the submap.
337: *
338: * @throws RuntimeExceptionWrapper if a {@link
339: * com.sleepycat.je.DatabaseException} is thrown.
340: */
341: public SortedMap subMap(Object fromKey, boolean fromInclusive,
342: Object toKey, boolean toInclusive) {
343:
344: try {
345: return new StoredSortedMap(view.subView(fromKey,
346: fromInclusive, toKey, toInclusive, null));
347: } catch (Exception e) {
348: throw StoredContainer.convertException(e);
349: }
350: }
351: }
|