Apache Solr open-source search software
SOLR-4234: Add support for binary files in ZooKeeper.
Mark Robert Millerce5f508ff560
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 413583c27a1..647b60bfa4f 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -80,6 +80,8 @@ New Features* SOLR-4785: New MaxScoreQParserPlugin returning max() instead of sum() of terms (janhoy)+* SOLR-4234: Add support for binary files in ZooKeeper. (Eric Pugh via Mark Miller)+Bug Fixes----------------------diff --git a/solr/core/src/java/org/apache/solr/cloud/ZkController.java b/solr/core/src/java/org/apache/solr/cloud/ZkController.javaindex 922b486aae4..496da732505 100644--- a/solr/core/src/java/org/apache/solr/cloud/ZkController.java+++ b/solr/core/src/java/org/apache/solr/cloud/ZkController.java@@ -1251,7 +1251,7 @@ public final class ZkController {byte[] data = zkClient.getData(zkPath + "/" + file, null, null, true);dir.mkdirs();log.info("Write file " + new File(dir, file));- FileUtils.writeStringToFile(new File(dir, file), new String(data, "UTF-8"), "UTF-8");+ FileUtils.writeByteArrayToFile(new File(dir, file), data);} else {downloadFromZK(zkClient, zkPath + "/" + file, new File(dir, file));}diff --git a/solr/core/src/java/org/apache/solr/handler/admin/ShowFileRequestHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/ShowFileRequestHandler.javaindex c40a8ca1ced..c971c85313c 100644--- a/solr/core/src/java/org/apache/solr/handler/admin/ShowFileRequestHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/admin/ShowFileRequestHandler.java@@ -191,9 +191,7 @@ public class ShowFileRequestHandler extends RequestHandlerBaseModifiableSolrParams params = new ModifiableSolrParams(req.getParams());params.set(CommonParams.WT, "raw");req.setParams(params);-- ContentStreamBase content = new ContentStreamBase.StringStream(- new String(zkClient.getData(adminFile, null, null, true), "UTF-8"));+ ContentStreamBase content = new ContentStreamBase.ByteArrayStream(zkClient.getData(adminFile, null, null, true), adminFile);content.setContentType(req.getParams().get(USE_CONTENT_TYPE));rsp.add(RawResponseWriter.CONTENT, content);diff --git a/solr/core/src/test/org/apache/solr/cloud/ZkCLITest.java b/solr/core/src/test/org/apache/solr/cloud/ZkCLITest.javaindex 70c4ad4bc03..704a15585d7 100644--- a/solr/core/src/test/org/apache/solr/cloud/ZkCLITest.java+++ b/solr/core/src/test/org/apache/solr/cloud/ZkCLITest.java@@ -18,6 +18,7 @@ package org.apache.solr.cloud;*/import java.io.File;+import java.util.Collection;import java.util.List;import org.apache.commons.io.FileUtils;@@ -186,6 +187,22 @@ public class ZkCLITest extends SolrTestCaseJ4 {List<String> zkFiles = zkClient.getChildren(ZkController.CONFIGS_ZKNODE + "/" + confsetname, null, true);assertEquals(files.length, zkFiles.size());+ File sourceConfDir = new File(ExternalPaths.EXAMPLE_HOME + File.separator + "collection1"+ + File.separator + "conf");+ Collection<File> sourceFiles = FileUtils.listFiles(sourceConfDir,null,true);+ for (File sourceFile :sourceFiles){+ if (!sourceFile.isHidden()){+ int indexOfRelativePath = sourceFile.getAbsolutePath().lastIndexOf("collection1/conf");+ String relativePathofFile = sourceFile.getAbsolutePath().substring(indexOfRelativePath + 17, sourceFile.getAbsolutePath().length());+ File downloadedFile = new File(confDir,relativePathofFile);+ assertTrue("Make sure we did download each file in the original configuration",downloadedFile.exists());+ assertTrue("Content didn't change",FileUtils.contentEquals(sourceFile,downloadedFile));++ }++ }++// test reset zkargs = new String[] {"-zkhost", zkServer.getZkAddress(), "-cmd","clear", "/"};diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/SolrZkClient.java b/solr/solrj/src/java/org/apache/solr/common/cloud/SolrZkClient.javaindex 59cb15b94b0..bf69b2c8df1 100644--- a/solr/solrj/src/java/org/apache/solr/common/cloud/SolrZkClient.java+++ b/solr/solrj/src/java/org/apache/solr/common/cloud/SolrZkClient.java@@ -311,13 +311,13 @@ public class SolrZkClient {public void makePath(String path, File file, boolean failOnExists, boolean retryOnConnLoss)throws IOException, KeeperException, InterruptedException {- makePath(path, FileUtils.readFileToString(file).getBytes("UTF-8"),+ makePath(path, FileUtils.readFileToByteArray(file),CreateMode.PERSISTENT, null, failOnExists, retryOnConnLoss);}public void makePath(String path, File file, boolean retryOnConnLoss) throws IOException,KeeperException, InterruptedException {- makePath(path, FileUtils.readFileToString(file).getBytes("UTF-8"), retryOnConnLoss);+ makePath(path, FileUtils.readFileToByteArray(file), retryOnConnLoss);}public void makePath(String path, CreateMode createMode, boolean retryOnConnLoss) throws KeeperException,diff --git a/solr/solrj/src/java/org/apache/solr/common/util/ContentStreamBase.java b/solr/solrj/src/java/org/apache/solr/common/util/ContentStreamBase.javaindex 877da360999..d531b0faf33 100755--- a/solr/solrj/src/java/org/apache/solr/common/util/ContentStreamBase.java+++ b/solr/solrj/src/java/org/apache/solr/common/util/ContentStreamBase.java@@ -233,4 +233,27 @@ public abstract class ContentStreamBase implements ContentStreampublic void setSourceInfo(String sourceInfo) {this.sourceInfo = sourceInfo;}++ /**+ * Construct a <code>ContentStream</code> from a <code>File</code>+ */+ public static class ByteArrayStream extends ContentStreamBase+ {+ private final byte[] bytes;++ public ByteArrayStream( byte[] bytes, String source ) {+ this.bytes = bytes;++ this.contentType = null;+ name = source;+ size = new Long(bytes.length);+ sourceInfo = source;+ }+++ @Override+ public InputStream getStream() throws IOException {+ return new ByteArrayInputStream( bytes );+ }+ }}