You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by Darren Hobbs <da...@farfetched.org> on 2002/10/20 01:30:59 UTC

[patch] Factory method to preload a Directory into a RAMDirectory

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


Re: [patch] Factory method to preload a Directory into a RAMDirectory

Posted by Otis Gospodnetic <ot...@yahoo.com>.
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 <da...@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:  
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>


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

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>