public void testWriteRecords() { HistoryWriter writer = this.history.getWriter(); try { for (int i = 0; i < 202; i++) { writer.addRecord(new String[] {"" + random.nextInt(), "name" + i, i % 2 == 0 ? "m" : "f"}); } } catch (Exception e) { fail("Could not write records. Reason: " + e); } }
public void testWriteRecordsWithMaxNumber() { HistoryWriter writer = this.history.getWriter(); HistoryReader reader = this.history.getReader(); try { for (int i = 0; i < 20; i++) { writer.addRecord(new String[] {"" + i, "name" + i, i % 2 == 0 ? "m" : "f"}, 20); synchronized (this) { try { wait(100); } catch (Throwable t) { } } } QueryResultSet<HistoryRecord> recs = reader.findLast(20); int count = 0; while (recs.hasNext()) { count++; recs.next(); } assertEquals("Wrong count of messages", 20, count); writer.addRecord(new String[] {"" + 21, "name" + 21, "f"}, 20); recs = reader.findLast(20); count = 0; boolean foundFirstMessage = false; while (recs.hasNext()) { count++; HistoryRecord hr = recs.next(); if (hr.getPropertyValues()[0].equals("0")) foundFirstMessage = true; } assertEquals("Wrong count of messages", 20, count); assertFalse("Wrong message removed, must be the first one", foundFirstMessage); } catch (Exception e) { e.printStackTrace(); fail("Could not write records. Reason: " + e); } }
/** Adds recent message in history. */ private void saveRecentMessageToHistory(ComparableEvtObj msc) { synchronized (historyID) { // and create it try { History history = getHistory(); HistoryWriter writer = history.getWriter(); SimpleDateFormat sdf = new SimpleDateFormat(HistoryService.DATE_FORMAT); writer.addRecord( new String[] { msc.getProtocolProviderService().getAccountID().getAccountUniqueID(), msc.getContactAddress(), sdf.format(msc.getTimestamp()), RECENT_MSGS_VER }, NUMBER_OF_MSGS_IN_HISTORY); } catch (IOException ex) { logger.error("cannot create recent_messages history", ex); return; } } }
/** Updates recent message in history. */ private void updateRecentMessageToHistory(final ComparableEvtObj msg) { synchronized (historyID) { // and create it try { History history = getHistory(); HistoryWriter writer = history.getWriter(); writer.updateRecord( new HistoryWriter.HistoryRecordUpdater() { HistoryRecord hr; @Override public void setHistoryRecord(HistoryRecord historyRecord) { this.hr = historyRecord; } @Override public boolean isMatching() { boolean providerFound = false; boolean contactFound = false; for (int i = 0; i < hr.getPropertyNames().length; i++) { String propName = hr.getPropertyNames()[i]; if (propName.equals(STRUCTURE_NAMES[0])) { if (msg.getProtocolProviderService() .getAccountID() .getAccountUniqueID() .equals(hr.getPropertyValues()[i])) { providerFound = true; } } else if (propName.equals(STRUCTURE_NAMES[1])) { if (msg.getContactAddress().equals(hr.getPropertyValues()[i])) { contactFound = true; } } } return contactFound && providerFound; } @Override public Map<String, String> getUpdateChanges() { HashMap<String, String> map = new HashMap<String, String>(); SimpleDateFormat sdf = new SimpleDateFormat(HistoryService.DATE_FORMAT); for (int i = 0; i < hr.getPropertyNames().length; i++) { String propName = hr.getPropertyNames()[i]; if (propName.equals(STRUCTURE_NAMES[0])) { map.put( propName, msg.getProtocolProviderService().getAccountID().getAccountUniqueID()); } else if (propName.equals(STRUCTURE_NAMES[1])) { map.put(propName, msg.getContactAddress()); } else if (propName.equals(STRUCTURE_NAMES[2])) { map.put(propName, sdf.format(msg.getTimestamp())); } else if (propName.equals(STRUCTURE_NAMES[3])) map.put(propName, RECENT_MSGS_VER); } return map; } }); } catch (IOException ex) { logger.error("cannot create recent_messages history", ex); return; } } }