Source Code Cross Referenced for IndexModifier.java in  » Net » lucene-connector » org » apache » lucene » index » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » lucene connector » org.apache.lucene.index 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.lucene.index;
002:
003:        /**
004:         * Licensed to the Apache Software Foundation (ASF) under one or more
005:         * contributor license agreements.  See the NOTICE file distributed with
006:         * this work for additional information regarding copyright ownership.
007:         * The ASF licenses this file to You under the Apache License, Version 2.0
008:         * (the "License"); you may not use this file except in compliance with
009:         * the License.  You may obtain a copy of the License at
010:         *
011:         *     http://www.apache.org/licenses/LICENSE-2.0
012:         *
013:         * Unless required by applicable law or agreed to in writing, software
014:         * distributed under the License is distributed on an "AS IS" BASIS,
015:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016:         * See the License for the specific language governing permissions and
017:         * limitations under the License.
018:         */
019:
020:        import org.apache.lucene.analysis.Analyzer;
021:        import org.apache.lucene.document.Document;
022:        import org.apache.lucene.store.Directory;
023:        import org.apache.lucene.store.FSDirectory;
024:        import org.apache.lucene.store.LockObtainFailedException;
025:
026:        import java.io.File;
027:        import java.io.IOException;
028:        import java.io.PrintStream;
029:
030:        /**
031:         * <p>[Note that as of <b>2.1</b>, all but one of the
032:         * methods in this class are available via {@link
033:         * IndexWriter}.  The one method that is not available is
034:         * {@link #deleteDocument(int)}.]</p>
035:         *
036:         * A class to modify an index, i.e. to delete and add documents. This
037:         * class hides {@link IndexReader} and {@link IndexWriter} so that you
038:         * do not need to care about implementation details such as that adding
039:         * documents is done via IndexWriter and deletion is done via IndexReader.
040:         * 
041:         * <p>Note that you cannot create more than one <code>IndexModifier</code> object
042:         * on the same directory at the same time.
043:         * 
044:         * <p>Example usage:
045:         * 
046:         <!-- ======================================================== -->
047:         <!-- = Java Sourcecode to HTML automatically converted code = -->
048:         <!-- =   Java2Html Converter V4.1 2004 by Markus Gebhard  markus@jave.de   = -->
049:         <!-- =     Further information: http://www.java2html.de     = -->
050:         <div align="left" class="java">
051:         <table border="0" cellpadding="3" cellspacing="0" bgcolor="#ffffff">
052:         <tr>
053:         <!-- start source code -->
054:         <td nowrap="nowrap" valign="top" align="left">
055:         <code>
056:         <font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Analyzer&nbsp;analyzer&nbsp;=&nbsp;</font><font color="#7f0055"><b>new&nbsp;</b></font><font color="#000000">StandardAnalyzer</font><font color="#000000">()</font><font color="#000000">;</font><br/>
057:         <font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#3f7f5f">//&nbsp;create&nbsp;an&nbsp;index&nbsp;in&nbsp;/tmp/index,&nbsp;overwriting&nbsp;an&nbsp;existing&nbsp;one:</font><br/>
058:         <font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">IndexModifier&nbsp;indexModifier&nbsp;=&nbsp;</font><font color="#7f0055"><b>new&nbsp;</b></font><font color="#000000">IndexModifier</font><font color="#000000">(</font><font color="#2a00ff">&#34;/tmp/index&#34;</font><font color="#000000">,&nbsp;analyzer,&nbsp;</font><font color="#7f0055"><b>true</b></font><font color="#000000">)</font><font color="#000000">;</font><br/>
059:         <font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Document&nbsp;doc&nbsp;=&nbsp;</font><font color="#7f0055"><b>new&nbsp;</b></font><font color="#000000">Document</font><font color="#000000">()</font><font color="#000000">;</font><br/>
060:         <font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">doc.add</font><font color="#000000">(</font><font color="#7f0055"><b>new&nbsp;</b></font><font color="#000000">Field</font><font color="#000000">(</font><font color="#2a00ff">&#34;id&#34;</font><font color="#000000">,&nbsp;</font><font color="#2a00ff">&#34;1&#34;</font><font color="#000000">,&nbsp;Field.Store.YES,&nbsp;Field.Index.UN_TOKENIZED</font><font color="#000000">))</font><font color="#000000">;</font><br/>
061:         <font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">doc.add</font><font color="#000000">(</font><font color="#7f0055"><b>new&nbsp;</b></font><font color="#000000">Field</font><font color="#000000">(</font><font color="#2a00ff">&#34;body&#34;</font><font color="#000000">,&nbsp;</font><font color="#2a00ff">&#34;a&nbsp;simple&nbsp;test&#34;</font><font color="#000000">,&nbsp;Field.Store.YES,&nbsp;Field.Index.TOKENIZED</font><font color="#000000">))</font><font color="#000000">;</font><br/>
062:         <font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">indexModifier.addDocument</font><font color="#000000">(</font><font color="#000000">doc</font><font color="#000000">)</font><font color="#000000">;</font><br/>
063:         <font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><b>int&nbsp;</b></font><font color="#000000">deleted&nbsp;=&nbsp;indexModifier.delete</font><font color="#000000">(</font><font color="#7f0055"><b>new&nbsp;</b></font><font color="#000000">Term</font><font color="#000000">(</font><font color="#2a00ff">&#34;id&#34;</font><font color="#000000">,&nbsp;</font><font color="#2a00ff">&#34;1&#34;</font><font color="#000000">))</font><font color="#000000">;</font><br/>
064:         <font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#2a00ff">&#34;Deleted&nbsp;&#34;&nbsp;</font><font color="#000000">+&nbsp;deleted&nbsp;+&nbsp;</font><font color="#2a00ff">&#34;&nbsp;document&#34;</font><font color="#000000">)</font><font color="#000000">;</font><br/>
065:         <font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">indexModifier.flush</font><font color="#000000">()</font><font color="#000000">;</font><br/>
066:         <font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#000000">indexModifier.docCount</font><font color="#000000">()&nbsp;</font><font color="#000000">+&nbsp;</font><font color="#2a00ff">&#34;&nbsp;docs&nbsp;in&nbsp;index&#34;</font><font color="#000000">)</font><font color="#000000">;</font><br/>
067:         <font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">indexModifier.close</font><font color="#000000">()</font><font color="#000000">;</font></code>
068:
069:         </td>
070:         <!-- end source code -->
071:         </tr>
072:         </table>
073:         </div>
074:         <!-- =       END of automatically generated HTML code       = -->
075:         <!-- ======================================================== -->
076:         *
077:         * <p>Not all methods of IndexReader and IndexWriter are offered by this
078:         * class. If you need access to additional methods, either use those classes
079:         * directly or implement your own class that extends <code>IndexModifier</code>.
080:         *
081:         * <p>Although an instance of this class can be used from more than one
082:         * thread, you will not get the best performance. You might want to use
083:         * IndexReader and IndexWriter directly for that (but you will need to
084:         * care about synchronization yourself then).
085:         *
086:         * <p>While you can freely mix calls to add() and delete() using this class,
087:         * you should batch you calls for best performance. For example, if you
088:         * want to update 20 documents, you should first delete all those documents,
089:         * then add all the new documents.
090:         *
091:         * @author Daniel Naber
092:         * @deprecated Please use {@link IndexWriter} instead.
093:         */
094:        public class IndexModifier {
095:
096:            protected IndexWriter indexWriter = null;
097:            protected IndexReader indexReader = null;
098:
099:            protected Directory directory = null;
100:            protected Analyzer analyzer = null;
101:            protected boolean open = false;
102:
103:            // Lucene defaults:
104:            protected PrintStream infoStream = null;
105:            protected boolean useCompoundFile = true;
106:            protected int maxBufferedDocs = IndexWriter.DEFAULT_MAX_BUFFERED_DOCS;
107:            protected int maxFieldLength = IndexWriter.DEFAULT_MAX_FIELD_LENGTH;
108:            protected int mergeFactor = IndexWriter.DEFAULT_MERGE_FACTOR;
109:
110:            /**
111:             * Open an index with write access.
112:             *
113:             * @param directory the index directory
114:             * @param analyzer the analyzer to use for adding new documents
115:             * @param create <code>true</code> to create the index or overwrite the existing one;
116:             * 	<code>false</code> to append to the existing index
117:             * @throws CorruptIndexException if the index is corrupt
118:             * @throws LockObtainFailedException if another writer
119:             *  has this index open (<code>write.lock</code> could not
120:             *  be obtained)
121:             * @throws IOException if there is a low-level IO error
122:             */
123:            public IndexModifier(Directory directory, Analyzer analyzer,
124:                    boolean create) throws CorruptIndexException,
125:                    LockObtainFailedException, IOException {
126:                init(directory, analyzer, create);
127:            }
128:
129:            /**
130:             * Open an index with write access.
131:             *
132:             * @param dirName the index directory
133:             * @param analyzer the analyzer to use for adding new documents
134:             * @param create <code>true</code> to create the index or overwrite the existing one;
135:             * 	<code>false</code> to append to the existing index
136:             * @throws CorruptIndexException if the index is corrupt
137:             * @throws LockObtainFailedException if another writer
138:             *  has this index open (<code>write.lock</code> could not
139:             *  be obtained)
140:             * @throws IOException if there is a low-level IO error
141:             */
142:            public IndexModifier(String dirName, Analyzer analyzer,
143:                    boolean create) throws CorruptIndexException,
144:                    LockObtainFailedException, IOException {
145:                Directory dir = FSDirectory.getDirectory(dirName);
146:                init(dir, analyzer, create);
147:            }
148:
149:            /**
150:             * Open an index with write access.
151:             *
152:             * @param file the index directory
153:             * @param analyzer the analyzer to use for adding new documents
154:             * @param create <code>true</code> to create the index or overwrite the existing one;
155:             * 	<code>false</code> to append to the existing index
156:             * @throws CorruptIndexException if the index is corrupt
157:             * @throws LockObtainFailedException if another writer
158:             *  has this index open (<code>write.lock</code> could not
159:             *  be obtained)
160:             * @throws IOException if there is a low-level IO error
161:             */
162:            public IndexModifier(File file, Analyzer analyzer, boolean create)
163:                    throws CorruptIndexException, LockObtainFailedException,
164:                    IOException {
165:                Directory dir = FSDirectory.getDirectory(file);
166:                init(dir, analyzer, create);
167:            }
168:
169:            /**
170:             * Initialize an IndexWriter.
171:             * @throws CorruptIndexException if the index is corrupt
172:             * @throws LockObtainFailedException if another writer
173:             *  has this index open (<code>write.lock</code> could not
174:             *  be obtained)
175:             * @throws IOException if there is a low-level IO error
176:             */
177:            protected void init(Directory directory, Analyzer analyzer,
178:                    boolean create) throws CorruptIndexException,
179:                    LockObtainFailedException, IOException {
180:                this .directory = directory;
181:                synchronized (this .directory) {
182:                    this .analyzer = analyzer;
183:                    indexWriter = new IndexWriter(directory, analyzer, create);
184:                    open = true;
185:                }
186:            }
187:
188:            /**
189:             * Throw an IllegalStateException if the index is closed.
190:             * @throws IllegalStateException
191:             */
192:            protected void assureOpen() {
193:                if (!open) {
194:                    throw new IllegalStateException("Index is closed");
195:                }
196:            }
197:
198:            /**
199:             * Close the IndexReader and open an IndexWriter.
200:             * @throws CorruptIndexException if the index is corrupt
201:             * @throws LockObtainFailedException if another writer
202:             *  has this index open (<code>write.lock</code> could not
203:             *  be obtained)
204:             * @throws IOException if there is a low-level IO error
205:             */
206:            protected void createIndexWriter() throws CorruptIndexException,
207:                    LockObtainFailedException, IOException {
208:                if (indexWriter == null) {
209:                    if (indexReader != null) {
210:                        indexReader.close();
211:                        indexReader = null;
212:                    }
213:                    indexWriter = new IndexWriter(directory, analyzer, false);
214:                    // IndexModifier cannot use ConcurrentMergeScheduler
215:                    // because it synchronizes on the directory which can
216:                    // cause deadlock
217:                    indexWriter.setMergeScheduler(new SerialMergeScheduler());
218:                    indexWriter.setInfoStream(infoStream);
219:                    indexWriter.setUseCompoundFile(useCompoundFile);
220:                    if (maxBufferedDocs != IndexWriter.DISABLE_AUTO_FLUSH)
221:                        indexWriter.setMaxBufferedDocs(maxBufferedDocs);
222:                    indexWriter.setMaxFieldLength(maxFieldLength);
223:                    indexWriter.setMergeFactor(mergeFactor);
224:                }
225:            }
226:
227:            /**
228:             * Close the IndexWriter and open an IndexReader.
229:             * @throws CorruptIndexException if the index is corrupt
230:             * @throws IOException if there is a low-level IO error
231:             */
232:            protected void createIndexReader() throws CorruptIndexException,
233:                    IOException {
234:                if (indexReader == null) {
235:                    if (indexWriter != null) {
236:                        indexWriter.close();
237:                        indexWriter = null;
238:                    }
239:                    indexReader = IndexReader.open(directory);
240:                }
241:            }
242:
243:            /**
244:             * Make sure all changes are written to disk.
245:             * @throws CorruptIndexException if the index is corrupt
246:             * @throws LockObtainFailedException if another writer
247:             *  has this index open (<code>write.lock</code> could not
248:             *  be obtained)
249:             * @throws IOException if there is a low-level IO error
250:             */
251:            public void flush() throws CorruptIndexException,
252:                    LockObtainFailedException, IOException {
253:                synchronized (directory) {
254:                    assureOpen();
255:                    if (indexWriter != null) {
256:                        indexWriter.close();
257:                        indexWriter = null;
258:                        createIndexWriter();
259:                    } else {
260:                        indexReader.close();
261:                        indexReader = null;
262:                        createIndexReader();
263:                    }
264:                }
265:            }
266:
267:            /**
268:             * Adds a document to this index, using the provided analyzer instead of the
269:             * one specific in the constructor.  If the document contains more than
270:             * {@link #setMaxFieldLength(int)} terms for a given field, the remainder are
271:             * discarded.
272:             * @see IndexWriter#addDocument(Document, Analyzer)
273:             * @throws IllegalStateException if the index is closed
274:             * @throws CorruptIndexException if the index is corrupt
275:             * @throws LockObtainFailedException if another writer
276:             *  has this index open (<code>write.lock</code> could not
277:             *  be obtained)
278:             * @throws IOException if there is a low-level IO error
279:             */
280:            public void addDocument(Document doc, Analyzer docAnalyzer)
281:                    throws CorruptIndexException, LockObtainFailedException,
282:                    IOException {
283:                synchronized (directory) {
284:                    assureOpen();
285:                    createIndexWriter();
286:                    if (docAnalyzer != null)
287:                        indexWriter.addDocument(doc, docAnalyzer);
288:                    else
289:                        indexWriter.addDocument(doc);
290:                }
291:            }
292:
293:            /**
294:             * Adds a document to this index.  If the document contains more than
295:             * {@link #setMaxFieldLength(int)} terms for a given field, the remainder are
296:             * discarded.
297:             * @see IndexWriter#addDocument(Document)
298:             * @throws IllegalStateException if the index is closed
299:             * @throws CorruptIndexException if the index is corrupt
300:             * @throws LockObtainFailedException if another writer
301:             *  has this index open (<code>write.lock</code> could not
302:             *  be obtained)
303:             * @throws IOException if there is a low-level IO error
304:             */
305:            public void addDocument(Document doc) throws CorruptIndexException,
306:                    LockObtainFailedException, IOException {
307:                addDocument(doc, null);
308:            }
309:
310:            /**
311:             * Deletes all documents containing <code>term</code>.
312:             * This is useful if one uses a document field to hold a unique ID string for
313:             * the document.  Then to delete such a document, one merely constructs a
314:             * term with the appropriate field and the unique ID string as its text and
315:             * passes it to this method.  Returns the number of documents deleted.
316:             * @return the number of documents deleted
317:             * @see IndexReader#deleteDocuments(Term)
318:             * @throws IllegalStateException if the index is closed
319:             * @throws StaleReaderException if the index has changed
320:             *  since this reader was opened
321:             * @throws CorruptIndexException if the index is corrupt
322:             * @throws LockObtainFailedException if another writer
323:             *  has this index open (<code>write.lock</code> could not
324:             *  be obtained)
325:             * @throws IOException if there is a low-level IO error
326:             */
327:            public int deleteDocuments(Term term) throws StaleReaderException,
328:                    CorruptIndexException, LockObtainFailedException,
329:                    IOException {
330:                synchronized (directory) {
331:                    assureOpen();
332:                    createIndexReader();
333:                    return indexReader.deleteDocuments(term);
334:                }
335:            }
336:
337:            /**
338:             * Deletes the document numbered <code>docNum</code>.
339:             * @see IndexReader#deleteDocument(int)
340:             * @throws StaleReaderException if the index has changed
341:             *  since this reader was opened
342:             * @throws CorruptIndexException if the index is corrupt
343:             * @throws LockObtainFailedException if another writer
344:             *  has this index open (<code>write.lock</code> could not
345:             *  be obtained)
346:             * @throws IllegalStateException if the index is closed
347:             */
348:            public void deleteDocument(int docNum) throws StaleReaderException,
349:                    CorruptIndexException, LockObtainFailedException,
350:                    IOException {
351:                synchronized (directory) {
352:                    assureOpen();
353:                    createIndexReader();
354:                    indexReader.deleteDocument(docNum);
355:                }
356:            }
357:
358:            /**
359:             * Returns the number of documents currently in this index.
360:             * @see IndexWriter#docCount()
361:             * @see IndexReader#numDocs()
362:             * @throws IllegalStateException if the index is closed
363:             */
364:            public int docCount() {
365:                synchronized (directory) {
366:                    assureOpen();
367:                    if (indexWriter != null) {
368:                        return indexWriter.docCount();
369:                    } else {
370:                        return indexReader.numDocs();
371:                    }
372:                }
373:            }
374:
375:            /**
376:             * Merges all segments together into a single segment, optimizing an index
377:             * for search.
378:             * @see IndexWriter#optimize()
379:             * @throws IllegalStateException if the index is closed
380:             * @throws CorruptIndexException if the index is corrupt
381:             * @throws LockObtainFailedException if another writer
382:             *  has this index open (<code>write.lock</code> could not
383:             *  be obtained)
384:             * @throws IOException if there is a low-level IO error
385:             */
386:            public void optimize() throws CorruptIndexException,
387:                    LockObtainFailedException, IOException {
388:                synchronized (directory) {
389:                    assureOpen();
390:                    createIndexWriter();
391:                    indexWriter.optimize();
392:                }
393:            }
394:
395:            /**
396:             * If non-null, information about merges and a message when
397:             * {@link #getMaxFieldLength()} is reached will be printed to this.
398:             * <p>Example: <tt>index.setInfoStream(System.err);</tt>
399:             * @see IndexWriter#setInfoStream(PrintStream)
400:             * @throws IllegalStateException if the index is closed
401:             */
402:            public void setInfoStream(PrintStream infoStream) {
403:                synchronized (directory) {
404:                    assureOpen();
405:                    if (indexWriter != null) {
406:                        indexWriter.setInfoStream(infoStream);
407:                    }
408:                    this .infoStream = infoStream;
409:                }
410:            }
411:
412:            /**
413:             * @see IndexModifier#setInfoStream(PrintStream)
414:             * @throws CorruptIndexException if the index is corrupt
415:             * @throws LockObtainFailedException if another writer
416:             *  has this index open (<code>write.lock</code> could not
417:             *  be obtained)
418:             * @throws IOException if there is a low-level IO error
419:             */
420:            public PrintStream getInfoStream() throws CorruptIndexException,
421:                    LockObtainFailedException, IOException {
422:                synchronized (directory) {
423:                    assureOpen();
424:                    createIndexWriter();
425:                    return indexWriter.getInfoStream();
426:                }
427:            }
428:
429:            /**
430:             * Setting to turn on usage of a compound file. When on, multiple files
431:             * for each segment are merged into a single file once the segment creation
432:             * is finished. This is done regardless of what directory is in use.
433:             * @see IndexWriter#setUseCompoundFile(boolean)
434:             * @throws IllegalStateException if the index is closed
435:             */
436:            public void setUseCompoundFile(boolean useCompoundFile) {
437:                synchronized (directory) {
438:                    assureOpen();
439:                    if (indexWriter != null) {
440:                        indexWriter.setUseCompoundFile(useCompoundFile);
441:                    }
442:                    this .useCompoundFile = useCompoundFile;
443:                }
444:            }
445:
446:            /**
447:             * @see IndexModifier#setUseCompoundFile(boolean)
448:             * @throws CorruptIndexException if the index is corrupt
449:             * @throws LockObtainFailedException if another writer
450:             *  has this index open (<code>write.lock</code> could not
451:             *  be obtained)
452:             * @throws IOException if there is a low-level IO error
453:             */
454:            public boolean getUseCompoundFile() throws CorruptIndexException,
455:                    LockObtainFailedException, IOException {
456:                synchronized (directory) {
457:                    assureOpen();
458:                    createIndexWriter();
459:                    return indexWriter.getUseCompoundFile();
460:                }
461:            }
462:
463:            /**
464:             * The maximum number of terms that will be indexed for a single field in a
465:             * document.  This limits the amount of memory required for indexing, so that
466:             * collections with very large files will not crash the indexing process by
467:             * running out of memory.<p/>
468:             * Note that this effectively truncates large documents, excluding from the
469:             * index terms that occur further in the document.  If you know your source
470:             * documents are large, be sure to set this value high enough to accomodate
471:             * the expected size.  If you set it to Integer.MAX_VALUE, then the only limit
472:             * is your memory, but you should anticipate an OutOfMemoryError.<p/>
473:             * By default, no more than 10,000 terms will be indexed for a field.
474:             * @see IndexWriter#setMaxFieldLength(int)
475:             * @throws IllegalStateException if the index is closed
476:             */
477:            public void setMaxFieldLength(int maxFieldLength) {
478:                synchronized (directory) {
479:                    assureOpen();
480:                    if (indexWriter != null) {
481:                        indexWriter.setMaxFieldLength(maxFieldLength);
482:                    }
483:                    this .maxFieldLength = maxFieldLength;
484:                }
485:            }
486:
487:            /**
488:             * @see IndexModifier#setMaxFieldLength(int)
489:             * @throws CorruptIndexException if the index is corrupt
490:             * @throws LockObtainFailedException if another writer
491:             *  has this index open (<code>write.lock</code> could not
492:             *  be obtained)
493:             * @throws IOException if there is a low-level IO error
494:             */
495:            public int getMaxFieldLength() throws CorruptIndexException,
496:                    LockObtainFailedException, IOException {
497:                synchronized (directory) {
498:                    assureOpen();
499:                    createIndexWriter();
500:                    return indexWriter.getMaxFieldLength();
501:                }
502:            }
503:
504:            /**
505:             * Determines the minimal number of documents required before the buffered
506:             * in-memory documents are merging and a new Segment is created.
507:             * Since Documents are merged in a {@link org.apache.lucene.store.RAMDirectory},
508:             * large value gives faster indexing.  At the same time, mergeFactor limits
509:             * the number of files open in a FSDirectory.
510:             *
511:             * <p>The default value is 10.
512:             *
513:             * @see IndexWriter#setMaxBufferedDocs(int)
514:             * @throws IllegalStateException if the index is closed
515:             * @throws IllegalArgumentException if maxBufferedDocs is smaller than 2
516:             */
517:            public void setMaxBufferedDocs(int maxBufferedDocs) {
518:                synchronized (directory) {
519:                    assureOpen();
520:                    if (indexWriter != null) {
521:                        indexWriter.setMaxBufferedDocs(maxBufferedDocs);
522:                    }
523:                    this .maxBufferedDocs = maxBufferedDocs;
524:                }
525:            }
526:
527:            /**
528:             * @see IndexModifier#setMaxBufferedDocs(int)
529:             * @throws CorruptIndexException if the index is corrupt
530:             * @throws LockObtainFailedException if another writer
531:             *  has this index open (<code>write.lock</code> could not
532:             *  be obtained)
533:             * @throws IOException if there is a low-level IO error
534:             */
535:            public int getMaxBufferedDocs() throws CorruptIndexException,
536:                    LockObtainFailedException, IOException {
537:                synchronized (directory) {
538:                    assureOpen();
539:                    createIndexWriter();
540:                    return indexWriter.getMaxBufferedDocs();
541:                }
542:            }
543:
544:            /**
545:             * Determines how often segment indices are merged by addDocument().  With
546:             * smaller values, less RAM is used while indexing, and searches on
547:             * unoptimized indices are faster, but indexing speed is slower.  With larger
548:             * values, more RAM is used during indexing, and while searches on unoptimized
549:             * indices are slower, indexing is faster.  Thus larger values (&gt; 10) are best
550:             * for batch index creation, and smaller values (&lt; 10) for indices that are
551:             * interactively maintained.
552:             * <p>This must never be less than 2.  The default value is 10.
553:             *
554:             * @see IndexWriter#setMergeFactor(int)
555:             * @throws IllegalStateException if the index is closed
556:             */
557:            public void setMergeFactor(int mergeFactor) {
558:                synchronized (directory) {
559:                    assureOpen();
560:                    if (indexWriter != null) {
561:                        indexWriter.setMergeFactor(mergeFactor);
562:                    }
563:                    this .mergeFactor = mergeFactor;
564:                }
565:            }
566:
567:            /**
568:             * @see IndexModifier#setMergeFactor(int)
569:             * @throws CorruptIndexException if the index is corrupt
570:             * @throws LockObtainFailedException if another writer
571:             *  has this index open (<code>write.lock</code> could not
572:             *  be obtained)
573:             * @throws IOException if there is a low-level IO error
574:             */
575:            public int getMergeFactor() throws CorruptIndexException,
576:                    LockObtainFailedException, IOException {
577:                synchronized (directory) {
578:                    assureOpen();
579:                    createIndexWriter();
580:                    return indexWriter.getMergeFactor();
581:                }
582:            }
583:
584:            /**
585:             * Close this index, writing all pending changes to disk.
586:             *
587:             * @throws IllegalStateException if the index has been closed before already
588:             * @throws CorruptIndexException if the index is corrupt
589:             * @throws IOException if there is a low-level IO error
590:             */
591:            public void close() throws CorruptIndexException, IOException {
592:                synchronized (directory) {
593:                    if (!open)
594:                        throw new IllegalStateException(
595:                                "Index is closed already");
596:                    if (indexWriter != null) {
597:                        indexWriter.close();
598:                        indexWriter = null;
599:                    } else {
600:                        indexReader.close();
601:                        indexReader = null;
602:                    }
603:                    open = false;
604:                }
605:            }
606:
607:            public String toString() {
608:                return "Index@" + directory;
609:            }
610:
611:            /*
612:            // used as an example in the javadoc:
613:            public static void main(String[] args) throws IOException {
614:              Analyzer analyzer = new StandardAnalyzer();
615:              // create an index in /tmp/index, overwriting an existing one:
616:              IndexModifier indexModifier = new IndexModifier("/tmp/index", analyzer, true);
617:              Document doc = new Document();
618:              doc.add(new Fieldable("id", "1", Fieldable.Store.YES, Fieldable.Index.UN_TOKENIZED));
619:              doc.add(new Fieldable("body", "a simple test", Fieldable.Store.YES, Fieldable.Index.TOKENIZED));
620:              indexModifier.addDocument(doc);
621:              int deleted = indexModifier.delete(new Term("id", "1"));
622:              System.out.println("Deleted " + deleted + " document");
623:              indexModifier.flush();
624:              System.out.println(indexModifier.docCount() + " docs in index");
625:              indexModifier.close();
626:            }*/
627:
628:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.