📄 testfieldsreader.java
字号:
FSDirectory tmpDir = FSDirectory.getDirectory(file); assertTrue(tmpDir != null); IndexWriter writer = new IndexWriter(tmpDir, new WhitespaceAnalyzer(), true); writer.setUseCompoundFile(false); writer.addDocument(testDoc); writer.close(); assertTrue(fieldInfos != null); FieldsReader reader; long lazyTime = 0; long regularTime = 0; int length = 50; Set lazyFieldNames = new HashSet(); lazyFieldNames.add(DocHelper.LARGE_LAZY_FIELD_KEY); SetBasedFieldSelector fieldSelector = new SetBasedFieldSelector(Collections.EMPTY_SET, lazyFieldNames); for (int i = 0; i < length; i++) { reader = new FieldsReader(tmpDir, TEST_SEGMENT_NAME, fieldInfos); assertTrue(reader != null); assertTrue(reader.size() == 1); Document doc; doc = reader.doc(0, null);//Load all of them assertTrue("doc is null and it shouldn't be", doc != null); Fieldable field = doc.getFieldable(DocHelper.LARGE_LAZY_FIELD_KEY); assertTrue("field is lazy", field.isLazy() == false); String value; long start; long finish; start = System.currentTimeMillis(); //On my machine this was always 0ms. value = field.stringValue(); finish = System.currentTimeMillis(); assertTrue("value is null and it shouldn't be", value != null); assertTrue("field is null and it shouldn't be", field != null); regularTime += (finish - start); reader.close(); reader = null; doc = null; //Hmmm, are we still in cache??? System.gc(); reader = new FieldsReader(tmpDir, TEST_SEGMENT_NAME, fieldInfos); doc = reader.doc(0, fieldSelector); field = doc.getFieldable(DocHelper.LARGE_LAZY_FIELD_KEY); assertTrue("field is not lazy", field.isLazy() == true); start = System.currentTimeMillis(); //On my machine this took around 50 - 70ms value = field.stringValue(); finish = System.currentTimeMillis(); assertTrue("value is null and it shouldn't be", value != null); lazyTime += (finish - start); reader.close(); } System.out.println("Average Non-lazy time (should be very close to zero): " + regularTime / length + " ms for " + length + " reads"); System.out.println("Average Lazy Time (should be greater than zero): " + lazyTime / length + " ms for " + length + " reads"); } public void testLoadSize() throws IOException { FieldsReader reader = new FieldsReader(dir, TEST_SEGMENT_NAME, fieldInfos); Document doc; doc = reader.doc(0, new FieldSelector(){ public FieldSelectorResult accept(String fieldName) { if (fieldName.equals(DocHelper.TEXT_FIELD_1_KEY) || fieldName.equals(DocHelper.COMPRESSED_TEXT_FIELD_2_KEY) || fieldName.equals(DocHelper.LAZY_FIELD_BINARY_KEY)) return FieldSelectorResult.SIZE; else if (fieldName.equals(DocHelper.TEXT_FIELD_3_KEY)) return FieldSelectorResult.LOAD; else return FieldSelectorResult.NO_LOAD; } }); Fieldable f1 = doc.getFieldable(DocHelper.TEXT_FIELD_1_KEY); Fieldable f3 = doc.getFieldable(DocHelper.TEXT_FIELD_3_KEY); Fieldable fb = doc.getFieldable(DocHelper.LAZY_FIELD_BINARY_KEY); assertTrue(f1.isBinary()); assertTrue(!f3.isBinary()); assertTrue(fb.isBinary()); assertSizeEquals(2*DocHelper.FIELD_1_TEXT.length(), f1.binaryValue()); assertEquals(DocHelper.FIELD_3_TEXT, f3.stringValue()); assertSizeEquals(DocHelper.LAZY_FIELD_BINARY_BYTES.length, fb.binaryValue()); reader.close(); } private void assertSizeEquals(int size, byte[] sizebytes) { assertEquals((byte) (size>>>24), sizebytes[0]); assertEquals((byte) (size>>>16), sizebytes[1]); assertEquals((byte) (size>>> 8), sizebytes[2]); assertEquals((byte) size , sizebytes[3]); } public static class FaultyFSDirectory extends Directory { FSDirectory fsDir; public FaultyFSDirectory(File dir) throws IOException { fsDir = FSDirectory.getDirectory(dir); lockFactory = fsDir.getLockFactory(); } public IndexInput openInput(String name) throws IOException { return new FaultyIndexInput(fsDir.openInput(name)); } public String[] list() throws IOException { return fsDir.list(); } public boolean fileExists(String name) throws IOException { return fsDir.fileExists(name); } public long fileModified(String name) throws IOException { return fsDir.fileModified(name); } public void touchFile(String name) throws IOException { fsDir.touchFile(name); } public void deleteFile(String name) throws IOException { fsDir.deleteFile(name); } public void renameFile(String name, String newName) throws IOException { fsDir.renameFile(name, newName); } public long fileLength(String name) throws IOException { return fsDir.fileLength(name); } public IndexOutput createOutput(String name) throws IOException { return fsDir.createOutput(name); } public void close() throws IOException { fsDir.close(); } } private static class FaultyIndexInput extends BufferedIndexInput { IndexInput delegate; static boolean doFail; int count; private FaultyIndexInput(IndexInput delegate) { this.delegate = delegate; } private void simOutage() throws IOException { if (doFail && count++ % 2 == 1) { throw new IOException("Simulated network outage"); } } public void readInternal(byte[] b, int offset, int length) throws IOException { simOutage(); delegate.readBytes(b, offset, length); } public void seekInternal(long pos) throws IOException { //simOutage(); delegate.seek(pos); } public long length() { return delegate.length(); } public void close() throws IOException { delegate.close(); } } // LUCENE-1262 public void testExceptions() throws Throwable { String tempDir = System.getProperty("java.io.tmpdir"); if (tempDir == null) throw new IOException("java.io.tmpdir undefined, cannot run test"); File indexDir = new File(tempDir, "testfieldswriterexceptions"); try { Directory dir = new FaultyFSDirectory(indexDir); IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true); for(int i=0;i<2;i++) writer.addDocument(testDoc); writer.optimize(); writer.close(); IndexReader reader = IndexReader.open(dir); FaultyIndexInput.doFail = true; boolean exc = false; for(int i=0;i<2;i++) { try { reader.document(i); } catch (IOException ioe) { // expected exc = true; } try { reader.document(i); } catch (IOException ioe) { // expected exc = true; } } assertTrue(exc); reader.close(); dir.close(); } finally { _TestUtil.rmDir(indexDir); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -