# HG changeset patch # User Earwin Burrfoot # Date 1269243135 -10800 # Node ID 7fc8fc1c9082d098bece0279ab377c3e59350600 # Parent 9e755a9782abbb4b6b9350d2c96916b6b5b94447 [mq]: LUCENE-2339 diff --git a/src/java/org/apache/lucene/store/Directory.java b/src/java/org/apache/lucene/store/Directory.java --- a/src/java/org/apache/lucene/store/Directory.java +++ b/src/java/org/apache/lucene/store/Directory.java @@ -20,6 +20,10 @@ import java.io.IOException; import java.io.Closeable; +import java.util.ArrayList; +import static java.util.Arrays.asList; +import java.util.Iterator; +import java.util.List; import org.apache.lucene.index.IndexFileNameFilter; /** A Directory is a flat list of files. Files may be written once, when they @@ -159,42 +163,56 @@ } /** - * 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. + * 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. + *

+ *

NOTE: the source directory cannot change while this method is running. Otherwise the results are + * undefined and you could easily hit a FileNotFoundException. + *

+ *

NOTE: this method only copies files that look like index files (ie, have extensions matching the known + * extensions of index files). * - *

NOTE: the source directory cannot change - * while this method is running. Otherwise the results - * are undefined and you could easily hit a - * FileNotFoundException. - * - *

NOTE: this method only copies files that look - * like index files (ie, have extensions matching the - * known extensions of index files). - * - * @param src source directory - * @param dest destination directory + * @param src source directory + * @param dest destination directory * @param closeDirSrc if true, 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.listAll(); + List filenames = new ArrayList(asList(src.listAll())); IndexFileNameFilter filter = IndexFileNameFilter.getFilter(); + for (Iterator iterator = filenames.iterator(); iterator.hasNext();) + if (!filter.accept(null, iterator.next())) + iterator.remove(); + + copy(src, dest, filenames); + + if (closeDirSrc) + src.close(); + } + + /** + * Copy given files 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. + *

+ *

NOTE: the source directory cannot change while this method is running. Otherwise the results are + * undefined and you could easily hit a FileNotFoundException.

+ * + * @param src source directory + * @param dest destination directory + * @param filenames file names to be copied + */ + public static void copy(Directory src, Directory dest, Iterable filenames) throws IOException { byte[] buf = new byte[BufferedIndexOutput.BUFFER_SIZE]; - for (int i = 0; i < files.length; i++) { - - if (!filter.accept(null, files[i])) - continue; + for (String filename : filenames) { IndexOutput os = null; IndexInput is = null; try { // create file in dest directory - os = dest.createOutput(files[i]); + os = dest.createOutput(filename); // read current file - is = src.openInput(files[i]); + is = src.openInput(filename); // and copy to dest directory long len = is.length(); long readCount = 0; @@ -215,8 +233,6 @@ } } } - if(closeDirSrc) - src.close(); } /**