SOLR-4234: Add support for binary files in ZooKeeper.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1481675 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Robert Miller 2013-05-13 00:30:25 +00:00
parent 1cd6be0712
commit ce5f508ff5
6 changed files with 46 additions and 6 deletions

View File

@ -80,6 +80,8 @@ New Features
* SOLR-4785: New MaxScoreQParserPlugin returning max() instead of sum() of terms (janhoy) * 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 Bug Fixes
---------------------- ----------------------

View File

@ -1251,7 +1251,7 @@ public final class ZkController {
byte[] data = zkClient.getData(zkPath + "/" + file, null, null, true); byte[] data = zkClient.getData(zkPath + "/" + file, null, null, true);
dir.mkdirs(); dir.mkdirs();
log.info("Write file " + new File(dir, file)); 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 { } else {
downloadFromZK(zkClient, zkPath + "/" + file, new File(dir, file)); downloadFromZK(zkClient, zkPath + "/" + file, new File(dir, file));
} }

View File

@ -191,9 +191,7 @@ public class ShowFileRequestHandler extends RequestHandlerBase
ModifiableSolrParams params = new ModifiableSolrParams(req.getParams()); ModifiableSolrParams params = new ModifiableSolrParams(req.getParams());
params.set(CommonParams.WT, "raw"); params.set(CommonParams.WT, "raw");
req.setParams(params); req.setParams(params);
ContentStreamBase content = new ContentStreamBase.ByteArrayStream(zkClient.getData(adminFile, null, null, true), adminFile);
ContentStreamBase content = new ContentStreamBase.StringStream(
new String(zkClient.getData(adminFile, null, null, true), "UTF-8"));
content.setContentType(req.getParams().get(USE_CONTENT_TYPE)); content.setContentType(req.getParams().get(USE_CONTENT_TYPE));
rsp.add(RawResponseWriter.CONTENT, content); rsp.add(RawResponseWriter.CONTENT, content);

View File

@ -18,6 +18,7 @@ package org.apache.solr.cloud;
*/ */
import java.io.File; import java.io.File;
import java.util.Collection;
import java.util.List; import java.util.List;
import org.apache.commons.io.FileUtils; 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); List<String> zkFiles = zkClient.getChildren(ZkController.CONFIGS_ZKNODE + "/" + confsetname, null, true);
assertEquals(files.length, zkFiles.size()); 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 zk // test reset zk
args = new String[] {"-zkhost", zkServer.getZkAddress(), "-cmd", args = new String[] {"-zkhost", zkServer.getZkAddress(), "-cmd",
"clear", "/"}; "clear", "/"};

View File

@ -311,13 +311,13 @@ public class SolrZkClient {
public void makePath(String path, File file, boolean failOnExists, boolean retryOnConnLoss) public void makePath(String path, File file, boolean failOnExists, boolean retryOnConnLoss)
throws IOException, KeeperException, InterruptedException { throws IOException, KeeperException, InterruptedException {
makePath(path, FileUtils.readFileToString(file).getBytes("UTF-8"), makePath(path, FileUtils.readFileToByteArray(file),
CreateMode.PERSISTENT, null, failOnExists, retryOnConnLoss); CreateMode.PERSISTENT, null, failOnExists, retryOnConnLoss);
} }
public void makePath(String path, File file, boolean retryOnConnLoss) throws IOException, public void makePath(String path, File file, boolean retryOnConnLoss) throws IOException,
KeeperException, InterruptedException { 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, public void makePath(String path, CreateMode createMode, boolean retryOnConnLoss) throws KeeperException,

View File

@ -233,4 +233,27 @@ public abstract class ContentStreamBase implements ContentStream
public void setSourceInfo(String sourceInfo) { public void setSourceInfo(String sourceInfo) {
this.sourceInfo = 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 );
}
}
} }