Container.Indexed, Container.Sortable, Container.ItemSetChangeNotifier {
/** Query delegate */
- private QueryDelegate delegate;
+ private QueryDelegate queryDelegate;
/** Auto commit mode, default = false */
private boolean autoCommit = false;
/** Number of items to cache = CACHE_RATIO x pageLength */
public static final int CACHE_RATIO = 2;
+ /** Amount of cache to overlap with previous page */
+ private int cacheOverlap = pageLength;
+
/** Item and index caches */
private final Map<Integer, RowId> itemIndexes = new HashMap<Integer, RowId>();
private final CacheMap<RowId, RowItem> cachedItems = new CacheMap<RowId, RowItem>();
throw new IllegalArgumentException(
"QueryDelegate must not be null.");
}
- this.delegate = delegate;
+ queryDelegate = delegate;
getPropertyIds();
- cachedItems.setCacheLimit(CACHE_RATIO * getPageLength());
+ cachedItems.setCacheLimit(CACHE_RATIO * getPageLength() + cacheOverlap);
}
/**************************************/
@Override
public Object addItem() throws UnsupportedOperationException {
- Object emptyKey[] = new Object[delegate.getPrimaryKeyColumns().size()];
+ Object emptyKey[] = new Object[queryDelegate.getPrimaryKeyColumns()
+ .size()];
RowId itemId = new TemporaryRowId(emptyKey);
// Create new empty column properties for the row item.
List<ColumnProperty> itemProperties = new ArrayList<ColumnProperty>();
if (autoCommit) {
/* Add and commit instantly */
try {
- if (delegate instanceof TableQuery) {
- itemId = ((TableQuery) delegate)
+ if (queryDelegate instanceof TableQuery) {
+ itemId = ((TableQuery) queryDelegate)
.storeRowImmediately(newRowItem);
} else {
- delegate.beginTransaction();
- delegate.storeRow(newRowItem);
- delegate.commit();
+ queryDelegate.beginTransaction();
+ queryDelegate.storeRow(newRowItem);
+ queryDelegate.commit();
}
refresh();
if (notificationsEnabled) {
getLogger().log(Level.WARNING,
"Failed to add row to DB. Rolling back.", e);
try {
- delegate.rollback();
+ queryDelegate.rollback();
} catch (SQLException ee) {
getLogger().log(Level.SEVERE,
"Failed to roll back row addition", e);
if (itemId instanceof RowId && !(itemId instanceof TemporaryRowId)) {
try {
- return delegate.containsRowWithKey(((RowId) itemId).getId());
+ return queryDelegate.containsRowWithKey(((RowId) itemId)
+ .getId());
} catch (Exception e) {
/* Query failed, just return false. */
getLogger().log(Level.WARNING, "containsId query failed", e);
ResultSet rs = null;
try {
// Load ALL rows :(
- delegate.beginTransaction();
- rs = delegate.getResults(0, 0);
- List<String> pKeys = delegate.getPrimaryKeyColumns();
+ queryDelegate.beginTransaction();
+ rs = queryDelegate.getResults(0, 0);
+ List<String> pKeys = queryDelegate.getPrimaryKeyColumns();
while (rs.next()) {
RowId id = null;
if (pKeys.isEmpty()) {
}
rs.getStatement().close();
rs.close();
- delegate.commit();
+ queryDelegate.commit();
} catch (SQLException e) {
getLogger().log(Level.WARNING,
"getItemIds() failed, rolling back.", e);
try {
- delegate.rollback();
+ queryDelegate.rollback();
} catch (SQLException e1) {
getLogger().log(Level.SEVERE, "Failed to roll back state", e1);
}
return false;
}
try {
- delegate.beginTransaction();
- boolean success = delegate.removeRow((RowItem) i);
- delegate.commit();
+ queryDelegate.beginTransaction();
+ boolean success = queryDelegate.removeRow((RowItem) i);
+ queryDelegate.commit();
refresh();
if (notificationsEnabled) {
CacheFlushNotifier.notifyOfCacheFlush(this);
getLogger().log(Level.WARNING,
"Failed to remove row, rolling back", e);
try {
- delegate.rollback();
+ queryDelegate.rollback();
} catch (SQLException ee) {
/* Nothing can be done here */
getLogger().log(Level.SEVERE,
getLogger().log(Level.WARNING,
"Failed to remove row, rolling back", e);
try {
- delegate.rollback();
+ queryDelegate.rollback();
} catch (SQLException ee) {
/* Nothing can be done here */
getLogger().log(Level.SEVERE,
if (autoCommit) {
/* Remove and commit instantly. */
try {
- delegate.beginTransaction();
+ queryDelegate.beginTransaction();
boolean success = true;
for (Object id : getItemIds()) {
- if (!delegate.removeRow((RowItem) getItem(id))) {
+ if (!queryDelegate.removeRow((RowItem) getItem(id))) {
success = false;
}
}
if (success) {
- delegate.commit();
+ queryDelegate.commit();
getLogger().log(Level.FINER, "All rows removed from DB...");
refresh();
if (notificationsEnabled) {
CacheFlushNotifier.notifyOfCacheFlush(this);
}
} else {
- delegate.rollback();
+ queryDelegate.rollback();
}
return success;
} catch (SQLException e) {
getLogger().log(Level.WARNING,
"removeAllItems() failed, rolling back", e);
try {
- delegate.rollback();
+ queryDelegate.rollback();
} catch (SQLException ee) {
/* Nothing can be done here */
getLogger().log(Level.SEVERE, "Failed to roll back", ee);
getLogger().log(Level.WARNING,
"removeAllItems() failed, rolling back", e);
try {
- delegate.rollback();
+ queryDelegate.rollback();
} catch (SQLException ee) {
/* Nothing can be done here */
getLogger().log(Level.SEVERE, "Failed to roll back", ee);
int size = size();
// this protects against infinite looping
int counter = 0;
+ int oldIndex;
while (counter < size) {
- for (Integer i : itemIndexes.keySet()) {
- if (itemIndexes.get(i).equals(itemId)) {
- return i;
+ if (itemIndexes.containsValue(itemId)) {
+ for (Integer idx : itemIndexes.keySet()) {
+ if (itemIndexes.get(idx).equals(itemId)) {
+ return idx;
+ }
}
- counter++;
}
+ oldIndex = currentOffset;
// load in the next page.
- int nextIndex = (currentOffset / (pageLength * CACHE_RATIO) + 1)
- * (pageLength * CACHE_RATIO);
+ int nextIndex = currentOffset + pageLength * CACHE_RATIO
+ + cacheOverlap;
if (nextIndex >= size) {
// Container wrapped around, start from index 0.
nextIndex = 0;
}
updateOffsetAndCache(nextIndex);
+
+ // Update counter
+ if (currentOffset > oldIndex) {
+ counter += currentOffset - oldIndex;
+ } else {
+ counter += size - oldIndex;
+ }
}
// safeguard in case item not found
return -1;
*/
private void setPageLengthInternal(int pageLength) {
this.pageLength = pageLength > 0 ? pageLength : DEFAULT_PAGE_LENGTH;
- cachedItems.setCacheLimit(CACHE_RATIO * getPageLength());
+ cacheOverlap = getPageLength();
+ cachedItems.setCacheLimit(CACHE_RATIO * getPageLength() + cacheOverlap);
}
/**
try {
getLogger().log(Level.FINER,
"Commiting changes through delegate...");
- delegate.beginTransaction();
+ queryDelegate.beginTransaction();
/* Perform buffered deletions */
for (RowItem item : removedItems.values()) {
- if (!delegate.removeRow(item)) {
+ if (!queryDelegate.removeRow(item)) {
throw new SQLException("Removal failed for row with ID: "
+ item.getId());
}
}
/* Perform buffered modifications */
for (RowItem item : modifiedItems) {
- if (delegate.storeRow(item) > 0) {
+ if (queryDelegate.storeRow(item) > 0) {
/*
* Also reset the modified state in the item in case it is
* reused e.g. in a form.
*/
item.commit();
} else {
- delegate.rollback();
+ queryDelegate.rollback();
refresh();
throw new ConcurrentModificationException(
"Item with the ID '" + item.getId()
}
/* Perform buffered additions */
for (RowItem item : addedItems) {
- delegate.storeRow(item);
+ queryDelegate.storeRow(item);
}
- delegate.commit();
+ queryDelegate.commit();
removedItems.clear();
addedItems.clear();
modifiedItems.clear();
CacheFlushNotifier.notifyOfCacheFlush(this);
}
} catch (SQLException e) {
- delegate.rollback();
+ queryDelegate.rollback();
throw e;
} catch (OptimisticLockException e) {
- delegate.rollback();
+ queryDelegate.rollback();
throw e;
}
}
void itemChangeNotification(RowItem changedItem) {
if (autoCommit) {
try {
- delegate.beginTransaction();
- if (delegate.storeRow(changedItem) == 0) {
- delegate.rollback();
+ queryDelegate.beginTransaction();
+ if (queryDelegate.storeRow(changedItem) == 0) {
+ queryDelegate.rollback();
refresh();
throw new ConcurrentModificationException(
"Item with the ID '" + changedItem.getId()
+ "' has been externally modified.");
}
- delegate.commit();
+ queryDelegate.commit();
if (notificationsEnabled) {
CacheFlushNotifier.notifyOfCacheFlush(this);
}
getLogger().log(Level.WARNING,
"itemChangeNotification failed, rolling back...", e);
try {
- delegate.rollback();
+ queryDelegate.rollback();
} catch (SQLException ee) {
/* Nothing can be done here */
getLogger().log(Level.SEVERE, "Rollback failed", e);
* Index of the item that was requested, but not found in cache
*/
private void updateOffsetAndCache(int index) {
- if (itemIndexes.containsKey(index)) {
- return;
- }
- currentOffset = (index / (pageLength * CACHE_RATIO))
- * (pageLength * CACHE_RATIO);
+
+ int oldOffset = currentOffset;
+
+ currentOffset = (index / pageLength) * pageLength - cacheOverlap;
+
if (currentOffset < 0) {
currentOffset = 0;
}
+
+ if (oldOffset == currentOffset && !cachedItems.isEmpty()) {
+ return;
+ }
+
getPage();
}
}
try {
try {
- delegate.setFilters(filters);
+ queryDelegate.setFilters(filters);
} catch (UnsupportedOperationException e) {
getLogger().log(Level.FINE,
"The query delegate doesn't support filtering", e);
}
try {
- delegate.setOrderBy(sorters);
+ queryDelegate.setOrderBy(sorters);
} catch (UnsupportedOperationException e) {
getLogger().log(Level.FINE,
"The query delegate doesn't support sorting", e);
}
- int newSize = delegate.getCount();
+ int newSize = queryDelegate.getCount();
sizeUpdated = new Date();
sizeDirty = false;
if (newSize != size) {
private void getPropertyIds() throws SQLException {
propertyIds.clear();
propertyTypes.clear();
- delegate.setFilters(null);
- delegate.setOrderBy(null);
+ queryDelegate.setFilters(null);
+ queryDelegate.setOrderBy(null);
ResultSet rs = null;
ResultSetMetaData rsmd = null;
try {
- delegate.beginTransaction();
- rs = delegate.getResults(0, 1);
+ queryDelegate.beginTransaction();
+ rs = queryDelegate.getResults(0, 1);
rsmd = rs.getMetaData();
boolean resultExists = rs.next();
Class<?> type = null;
boolean persistable = !rsmd.isReadOnly(i);
- if (delegate instanceof TableQuery) {
+ if (queryDelegate instanceof TableQuery) {
if (rsmd.getColumnLabel(i).equals(
- ((TableQuery) delegate).getVersionColumn())) {
+ ((TableQuery) queryDelegate).getVersionColumn())) {
readOnly = true;
}
}
propertyPersistable.put(colName, persistable);
propertyNullable.put(colName,
rsmd.isNullable(i) == ResultSetMetaData.columnNullable);
- propertyPrimaryKey.put(colName, delegate.getPrimaryKeyColumns()
+ propertyPrimaryKey.put(colName, queryDelegate
+ .getPrimaryKeyColumns()
.contains(rsmd.getColumnLabel(i)));
propertyTypes.put(colName, type);
}
rs.getStatement().close();
rs.close();
- delegate.commit();
+ queryDelegate.commit();
getLogger().log(Level.FINER, "Property IDs fetched.");
} catch (SQLException e) {
getLogger().log(Level.WARNING,
"Failed to fetch property ids, rolling back", e);
try {
- delegate.rollback();
+ queryDelegate.rollback();
} catch (SQLException e1) {
getLogger().log(Level.SEVERE, "Failed to roll back", e1);
}
itemIndexes.clear();
try {
try {
- delegate.setOrderBy(sorters);
+ queryDelegate.setOrderBy(sorters);
} catch (UnsupportedOperationException e) {
/* The query delegate doesn't support sorting. */
/* No need to do anything. */
getLogger().log(Level.FINE,
"The query delegate doesn't support sorting", e);
}
- delegate.beginTransaction();
- int fetchedRows = pageLength * CACHE_RATIO;
- rs = delegate.getResults(currentOffset, fetchedRows);
+ queryDelegate.beginTransaction();
+ int fetchedRows = pageLength * CACHE_RATIO + cacheOverlap;
+ rs = queryDelegate.getResults(currentOffset, fetchedRows);
rsmd = rs.getMetaData();
- List<String> pKeys = delegate.getPrimaryKeyColumns();
+ List<String> pKeys = queryDelegate.getPrimaryKeyColumns();
// }
/* Create new items and column properties */
ColumnProperty cp = null;
int rowCount = currentOffset;
- if (!delegate.implementationRespectsPagingLimits()) {
+ if (!queryDelegate.implementationRespectsPagingLimits()) {
rowCount = currentOffset = 0;
setPageLengthInternal(size);
}
}
rs.getStatement().close();
rs.close();
- delegate.commit();
+ queryDelegate.commit();
getLogger().log(Level.FINER, "Fetched {0} rows starting from {1}",
new Object[] { fetchedRows, currentOffset });
} catch (SQLException e) {
getLogger().log(Level.WARNING,
"Failed to fetch rows, rolling back", e);
try {
- delegate.rollback();
+ queryDelegate.rollback();
} catch (SQLException e1) {
getLogger().log(Level.SEVERE, "Failed to roll back", e1);
}
*/
private boolean isColumnIdentifierValid(String identifier) {
if (identifier.equalsIgnoreCase("rownum")
- && delegate instanceof TableQuery) {
- TableQuery tq = (TableQuery) delegate;
+ && queryDelegate instanceof TableQuery) {
+ TableQuery tq = (TableQuery) queryDelegate;
if (tq.getSqlGenerator() instanceof MSSQLGenerator
|| tq.getSqlGenerator() instanceof OracleGenerator) {
return false;
* @return current querydelegate
*/
protected QueryDelegate getQueryDelegate() {
- return delegate;
+ return queryDelegate;
}
/************************************/
* @param listener
*/
public void addRowIdChangeListener(RowIdChangeListener listener) {
- if (delegate instanceof QueryDelegate.RowIdChangeNotifier) {
- ((QueryDelegate.RowIdChangeNotifier) delegate)
+ if (queryDelegate instanceof QueryDelegate.RowIdChangeNotifier) {
+ ((QueryDelegate.RowIdChangeNotifier) queryDelegate)
.addListener(listener);
}
}
* @param listener
*/
public void removeRowIdChangeListener(RowIdChangeListener listener) {
- if (delegate instanceof QueryDelegate.RowIdChangeNotifier) {
- ((QueryDelegate.RowIdChangeNotifier) delegate)
+ if (queryDelegate instanceof QueryDelegate.RowIdChangeNotifier) {
+ ((QueryDelegate.RowIdChangeNotifier) queryDelegate)
.removeListener(listener);
}
}