private String getProperty(final String propertyName) throws IOException { if (_dir == null) { return null; } try { return doReadOperation(new ReadOperation<String>() { public String execute(final IndexReader reader, final Searcher searcher, final Analyzer analyzer) throws IOException, ParseException { Hits hits = searcher.search(new TermQuery(new Term(FIELD_PROPERTY_KEY, propertyName))); Iterator<?> iterator = hits.iterator(); if (iterator.hasNext()) { return ((Hit) iterator.next()).get(FIELD_PROPERTY_VALUE); } return null; } }, false); } catch (QuerySyntaxException ex) { throw new NoQueryPerformedException(ex); } }
@Override public Iterator<Conversation> iterator() { final Iterator<Hit> hitsIterator = hits.iterator(); // Advance the iterator until we hit the index. for (int i=0; i<index; i++) { hitsIterator.next(); } return new Iterator<Conversation>() { private Conversation nextElement = null; public boolean hasNext() { if (nextElement == null) { nextElement = getNextElement(); if (nextElement == null) { return false; } } return true; } public Conversation next() { Conversation element; if (nextElement != null) { element = nextElement; nextElement = null; } else { element = getNextElement(); if (element == null) { throw new NoSuchElementException(); } } return element; } public void remove() { throw new UnsupportedOperationException(); } private Conversation getNextElement() { if (!hitsIterator.hasNext()) { return null; } // If we've reached the end index, stop iterating. else if (index >= endIndex) { return null; } while (hitsIterator.hasNext()) { try { Hit hit = hitsIterator.next(); // Advance the index. index++; long conversationID = Long.parseLong(hit.get("conversationID")); return new Conversation(conversationManager, conversationID); } catch (Exception e) { Log.error(e.getMessage(), e); } } return null; } }; }