/** * Returns the cached recent messages history. * * @return * @throws IOException */ private History getHistory() throws IOException { synchronized (historyID) { HistoryService historyService = MessageHistoryActivator.getMessageHistoryService().getHistoryService(); if (history == null) { history = historyService.createHistory(historyID, recordStructure); // lets check the version if not our version, re-create // history (delete it) HistoryReader reader = history.getReader(); boolean delete = false; QueryResultSet<HistoryRecord> res = reader.findLast(1); if (res != null && res.hasNext()) { HistoryRecord hr = res.next(); if (hr.getPropertyValues().length >= 4) { if (!hr.getPropertyValues()[3].equals(RECENT_MSGS_VER)) delete = true; } else delete = true; } if (delete) { // delete it try { historyService.purgeLocallyStoredHistory(historyID); history = historyService.createHistory(historyID, recordStructure); } catch (IOException ex) { logger.error("Cannot delete recent_messages history", ex); } } } return history; } }
/** * Crops the existing result file and extracts a subset from the given starting point to the * ending point. * * @param start the beginning of the subset. * @param length the end of the subset. * @return ResultSet a subset of the current result set. */ public ResultSet getResultSet(int start, int length) { length = length < docids.length ? length : docids.length; QueryResultSet resultSet = new QueryResultSet(length); resultSet.setExactResultSize(this.exactResultSize); System.arraycopy(docids, start, resultSet.getDocids(), 0, length); System.arraycopy(scores, start, resultSet.getScores(), 0, length); System.arraycopy(occurrences, start, resultSet.getOccurrences(), 0, length); return resultSet; }
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); } }
/** * Extracts a subset of the resultset given by the list parameter, which contains a list of * <b>positions</b> in the resultset that should be saved. <br> * <b>NB:</b>The metadata hashtable is NOT reduced. * * @param positions int[] the list of elements in the current list that should be kept. * @return a subset of the current result set specified by the list. */ public ResultSet getResultSet(int[] positions) { int NewSize = positions.length; // if (logger.isDebugEnabled()) // logger.debug("New results size is "+NewSize); QueryResultSet resultSet = new QueryResultSet(NewSize); resultSet.setExactResultSize(this.exactResultSize); int newDocids[] = resultSet.getDocids(); double newScores[] = resultSet.getScores(); short newOccurs[] = resultSet.getOccurrences(); int thisPosition; for (int i = 0; i < NewSize; i++) { thisPosition = positions[i]; // if (logger.isDebugEnabled()) // logger.debug("adding result at "+i); newDocids[i] = docids[thisPosition]; newScores[i] = scores[thisPosition]; newOccurs[i] = occurrences[thisPosition]; } return resultSet; }
public void testReadRecords() { HistoryReader reader = this.history.getReader(); QueryResultSet<HistoryRecord> result = reader.findByKeyword("name2", "name"); assertTrue("Nothing found", result.hasNext()); while (result.hasNext()) { HistoryRecord record = result.nextRecord(); String[] vals = record.getPropertyValues(); try { int n = Integer.parseInt(vals[1].substring(4)); assertEquals(3, vals.length); assertEquals(n % 2 == 0 ? "m" : "f", vals[2]); } catch (Exception e) { fail("Bad data! Expected nameXXXX, where XXXX is " + "an integer, but found: " + vals[0]); } } }