Optimizes access to history service from recent messages.

fix-message-formatting 5133
Damian Minkov 12 years ago
parent 2e0a679719
commit 0367129e2a

@ -86,6 +86,11 @@ public class MessageSourceService
private static final HistoryID historyID = HistoryID.createFromRawID(
new String[] { "recent_messages"});
/**
* The cache for recent messages.
*/
private History history = null;
/**
* List of recent messages.
*/
@ -162,7 +167,7 @@ public ContactQuery createContactQuery(String queryString)
return recentQuery;
}
private List<MessageSourceContact> getRecentMessages()
private synchronized List<MessageSourceContact> getRecentMessages()
{
if(recentMessages == null)
{
@ -203,6 +208,38 @@ private List<MessageSourceContact> getRecentMessages()
return recentMessages;
}
/**
* Returns the cached recent messages history.
* @return
* @throws IOException
*/
private History getHistory()
throws IOException
{
synchronized(historyID)
{
MessageHistoryServiceImpl msgService
= MessageHistoryActivator.getMessageHistoryService();
HistoryService historyService = msgService.getHistoryService();
// if not existing, return to search for initial load
if (history == null
&& !historyService.isHistoryCreated(historyID))
return null;
if(history == null)
{
if (historyService.isHistoryExisting(historyID))
history = historyService.getHistory(historyID);
else
history = historyService.createHistory(
historyID, recordStructure);
}
return history;
}
}
/**
* Loads recent messages if saved in history.
* @return
@ -211,27 +248,17 @@ private List<MessageSourceContact> getRecentMessagesFromHistory()
{
MessageHistoryServiceImpl msgService
= MessageHistoryActivator.getMessageHistoryService();
HistoryService historyService = msgService.getHistoryService();
// if not existing, return to search for initial load
if (!historyService.isHistoryCreated(historyID))
return null;
List<MessageSourceContact> res
= new LinkedList<MessageSourceContact>();
// and load it
try
{
SimpleDateFormat sdf
= new SimpleDateFormat(HistoryService.DATE_FORMAT);
History history = getHistory();
History history;
if (historyService.isHistoryExisting(historyID))
history = historyService.getHistory(historyID);
else
history
= historyService.createHistory(historyID, recordStructure);
if(history == null)
return null;
List<MessageSourceContact> res
= new LinkedList<MessageSourceContact>();
Iterator<HistoryRecord> recs
= history.getReader().findLast(numberOfMessages);
@ -261,14 +288,14 @@ else if (propName.equals(STRUCTURE_NAMES[1]))
res.add(new MessageSourceContact(ev, this));
}
}
return res;
}
catch(IOException ex)
{
logger.error("cannot create recent_messages history", ex);
return null;
}
return res;
}
/**
@ -276,53 +303,54 @@ else if (propName.equals(STRUCTURE_NAMES[1]))
*/
private void saveRecentMessagesToHistory()
{
HistoryService historyService = MessageHistoryActivator
.getMessageHistoryService().getHistoryService();
if (historyService.isHistoryExisting(historyID))
synchronized(historyID)
{
// delete it
try
{
historyService.purgeLocallyStoredHistory(historyID);
}
catch(IOException ex)
HistoryService historyService = MessageHistoryActivator
.getMessageHistoryService().getHistoryService();
if (historyService.isHistoryExisting(historyID))
{
logger.error("Cannot delete recent_messages history", ex);
return;
// delete it
try
{
historyService.purgeLocallyStoredHistory(historyID);
}
catch(IOException ex)
{
logger.error("Cannot delete recent_messages history", ex);
return;
}
}
}
// and create it
try
{
History history = historyService.createHistory(
historyID, recordStructure);
// and create it
try
{
history = historyService.createHistory(
historyID, recordStructure);
HistoryWriter writer = history.getWriter();
SimpleDateFormat sdf
= new SimpleDateFormat(HistoryService.DATE_FORMAT);
HistoryWriter writer = history.getWriter();
List<MessageSourceContact> messages = getRecentMessages();
List<MessageSourceContact> messages = getRecentMessages();
synchronized(messages)
{
for(MessageSourceContact msc : messages)
synchronized(messages)
{
writer.addRecord(
new String[]
{
msc.getProtocolProviderService()
.getAccountID().getAccountUniqueID(),
msc.getContactAddress()
});
for(MessageSourceContact msc : messages)
{
writer.addRecord(
new String[]
{
msc.getProtocolProviderService()
.getAccountID().getAccountUniqueID(),
msc.getContactAddress()
});
}
}
}
}
catch(IOException ex)
{
logger.error("cannot create recent_messages history", ex);
return;
catch(IOException ex)
{
logger.error("cannot create recent_messages history", ex);
return;
}
}
}

Loading…
Cancel
Save