Skip to content

Commit

Permalink
Merge pull request apache#41 from bsidhom/flat-namespace
Browse files Browse the repository at this point in the history
Use character replacement instead of regex for artifact flattening
  • Loading branch information
bsidhom authored Mar 27, 2018
2 parents b6d4021 + 5b77613 commit 3f8bc1b
Showing 1 changed file with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@

package org.apache.beam.runners.core.construction;

import static com.google.common.base.Preconditions.checkState;

import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.BaseEncoding;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
Expand All @@ -47,8 +43,6 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import org.apache.beam.model.jobmanagement.v1.ArtifactApi.ArtifactChunk;
import org.apache.beam.model.jobmanagement.v1.ArtifactApi.ArtifactMetadata;
Expand All @@ -67,13 +61,6 @@ public class ArtifactServiceStager {
// 2 MB per file-request
private static final int DEFAULT_BUFFER_SIZE = 2 * 1024 * 1024;

private static final Pattern PATH_ESCAPE_PATTERN = Pattern.compile("[_\\\\/.]");
private static final Map<String, String> PATH_ESCAPE_MAP = ImmutableMap.of(
"_", Matcher.quoteReplacement("__"),
"\\", Matcher.quoteReplacement("_."),
"/", Matcher.quoteReplacement("._"),
".", Matcher.quoteReplacement(".."));

public static ArtifactServiceStager overChannel(Channel channel) {
return overChannel(channel, DEFAULT_BUFFER_SIZE);
}
Expand Down Expand Up @@ -260,14 +247,26 @@ boolean isSuccess() {
}

private static String escapePath(String path) {
Matcher m = PATH_ESCAPE_PATTERN.matcher(path);
StringBuffer result = new StringBuffer();
while (m.find()) {
String replacement = PATH_ESCAPE_MAP.get(m.group());
checkState(replacement != null);
m.appendReplacement(result, replacement);
StringBuilder result = new StringBuilder(path.length() * 2);
for (int i = 0; i < path.length(); i++) {
char c = path.charAt(i);
switch (c) {
case '_':
result.append("__");
break;
case '\\':
result.append("_.");
break;
case '/':
result.append("._");
break;
case '.':
result.append("..");
break;
default:
result.append(c);
}
}
m.appendTail(result);
return result.toString();
}
}

0 comments on commit 3f8bc1b

Please sign in to comment.