📄 storedlist.java
字号:
/*- * See the file LICENSE for redistribution information. * * Copyright (c) 2000-2006 * Oracle Corporation. All rights reserved. * * $Id: StoredList.java,v 12.4 2006/08/31 18:14:08 bostic Exp $ */package com.sleepycat.collections;import java.util.Collection;import java.util.Iterator;import java.util.List;import java.util.ListIterator;import com.sleepycat.bind.EntityBinding;import com.sleepycat.bind.EntryBinding;import com.sleepycat.bind.RecordNumberBinding;import com.sleepycat.db.Database;import com.sleepycat.db.DatabaseEntry;import com.sleepycat.db.DatabaseException;import com.sleepycat.db.OperationStatus;import com.sleepycat.util.keyrange.KeyRangeException;/** * A List view of a {@link Database}. * * <p>For all stored lists the keys of the underlying Database * must have record number format, and therefore the store or index must be a * RECNO, RECNO-RENUMBER, QUEUE, or BTREE-RECNUM database. Only RECNO-RENUMBER * allows true list behavior where record numbers are renumbered following the * position of an element that is added or removed. For the other access * methods (RECNO, QUEUE, and BTREE-RECNUM), stored Lists are most useful as * read-only collections where record numbers are not required to be * sequential.</p> * * <p>In addition to the standard List methods, this class provides the * following methods for stored lists only. Note that the use of these methods * is not compatible with the standard Java collections interface.</p> * <ul> * <li>{@link #append(Object)}</li> * </ul> * * @author Mark Hayes */public class StoredList extends StoredCollection implements List { private static final EntryBinding DEFAULT_KEY_BINDING = new IndexKeyBinding(1); private int baseIndex = 1; private boolean isSubList; /** * Creates a list view of a {@link Database}. * * @param database is the Database underlying the new collection. * * @param valueBinding is the binding used to translate between value * buffers and value objects. * * @param writeAllowed is true to create a read-write collection or false * to create a read-only collection. * * @throws IllegalArgumentException if formats are not consistently * defined or a parameter is invalid. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public StoredList(Database database, EntryBinding valueBinding, boolean writeAllowed) { super(new DataView(database, DEFAULT_KEY_BINDING, valueBinding, null, writeAllowed, null)); } /** * Creates a list entity view of a {@link Database}. * * @param database is the Database underlying the new collection. * * @param valueEntityBinding is the binding used to translate between * key/value buffers and entity value objects. * * @param writeAllowed is true to create a read-write collection or false * to create a read-only collection. * * @throws IllegalArgumentException if formats are not consistently * defined or a parameter is invalid. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public StoredList(Database database, EntityBinding valueEntityBinding, boolean writeAllowed) { super(new DataView(database, DEFAULT_KEY_BINDING, null, valueEntityBinding, writeAllowed, null)); } /** * Creates a list view of a {@link Database} with a {@link * PrimaryKeyAssigner}. Writing is allowed for the created list. * * @param database is the Database underlying the new collection. * * @param valueBinding is the binding used to translate between value * buffers and value objects. * * @param keyAssigner is used by the {@link #add} and {@link #append} * methods to assign primary keys. * * @throws IllegalArgumentException if formats are not consistently * defined or a parameter is invalid. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public StoredList(Database database, EntryBinding valueBinding, PrimaryKeyAssigner keyAssigner) { super(new DataView(database, DEFAULT_KEY_BINDING, valueBinding, null, true, keyAssigner)); } /** * Creates a list entity view of a {@link Database} with a {@link * PrimaryKeyAssigner}. Writing is allowed for the created list. * * @param database is the Database underlying the new collection. * * @param valueEntityBinding is the binding used to translate between * key/value buffers and entity value objects. * * @param keyAssigner is used by the {@link #add} and {@link #append} * methods to assign primary keys. * * @throws IllegalArgumentException if formats are not consistently * defined or a parameter is invalid. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public StoredList(Database database, EntityBinding valueEntityBinding, PrimaryKeyAssigner keyAssigner) { super(new DataView(database, DEFAULT_KEY_BINDING, null, valueEntityBinding, true, keyAssigner)); } private StoredList(DataView view, int baseIndex) { super(view); this.baseIndex = baseIndex; this.isSubList = true; } /** * Inserts the specified element at the specified position in this list * (optional operation). * This method conforms to the {@link List#add(int, Object)} interface. * * @throws UnsupportedOperationException if the collection is a sublist, or * if the collection is indexed, or if the collection is read-only, or if * the RECNO-RENUMBER access method was not used. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public void add(int index, Object value) { checkIterAddAllowed(); DataCursor cursor = null; boolean doAutoCommit = beginAutoCommit(); try { cursor = new DataCursor(view, true); OperationStatus status = cursor.getSearchKey(new Long(index), null, false); if (status == OperationStatus.SUCCESS) { cursor.putBefore(value); closeCursor(cursor); } else { closeCursor(cursor); cursor = null; view.append(value, null, null); } commitAutoCommit(doAutoCommit); } catch (Exception e) { closeCursor(cursor); throw handleException(e, doAutoCommit); } } /** * Appends the specified element to the end of this list (optional * operation). * This method conforms to the {@link List#add(Object)} interface. * * @throws UnsupportedOperationException if the collection is a sublist, or * if the collection is indexed, or if the collection is read-only, or if * the RECNO-RENUMBER access method was not used. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public boolean add(Object value) { checkIterAddAllowed(); boolean doAutoCommit = beginAutoCommit(); try { view.append(value, null, null); commitAutoCommit(doAutoCommit); return true; } catch (Exception e) { throw handleException(e, doAutoCommit); } } /** * Appends a given value returning the newly assigned index. * If a {@link com.sleepycat.collections.PrimaryKeyAssigner} is associated * with Store for this list, it will be used to assigned the returned * index. Otherwise the Store must be a QUEUE or RECNO database and the * next available record number is assigned as the index. This method does * not exist in the standard {@link List} interface. * * @param value the value to be appended. * * @return the assigned index. * * @throws UnsupportedOperationException if the collection is indexed, or * if the collection is read-only, or if the Store has no {@link * com.sleepycat.collections.PrimaryKeyAssigner} and is not a QUEUE or * RECNO database. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public int append(Object value) { boolean doAutoCommit = beginAutoCommit(); try { Object[] key = new Object[1]; view.append(value, key, null); commitAutoCommit(doAutoCommit); return ((Number) key[0]).intValue(); } catch (Exception e) { throw handleException(e, doAutoCommit); } } void checkIterAddAllowed() throws UnsupportedOperationException { if (isSubList) { throw new UnsupportedOperationException("cannot add to subList"); } if (!view.keysRenumbered) { // RECNO-RENUM throw new UnsupportedOperationException( "requires renumbered keys"); } } /** * Inserts all of the elements in the specified collection into this list * at the specified position (optional operation). * This method conforms to the {@link List#addAll(int, Collection)} * interface. * * @throws UnsupportedOperationException if the collection is a sublist, or * if the collection is indexed, or if the collection is read-only, or if * the RECNO-RENUMBER access method was not used. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public boolean addAll(int index, Collection coll) { checkIterAddAllowed(); DataCursor cursor = null; Iterator i = null; boolean doAutoCommit = beginAutoCommit(); try { i = storedOrExternalIterator(coll); if (!i.hasNext()) { return false; } cursor = new DataCursor(view, true); OperationStatus status = cursor.getSearchKey(new Long(index), null, false); if (status == OperationStatus.SUCCESS) { while (i.hasNext()) { cursor.putBefore(i.next()); } closeCursor(cursor); } else { closeCursor(cursor); cursor = null; while (i.hasNext()) { view.append(i.next(), null, null); } } StoredIterator.close(i); commitAutoCommit(doAutoCommit); return true; } catch (Exception e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -