Repository: accumulo Updated Branches: refs/heads/master feff9e62f -> ff605865d
ACCUMULO-2187 Refactor method to read file in AddSplitsCommand and CreateTableCommand Signed-off-by: Bill Havanki <bhava...@cloudera.com> Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/2d68f577 Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/2d68f577 Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/2d68f577 Branch: refs/heads/master Commit: 2d68f5772cce9ef995d97730f4c9f95f203d72b1 Parents: feff9e6 Author: Vikram Srivastava <vikr...@cloudera.com> Authored: Thu Mar 6 12:45:23 2014 -0800 Committer: Bill Havanki <bhava...@cloudera.com> Committed: Fri Mar 7 11:00:58 2014 -0500 ---------------------------------------------------------------------- .../accumulo/core/util/shell/ShellUtil.java | 58 +++++++++++++++++ .../util/shell/commands/AddSplitsCommand.java | 15 +---- .../util/shell/commands/CreateTableCommand.java | 19 +----- .../accumulo/core/util/shell/ShellUtilTest.java | 66 ++++++++++++++++++++ 4 files changed, 128 insertions(+), 30 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/accumulo/blob/2d68f577/core/src/main/java/org/apache/accumulo/core/util/shell/ShellUtil.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/util/shell/ShellUtil.java b/core/src/main/java/org/apache/accumulo/core/util/shell/ShellUtil.java new file mode 100644 index 0000000..831895b --- /dev/null +++ b/core/src/main/java/org/apache/accumulo/core/util/shell/ShellUtil.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.accumulo.core.util.shell; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.List; +import java.util.Scanner; + +import org.apache.accumulo.core.Constants; +import org.apache.commons.codec.binary.Base64; +import org.apache.hadoop.io.Text; + +import com.google.common.collect.Lists; + +public class ShellUtil { + + /** + * Scans the given file line-by-line (ignoring empty lines) and returns a list + * containing those lines. If decode is set to true, every line is decoded using + * {@link Base64.decodeBase64} before inserting in the list. + * + * @param filename Path to the file that needs to be scanned + * @param decode Whether to decode lines in the file + * @return List of {@link Text} objects containing data in the given file + * @throws FileNotFoundException if the given file doesn't exist + */ + public static List<Text> scanFile(String filename, boolean decode) throws FileNotFoundException { + String line; + Scanner file = new Scanner(new File(filename), Constants.UTF8.name()); + List<Text> result = Lists.newArrayList(); + try { + while (file.hasNextLine()) { + line = file.nextLine(); + if (!line.isEmpty()) { + result.add(decode ? new Text(Base64.decodeBase64(line.getBytes(Constants.UTF8))) : new Text(line)); + } + } + } finally { + file.close(); + } + return result; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/accumulo/blob/2d68f577/core/src/main/java/org/apache/accumulo/core/util/shell/commands/AddSplitsCommand.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/util/shell/commands/AddSplitsCommand.java b/core/src/main/java/org/apache/accumulo/core/util/shell/commands/AddSplitsCommand.java index 6bd260c..b8ba621 100644 --- a/core/src/main/java/org/apache/accumulo/core/util/shell/commands/AddSplitsCommand.java +++ b/core/src/main/java/org/apache/accumulo/core/util/shell/commands/AddSplitsCommand.java @@ -16,18 +16,16 @@ */ package org.apache.accumulo.core.util.shell.commands; -import java.io.File; import java.util.TreeSet; -import org.apache.accumulo.core.Constants; import org.apache.accumulo.core.client.TableNotFoundException; import org.apache.accumulo.core.util.shell.Shell; import org.apache.accumulo.core.util.shell.Shell.Command; +import org.apache.accumulo.core.util.shell.ShellUtil; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.MissingArgumentException; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; -import org.apache.commons.codec.binary.Base64; import org.apache.hadoop.io.Text; public class AddSplitsCommand extends Command { @@ -40,16 +38,7 @@ public class AddSplitsCommand extends Command { final TreeSet<Text> splits = new TreeSet<Text>(); if (cl.hasOption(optSplitsFile.getOpt())) { - final String f = cl.getOptionValue(optSplitsFile.getOpt()); - - String line; - java.util.Scanner file = new java.util.Scanner(new File(f), Constants.UTF8.name()); - while (file.hasNextLine()) { - line = file.nextLine(); - if (!line.isEmpty()) { - splits.add(decode ? new Text(Base64.decodeBase64(line.getBytes(Constants.UTF8))) : new Text(line)); - } - } + splits.addAll(ShellUtil.scanFile(cl.getOptionValue(optSplitsFile.getOpt()), decode)); } else { if (cl.getArgList().isEmpty()) { throw new MissingArgumentException("No split points specified"); http://git-wip-us.apache.org/repos/asf/accumulo/blob/2d68f577/core/src/main/java/org/apache/accumulo/core/util/shell/commands/CreateTableCommand.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/util/shell/commands/CreateTableCommand.java b/core/src/main/java/org/apache/accumulo/core/util/shell/commands/CreateTableCommand.java index 25b92be..bc5f1d1 100644 --- a/core/src/main/java/org/apache/accumulo/core/util/shell/commands/CreateTableCommand.java +++ b/core/src/main/java/org/apache/accumulo/core/util/shell/commands/CreateTableCommand.java @@ -16,16 +16,13 @@ */ package org.apache.accumulo.core.util.shell.commands; -import java.io.File; import java.io.IOException; import java.util.Map; import java.util.Map.Entry; -import java.util.Scanner; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; -import org.apache.accumulo.core.Constants; import org.apache.accumulo.core.client.AccumuloException; import org.apache.accumulo.core.client.AccumuloSecurityException; import org.apache.accumulo.core.client.TableExistsException; @@ -37,12 +34,12 @@ import org.apache.accumulo.core.iterators.IteratorUtil; import org.apache.accumulo.core.security.VisibilityConstraint; import org.apache.accumulo.core.util.shell.Shell; import org.apache.accumulo.core.util.shell.Shell.Command; +import org.apache.accumulo.core.util.shell.ShellUtil; import org.apache.accumulo.core.util.shell.Token; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; import org.apache.commons.cli.OptionGroup; import org.apache.commons.cli.Options; -import org.apache.commons.codec.binary.Base64; import org.apache.hadoop.io.Text; public class CreateTableCommand extends Command { @@ -75,19 +72,7 @@ public class CreateTableCommand extends Command { final boolean decode = cl.hasOption(base64Opt.getOpt()); if (cl.hasOption(createTableOptSplit.getOpt())) { - final String f = cl.getOptionValue(createTableOptSplit.getOpt()); - - String line; - Scanner file = new Scanner(new File(f), Constants.UTF8.name()); - try { - while (file.hasNextLine()) { - line = file.nextLine(); - if (!line.isEmpty()) - partitions.add(decode ? new Text(Base64.decodeBase64(line.getBytes(Constants.UTF8 ))) : new Text(line)); - } - } finally { - file.close(); - } + partitions.addAll(ShellUtil.scanFile(cl.getOptionValue(createTableOptSplit.getOpt()), decode)); } else if (cl.hasOption(createTableOptCopySplits.getOpt())) { final String oldTable = cl.getOptionValue(createTableOptCopySplits.getOpt()); if (!shellState.getConnector().tableOperations().exists(oldTable)) { http://git-wip-us.apache.org/repos/asf/accumulo/blob/2d68f577/core/src/test/java/org/apache/accumulo/core/util/shell/ShellUtilTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/util/shell/ShellUtilTest.java b/core/src/test/java/org/apache/accumulo/core/util/shell/ShellUtilTest.java new file mode 100644 index 0000000..3d5cbec --- /dev/null +++ b/core/src/test/java/org/apache/accumulo/core/util/shell/ShellUtilTest.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.accumulo.core.util.shell; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.List; + +import org.apache.accumulo.core.Constants; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.io.FileUtils; +import org.apache.hadoop.io.Text; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import com.google.common.collect.ImmutableList; + +public class ShellUtilTest { + + @Rule + public TemporaryFolder folder = new TemporaryFolder(new File(System.getProperty("user.dir") + "/target")); + + // String with 3 lines, with one empty line + private static final String FILEDATA = "line1\n\nline2"; + + @Test + public void testWithoutDecode() throws IOException { + File testFile = new File(folder.getRoot(), "testFileNoDecode.txt"); + FileUtils.writeStringToFile(testFile, FILEDATA); + List<Text> output = ShellUtil.scanFile(testFile.getAbsolutePath(), false); + assertEquals(ImmutableList.of(new Text("line1"), new Text("line2")), output); + } + + @Test + public void testWithDecode() throws IOException { + File testFile = new File(folder.getRoot(), "testFileWithDecode.txt"); + FileUtils.writeStringToFile(testFile, FILEDATA); + List<Text> output = ShellUtil.scanFile(testFile.getAbsolutePath(), true); + assertEquals(ImmutableList.of( + new Text(Base64.decodeBase64("line1".getBytes(Constants.UTF8))), + new Text(Base64.decodeBase64("line2".getBytes(Constants.UTF8)))), output); + } + + @Test(expected=FileNotFoundException.class) + public void testWithMissingFile() throws FileNotFoundException { + ShellUtil.scanFile("missingFile.txt", false); + } +}