You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@lucene.apache.org by mi...@apache.org on 2007/01/22 20:21:56 UTC

svn commit: r498755 - in /lucene/java/trunk: CHANGES.txt src/java/org/apache/lucene/store/Directory.java src/java/org/apache/lucene/store/RAMDirectory.java

Author: mikemccand
Date: Mon Jan 22 11:21:56 2007
New Revision: 498755

URL: http://svn.apache.org/viewvc?view=rev&rev=498755
Log:
LUCENE-780: add static Directory.copy() method to copy files from one Directory to another

Modified:
    lucene/java/trunk/CHANGES.txt
    lucene/java/trunk/src/java/org/apache/lucene/store/Directory.java
    lucene/java/trunk/src/java/org/apache/lucene/store/RAMDirectory.java

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?view=diff&rev=498755&r1=498754&r2=498755
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Mon Jan 22 11:21:56 2007
@@ -189,7 +189,10 @@
     take a boolean "create" argument.  Instead you should use
     IndexWriter's "create" argument to create a new index.
     (Mike McCandless)
-    
+
+17. LUCENE-780: Add a static Directory.copy() method to copy files
+    from one Directory to another.  (Jiri Kuhn via Mike McCandless)
+
 Bug fixes
 
  1. Fixed the web application demo (built with "ant war-demo") which

Modified: lucene/java/trunk/src/java/org/apache/lucene/store/Directory.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/store/Directory.java?view=diff&rev=498755&r1=498754&r2=498755
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/store/Directory.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/store/Directory.java Mon Jan 22 11:21:56 2007
@@ -124,4 +124,49 @@
   public String getLockID() {
       return this.toString();
   }
+
+  /**
+   * Copy contents of a directory src to a directory dest.
+   * If a file in src already exists in dest then the
+   * one in dest will be blindly overwritten.
+   *
+   * @param src source directory
+   * @param dest destination directory
+   * @param closeDirSrc if <code>true</code>, call {@link #close()} method on source directory
+   * @throws IOException
+   */
+  public static void copy(Directory src, Directory dest, boolean closeDirSrc) throws IOException {
+      final String[] files = src.list();
+      byte[] buf = new byte[BufferedIndexOutput.BUFFER_SIZE];
+      for (int i = 0; i < files.length; i++) {
+        IndexOutput os = null;
+        IndexInput is = null;
+        try {
+          // create file in dest directory
+          os = dest.createOutput(files[i]);
+          // read current file
+          is = src.openInput(files[i]);
+          // and copy to dest directory
+          long len = is.length();
+          long readCount = 0;
+          while (readCount < len) {
+            int toRead = readCount + BufferedIndexOutput.BUFFER_SIZE > len ? (int)(len - readCount) : BufferedIndexOutput.BUFFER_SIZE;
+            is.readBytes(buf, 0, toRead);
+            os.writeBytes(buf, toRead);
+            readCount += toRead;
+          }
+        } finally {
+          // graceful cleanup
+          try {
+            if (os != null)
+              os.close();
+          } finally {
+            if (is != null)
+              is.close();
+          }
+        }
+      }
+      if(closeDirSrc)
+        src.close();
+  }
 }

Modified: lucene/java/trunk/src/java/org/apache/lucene/store/RAMDirectory.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/store/RAMDirectory.java?view=diff&rev=498755&r1=498754&r2=498755
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/store/RAMDirectory.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/store/RAMDirectory.java Mon Jan 22 11:21:56 2007
@@ -74,29 +74,7 @@
   
   private RAMDirectory(Directory dir, boolean closeDir) throws IOException {
     this();
-    final String[] files = dir.list();
-    byte[] buf = new byte[BufferedIndexOutput.BUFFER_SIZE];
-    for (int i = 0; i < files.length; i++) {
-      // make place on ram disk
-      IndexOutput os = createOutput(files[i]);
-      // read current file
-      IndexInput is = dir.openInput(files[i]);
-      // and copy to ram disk
-      long len = is.length();
-      long readCount = 0;
-      while (readCount < len) {
-        int toRead = readCount + BufferedIndexOutput.BUFFER_SIZE > len ? (int)(len - readCount) : BufferedIndexOutput.BUFFER_SIZE;
-        is.readBytes(buf, 0, toRead);
-        os.writeBytes(buf, toRead);
-        readCount += toRead;
-      }
-
-      // graceful cleanup
-      is.close();
-      os.close();
-    }
-    if(closeDir)
-      dir.close();
+    Directory.copy(dir, this, closeDir);
   }
 
   /**