You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by petite_abeille <pe...@mac.com> on 2002/04/28 10:15:35 UTC
FileNotFoundException: code example
Hello again,
attached is the source code of the only class interacting directly with
Lucene in my app. Sorry for not providing a complete test case as it's
hard for me to come up with something self contained. Maybe there is
something that's obviously wrong in what I'm doing.
Thanks for any help.
PA
Re: Homogeneous vs Heterogeneous indexes (was: FileNotFoundException)
Posted by petite_abeille <pe...@mac.com>.
On Tuesday, April 30, 2002, at 01:57 AM, Steven J. Owens wrote:
> Just be glad you aren't doing this on Solaris with JDK 1.1.6
I know... In fact I'm looking forward to port my stuff to 1.4... As my
app is very much IO bond I'm really excited by this nio madness... :-)
> Yes and no. Setting ulimit to a reasonable number of open files is not
> only not a patch, it's the "right" way to do it.
Of course... Nothing is really black or white... What I wanted to say is
that -as a first strike- *I* prefer not to mess around with system
parameters.
> I understand where you're coming from, really, and in a certain way,
> it makes sense
Thanks. I already feel less alone... ;-)
> BUT... sometimes the impulse for clean, good design takes you too far
> down a blind alley.
Sure. At the end of the day, everything is a tradeoff...
> Sometimes there is no elegant solution. Sometimes there is no "best"
> way, only one of a limited set of options with different tradeoffs.
Absolutely.
> Most serious applications have to have some sort of OS variable
> tweaking, you're just used to having it done invisibly and painlessly.
Agree. In fact that's my first desktop application for nearly a decade.
I usually work on large scale system. And let me tell you, it's a very
different pair of sleeves... ;-)
> You could figure out the "right" way to set the system configuration
> on install or launch.
One of my design "goal" is to try to avoid these sort of tweakings as
much as I can.
> You could look at the alternative techniques for indexing in Lucene
That's another one of those nasty tradeoffs... ;-) Memory is even more
precious than file descriptors in my situation... Specially with a jvm
that have this funky notion of constraining your memory usage...
> if there's anything you're doing wrong (perhaps opening files and not
> closing them, and leaving them for the garbage collector to eventually
> get around to closing?)
Sure. I went through all those sanity checks. Also, in my case, the
garbage collector is my friend as I'm using the java.lang.ref API
extensively.
> or if you have a pessimal usage pattern that exacerbates the situation.
Ummmm...?!? You lost me here... What's a "pessimal usage pattern"?
> if you can come up with a scheme to run Lucene indexing with modified
> code for keeping track of file resources.
Sure, there are many thing that one could do... However, I have to
balance how much time I want to invest into any one of those allays. One
thing I really like about Lucene is it very simple API and usage. So far
it has worked out pretty well for me as I'm using it pretty extensively.
And I seem to have found -at last- a good balance between the different
constrains I'm operating under.
> an anomalous situation (use on a client/desktop machine)
"Anomalous situation"?!?! Ummm... Lucene is just an API... Hopefully
it's not bundled with some "dogma" attached to it... However, I'm kind
of starting to wander about that considering some of the -very
defensive- responses I got to my postings... Oh, well... I will just go
back to my cave... :-(
> could configure lucene to be careful about how many files it keeps open
> at any given time.
That will be great! On a somewhat related note, I have decided to stick
with the com.lucene package for the time being.... I was pretty excited
when the rc stuff came out, but it just didn't work out for me. My
resources problem just went from bad to worse. And also, I have two
issues with the release candidate: locking and reference counting.
Locking. I don't have anything against locking per see. However, I
really don't like how it's implemented in the rc. Using files just do
not work for me. It creates too many problems when something goes wrong
(eg the app is killed without warning and I have to clean up all those
locks by myself). What about using sockets or something to rendez-vous
on an index? Or at a bare minimum, be able to disable the locking all
together. I understand that most people are using Lucene under a very
different setup that I do, but nevertheless it should not hurt to make
it configurable. Anyway, it does not work for older jvm as noted in the
source code. Last, but not least, I'm always get very scared when I see
some "platform" dependent code somewhere (eg "if version 1 then ") ;-)
Reference counting. Well, as noted in a comment in the source code, the
reference API is really the way to go... And trying to be backward
compatible to version 0.9 is somehow missing the forest for the tree...
Just my two cents in any case. And yes, I'm well aware that I can fix
all these issue by myself... And start to contribute to Lucene instead
of just ranting left and right... But also keep in mind that I'm just a
humble Lucene user. And there seem to be a very clear distinction
between "user" and "developer" in Lucene's world... ;-)
Thanks for your response in any case. I hope I didn't "offend" too many
people with my ramblings ;-)
PA.
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Homogeneous vs Heterogeneous indexes (was: FileNotFoundException)
Posted by "Steven J. Owens" <pu...@darksleep.com>.
petite,
On Mon, Apr 29, 2002 at 07:54:43PM +0200, petite_abeille wrote:
> As a final note, several people suggested to increase the number of file
> descriptors per process with something like "ulimit"...
Just be glad you aren't doing this on Solaris with JDK 1.1.6,
where I first ran into ulimit issues - back when I encountered this
problem, the solaris default ulimit setting was 24 files, and JDK
1.1.6 reported the problem as an "OutOfMemory" error! Looks like
things are improving :-).
> From what I learned today, I think it's a *bad* idea to have to
> change some system parameters just because your/my app is written in
> such a way that it may run out of some system resources. Your/my app
> has to fit in the system. Hacking "ulimit" and/or other system
> parameters is just a quick patch that will -at best- delay dealing
> with the real problem that's usually one of design.
Yes and no. Setting ulimit to a reasonable number of open files
is not only not a patch, it's the "right" way to do it. I understand
where you're coming from, really, and in a certain way, it makes
sense, BUT... sometimes the impulse for clean, good design takes you
too far down a blind alley. Sometimes there is no elegant solution.
Sometimes there is no "best" way, only one of a limited set of options
with different tradeoffs.
By definition, Lucene is an application that trades off up front
CPU (for indexing) and file resources (for storage) for request-time
speed. The OS's job is to manage resources, and open files are one of
those resources. That's the tradeoff here, and it's reasonable and
expected. Most serious applications have to have some sort of OS
variable tweaking, you're just used to having it done invisibly and
painlessly.
That said, since you're working on a client/desktop application,
not a server application, you need to think about ways to handle this:
You could figure out the "right" way to set the system
configuration on install or launch.
You could look at the alternative techniques for indexing in
Lucene, and see if any approaches there can help - for example, maybe
doing a lot of the more intense indexing work in a RAMDirectory, then
merging it into a normal file-based Directory.
You could look more closely at what your application is doing,
and see if there's anything you're doing wrong (perhaps opening files
and not closing them, and leaving them for the garbage collector to
eventually get around to closing?) or if you have a pessimal usage
pattern that exacerbates the situation.
You could take a closer look at the lucene indexing and file
management stuff, and see if you can come up with a scheme to run
Lucene indexing with modified code for keeping track of file
resources.
I'll bet Doug and the other developers would rather not add
open-file managmeent as a main, permanent part of lucene, since it
would add overhead to all uses of lucene just to deal with an
anomalous situation (use on a client/desktop machine). But they might
be interested in a way to offer it as an optional feature, where
people using lucene in a constrained environment could configure
lucene to be careful about how many files it keeps open at any given
time.
Steven J. Owens
puff@darksleep.com
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Homogeneous vs Heterogeneous indexes (was: FileNotFoundException)
Posted by Joshua O'Madadhain <jm...@ics.uci.edu>.
On Mon, 29 Apr 2002, petite_abeille wrote:
> As a final note, several people suggested to increase the number of
> file descriptors per process with something like "ulimit"... From what
> I learned today, I think it's a *bad* idea to have to change some
> system parameters just because your/my app is written in such a way
> that it may run out of some system resources. Your/my app has to fit
> in the system. Hacking "ulimit" and/or other system parameters is
> just a quick patch that will -at best- delay dealing with the real
> problem that's usually one of design.
If you have a suggestion for how Lucene could use fewer file descriptors
while still maintaining its performance, I'm sure that the developers
would be interested to hear it.
However, some programs do require more resources than others. If--as I
suspect is true in this case--this is a consequence of the complexity of
the task, then there's not much point in complaining about it.
Joshua
jmadden@ics.uci.edu...Obscurium Per Obscurius...www.ics.uci.edu/~jmadden
Joshua Madden: Information Scientist, Musician, Philosopher-At-Tall
It's that moment of dawning comprehension that I live for--Bill Watterson
My opinions are too rational and insightful to be those of any organization.
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Homogeneous vs Heterogeneous indexes (was: FileNotFoundException)
Posted by petite_abeille <pe...@mac.com>.
First of, thanks to Jagadesh Nandasamy who directed me to the right
direction.
It seems, that in my situation, more homogeneous indexes work better
than fewer heterogeneous indexes:
I have a dozen class that I'm indexing. They vary from two fields to
more than a dozen field per document (aka object). I went through
different indexing strategy with them (per class, per date, per root
class, ... ) to see how it goes. In any case, while trying to use my
stuff with rc4 I consolidated all my different class indexes into one
root class index to see if I could reduce my resources consumption. Less
indexes, less RandomAccessFile was the rational. Well, I was wrong. In
fact the exact opposite seems to hold true: more -homogeneous- indexes
use overall less RandomAccessFile than less -heterogeneous- indexes...
One of those -not so obvious- thing you have to learn the hard way I
guess... ;-)
In any case, I would like to thanks again Jagadesh for his insight. Also
thanks to Pier Fumagalli for pointing out "LSOF". A very handy tool
indeed.
As a final note, several people suggested to increase the number of file
descriptors per process with something like "ulimit"... From what I
learned today, I think it's a *bad* idea to have to change some system
parameters just because your/my app is written in such a way that it may
run out of some system resources. Your/my app has to fit in the system.
Hacking "ulimit" and/or other system parameters is just a quick patch
that will -at best- delay dealing with the real problem that's usually
one of design.
Just my two cents.
PA.
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: FileNotFoundException: code example
Posted by Jagadesh Nandasamy <ja...@eself.com>.
Hi petite,
I will try to be brief...
In lucene the number of files created depends on the number of
fields the document has....
so lets take an example you want to index 100 files
if each file contains 10 fields
document.add(Field.Text(UNIQUE_ID, "12345678"))
...
...
...
document.add(new Field(UNIQUE_TYPE, "xxxxxxxxxxxxx", true,
true, false));
document.add(new Field(PATH, "c:\xxx\yyy\zzz.doc", true,
true, false));
if in all the 100 documents, if all the 10 fields created have
their field's key or name
(ie UNIQUE_ID, UNIQUE_TYPE, PATH)the same then the number of
files created by lucene
remains under control. (MIND YOU the values of the fields can be
different)
Say for the above scenerio if the number of index files created
are about 80(for 100 documents
with 10 field's each), If you add another million documents with
same 10 fields the number
of index files would remain the same it would not create any
more _f12 , _xxx files.
In contrast say for the same number of documents if you create
10 fields that are different
for different documents like for the first document if you
create a field like
document.add(new Field("Doc_PATH_1", "c:\xxx\yyy\zzz.doc",
true, true, false));
and
document.add(new Field("Doc_PATH_2", "c:\xxx\yyy\zzz.doc",
true, true, false));
for second document.
I think for each new field that is created about 3 files are
created in index directory
so you would end up having 1000's of files in index directory
which would cause the
"Too many files opened problem".
And i think you dont have to be bothered about which OS you are
using.
Hope this helps...
-Jaggi
petite_abeille wrote:
> Hello again,
>
> attached is the source code of the only class interacting directly
> with Lucene in my app. Sorry for not providing a complete test case as
> it's hard for me to come up with something self contained. Maybe there
> is something that's obviously wrong in what I'm doing.
>
> Thanks for any help.
>
> PA
>
>
>------------------------------------------------------------------------
>
>//
>// ===========================================================================
>//
>// Title: SZIndex.java
>// Description: [Description]
>// Author: Raphael Szwarc <ra...@hotmail.com>
>// Creation Date: Wed Sep 12 2001
>// Legal: Copyright (C) 2001 Raphael Szwarc. All Rights Reserved.
>//
>// ---------------------------------------------------------------------------
>//
>
>package alt.dev.szobject;
>
>import com.lucene.store.Directory;
>import com.lucene.store.FSDirectory;
>import com.lucene.store.RAMDirectory;
>import com.lucene.document.Field;
>import com.lucene.document.DateField;
>import com.lucene.document.Document;
>import com.lucene.analysis.Analyzer;
>import com.lucene.analysis.standard.StandardAnalyzer;
>import com.lucene.index.IndexWriter;
>import com.lucene.index.IndexReader;
>import com.lucene.index.Term;
>import com.lucene.search.IndexSearcher;
>import com.lucene.search.MultiSearcher;
>import com.lucene.search.Searcher;
>import com.lucene.search.Query;
>import com.lucene.search.Hits;
>
>import java.io.FilenameFilter;
>import java.io.File;
>import java.io.IOException;
>
>import java.util.Map;
>import java.util.Collection;
>import java.util.Date;
>import java.util.Iterator;
>
>import alt.dev.szfoundation.SZHexCoder;
>import alt.dev.szfoundation.SZDate;
>import alt.dev.szfoundation.SZSystem;
>import alt.dev.szfoundation.SZLog;
>
>final class SZIndex extends Object
>{
>
>// ===========================================================================
>// Constant(s)
>// ---------------------------------------------------------------------------
>
> private static final String Extension = ".index";
>
>// ===========================================================================
>// Class variable(s)
>// ---------------------------------------------------------------------------
>
> private static final Filter _filter = new Filter();
>
>// ===========================================================================
>// Instance variable(s)
>// ---------------------------------------------------------------------------
>
> private String _path = null;
> private transient File _directory = null;
> private transient Directory _indexDirectory = null;
> private transient IndexWriter _writer = null;
>
> private transient IndexReader _reader = null;
> private transient Searcher _searcher = null;
>
> private transient Directory _ramDirectory = null;
> private transient IndexWriter _ramWriter = null;
> private transient int _counter = 0;
>
>// ===========================================================================
>// Constructor method(s)
>// ---------------------------------------------------------------------------
>
> private SZIndex()
> {
> super();
> }
>
>// ===========================================================================
>// Class method(s)
>// ---------------------------------------------------------------------------
>
> static FilenameFilter filter()
> {
> return _filter;
> }
>
> static String stringByDeletingPathExtension(String aPath)
> {
> if ( aPath != null )
> {
> int anIndex = aPath.lastIndexOf( SZIndex.Extension );
>
> if ( anIndex > 0 )
> {
> aPath = aPath.substring( 0, anIndex );
> }
>
> return aPath;
> }
>
> throw new IllegalArgumentException( "SZIndex.stringByDeletingPathExtension: null path." );
> }
>
> static SZIndex indexWithNameInDirectory(String aName, File aDirectory)
> {
> if ( aName != null )
> {
> if ( aDirectory != null )
> {
> String anEncodedName = SZHexCoder.encode( aName.getBytes() );
> //String aPath = aDirectory.getPath() + File.separator + anEncodedName + SZIndex.Extension + File.separator;
> String aPath = aDirectory.getPath() + File.separator + aName + SZIndex.Extension + File.separator;
> SZIndex anIndex = new SZIndex();
>
> anIndex.setPath( aPath );
>
> return anIndex;
> }
>
> throw new IllegalArgumentException( "SZIndex.indexWithNameInDirectory: null directory." );
> }
>
> throw new IllegalArgumentException( "SZIndex.indexWithNameInDirectory: null name." );
> }
>
> static String stringForValue(Object aValue )
> {
> if ( aValue != null )
> {
> String aStringValue = null;
>
> if ( ( aValue instanceof SZDate ) == true )
> {
> aValue = ( (SZDate) aValue ).internalDate();
> }
> else
> if ( ( aValue instanceof SZPersistent ) == true )
> {
> aValue = ( (SZPersistent) aValue ).id();
> }
>
> if ( ( aValue instanceof Date ) == true )
> {
> aStringValue = DateField.dateToString( (Date) aValue );
> }
> else
> if ( ( aValue instanceof SZID ) == true )
> {
> aStringValue = ( (SZID) aValue ).uuidString();
> }
> else
> {
> aStringValue = aValue.toString();
> }
>
> return aStringValue;
> }
>
> throw new IllegalArgumentException( "SZIndex.stringForValue: null value." );
> }
>
>// ===========================================================================
>// Instance method(s)
>// ---------------------------------------------------------------------------
>
> private String path()
> {
> return _path;
> }
>
> private void setPath(String aValue)
> {
> _path = aValue;
> }
>
> private File directory()
> {
> if ( _directory == null )
> {
> String aPath = this.path();
>
> if ( aPath != null )
> {
> _directory = new File( aPath );
>
> if ( _directory.exists() == false )
> {
> _directory.mkdirs();
> }
> }
> else
> {
> throw new IllegalStateException( "SZIndex.directory: null path." );
> }
> }
>
> return _directory;
> }
>
> private boolean shouldCreate()
> {
> File aDirectory = this.directory();
> String[] aList = aDirectory.list();
>
> if ( ( aList == null ) || ( aList.length == 0 ) )
> {
> return true;
> }
>
> return false;
> }
>
> boolean exists()
> {
> File aFile = this.directory();
>
> if ( aFile != null )
> {
> return aFile.exists();
> }
>
> return false;
> }
>
> private SZDate lastModifiedDate()
> {
> if ( this.exists() == true )
> {
> File aDirectory = this.directory();
> Date aDate = new Date( aDirectory.lastModified() );
> SZDate aCalendarDate = SZDate.dateWithDate( aDate );
>
> return aCalendarDate;
> }
>
> return null;
> }
>
> public int hashCode()
> {
> return this.path().hashCode();
> }
>
> public boolean equals(Object anObject)
> {
> if ( this == anObject )
> {
> return true;
> }
>
> return this.path().equals( ( (SZIndex) anObject ).path() );
> }
>
> protected void finalize() throws Throwable
> {
> if ( _writer != null )
> {
> this.optimize();
> }
>
> super.finalize();
> }
>
>// ===========================================================================
>// Index method(s)
>// ---------------------------------------------------------------------------
>
> synchronized void optimize()
> {
> try
> {
> this.flush();
>
> if ( _writer != null )
> {
> _writer.optimize();
> _writer.close();
>
> }
>
> _writer = null;
> _indexDirectory = null;
> }
> catch(Exception anException)
> {
> anException.printStackTrace();
>
> SZLog.warning( anException );
>
> _writer = null;
> _indexDirectory = null;
>
> SZSystem.gc();
> }
> }
>
>
> private Directory indexDirectory() throws IOException
> {
> if ( _indexDirectory == null )
> {
> File aFile = this.directory();
> boolean shouldCreate = this.shouldCreate();
>
> //_indexDirectory = FSDirectory.getDirectory( aFile, shouldCreate );
> _indexDirectory = new FSDirectory( aFile, shouldCreate );
> }
>
> return _indexDirectory;
> }
>
> private IndexWriter writer() throws IOException
> {
> if ( _writer == null )
> {
> Directory aDirectory = this.indexDirectory();
> Analyzer anAnalyzer = new StandardAnalyzer();
> boolean shouldCreate = this.shouldCreate();
>
> _writer = new IndexWriter( aDirectory, anAnalyzer, shouldCreate );
> _writer.mergeFactor = 2;
> }
>
> return _writer;
> }
>
> private IndexReader reader() throws IOException
> {
> if ( _reader == null )
> {
> System.gc();
>
> _reader = IndexReader.open( this.indexDirectory() );
> }
>
> return _reader;
> }
>
> private Searcher searcher() throws IOException
> {
> if ( _searcher == null )
> {
> System.gc();
>
> _searcher = new IndexSearcher( this.reader() );
> }
>
> if ( _ramDirectory != null )
> {
> Searcher aRamSearcher = new IndexSearcher( IndexReader.open( _ramDirectory ) );
>
> return new MultiSearcher( new Searcher[] { aRamSearcher, _searcher } );
> }
>
> return _searcher;
> }
>
>// ===========================================================================
>// RAM method(s)
>// ---------------------------------------------------------------------------
>
> private Directory ramDirectory() throws IOException
> {
> if ( _ramDirectory == null )
> {
> _ramDirectory = new RAMDirectory();
> }
>
> return _ramDirectory;
> }
>
> private IndexWriter ramWriter() throws IOException
> {
> if ( _ramWriter == null )
> {
> Directory aDirectory = this.ramDirectory();
> Analyzer anAnalyzer = new StandardAnalyzer();
>
> _ramWriter = new IndexWriter( aDirectory, anAnalyzer, true );
> }
>
> return _ramWriter;
> }
>
> private void flush() throws IOException
> {
> if ( ( _ramDirectory != null ) &&
> ( _ramDirectory.list() != null ) &&
> ( _ramDirectory.list().length > 0 ) &&
> ( _ramWriter != null ) )
> {
> _ramWriter.optimize();
> _ramWriter.close();
>
> this.writer().addIndexes( new Directory[] { _ramDirectory } );
>
> _ramWriter = null;
> _ramDirectory = null;
>
> _reader = null;
> _searcher = null;
> }
> }
>
>// ===========================================================================
>// Indexing method(s)
>// ---------------------------------------------------------------------------
>
> synchronized Hits search(Query aQuery) throws IOException
> {
> if ( aQuery != null )
> {
> if ( this.shouldCreate() == false )
> {
> return this.searcher().search( aQuery );
> }
>
> return null;
> }
>
> throw new IllegalArgumentException( "SZIndex.search: null query." );
> }
>
> synchronized void deleteIndexWithID(SZID anID) throws IOException
> {
> if ( anID != null )
> {
> if ( this.shouldCreate() == false )
> {
> String aValue = SZIndex.stringForValue( anID );
> Term aTerm = new Term( SZDescription.IDKey, aValue );
> IndexReader aReader = this.reader();
>
> aReader.delete( aTerm );
> }
>
> return;
> }
>
> throw new IllegalArgumentException( "SZIndex.deleteIndexWithID: null id." );
> }
>
> synchronized void indexValuesWithID(Map someValues, SZID anID) throws IOException
> {
> if ( someValues != null )
> {
> if ( anID != null )
> {
> Class aClass = anID.entity();
> SZDescription aDescription = SZDescription.descriptionForClass( aClass );
> Collection someUniqueKeys = aDescription.uniqueKeys();
> String anIdentifier = SZIndex.stringForValue( anID );
> Field anIdentifierField = Field.Keyword( SZDescription.IDKey, anIdentifier );
> String aClassName = anID.entity().getName();
> Field aClassField = Field.Keyword( SZDescription.ClassKey, aClassName );
> Document aDocument = new Document();
> IndexWriter aWriter = this.ramWriter();
>
> aDocument.add( anIdentifierField );
> aDocument.add( aClassField );
>
> for( Iterator anIterator = someValues.keySet().iterator(); anIterator.hasNext(); )
> {
> Object aKey = anIterator.next();
> Object aValue = someValues.get( aKey );
> String aKeyName = aKey.toString();
> String aStringValue = SZIndex.stringForValue( aValue );
> Field aField = null;
>
> if ( ( ( aValue instanceof SZPersistent ) == true ) ||
> ( ( someUniqueKeys != null ) && ( someUniqueKeys.contains( aKeyName ) == true ) ) )
> {
> aField = new Field( aKeyName, aStringValue, false, true, false) ;
> }
> else
> {
> aField = Field.UnStored( aKeyName, aStringValue );
> }
>
> aDocument.add( aField );
> }
>
> aWriter.addDocument( aDocument );
> aWriter.optimize();
>
> _counter += 1;
>
> if ( _counter > 100 )
> {
> this.flush();
> _counter = 0;
> }
>
> return;
> }
>
> throw new IllegalArgumentException( "SZIndex.indexValues: null id." );
> }
>
> throw new IllegalArgumentException( "SZIndex.indexValues: null values." );
> }
>
>// ===========================================================================
>// FilenameFilter method(s)
>// ---------------------------------------------------------------------------
>
> private static final class Filter extends Object implements FilenameFilter
> {
>
> private Filter()
> {
> super();
> }
>
> public boolean accept(File aDirectory, String aName)
> {
> if ( aName.endsWith( SZIndex.Extension ) == true )
> {
> File aFile = new File( aDirectory, aName );
>
> if ( aFile.isDirectory() == true )
> {
> return true;
> }
> }
>
> return false;
> }
> }
>
>}
>
>
>------------------------------------------------------------------------
>
>--
>To unsubscribe, e-mail: <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>
>
> SZIndex.java
>
> Content-Type:
>
> text/plain
> Content-Encoding:
>
> 7bit
>
>
> ------------------------------------------------------------------------
> Part 1.3
>
> Content-Type:
>
> text/plain
>
>
Re: FileNotFoundException: code example
Posted by Otis Gospodnetic <ot...@yahoo.com>.
Hello,
I'll put my comments inline...
--- petite_abeille <pe...@mac.com> wrote:
> Hello again,
>
> attached is the source code of the only class interacting directly
> with
> Lucene in my app. Sorry for not providing a complete test case as
> it's
> hard for me to come up with something self contained. Maybe there is
> something that's obviously wrong in what I'm doing.
>
> Thanks for any help.
>
> PA
>
> > //
> //
>
===========================================================================
> //
> // Title: SZIndex.java
> // Description: [Description]
> // Author: Raphael Szwarc <ra...@hotmail.com>
> // Creation Date: Wed Sep 12 2001
> // Legal: Copyright (C) 2001 Raphael Szwarc. All Rights Reserved.
> //
> //
>
---------------------------------------------------------------------------
> //
>
> package alt.dev.szobject;
>
> import com.lucene.store.Directory;
> import com.lucene.store.FSDirectory;
> import com.lucene.store.RAMDirectory;
> import com.lucene.document.Field;
> import com.lucene.document.DateField;
> import com.lucene.document.Document;
> import com.lucene.analysis.Analyzer;
> import com.lucene.analysis.standard.StandardAnalyzer;
> import com.lucene.index.IndexWriter;
> import com.lucene.index.IndexReader;
> import com.lucene.index.Term;
> import com.lucene.search.IndexSearcher;
> import com.lucene.search.MultiSearcher;
> import com.lucene.search.Searcher;
> import com.lucene.search.Query;
> import com.lucene.search.Hits;
>
> import java.io.FilenameFilter;
> import java.io.File;
> import java.io.IOException;
>
> import java.util.Map;
> import java.util.Collection;
> import java.util.Date;
> import java.util.Iterator;
>
> import alt.dev.szfoundation.SZHexCoder;
> import alt.dev.szfoundation.SZDate;
> import alt.dev.szfoundation.SZSystem;
> import alt.dev.szfoundation.SZLog;
>
> final class SZIndex extends Object
> {
>
> //
>
===========================================================================
> // Constant(s)
> //
>
---------------------------------------------------------------------------
>
> private static final String Extension = ".index";
>
> //
>
===========================================================================
> // Class variable(s)
> //
>
---------------------------------------------------------------------------
>
> private static final Filter _filter = new Filter();
>
> //
>
===========================================================================
> // Instance variable(s)
> //
>
---------------------------------------------------------------------------
>
> private String _path = null;
> private transient File _directory = null;
> private transient Directory _indexDirectory = null;
> private transient IndexWriter _writer = null;
>
> private transient IndexReader _reader = null;
> private transient Searcher _searcher = null;
>
> private transient Directory _ramDirectory = null;
> private transient IndexWriter _ramWriter = null;
> private transient int _counter = 0;
>
> //
>
===========================================================================
> // Constructor method(s)
> //
>
---------------------------------------------------------------------------
>
> private SZIndex()
> {
> super();
> }
>
> //
>
===========================================================================
> // Class method(s)
> //
>
---------------------------------------------------------------------------
>
> static FilenameFilter filter()
> {
> return _filter;
> }
>
> static String stringByDeletingPathExtension(String aPath)
> {
> if ( aPath != null )
> {
> int anIndex = aPath.lastIndexOf( SZIndex.Extension );
>
> if ( anIndex > 0 )
> {
> aPath = aPath.substring( 0, anIndex );
> }
>
> return aPath;
> }
>
> throw new IllegalArgumentException(
> "SZIndex.stringByDeletingPathExtension: null path." );
> }
>
> static SZIndex indexWithNameInDirectory(String aName, File
> aDirectory)
> {
> if ( aName != null )
> {
> if ( aDirectory != null )
> {
> String anEncodedName = SZHexCoder.encode( aName.getBytes() );
> //String aPath = aDirectory.getPath() + File.separator +
> anEncodedName + SZIndex.Extension + File.separator;
> String aPath = aDirectory.getPath() + File.separator + aName +
> SZIndex.Extension + File.separator;
> SZIndex anIndex = new SZIndex();
>
> anIndex.setPath( aPath );
>
> return anIndex;
> }
>
> throw new IllegalArgumentException(
> "SZIndex.indexWithNameInDirectory: null directory." );
> }
>
> throw new IllegalArgumentException(
> "SZIndex.indexWithNameInDirectory: null name." );
> }
>
> static String stringForValue(Object aValue )
> {
> if ( aValue != null )
> {
> String aStringValue = null;
>
> if ( ( aValue instanceof SZDate ) == true )
> {
> aValue = ( (SZDate) aValue ).internalDate();
> }
> else
> if ( ( aValue instanceof SZPersistent ) == true )
> {
> aValue = ( (SZPersistent) aValue ).id();
> }
>
> if ( ( aValue instanceof Date ) == true )
> {
> aStringValue = DateField.dateToString( (Date) aValue );
> }
> else
> if ( ( aValue instanceof SZID ) == true )
> {
> aStringValue = ( (SZID) aValue ).uuidString();
> }
> else
> {
> aStringValue = aValue.toString();
> }
>
> return aStringValue;
> }
>
> throw new IllegalArgumentException( "SZIndex.stringForValue: null
> value." );
> }
>
> //
>
===========================================================================
> // Instance method(s)
> //
>
---------------------------------------------------------------------------
>
> private String path()
> {
> return _path;
> }
>
> private void setPath(String aValue)
> {
> _path = aValue;
> }
>
> private File directory()
> {
> if ( _directory == null )
> {
> String aPath = this.path();
>
> if ( aPath != null )
> {
> _directory = new File( aPath );
>
> if ( _directory.exists() == false )
> {
> _directory.mkdirs();
> }
> }
> else
> {
> throw new IllegalStateException( "SZIndex.directory: null path."
> );
> }
> }
>
> return _directory;
> }
>
> private boolean shouldCreate()
> {
> File aDirectory = this.directory();
> String[] aList = aDirectory.list();
>
> if ( ( aList == null ) || ( aList.length == 0 ) )
> {
> return true;
> }
>
> return false;
> }
>
> boolean exists()
OG: one may think this method checks for existence of an index, but it
only checks for existence of a directory. Perhaps directoryExists()
would be a better name.
> {
> File aFile = this.directory();
>
> if ( aFile != null )
> {
> return aFile.exists();
> }
>
> return false;
> }
>
> private SZDate lastModifiedDate()
> {
> if ( this.exists() == true )
> {
> File aDirectory = this.directory();
> Date aDate = new Date( aDirectory.lastModified() );
> SZDate aCalendarDate = SZDate.dateWithDate( aDate );
>
> return aCalendarDate;
> }
>
> return null;
> }
>
> public int hashCode()
> {
> return this.path().hashCode();
> }
>
> public boolean equals(Object anObject)
> {
> if ( this == anObject )
> {
> return true;
> }
>
> return this.path().equals( ( (SZIndex) anObject ).path() );
> }
>
> protected void finalize() throws Throwable
> {
> if ( _writer != null )
> {
> this.optimize();
OG: perhaps you want to close some stuff here, although I'm not sure
about doing that in finalize()...
> }
>
> super.finalize();
> }
>
> //
>
===========================================================================
> // Index method(s)
> //
>
---------------------------------------------------------------------------
>
> synchronized void optimize()
> {
> try
> {
> this.flush();
>
> if ( _writer != null )
> {
> _writer.optimize();
OG: optimize can throw IOException. In that case your close() will not
get executed. Maybe you can use a finally block.
> _writer.close();
>
> }
>
> _writer = null;
> _indexDirectory = null;
> }
> catch(Exception anException)
> {
> anException.printStackTrace();
>
> SZLog.warning( anException );
>
> _writer = null;
> _indexDirectory = null;
OG: duplicate assignments, suitable for finally block.
> SZSystem.gc();
> }
> }
>
>
> private Directory indexDirectory() throws IOException
> {
> if ( _indexDirectory == null )
> {
> File aFile = this.directory();
> boolean shouldCreate = this.shouldCreate();
>
> //_indexDirectory = FSDirectory.getDirectory( aFile, shouldCreate
> );
> _indexDirectory = new FSDirectory( aFile, shouldCreate );
> }
>
> return _indexDirectory;
> }
>
> private IndexWriter writer() throws IOException
> {
> if ( _writer == null )
> {
> Directory aDirectory = this.indexDirectory();
> Analyzer anAnalyzer = new StandardAnalyzer();
> boolean shouldCreate = this.shouldCreate();
>
> _writer = new IndexWriter( aDirectory, anAnalyzer, shouldCreate );
> _writer.mergeFactor = 2;
> }
>
> return _writer;
> }
>
> private IndexReader reader() throws IOException
> {
> if ( _reader == null )
> {
> System.gc();
>
> _reader = IndexReader.open( this.indexDirectory() );
> }
OG: you are opening an IndexReader, but I don't think I saw it being
closed anywhere.
> return _reader;
> }
>
> private Searcher searcher() throws IOException
> {
> if ( _searcher == null )
> {
> System.gc();
>
> _searcher = new IndexSearcher( this.reader() );
> }
>
> if ( _ramDirectory != null )
> {
> Searcher aRamSearcher = new IndexSearcher( IndexReader.open(
> _ramDirectory ) );
OG: another open...
> return new MultiSearcher( new Searcher[] { aRamSearcher, _searcher
> } );
> }
>
> return _searcher;
> }
>
> //
>
===========================================================================
> // RAM method(s)
> //
>
---------------------------------------------------------------------------
>
> private Directory ramDirectory() throws IOException
> {
> if ( _ramDirectory == null )
> {
> _ramDirectory = new RAMDirectory();
> }
>
> return _ramDirectory;
> }
>
> private IndexWriter ramWriter() throws IOException
> {
> if ( _ramWriter == null )
> {
> Directory aDirectory = this.ramDirectory();
> Analyzer anAnalyzer = new StandardAnalyzer();
>
> _ramWriter = new IndexWriter( aDirectory, anAnalyzer, true );
> }
>
> return _ramWriter;
> }
>
> private void flush() throws IOException
> {
> if ( ( _ramDirectory != null ) &&
> ( _ramDirectory.list() != null ) &&
> ( _ramDirectory.list().length > 0 ) &&
> ( _ramWriter != null ) )
> {
> _ramWriter.optimize();
> _ramWriter.close();
>
> this.writer().addIndexes( new Directory[] { _ramDirectory } );
>
> _ramWriter = null;
> _ramDirectory = null;
>
> _reader = null;
> _searcher = null;
OG: both IndexReader and IndexSearcher have a close() method. Have you
tried calling them here? Does it help? You can still assign nulls
later to help GC.
> }
> }
>
> //
>
===========================================================================
> // Indexing method(s)
> //
>
---------------------------------------------------------------------------
>
> synchronized Hits search(Query aQuery) throws IOException
> {
> if ( aQuery != null )
> {
> if ( this.shouldCreate() == false )
> {
> return this.searcher().search( aQuery );
> }
>
> return null;
> }
>
> throw new IllegalArgumentException( "SZIndex.search: null query."
> );
> }
>
> synchronized void deleteIndexWithID(SZID anID) throws IOException
> {
> if ( anID != null )
> {
> if ( this.shouldCreate() == false )
> {
> String aValue = SZIndex.stringForValue( anID );
> Term aTerm = new Term( SZDescription.IDKey, aValue );
> IndexReader aReader = this.reader();
>
> aReader.delete( aTerm );
> }
>
> return;
> }
>
> throw new IllegalArgumentException( "SZIndex.deleteIndexWithID:
> null id." );
> }
>
> synchronized void indexValuesWithID(Map someValues, SZID anID)
> throws IOException
> {
> if ( someValues != null )
> {
> if ( anID != null )
> {
> Class aClass = anID.entity();
> SZDescription aDescription = SZDescription.descriptionForClass(
> aClass );
> Collection someUniqueKeys = aDescription.uniqueKeys();
> String anIdentifier = SZIndex.stringForValue( anID );
> Field anIdentifierField = Field.Keyword( SZDescription.IDKey,
> anIdentifier );
> String aClassName = anID.entity().getName();
> Field aClassField = Field.Keyword( SZDescription.ClassKey,
> aClassName );
> Document aDocument = new Document();
> IndexWriter aWriter = this.ramWriter();
>
> aDocument.add( anIdentifierField );
> aDocument.add( aClassField );
>
> for( Iterator anIterator = someValues.keySet().iterator();
> anIterator.hasNext(); )
> {
> Object aKey = anIterator.next();
> Object aValue = someValues.get( aKey );
> String aKeyName = aKey.toString();
> String aStringValue = SZIndex.stringForValue( aValue );
> Field aField = null;
>
> if ( ( ( aValue instanceof SZPersistent ) == true ) ||
> ( ( someUniqueKeys != null ) && ( someUniqueKeys.contains(
> aKeyName ) == true ) ) )
> {
> aField = new Field( aKeyName, aStringValue, false, true, false)
> ;
> }
> else
> {
> aField = Field.UnStored( aKeyName, aStringValue );
> }
>
> aDocument.add( aField );
> }
>
> aWriter.addDocument( aDocument );
> aWriter.optimize();
>
> _counter += 1;
>
> if ( _counter > 100 )
> {
> this.flush();
> _counter = 0;
> }
>
> return;
> }
>
> throw new IllegalArgumentException( "SZIndex.indexValues: null
> id." );
> }
>
> throw new IllegalArgumentException( "SZIndex.indexValues: null
> values." );
> }
>
> //
>
===========================================================================
> // FilenameFilter method(s)
> //
>
---------------------------------------------------------------------------
>
> private static final class Filter extends Object implements
> FilenameFilter
> {
>
> private Filter()
> {
> super();
> }
>
> public boolean accept(File aDirectory, String aName)
> {
> if ( aName.endsWith( SZIndex.Extension ) == true )
> {
> File aFile = new File( aDirectory, aName );
>
> if ( aFile.isDirectory() == true )
> {
> return true;
> }
> }
>
> return false;
> }
> }
>
> }
That's all I can see.
Otis
__________________________________________________
Do You Yahoo!?
Yahoo! Health - your guide to health and wellness
http://health.yahoo.com
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>