@Override protected EntityInfo loadEntityInfo(int index) { try { return extractor.extract(index); } catch (IOException e) { throw new CacheException("Cannot load result at index " + index, e); } } }
@Override protected EntityInfo loadEntityInfo(int index) { try { return extractor.extract(index); } catch (IOException e) { throw new CacheException("Cannot load result at index " + index, e); } } }
/** * Extract an entityInfo, either from cache or from the index. * @param x the position in the index. * @return */ private EntityInfo getEntityInfo(int x) { EntityInfo entityInfo = einfo == null ? null : einfo.get(); if ( entityInfo == null ) { try { entityInfo = documentExtractor.extract( x ); } catch (IOException e) { throw new SearchException( "Unable to read Lucene topDocs[" + x + "]", e ); } einfo = new SoftReference<EntityInfo>( entityInfo ); } return entityInfo; }
/** * Utility to extract the cache key of a DocumentExtractor and use the KeyTransformationHandler to turn the string * into the actual key object. * * @param extractor * @param docIndex * @return */ Object extractKey(DocumentExtractor extractor, int docIndex) { String strKey; try { strKey = (String) extractor.extract(docIndex).getId(); } catch (IOException e) { throw new SearchException("Error while extracting key", e); } return keyTransformationHandler.stringToKey(strKey); } }
public static Object extractKey(DocumentExtractor extractor, Cache<?, ?> cache, KeyTransformationHandler keyTransformationHandler, int docIndex) { String bufferDocumentId; try { bufferDocumentId = (String) extractor.extract(docIndex).getId(); } catch (IOException e) { log.error("Error while extracting key...", e); return null; } Object key = keyTransformationHandler.stringToKey(bufferDocumentId, cache .getAdvancedCache().getClassLoader()); return key; }
@Test(expectedExceptions = IllegalArgumentException.class) public void testLazyIteratorInitWithInvalidFetchSize() throws IOException { DocumentExtractor extractor = mock(DocumentExtractor.class); when(extractor.extract(anyInt())).thenAnswer((Answer<EntityInfo>) invocation -> { int index = (Integer) invocation.getArguments()[0]; return entityInfos.get(index); }); cache = mock(AdvancedCache.class); new LazyIterator<>(extractor, new EntityLoader(cache, new KeyTransformationHandler(null)), getFetchSize()); }
@BeforeMethod public void setUp() throws Exception { super.setUp(); extractor = mock(DocumentExtractor.class); when(extractor.getMaxIndex()).thenReturn(entityInfos.size() - 1); when(extractor.extract(anyInt())).thenAnswer((Answer<EntityInfo>) invocation -> { int index = (Integer) invocation.getArguments()[0]; return entityInfos.get(index); }); iterator = new LazyIterator<>(extractor, new EntityLoader(cache, new KeyTransformationHandler(null)), getFetchSize()); }
DocumentExtractor extractor = buildDocumentExtractor( searcher, queryHits, first, max ); for ( int index = first; index <= max; index++ ) { infos.add( extractor.extract( index ) );
DocumentExtractor extractor = buildDocumentExtractor( searcher, queryHits, first, max ); for ( int index = first; index <= max; index++ ) { infos.add( extractor.extract( index ) );
private NodeTopDocs collectKeys(DocumentExtractor extractor, HSQuery query) { TopDocs topDocs = extractor.getTopDocs(); int topDocsLength = topDocs.scoreDocs.length; Object[] keys = null; Object[] projections = null; if (query.getProjectedFields() == null) { keys = new Object[topDocsLength]; // collecting keys (it's a eager query!) for (int i = 0; i < topDocsLength; i++) { keys[i] = extractKey(extractor, i); } } else { projections = new Object[topDocsLength]; try { for (int docIndex = 0; docIndex < topDocsLength; docIndex++) { projections[docIndex] = extractor.extract(docIndex).getProjection(); } } catch (IOException e) { throw new SearchException("Error while extracting projection", e); } } return new NodeTopDocs(cache.getRpcManager().getAddress(), topDocs, keys, projections); } }
@Test public void scrollForwardToArbitraryPosition() throws Exception { generateData( 0, 1000 ); Query query = builder().all().createQuery(); try ( DocumentExtractor extractor = getQuery( query ) .queryDocumentExtractor() ) { EntityInfo info = extractor.extract( 1 ); assertNotNull( info ); assertEquals( 1, info.getId() ); info = extractor.extract( 500 ); assertNotNull( info ); assertEquals( 500, info.getId() ); } }
@Test public void scrollBackward() throws Exception { generateData( 0, 1001 ); Query query = builder().all().createQuery(); try ( DocumentExtractor extractor = getQuery( query ) .queryDocumentExtractor() ) { EntityInfo info = extractor.extract( 1000 ); assertNotNull( info ); assertEquals( 1000, info.getId() ); // Backtrack exactly 1000 positions info = extractor.extract( 0 ); assertNotNull( info ); assertEquals( 0, info.getId() ); } }
@Test public void scrollBeyondMaxResultWindow() throws Exception { generateData( 0, DEFAULT_MAX_RESULT_WINDOW + 10 ); Query query = builder().all().createQuery(); try ( DocumentExtractor extractor = getQuery( query ) .queryDocumentExtractor() ) { for ( int i = 0; i < DEFAULT_MAX_RESULT_WINDOW + 10; ++i ) { EntityInfo info = extractor.extract( i ); assertNotNull( info ); assertEquals( i, info.getId() ); } } }
HashSet<String> titles = new HashSet<String>( 3 ); for ( int id = 0; id < hits.totalHits; id++ ) { String projectedTitle = (String) extractor.extract( id ).getProjection()[0]; assertNotNull( projectedTitle ); titles.add( projectedTitle );