Check for concurrent cache updates.

This commit is contained in:
akwizgran
2020-01-24 14:48:47 +00:00
parent f4ec1e6a72
commit 3a444c814f

View File

@@ -15,8 +15,8 @@ import java.io.BufferedInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.logging.Logger; import java.util.logging.Logger;
@@ -50,9 +50,9 @@ class AttachmentRetrieverImpl implements AttachmentRetriever {
private final int minWidth, maxWidth; private final int minWidth, maxWidth;
private final int minHeight, maxHeight; private final int minHeight, maxHeight;
private final Map<MessageId, MutableLiveData<AttachmentItem>> private final ConcurrentMap<MessageId, MutableLiveData<AttachmentItem>>
itemsWithSize = new ConcurrentHashMap<>(); itemsWithSize = new ConcurrentHashMap<>();
private final Map<MessageId, MutableLiveData<AttachmentItem>> private final ConcurrentMap<MessageId, MutableLiveData<AttachmentItem>>
itemsWithoutSize = new ConcurrentHashMap<>(); itemsWithoutSize = new ConcurrentHashMap<>();
@Inject @Inject
@@ -99,15 +99,25 @@ class AttachmentRetrieverImpl implements AttachmentRetriever {
if (liveData == null) { if (liveData == null) {
AttachmentItem item = new AttachmentItem(h, AttachmentItem item = new AttachmentItem(h,
defaultSize, defaultSize, LOADING); defaultSize, defaultSize, LOADING);
final MutableLiveData<AttachmentItem> finalLiveData = liveData = new MutableLiveData<>(item);
new MutableLiveData<>(item); // add new LiveData to cache, checking for concurrent updates
// kick-off loading of attachment, will post to live data MutableLiveData<AttachmentItem> oldLiveData;
dbExecutor.execute( if (needsSize) {
() -> loadAttachmentItem(h, needsSize, finalLiveData)); oldLiveData = itemsWithSize.putIfAbsent(h.getMessageId(),
// add new LiveData to cache liveData);
liveData = finalLiveData; } else {
if (needsSize) itemsWithSize.put(h.getMessageId(), liveData); oldLiveData = itemsWithoutSize.putIfAbsent(h.getMessageId(),
else itemsWithoutSize.put(h.getMessageId(), liveData); liveData);
}
if (oldLiveData == null) {
// kick-off loading of attachment, will post to live data
MutableLiveData<AttachmentItem> finalLiveData = liveData;
dbExecutor.execute(() ->
loadAttachmentItem(h, needsSize, finalLiveData));
} else {
// Concurrent cache update - use the existing live data
liveData = oldLiveData;
}
} }
items.add(liveData); items.add(liveData);
} }
@@ -118,12 +128,15 @@ class AttachmentRetrieverImpl implements AttachmentRetriever {
@DatabaseExecutor @DatabaseExecutor
public void cacheAttachmentItemWithSize(MessageId conversationMessageId, public void cacheAttachmentItemWithSize(MessageId conversationMessageId,
AttachmentHeader h) throws DbException { AttachmentHeader h) throws DbException {
// If a live data is already cached we don't need to do anything
if (itemsWithSize.containsKey(h.getMessageId())) return;
try { try {
Attachment a = messagingManager.getAttachment(h); Attachment a = messagingManager.getAttachment(h);
AttachmentItem item = createAttachmentItem(a, true); AttachmentItem item = createAttachmentItem(a, true);
MutableLiveData<AttachmentItem> liveData = MutableLiveData<AttachmentItem> liveData =
new MutableLiveData<>(item); new MutableLiveData<>(item);
itemsWithSize.put(h.getMessageId(), liveData); // If a live data was concurrently cached, don't replace it
itemsWithSize.putIfAbsent(h.getMessageId(), liveData);
} catch (NoSuchMessageException e) { } catch (NoSuchMessageException e) {
LOG.info("Attachment not received yet"); LOG.info("Attachment not received yet");
} }