Skip to content

Commit

Permalink
Building parameter route for snapshot commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
dstreev committed Jan 27, 2021
1 parent 2dc5fba commit b41d17d
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 60 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/streever/hadoop/HadoopSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
128 changes: 72 additions & 56 deletions src/main/java/com/streever/hadoop/hdfs/shell/command/HdfsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -45,4 +45,6 @@ public PathDirectives(Direction direction) {
this.direction = direction;
}

public PathDirectives() {
}
}

0 comments on commit b41d17d

Please sign in to comment.