Skip to content

Commit

Permalink
Fix AbstractRemoteDataSource cache clearing (#18630)
Browse files Browse the repository at this point in the history
This patch also reduces the amount of RPC calls when dropping rows from
cache.

Change-Id: Ib69a807883bc885dcd877a008cec16e44fa2bfdd
  • Loading branch information
Teemu Suo-Anttila authored and Vaadin Code Review committed Aug 21, 2015
1 parent d40df1d commit 5db6f10
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
Expand Up @@ -107,10 +107,16 @@ public void updateRowData(JsonObject row) {


private DataRequestRpc rpcProxy = getRpcProxy(DataRequestRpc.class); private DataRequestRpc rpcProxy = getRpcProxy(DataRequestRpc.class);
private DetailsListener detailsListener; private DetailsListener detailsListener;
private JsonArray droppedRowKeys = Json.createArray();


@Override @Override
protected void requestRows(int firstRowIndex, int numberOfRows, protected void requestRows(int firstRowIndex, int numberOfRows,
RequestRowsCallback<JsonObject> callback) { RequestRowsCallback<JsonObject> callback) {
if (droppedRowKeys.length() > 0) {
rpcProxy.dropRows(droppedRowKeys);
droppedRowKeys = Json.createArray();
}

/* /*
* If you're looking at this code because you want to learn how to * If you're looking at this code because you want to learn how to
* use AbstactRemoteDataSource, please look somewhere else instead. * use AbstactRemoteDataSource, please look somewhere else instead.
Expand Down Expand Up @@ -235,10 +241,8 @@ protected void updateRowData(JsonObject row) {
} }


@Override @Override
protected void onDropFromCache(int rowIndex) { protected void onDropFromCache(int rowIndex, JsonObject row) {
super.onDropFromCache(rowIndex); droppedRowKeys.set(droppedRowKeys.length(), getRowKey(row));

rpcProxy.dropRow(getRowKey(getRow(rowIndex)));
} }
} }


Expand Down
45 changes: 37 additions & 8 deletions client/src/com/vaadin/client/data/AbstractRemoteDataSource.java
Expand Up @@ -287,8 +287,7 @@ private void checkCacheCoverage() {
* Simple case: no overlap between cached data and needed data. * Simple case: no overlap between cached data and needed data.
* Clear the cache and request new data * Clear the cache and request new data
*/ */
indexToRowMap.clear(); dropFromCache(cached);
keyToIndexMap.clear();
cached = Range.between(0, 0); cached = Range.between(0, 0);


handleMissingRows(getMaxCacheRange()); handleMissingRows(getMaxCacheRange());
Expand Down Expand Up @@ -330,27 +329,48 @@ private void discardStaleCacheEntries() {


private void dropFromCache(Range range) { private void dropFromCache(Range range) {
for (int i = range.getStart(); i < range.getEnd(); i++) { for (int i = range.getStart(); i < range.getEnd(); i++) {
// Called before dropping from cache, so we can actually do // Called after dropping from cache. Dropped row is passed as a
// something with the data before the drop. // parameter, but is no longer present in the DataSource
onDropFromCache(i);

T removed = indexToRowMap.remove(Integer.valueOf(i)); T removed = indexToRowMap.remove(Integer.valueOf(i));
onDropFromCache(i, removed);
keyToIndexMap.remove(getRowKey(removed)); keyToIndexMap.remove(getRowKey(removed));
} }
} }


/** /**
* A hook that can be overridden to do something whenever a row is about to * A hook that can be overridden to do something whenever a row has been
* be dropped from the cache. * dropped from the cache. DataSource no longer has anything in the given
* index.
* <p>
* NOTE: This method has been replaced. Override
* {@link #onDropFromCache(int, Object)} instead of this method.
* *
* @since 7.5.0 * @since 7.5.0
* @param rowIndex * @param rowIndex
* the index of the dropped row * the index of the dropped row
* @deprecated replaced by {@link #onDropFromCache(int, Object)}
*/ */
@Deprecated
protected void onDropFromCache(int rowIndex) { protected void onDropFromCache(int rowIndex) {
// noop // noop
} }


/**
* A hook that can be overridden to do something whenever a row has been
* dropped from the cache. DataSource no longer has anything in the given
* index.
*
* @since
* @param rowIndex
* the index of the dropped row
* @param removed
* the removed row object
*/
protected void onDropFromCache(int rowIndex, T removed) {
// Call old version as a fallback (someone might have used it)
onDropFromCache(rowIndex);
}

private void handleMissingRows(Range range) { private void handleMissingRows(Range range) {
if (range.isEmpty()) { if (range.isEmpty()) {
return; return;
Expand Down Expand Up @@ -481,6 +501,15 @@ protected void setRowData(int firstRowIndex, List<T> rowData) {
* updated before the widget settings. Support for this will be * updated before the widget settings. Support for this will be
* implemented later on. * implemented later on.
*/ */

// Run a dummy drop from cache for unused rows.
for (int i = 0; i < partition[0].length(); ++i) {
onDropFromCache(i + partition[0].getStart(), rowData.get(i));
}

for (int i = 0; i < partition[2].length(); ++i) {
onDropFromCache(i + partition[2].getStart(), rowData.get(i));
}
} }


// Eventually check whether all needed rows are now available // Eventually check whether all needed rows are now available
Expand Down
9 changes: 6 additions & 3 deletions server/src/com/vaadin/data/RpcDataProviderExtension.java
Expand Up @@ -123,7 +123,7 @@ public String getKey(Object itemId) {
* the item ids for which to get keys * the item ids for which to get keys
* @return keys for the {@code itemIds} * @return keys for the {@code itemIds}
*/ */
public List<String> getKeys(Collection<Object> itemIds) { public List<String> getKeys(Collection<?> itemIds) {
if (itemIds == null) { if (itemIds == null) {
throw new IllegalArgumentException("itemIds can't be null"); throw new IllegalArgumentException("itemIds can't be null");
} }
Expand Down Expand Up @@ -698,8 +698,11 @@ public void setPinned(String key, boolean isPinned) {
} }


@Override @Override
public void dropRow(String rowKey) { public void dropRows(JsonArray rowKeys) {
activeItemHandler.dropActiveItem(keyMapper.getItemId(rowKey)); for (int i = 0; i < rowKeys.length(); ++i) {
activeItemHandler.dropActiveItem(keyMapper
.getItemId(rowKeys.getString(i)));
}
} }
}); });


Expand Down
10 changes: 6 additions & 4 deletions shared/src/com/vaadin/shared/data/DataRequestRpc.java
Expand Up @@ -20,6 +20,8 @@
import com.vaadin.shared.annotations.NoLoadingIndicator; import com.vaadin.shared.annotations.NoLoadingIndicator;
import com.vaadin.shared.communication.ServerRpc; import com.vaadin.shared.communication.ServerRpc;


import elemental.json.JsonArray;

/** /**
* RPC interface used for requesting container data to the client. * RPC interface used for requesting container data to the client.
* *
Expand Down Expand Up @@ -59,13 +61,13 @@ public void requestRows(int firstRowIndex, int numberOfRows,
public void setPinned(String key, boolean isPinned); public void setPinned(String key, boolean isPinned);


/** /**
* Informs the server that an item is dropped from the client cache. * Informs the server that items have been dropped from the client cache.
* *
* @since * @since
* @param rowKey * @param rowKeys
* key mapping to item * array of dropped keys mapping to items
*/ */
@Delayed @Delayed
@NoLoadingIndicator @NoLoadingIndicator
public void dropRow(String rowKey); public void dropRows(JsonArray rowKeys);
} }

0 comments on commit 5db6f10

Please sign in to comment.