View Javadoc

1   package org.apache.lucene.index;
2   
3   import org.apache.lucene.store.*;
4   
5   import java.io.IOException;
6   import java.util.Iterator;
7   import java.util.List;
8   import java.util.Vector;
9   
10  /**
11   * @deprecated since 3.2 because the deleted document mechanism has changed in Lucene 2.2 and later
12   * @see com.atlassian.bonnie.ILuceneConnection#truncateIndex()
13   */
14  public class IndexUtils
15  {
16      /**
17       * Truncates an index. Instead of deleting all documents in an index via an IndexReader,
18       * All available segments are marked as deleted by their files to the "deletable" file.
19       * This has the benefit of faster index merges, whilst not offending any open readers.
20       * @param dir
21       * @throws IOException
22       * @deprecated since 3.2 because this method uses the internal deleted document structure of
23       * Lucene which has changed in recent versions
24       * @see com.atlassian.bonnie.ILuceneConnection#truncateIndex()
25       */
26      public static void truncateIndex(Directory dir) throws IOException
27      {
28          SegmentInfos segmentInfos = new SegmentInfos();
29          segmentInfos.read(dir);
30          Vector deletable = readDeleteableFiles(dir);
31          String[] indexfiles = dir.list();
32          for (Iterator it = segmentInfos.iterator(); it.hasNext();)
33          {
34              SegmentInfo si = (SegmentInfo) it.next();
35              for (int i = 0; i < indexfiles.length; ++i)
36              {
37                  if (indexfiles[i].startsWith(si.name)) deletable.add(indexfiles[i]);
38              }
39          }
40          writeDeleteableFiles(dir, deletable);
41          segmentInfos.clear();
42          segmentInfos.write(dir);
43      }
44  
45  
46      static Vector readDeleteableFiles(Directory directory) throws IOException
47      {
48          Vector result = new Vector();
49          if (!directory.fileExists("deletable"))
50              return result;
51  
52          IndexInput input = directory.openInput("deletable");
53          try
54          {
55              for (int i = input.readInt(); i > 0; i--)      // read file names
56                  result.addElement(input.readString());
57          }
58          finally
59          {
60              input.close();
61          }
62          return result;
63      }
64  
65      private static void writeDeleteableFiles(Directory directory, List files) throws IOException
66      {
67          IndexOutput output = directory.createOutput("deleteable.new");
68          try
69          {
70              output.writeInt(files.size());
71              for (Iterator it = files.iterator(); it.hasNext();)
72                  output.writeString((String) it.next());
73          }
74          finally
75          {
76              output.close();
77          }
78          directory.renameFile("deleteable.new", "deletable");
79      }
80  }