From b41d17d7d345509aefbc7af82f9282f1d0a76c71 Mon Sep 17 00:00:00 2001 From: "David W. Streever" Date: Wed, 27 Jan 2021 07:55:02 -0500 Subject: [PATCH] Building parameter route for snapshot commands. --- .../com/streever/hadoop/HadoopSession.java | 6 +- .../hdfs/shell/command/HdfsAbstract.java | 1 + .../hdfs/shell/command/HdfsCommand.java | 128 ++++++++++-------- .../hdfs/shell/command/PathBuilder.java | 5 + .../hdfs/shell/command/PathDirectives.java | 4 +- 5 files changed, 84 insertions(+), 60 deletions(-) diff --git a/src/main/java/com/streever/hadoop/HadoopSession.java b/src/main/java/com/streever/hadoop/HadoopSession.java index c3f94bb..46d31cd 100644 --- a/src/main/java/com/streever/hadoop/HadoopSession.java +++ b/src/main/java/com/streever/hadoop/HadoopSession.java @@ -1196,9 +1196,9 @@ public void initialize() throws Exception { getEnv().addCommand(new HdfsDisallowSnapshot("disallowSnapshot", getEnv(), Direction.NONE, 1, false, true)); getEnv().addCommand(new HdfsLsSnapshottableDir("lsSnapshottableDir", getEnv(), Direction.NONE, 1, false, true)); - getEnv().addCommand(new HdfsCommand("createSnapshot", getEnv(), Direction.NONE, 1, false, true)); - getEnv().addCommand(new HdfsCommand("deleteSnapshot", getEnv(), Direction.NONE, 1, false, false)); - getEnv().addCommand(new HdfsCommand("renameSnapshot", getEnv(), Direction.NONE, 2, false, false)); + getEnv().addCommand(new HdfsCommand("createSnapshot", getEnv())); + getEnv().addCommand(new HdfsCommand("deleteSnapshot", getEnv())); + getEnv().addCommand(new HdfsCommand("renameSnapshot", getEnv())); getEnv().addCommand(new SnapshotDiff("snapshotDiff", getEnv())); getEnv().addCommand(new HdfsCommand("du", getEnv(), Direction.NONE)); diff --git a/src/main/java/com/streever/hadoop/hdfs/shell/command/HdfsAbstract.java b/src/main/java/com/streever/hadoop/hdfs/shell/command/HdfsAbstract.java index f184396..f2c9fc9 100644 --- a/src/main/java/com/streever/hadoop/hdfs/shell/command/HdfsAbstract.java +++ b/src/main/java/com/streever/hadoop/hdfs/shell/command/HdfsAbstract.java @@ -64,6 +64,7 @@ public HdfsAbstract(String name, Environment env, Direction directionContext, in public HdfsAbstract(String name, Environment env) { super(name); this.env = env; + this.pathBuilder = new PathBuilder(env); } public Environment getEnv() { diff --git a/src/main/java/com/streever/hadoop/hdfs/shell/command/HdfsCommand.java b/src/main/java/com/streever/hadoop/hdfs/shell/command/HdfsCommand.java index b455c56..e0fb1a2 100644 --- a/src/main/java/com/streever/hadoop/hdfs/shell/command/HdfsCommand.java +++ b/src/main/java/com/streever/hadoop/hdfs/shell/command/HdfsCommand.java @@ -66,7 +66,7 @@ public HdfsCommand(String name, Environment env, Direction directionContext, int } public HdfsCommand(String name, Environment env) { - this(name,env, Direction.NONE); + super(name,env); } @Override @@ -106,75 +106,91 @@ public CommandReturn implementation(Environment env, CommandLine cmd, CommandRet String leftPath = null; String rightPath = null; - switch (pathDirectives.getDirection()) { - case REMOTE_LOCAL: - pathCount += 2; // Source and Destination Path Elements. - break; - case LOCAL_REMOTE: - pathCount += 2; // Source and Destination Path Elements. - - break; - case REMOTE_REMOTE: - pathCount += 2; // Source and Destination Path Elements. - - break; - default: // NONE - pathCount += 1; - } - - leftPath = pathBuilder.buildPath(Side.LEFT, cmdArgs); + if (pathDirectives != null) { + switch (pathDirectives.getDirection()) { + case REMOTE_LOCAL: + pathCount += 2; // Source and Destination Path Elements. + break; + case LOCAL_REMOTE: + pathCount += 2; // Source and Destination Path Elements. + + break; + case REMOTE_REMOTE: + pathCount += 2; // Source and Destination Path Elements. + + break; + default: // NONE + pathCount += 1; + } - // When the fs isn't the 'default', we need to 'fully' qualify it. - // When dealing with non-default/non-local filesystems, we need to prefix the uri with the namespace. - // For LOCAL_REMOTE calls, like put, don't prefix the leftPath. - if (!fss.equals(env.getFileSystemOrganizer().getDefaultFileSystemState()) && pathDirectives.getDirection() != Direction.LOCAL_REMOTE) { - leftPath = fss.getURI() + leftPath; - } + leftPath = pathBuilder.buildPath(Side.LEFT, cmdArgs); - if (pathDirectives.getDirection() != Direction.NONE) { - rightPath = pathBuilder.buildPath(Side.RIGHT, cmdArgs); + // When the fs isn't the 'default', we need to 'fully' qualify it. // When dealing with non-default/non-local filesystems, we need to prefix the uri with the namespace. - if (!fss.equals(env.getFileSystemOrganizer().getDefaultFileSystemState())) { - rightPath = fss.getURI() + rightPath; + // For LOCAL_REMOTE calls, like put, don't prefix the leftPath. + if (!fss.equals(env.getFileSystemOrganizer().getDefaultFileSystemState()) && pathDirectives.getDirection() != Direction.LOCAL_REMOTE) { + leftPath = fss.getURI() + leftPath; } - } + if (pathDirectives.getDirection() != Direction.NONE) { + rightPath = pathBuilder.buildPath(Side.RIGHT, cmdArgs); + // When dealing with non-default/non-local filesystems, we need to prefix the uri with the namespace. + if (!fss.equals(env.getFileSystemOrganizer().getDefaultFileSystemState())) { + rightPath = fss.getURI() + rightPath; + } - String[] newCmdArgs = new String[pathCount]; - if (rightPath != null) { - newCmdArgs[0] = leftPath; - newCmdArgs[1] = rightPath; - } else { - newCmdArgs[0] = leftPath; - } + } - argv = new String[cmdOpts.length + newCmdArgs.length + 1 + pathDirectives.getDirectives()]; + String[] newCmdArgs = new String[pathCount]; + if (rightPath != null) { + newCmdArgs[0] = leftPath; + newCmdArgs[1] = rightPath; + } else { + newCmdArgs[0] = leftPath; + } - int pos = 1; + argv = new String[cmdOpts.length + newCmdArgs.length + 1 + pathDirectives.getDirectives()]; - for (Option opt: cmdOpts) { - if (pos >= argv.length) { - System.out.println("OUT OF BOUNDS: " + pos + " " + argv.length); + int pos = 1; + + for (Option opt : cmdOpts) { + if (pos >= argv.length) { + System.out.println("OUT OF BOUNDS: " + pos + " " + argv.length); + } + argv[pos++] = "-" + opt.getOpt(); } - argv[pos++] = "-" + opt.getOpt(); - } - if (pathDirectives.isBefore()) { - for (int i = 0; i < pathDirectives.getDirectives(); i++) { - argv[pos++] = cmdArgs[i]; + if (pathDirectives.isBefore()) { + for (int i = 0; i < pathDirectives.getDirectives(); i++) { + argv[pos++] = cmdArgs[i]; + } } - } - for (String arg: newCmdArgs) { - argv[pos++] = arg; - } + for (String arg : newCmdArgs) { + argv[pos++] = arg; + } - if (!pathDirectives.isBefore()) { - for (int i = pathDirectives.getDirectives(); i > 0; i--) { - try { - argv[pos++] = cmdArgs[cmdArgs.length - (i)]; - } catch (Exception e) { - // Can happen when args are optional + if (!pathDirectives.isBefore()) { + for (int i = pathDirectives.getDirectives(); i > 0; i--) { + try { + argv[pos++] = cmdArgs[cmdArgs.length - (i)]; + } catch (Exception e) { + // Can happen when args are optional + } + } + } + } else { + if (cmdArgs.length == 0) { + argv = new String[2]; + } else { + argv = new String[cmdArgs.length + 1]; + } + // Assume first parameter is a path element. + String path = pathBuilder.buildPath(Side.LEFT, cmdArgs); + argv[1] = path; + if (cmdArgs.length > 1) { + for (int i = 2; i <= argv.length - 1; i++) { + argv[i] = cmdArgs[i-1]; } } } diff --git a/src/main/java/com/streever/hadoop/hdfs/shell/command/PathBuilder.java b/src/main/java/com/streever/hadoop/hdfs/shell/command/PathBuilder.java index 9948e73..53dec5c 100644 --- a/src/main/java/com/streever/hadoop/hdfs/shell/command/PathBuilder.java +++ b/src/main/java/com/streever/hadoop/hdfs/shell/command/PathBuilder.java @@ -14,6 +14,11 @@ public PathBuilder(Environment env, PathDirectives directives) { this.directives = directives; } + public PathBuilder(Environment env) { + this.env = env; + this.directives = new PathDirectives(); + } + public String buildPath(Side side, String[] args) { String rtn = null; diff --git a/src/main/java/com/streever/hadoop/hdfs/shell/command/PathDirectives.java b/src/main/java/com/streever/hadoop/hdfs/shell/command/PathDirectives.java index 71cb916..2005fd2 100644 --- a/src/main/java/com/streever/hadoop/hdfs/shell/command/PathDirectives.java +++ b/src/main/java/com/streever/hadoop/hdfs/shell/command/PathDirectives.java @@ -1,7 +1,7 @@ package com.streever.hadoop.hdfs.shell.command; public class PathDirectives { - private Direction direction = null; + private Direction direction = Direction.NONE; private int directives = 0; //default private boolean before = true; //default @@ -45,4 +45,6 @@ public PathDirectives(Direction direction) { this.direction = direction; } + public PathDirectives() { + } }