~ubuntu-branches/ubuntu/precise/rpm/precise-proposed

« back to all changes in this revision

Viewing changes to db/java/src/com/sleepycat/collections/StoredMap.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2009-06-25 18:57:20 UTC
  • mfrom: (1.1.5 upstream) (4.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20090625185720-617sjskgtgmf09vf
Tags: 4.7.0-7ubuntu1
* Merge from debian unstable, remaining changes:
  - change build depends from libdwarf-dev -> libdw-dev
    (libdwarf-dev is in universe)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*-
2
 
 * See the file LICENSE for redistribution information.
3
 
 *
4
 
 * Copyright (c) 2000-2004
5
 
 *      Sleepycat Software.  All rights reserved.
6
 
 *
7
 
 * $Id: StoredMap.java,v 1.4 2004/09/22 18:01:03 bostic Exp $
8
 
 */
9
 
 
10
 
package com.sleepycat.collections;
11
 
 
12
 
import java.util.Collection;
13
 
import java.util.Collections;
14
 
import java.util.Iterator;
15
 
import java.util.Map;
16
 
import java.util.Set;
17
 
 
18
 
import com.sleepycat.bind.EntityBinding;
19
 
import com.sleepycat.bind.EntryBinding;
20
 
import com.sleepycat.db.Database;
21
 
 
22
 
/**
23
 
 * A Map view of a {@link Database}.
24
 
 *
25
 
 * <p><em>Note that this class does not conform to the standard Java
26
 
 * collections interface in the following ways:</em></p>
27
 
 * <ul>
28
 
 * <li>The {@link #size} method always throws
29
 
 * <code>UnsupportedOperationException</code> because, for performance reasons,
30
 
 * databases do not maintain their total record count.</li>
31
 
 * <li>All iterators must be explicitly closed using {@link
32
 
 * StoredIterator#close()} or {@link StoredIterator#close(java.util.Iterator)}
33
 
 * to release the underlying database cursor resources.</li>
34
 
 * </ul>
35
 
 *
36
 
 * <p>In addition to the standard Map methods, this class provides the
37
 
 * following methods for stored maps only.  Note that the use of these methods
38
 
 * is not compatible with the standard Java collections interface.</p>
39
 
 * <ul>
40
 
 * <li>{@link #duplicates(Object)}</li>
41
 
 * <li>{@link #append(Object)}</li>
42
 
 * </ul>
43
 
 *
44
 
 * @author Mark Hayes
45
 
 */
46
 
public class StoredMap extends StoredContainer implements Map {
47
 
 
48
 
    private StoredKeySet keySet;
49
 
    private boolean keySetInitialized = false;
50
 
    private StoredEntrySet entrySet;
51
 
    private boolean entrySetInitialized = false;
52
 
    private StoredValueSet valueSet;
53
 
    private boolean valueSetInitialized = false;
54
 
 
55
 
    /**
56
 
     * Creates a map view of a {@link Database}.
57
 
     *
58
 
     * @param database is the Database underlying the new collection.
59
 
     *
60
 
     * @param keyBinding is the binding used to translate between key buffers
61
 
     * and key objects.
62
 
     *
63
 
     * @param valueBinding is the binding used to translate between value
64
 
     * buffers and value objects.
65
 
     *
66
 
     * @param writeAllowed is true to create a read-write collection or false
67
 
     * to create a read-only collection.
68
 
     *
69
 
     * @throws IllegalArgumentException if formats are not consistently
70
 
     * defined or a parameter is invalid.
71
 
     *
72
 
     * @throws RuntimeExceptionWrapper if a {@link
73
 
     * com.sleepycat.db.DatabaseException} is thrown.
74
 
     */
75
 
    public StoredMap(Database database, EntryBinding keyBinding,
76
 
                     EntryBinding valueBinding, boolean writeAllowed) {
77
 
 
78
 
        super(new DataView(database, keyBinding, valueBinding, null,
79
 
                           writeAllowed, null));
80
 
    }
81
 
 
82
 
    /**
83
 
     * Creates a map view of a {@link Database} with a {@link
84
 
     * PrimaryKeyAssigner}.  Writing is allowed for the created map.
85
 
     *
86
 
     * @param database is the Database underlying the new collection.
87
 
     *
88
 
     * @param keyBinding is the binding used to translate between key buffers
89
 
     * and key objects.
90
 
     *
91
 
     * @param valueBinding is the binding used to translate between value
92
 
     * buffers and value objects.
93
 
     *
94
 
     * @param keyAssigner is used by the {@link #append} method to assign
95
 
     * primary keys.
96
 
     *
97
 
     * @throws IllegalArgumentException if formats are not consistently
98
 
     * defined or a parameter is invalid.
99
 
     *
100
 
     * @throws RuntimeExceptionWrapper if a {@link
101
 
     * com.sleepycat.db.DatabaseException} is thrown.
102
 
     */
103
 
    public StoredMap(Database database, EntryBinding keyBinding,
104
 
                     EntryBinding valueBinding,
105
 
                     PrimaryKeyAssigner keyAssigner) {
106
 
 
107
 
        super(new DataView(database, keyBinding, valueBinding, null,
108
 
                           true, keyAssigner));
109
 
    }
110
 
 
111
 
    protected Object clone()
112
 
        throws CloneNotSupportedException {
113
 
 
114
 
        // cached collections must be cleared and recreated with the new view
115
 
        // of the map to inherit the new view's properties
116
 
        StoredMap other = (StoredMap) super.clone();
117
 
        other.keySet = null;
118
 
        other.keySetInitialized = false;
119
 
        other.entrySet = null;
120
 
        other.entrySetInitialized = false;
121
 
        other.valueSet = null;
122
 
        other.valueSetInitialized = false;
123
 
        return other;
124
 
    }
125
 
 
126
 
    /**
127
 
     * Creates a map entity view of a {@link Database}.
128
 
     *
129
 
     * @param database is the Database underlying the new collection.
130
 
     *
131
 
     * @param keyBinding is the binding used to translate between key buffers
132
 
     * and key objects.
133
 
     *
134
 
     * @param valueEntityBinding is the binding used to translate between
135
 
     * key/value buffers and entity value objects.
136
 
     *
137
 
     * @param writeAllowed is true to create a read-write collection or false
138
 
     * to create a read-only collection.
139
 
     *
140
 
     * @throws IllegalArgumentException if formats are not consistently
141
 
     * defined or a parameter is invalid.
142
 
     *
143
 
     * @throws RuntimeExceptionWrapper if a {@link
144
 
     * com.sleepycat.db.DatabaseException} is thrown.
145
 
     */
146
 
    public StoredMap(Database database, EntryBinding keyBinding,
147
 
                     EntityBinding valueEntityBinding, boolean writeAllowed) {
148
 
 
149
 
        super(new DataView(database, keyBinding, null, valueEntityBinding,
150
 
                           writeAllowed, null));
151
 
    }
152
 
 
153
 
    /**
154
 
     * Creates a map entity view of a {@link Database} with a {@link
155
 
     * PrimaryKeyAssigner}.  Writing is allowed for the created map.
156
 
     *
157
 
     * @param database is the Database underlying the new collection.
158
 
     *
159
 
     * @param keyBinding is the binding used to translate between key buffers
160
 
     * and key objects.
161
 
     *
162
 
     * @param valueEntityBinding is the binding used to translate between
163
 
     * key/value buffers and entity value objects.
164
 
     *
165
 
     * @param keyAssigner is used by the {@link #append} method to assign
166
 
     * primary keys.
167
 
     *
168
 
     * @throws IllegalArgumentException if formats are not consistently
169
 
     * defined or a parameter is invalid.
170
 
     *
171
 
     * @throws RuntimeExceptionWrapper if a {@link
172
 
     * com.sleepycat.db.DatabaseException} is thrown.
173
 
     */
174
 
    public StoredMap(Database database, EntryBinding keyBinding,
175
 
                     EntityBinding valueEntityBinding,
176
 
                     PrimaryKeyAssigner keyAssigner) {
177
 
 
178
 
        super(new DataView(database, keyBinding, null, valueEntityBinding,
179
 
                           true, keyAssigner));
180
 
    }
181
 
 
182
 
    StoredMap(DataView view) {
183
 
 
184
 
        super(view);
185
 
    }
186
 
 
187
 
    /**
188
 
     * Returns the value to which this map maps the specified key.  If
189
 
     * duplicates are allowed, this method returns the first duplicate, in the
190
 
     * order in which duplicates are configured, that maps to the specified
191
 
     * key.
192
 
     *
193
 
     * This method conforms to the {@link Map#get} interface.
194
 
     *
195
 
     * @throws RuntimeExceptionWrapper if a {@link
196
 
     * com.sleepycat.db.DatabaseException} is thrown.
197
 
     */
198
 
    public Object get(Object key) {
199
 
 
200
 
        return super.get(key);
201
 
    }
202
 
 
203
 
    /**
204
 
     * Associates the specified value with the specified key in this map
205
 
     * (optional operation).  If duplicates are allowed and the specified key
206
 
     * is already mapped to a value, this method appends the new duplicate
207
 
     * after the existing duplicates.  This method conforms to the {@link
208
 
     * Map#put} interface.
209
 
     *
210
 
     * <p>The key parameter may be null if an entity binding is used and the
211
 
     * key will be derived from the value (entity) parameter.  If an entity
212
 
     * binding is used and the key parameter is non-null, then the key
213
 
     * parameter must be equal to the key derived from the value parameter.</p>
214
 
     *
215
 
     * @return the previous value associated with specified key, or null if
216
 
     * there was no mapping for the key or if duplicates are allowed.
217
 
     *
218
 
     * @throws UnsupportedOperationException if the collection is indexed, or
219
 
     * if the collection is read-only.
220
 
     *
221
 
     * @throws IllegalArgumentException if an entity value binding is used and
222
 
     * the primary key of the value given is different than the existing stored
223
 
     * primary key.
224
 
     *
225
 
     * @throws RuntimeExceptionWrapper if a {@link
226
 
     * com.sleepycat.db.DatabaseException} is thrown.
227
 
     */
228
 
    public Object put(Object key, Object value) {
229
 
 
230
 
        return super.put(key, value);
231
 
    }
232
 
 
233
 
    /**
234
 
     * Appends a given value returning the newly assigned key.  If a {@link
235
 
     * PrimaryKeyAssigner} is associated with Store for this map, it will be
236
 
     * used to assigned the returned key.  Otherwise the Store must be a QUEUE
237
 
     * or RECNO database and the next available record number is assigned as
238
 
     * the key.  This method does not exist in the standard {@link Map}
239
 
     * interface.
240
 
     *
241
 
     * @param value the value to be appended.
242
 
     *
243
 
     * @return the assigned key.
244
 
     *
245
 
     * @throws UnsupportedOperationException if the collection is indexed, or
246
 
     * if the collection is read-only, or if the Store has no {@link
247
 
     * PrimaryKeyAssigner} and is not a QUEUE or RECNO database.
248
 
     *
249
 
     * @throws RuntimeExceptionWrapper if a {@link
250
 
     * com.sleepycat.db.DatabaseException} is thrown.
251
 
     */
252
 
    public Object append(Object value) {
253
 
 
254
 
        boolean doAutoCommit = beginAutoCommit();
255
 
        try {
256
 
            Object[] key = new Object[1];
257
 
            view.append(value, key, null);
258
 
            commitAutoCommit(doAutoCommit);
259
 
            return key[0];
260
 
        } catch (Exception e) {
261
 
            throw handleException(e, doAutoCommit);
262
 
        }
263
 
    }
264
 
 
265
 
    /**
266
 
     * Removes the mapping for this key from this map if present (optional
267
 
     * operation).  If duplicates are allowed, this method removes all
268
 
     * duplicates for the given key.  This method conforms to the {@link
269
 
     * Map#remove} interface.
270
 
     *
271
 
     * @throws UnsupportedOperationException if the collection is read-only.
272
 
     *
273
 
     * @throws RuntimeExceptionWrapper if a {@link
274
 
     * com.sleepycat.db.DatabaseException} is thrown.
275
 
     */
276
 
    public Object remove(Object key) {
277
 
 
278
 
        Object[] oldVal = new Object[1];
279
 
        removeKey(key, oldVal);
280
 
        return oldVal[0];
281
 
    }
282
 
 
283
 
    /**
284
 
     * Returns true if this map contains the specified key.  This method
285
 
     * conforms to the {@link Map#containsKey} interface.
286
 
     *
287
 
     * @throws RuntimeExceptionWrapper if a {@link
288
 
     * com.sleepycat.db.DatabaseException} is thrown.
289
 
     */
290
 
    public boolean containsKey(Object key) {
291
 
 
292
 
        return super.containsKey(key);
293
 
    }
294
 
 
295
 
    /**
296
 
     * Returns true if this map contains the specified value.  When an entity
297
 
     * binding is used, this method returns whether the map contains the
298
 
     * primary key and value mapping of the entity.  This method conforms to
299
 
     * the {@link Map#containsValue} interface.
300
 
     *
301
 
     * @throws RuntimeExceptionWrapper if a {@link
302
 
     * com.sleepycat.db.DatabaseException} is thrown.
303
 
     */
304
 
    public boolean containsValue(Object value) {
305
 
 
306
 
        return super.containsValue(value);
307
 
    }
308
 
 
309
 
    /**
310
 
     * Copies all of the mappings from the specified map to this map (optional
311
 
     * operation).  When duplicates are allowed, the mappings in the specified
312
 
     * map are effectively appended to the existing mappings in this map, that
313
 
     * is no previously existing mappings in this map are replaced.  This
314
 
     * method conforms to the {@link Map#putAll} interface.
315
 
     *
316
 
     * @throws UnsupportedOperationException if the collection is read-only, or
317
 
     * if the collection is indexed.
318
 
     *
319
 
     * @throws RuntimeExceptionWrapper if a {@link
320
 
     * com.sleepycat.db.DatabaseException} is thrown.
321
 
     */
322
 
    public void putAll(Map map) {
323
 
 
324
 
        boolean doAutoCommit = beginAutoCommit();
325
 
        Iterator entries = null;
326
 
        try {
327
 
            entries = map.entrySet().iterator();
328
 
            while (entries.hasNext()) {
329
 
                Map.Entry entry = (Map.Entry) entries.next();
330
 
                put(entry.getKey(), entry.getValue());
331
 
            }
332
 
            StoredIterator.close(entries);
333
 
            commitAutoCommit(doAutoCommit);
334
 
        } catch (Exception e) {
335
 
            StoredIterator.close(entries);
336
 
            throw handleException(e, doAutoCommit);
337
 
        }
338
 
    }
339
 
 
340
 
    /**
341
 
     * Returns a set view of the keys contained in this map.  A {@link
342
 
     * java.util.SortedSet} is returned if the map is ordered.  The returned
343
 
     * collection will be read-only if the map is read-only.  This method
344
 
     * conforms to the {@link Map#keySet()} interface.
345
 
     *
346
 
     * @return a {@link StoredKeySet} or a {@link StoredSortedKeySet} for this
347
 
     * map.
348
 
     *
349
 
     * @throws RuntimeExceptionWrapper if a {@link
350
 
     * com.sleepycat.db.DatabaseException} is thrown.
351
 
     *
352
 
     * @see #isOrdered
353
 
     * @see #isWriteAllowed
354
 
     */
355
 
    public Set keySet() {
356
 
 
357
 
        if (!keySetInitialized) {
358
 
            synchronized (this) {
359
 
                if (!keySetInitialized) {
360
 
                    DataView newView = view.keySetView();
361
 
                    if (isOrdered()) {
362
 
                        keySet = new StoredSortedKeySet(newView);
363
 
                    } else {
364
 
                        keySet = new StoredKeySet(newView);
365
 
                    }
366
 
                    keySetInitialized = true;
367
 
                }
368
 
            }
369
 
        }
370
 
        return keySet;
371
 
    }
372
 
 
373
 
    /**
374
 
     * Returns a set view of the mappings contained in this map.  A {@link
375
 
     * java.util.SortedSet} is returned if the map is ordered.  The returned
376
 
     * collection will be read-only if the map is read-only.  This method
377
 
     * conforms to the {@link Map#entrySet()} interface.
378
 
     *
379
 
     * @return a {@link StoredEntrySet} or a {@link StoredSortedEntrySet} for
380
 
     * this map.
381
 
     *
382
 
     * @throws RuntimeExceptionWrapper if a {@link
383
 
     * com.sleepycat.db.DatabaseException} is thrown.
384
 
     *
385
 
     * @see #isOrdered
386
 
     * @see #isWriteAllowed
387
 
     */
388
 
    public Set entrySet() {
389
 
 
390
 
        if (!entrySetInitialized) {
391
 
            synchronized (this) {
392
 
                if (!entrySetInitialized) {
393
 
                    if (isOrdered()) {
394
 
                        entrySet = new StoredSortedEntrySet(view);
395
 
                    } else {
396
 
                        entrySet = new StoredEntrySet(view);
397
 
                    }
398
 
                    entrySetInitialized = true;
399
 
                }
400
 
            }
401
 
        }
402
 
        return entrySet;
403
 
    }
404
 
 
405
 
    /**
406
 
     * Returns a collection view of the values contained in this map.  A {@link
407
 
     * java.util.SortedSet} is returned if the map is ordered and the
408
 
     * value/entity binding can be used to derive the map's key from its
409
 
     * value/entity object.  The returned collection will be read-only if the
410
 
     * map is read-only.  This method conforms to the {@link Map#entrySet()}
411
 
     * interface.
412
 
     *
413
 
     * @return a {@link StoredValueSet} or a {@link StoredSortedValueSet} for
414
 
     * this map.
415
 
     *
416
 
     * @throws RuntimeExceptionWrapper if a {@link
417
 
     * com.sleepycat.db.DatabaseException} is thrown.
418
 
     *
419
 
     * @see #isOrdered
420
 
     * @see #isWriteAllowed
421
 
     */
422
 
    public Collection values() {
423
 
 
424
 
        if (!valueSetInitialized) {
425
 
            synchronized (this) {
426
 
                if (!valueSetInitialized) {
427
 
                    DataView newView = view.valueSetView();
428
 
                    if (isOrdered() && newView.canDeriveKeyFromValue()) {
429
 
                        valueSet = new StoredSortedValueSet(newView);
430
 
                    } else {
431
 
                        valueSet = new StoredValueSet(newView);
432
 
                    }
433
 
                    valueSetInitialized = true;
434
 
                }
435
 
            }
436
 
        }
437
 
        return valueSet;
438
 
    }
439
 
 
440
 
    /**
441
 
     * Returns a new collection containing the values mapped to the given key
442
 
     * in this map.  This collection's iterator() method is particularly useful
443
 
     * for iterating over the duplicates for a given key, since this is not
444
 
     * supported by the standard Map interface.  This method does not exist in
445
 
     * the standard {@link Map} interface.
446
 
     *
447
 
     * <p>If no mapping for the given key is present, an empty collection is
448
 
     * returned.  If duplicates are not allowed, at most a single value will be
449
 
     * in the collection returned.  If duplicates are allowed, the returned
450
 
     * collection's add() method may be used to add values for the given
451
 
     * key.</p>
452
 
     *
453
 
     * @param key is the key for which values are to be returned.
454
 
     *
455
 
     * @throws RuntimeExceptionWrapper if a {@link
456
 
     * com.sleepycat.db.DatabaseException} is thrown.
457
 
     */
458
 
    public Collection duplicates(Object key) {
459
 
 
460
 
        try {
461
 
            DataView newView = view.valueSetView(key);
462
 
            return new StoredValueSet(newView, true);
463
 
        } catch (KeyRangeException e) {
464
 
            return Collections.EMPTY_SET;
465
 
        } catch (Exception e) {
466
 
            throw StoredContainer.convertException(e);
467
 
        }
468
 
    }
469
 
 
470
 
    /**
471
 
     * Compares the specified object with this map for equality.  A value
472
 
     * comparison is performed by this method and the stored values are
473
 
     * compared rather than calling the equals() method of each element.  This
474
 
     * method conforms to the {@link Map#equals} interface.
475
 
     *
476
 
     * @throws RuntimeExceptionWrapper if a {@link
477
 
     * com.sleepycat.db.DatabaseException} is thrown.
478
 
     */
479
 
    public boolean equals(Object other) {
480
 
 
481
 
        if (other instanceof Map) {
482
 
            return entrySet().equals(((Map) other).entrySet());
483
 
        } else {
484
 
            return false;
485
 
        }
486
 
    }
487
 
 
488
 
    /*
489
 
     * Add this in to keep FindBugs from whining at us about implementing
490
 
     * equals(), but not hashCode().
491
 
     */
492
 
    public int hashCode() {
493
 
        return super.hashCode();
494
 
    }
495
 
 
496
 
    /**
497
 
     * Converts the map to a string representation for debugging.  WARNING: All
498
 
     * mappings will be converted to strings and returned and therefore the
499
 
     * returned string may be very large.
500
 
     *
501
 
     * @return the string representation.
502
 
     *
503
 
     * @throws RuntimeExceptionWrapper if a {@link
504
 
     * com.sleepycat.db.DatabaseException} is thrown.
505
 
     */
506
 
    public String toString() {
507
 
 
508
 
        return entrySet().toString();
509
 
    }
510
 
}
511