lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Otis Gospodnetic <otis_gospodne...@yahoo.com>
Subject Re: [patch] Factory method to preload a Directory into a RAMDirectory
Date Sun, 20 Oct 2002 17:25:29 GMT
Thanks Darren.
We actually received 2 patches with this same purpose last week.  I
have one of them (the shorter one) in my copy of RAMDirectory, I just
want to test it before I commit it.

If you have other suggestions or patches that you can share in the
future, please let us know.

Thanks,
Otis


--- Darren Hobbs <darren@farfetched.org> wrote:
> Hi,
> 
> I'd like to submit the attached diff file as a suggestion for an
> additional factory method on the RAMDirectory class.
> The general idea is to preload a Directory into memory to allow fast
> searches at the cost of some delay at startup.
> 
> I've attached a unified diff with my proposed changes.
> 
> Regards,
> 
> -Darren
> 
> > Index:
> jakarta-lucene/src/java/org/apache/lucene/store/RAMDirectory.java
> ===================================================================
> RCS file:
>
/home/cvspublic/jakarta-lucene/src/java/org/apache/lucene/store/RAMDirectory.java,v
> retrieving revision 1.5
> diff -u -r1.5 RAMDirectory.java
> --- jakarta-lucene/src/java/org/apache/lucene/store/RAMDirectory.java
> 8 Aug 2002 17:56:19 -0000	1.5
> +++ jakarta-lucene/src/java/org/apache/lucene/store/RAMDirectory.java
> 19 Oct 2002 23:22:04 -0000
> @@ -71,6 +71,49 @@
>    public RAMDirectory() {
>    }
>  
> +  /** Constructs a RAMDirectory using the supplied {@link Directory}
> as a source.  Useful for pre-loading indexes */
> +  public static RAMDirectory getDirectory(Directory sourceDirectory)
> throws IOException {
> +    RAMDirectory newDirectory = new RAMDirectory();
> +    org.apache.lucene.store.OutputStream os = null;
> +    org.apache.lucene.store.InputStream is = null;
> +    try {
> +      String[] files = sourceDirectory.list();
> +      for (int i = 0; i < files.length; i++) {
> +        String fileName = files[i];
> +          os = newDirectory.createFile(fileName);
> +          is = sourceDirectory.openFile(fileName);
> +
> +          byte[] buf = new byte[8192];
> +          long toBeRead = is.length();
> +
> +          while (toBeRead > 0) {
> +            int len = (int) (toBeRead > buf.length ? buf.length :
> toBeRead);
> +            is.readBytes(buf, 0, len);
> +            os.writeBytes(buf, len);
> +
> +            toBeRead -= len;
> +          }
> +          is.close();
> +          os.close();
> +        }
> +      }
> +      finally {
> +        try {
> +          is.close();
> +        }
> +        catch (Exception e) {
> +          // Ignore exceptions in finally block - we're either
> already throwing one, or about to return
> +        }
> +        try {
> +          os.close();
> +        }
> +        catch (Exception e) {
> +          // Ignore exceptions in finally block - we're either
> already throwing one, or about to return
> +        }
> +    }
> +    return newDirectory;
> +  }
> +
>    /** Returns an array of strings, one for each file in the
> directory. */
>    public final String[] list() {
>      String[] result = new String[files.size()];
> 
> > --
> To unsubscribe, e-mail:  
> <mailto:lucene-dev-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
<mailto:lucene-dev-help@jakarta.apache.org>


__________________________________________________
Do you Yahoo!?
Y! Web Hosting - Let the expert host your web site
http://webhosting.yahoo.com/

--
To unsubscribe, e-mail:   <mailto:lucene-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:lucene-dev-help@jakarta.apache.org>


Mime
View raw message