⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testindexwriter.java

📁 一套java版本的搜索引擎源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
              System.out.println("  count is " + result);            }            if (result == END_COUNT) {              break;            }          }          // Javadocs state that temp free Directory space          // required is at most 2X total input size of          // indices so let's make sure:          assertTrue("max free Directory space required exceeded 1X the total input index sizes during " + methodName +                     ": max temp usage = " + (dir.getMaxUsedSizeInBytes()-startDiskUsage) + " bytes; " +                     "starting disk usage = " + startDiskUsage + " bytes; " +                     "input index disk usage = " + inputDiskUsage + " bytes",                     (dir.getMaxUsedSizeInBytes()-startDiskUsage) < 2*(startDiskUsage + inputDiskUsage));          writer.close();          dir.close();          // Try again with 1000 more bytes of free space:          diskFree += 1000;        }      }      startDir.close();    }    /**     * Make sure optimize doesn't use any more than 1X     * starting index size as its temporary free space     * required.     */    public void testOptimizeTempSpaceUsage() throws IOException {          MockRAMDirectory dir = new MockRAMDirectory();      IndexWriter writer  = new IndexWriter(dir, new WhitespaceAnalyzer(), true);      for(int j=0;j<500;j++) {        addDocWithIndex(writer, j);      }      writer.close();      long startDiskUsage = 0;      String[] files = dir.list();      for(int i=0;i<files.length;i++) {        startDiskUsage += dir.fileLength(files[i]);      }      dir.resetMaxUsedSizeInBytes();      writer  = new IndexWriter(dir, new WhitespaceAnalyzer(), false);      writer.optimize();      writer.close();      long maxDiskUsage = dir.getMaxUsedSizeInBytes();      assertTrue("optimized used too much temporary space: starting usage was " + startDiskUsage + " bytes; max temp usage was " + maxDiskUsage + " but should have been " + (2*startDiskUsage) + " (= 2X starting usage)",                 maxDiskUsage <= 2*startDiskUsage);    }    private String arrayToString(String[] l) {      String s = "";      for(int i=0;i<l.length;i++) {        if (i > 0) {          s += "\n    ";        }        s += l[i];      }      return s;    }    // Make sure we can open an index for create even when a    // reader holds it open (this fails pre lock-less    // commits on windows):    public void testCreateWithReader() throws IOException {        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, "lucenetestindexwriter");        try {          Directory dir = FSDirectory.getDirectory(indexDir);          // add one document & close writer          IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);          addDoc(writer);          writer.close();          // now open reader:          IndexReader reader = IndexReader.open(dir);          assertEquals("should be one document", reader.numDocs(), 1);          // now open index for create:          writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);          assertEquals("should be zero documents", writer.docCount(), 0);          addDoc(writer);          writer.close();          assertEquals("should be one document", reader.numDocs(), 1);          IndexReader reader2 = IndexReader.open(dir);          assertEquals("should be one document", reader2.numDocs(), 1);          reader.close();          reader2.close();        } finally {          rmDir(indexDir);        }    }    // Same test as above, but use IndexWriter constructor    // that takes File:    public void testCreateWithReader2() throws IOException {        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, "lucenetestindexwriter");        try {          // add one document & close writer          IndexWriter writer = new IndexWriter(indexDir, new WhitespaceAnalyzer(), true);          addDoc(writer);          writer.close();          // now open reader:          IndexReader reader = IndexReader.open(indexDir);          assertEquals("should be one document", reader.numDocs(), 1);          // now open index for create:          writer = new IndexWriter(indexDir, new WhitespaceAnalyzer(), true);          assertEquals("should be zero documents", writer.docCount(), 0);          addDoc(writer);          writer.close();          assertEquals("should be one document", reader.numDocs(), 1);          IndexReader reader2 = IndexReader.open(indexDir);          assertEquals("should be one document", reader2.numDocs(), 1);          reader.close();          reader2.close();        } finally {          rmDir(indexDir);        }    }    // Same test as above, but use IndexWriter constructor    // that takes String:    public void testCreateWithReader3() throws IOException {        String tempDir = System.getProperty("tempDir");        if (tempDir == null)            throw new IOException("java.io.tmpdir undefined, cannot run test");        String dirName = tempDir + "/lucenetestindexwriter";        try {          // add one document & close writer          IndexWriter writer = new IndexWriter(dirName, new WhitespaceAnalyzer(), true);          addDoc(writer);          writer.close();          // now open reader:          IndexReader reader = IndexReader.open(dirName);          assertEquals("should be one document", reader.numDocs(), 1);          // now open index for create:          writer = new IndexWriter(dirName, new WhitespaceAnalyzer(), true);          assertEquals("should be zero documents", writer.docCount(), 0);          addDoc(writer);          writer.close();          assertEquals("should be one document", reader.numDocs(), 1);          IndexReader reader2 = IndexReader.open(dirName);          assertEquals("should be one document", reader2.numDocs(), 1);          reader.close();          reader2.close();        } finally {          rmDir(new File(dirName));        }    }    // Simulate a writer that crashed while writing segments    // file: make sure we can still open the index (ie,    // gracefully fallback to the previous segments file),    // and that we can add to the index:    public void testSimulatedCrashedWriter() throws IOException {        Directory dir = new RAMDirectory();        IndexWriter writer = null;        writer  = new IndexWriter(dir, new WhitespaceAnalyzer(), true);        // add 100 documents        for (int i = 0; i < 100; i++) {            addDoc(writer);        }        // close        writer.close();        long gen = SegmentInfos.getCurrentSegmentGeneration(dir);        assertTrue("segment generation should be > 1 but got " + gen, gen > 1);        // Make the next segments file, with last byte        // missing, to simulate a writer that crashed while        // writing segments file:        String fileNameIn = SegmentInfos.getCurrentSegmentFileName(dir);        String fileNameOut = IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS,                                                                   "",                                                                   1+gen);        IndexInput in = dir.openInput(fileNameIn);        IndexOutput out = dir.createOutput(fileNameOut);        long length = in.length();        for(int i=0;i<length-1;i++) {          out.writeByte(in.readByte());        }        in.close();        out.close();        IndexReader reader = null;        try {          reader = IndexReader.open(dir);        } catch (Exception e) {          fail("reader failed to open on a crashed index");        }        reader.close();        try {          writer  = new IndexWriter(dir, new WhitespaceAnalyzer(), true);        } catch (Exception e) {          fail("writer failed to open on a crashed index");        }        // add 100 documents        for (int i = 0; i < 100; i++) {            addDoc(writer);        }        // close        writer.close();    }    // Simulate a corrupt index by removing last byte of    // latest segments file and make sure we get an    // IOException trying to open the index:    public void testSimulatedCorruptIndex1() throws IOException {        Directory dir = new RAMDirectory();        IndexWriter writer = null;        writer  = new IndexWriter(dir, new WhitespaceAnalyzer(), true);        // add 100 documents        for (int i = 0; i < 100; i++) {            addDoc(writer);        }        // close        writer.close();        long gen = SegmentInfos.getCurrentSegmentGeneration(dir);        assertTrue("segment generation should be > 1 but got " + gen, gen > 1);        String fileNameIn = SegmentInfos.getCurrentSegmentFileName(dir);        String fileNameOut = IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS,                                                                   "",                                                                   1+gen);        IndexInput in = dir.openInput(fileNameIn);        IndexOutput out = dir.createOutput(fileNameOut);        long length = in.length();        for(int i=0;i<length-1;i++) {          out.writeByte(in.readByte());        }        in.close();        out.close();        dir.deleteFile(fileNameIn);        IndexReader reader = null;        try {          reader = IndexReader.open(dir);          fail("reader did not hit IOException on opening a corrupt index");        } catch (Exception e) {        }        if (reader != null) {          reader.close();        }    }    // Simulate a corrupt index by removing one of the cfs    // files and make sure we get an IOException trying to    // open the index:    public void testSimulatedCorruptIndex2() throws IOException {        Directory dir = new RAMDirectory();        IndexWriter writer = null;        writer  = new IndexWriter(dir, new WhitespaceAnalyzer(), true);        // add 100 documents        for (int i = 0; i < 100; i++) {            addDoc(writer);        }        // close        writer.close();        long gen = SegmentInfos.getCurrentSegmentGeneration(dir);        assertTrue("segment generation should be > 1 but got " + gen, gen > 1);        String[] files = dir.list();        for(int i=0;i<files.length;i++) {          if (files[i].endsWith(".cfs")) {            dir.deleteFile(files[i]);            break;          }        }        IndexReader reader = null;        try {          reader = IndexReader.open(dir);          fail("reader did not hit IOException on opening a corrupt index");        } catch (Exception e) {        }        if (reader != null) {          reader.close();        }    }    // Make sure that a Directory implementation that does    // not use LockFactory at all (ie overrides makeLock and    // implements its own private locking) works OK.  This    // was raised on java-dev as loss of backwards    // compatibility.    public void testNullLockFactory() throws IOException {      final class MyRAMDirectory extends RAMDirectory {        private LockFactory myLockFactory;        MyRAMDirectory() {          lockFactory = null;          myLockFactory = new SingleInstanceLockFactory();        }        public Lock makeLock(String name) {          return myLockFactory.makeLock(name);        }      }            Directory dir = new MyRAMDirectory();      IndexWriter writer  = new IndexWriter(dir, new WhitespaceAnalyzer(), true);      for (int i = 0; i < 100; i++) {        addDoc(writer);      }      writer.close();      IndexReader reader = IndexReader.open(dir);      Term searchTerm = new Term("content", "aaa");              IndexSearcher searcher = new IndexSearcher(dir);      Hits hits = searcher.search(new TermQuery(searchTerm));      assertEquals("did not get right number of hits", 100, hits.length());      writer.close();      writer  = new IndexWriter(dir, new WhitespaceAnalyzer(), true);      writer.close();      dir.close();    }    private void rmDir(File dir) {        File[] files = dir.listFiles();        if (files != null) {          for (int i = 0; i < files.length; i++) {            files[i].delete();          }        }        dir.delete();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -