Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-780: add static Directory.copy() method to copy files from one Directory to another

Michael McCandless3b13126bf560
diff --git a/CHANGES.txt b/CHANGES.txt
index 6ec624ac496..96051d7421f 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -189,7 +189,10 @@ API Changes
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
diff --git a/src/java/org/apache/lucene/store/Directory.java b/src/java/org/apache/lucene/store/Directory.java
index 07b75c2b1c6..c10a280df15 100644
--- a/src/java/org/apache/lucene/store/Directory.java
+++ b/src/java/org/apache/lucene/store/Directory.java
@@ -124,4 +124,49 @@ public abstract class Directory {
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();
+ }
}
diff --git a/src/java/org/apache/lucene/store/RAMDirectory.java b/src/java/org/apache/lucene/store/RAMDirectory.java
index ef957cb1358..a25dba28f2d 100644
--- a/src/java/org/apache/lucene/store/RAMDirectory.java
+++ b/src/java/org/apache/lucene/store/RAMDirectory.java
@@ -74,29 +74,7 @@ public class RAMDirectory extends Directory implements Serializable {
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);
}
/**