Skip to content

Commit fabc74e

Browse files
committed
feat(ENGKNOW-2957): More control over link data path.
1 parent 395c96f commit fabc74e

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

model/src/main/java/org/gorpipe/gor/driver/GorDriverConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,10 @@ public interface GorDriverConfig extends Config {
187187
@Key(GOR_DRIVER_LINK_MANAGED_DATA_FILES_URL)
188188
@DefaultValue("")
189189
String managedLinkDataFilesURL();
190+
191+
String GOR_DRIVER_LINK_INFER_REPLACE = "GOR_DRIVER_LINK_INFER_REPLACE";
192+
@Documentation("Replacement patterns when inferring link file paths (<match pattern>[;<replace string>]).")
193+
@Key(GOR_DRIVER_LINK_INFER_REPLACE)
194+
@DefaultValue("")
195+
String linkeInferReplace();
190196
}

model/src/main/java/org/gorpipe/gor/driver/linkfile/LinkFile.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public abstract class LinkFile {
5959

6060
public static final int LINK_FILE_MAX_SIZE = 10000;
6161

62-
private static final boolean USE_LINK_CACHE = Boolean.parseBoolean(System.getProperty("gor.driver.cache.link", "true"));
62+
private static final boolean USE_LINK_CACHE = Boolean.parseBoolean(System.getProperty("gor.driver.link.cache", "true"));
6363
private static final Cache<StreamSource, String> linkCache = Caffeine.newBuilder()
6464
.maximumSize(10000)
6565
.expireAfterWrite(2, TimeUnit.HOURS).build();
@@ -101,11 +101,21 @@ public static String inferDataFileNameFromLinkFile(StreamSource linkSource, Stri
101101

102102
var linkPath = linkSource.getSourceReference().getUrl();
103103

104+
// Remove common the root if set.
105+
var pathReplacements = System.getenv("GOR_DRIVER_LINK_INFER_REPLACE");
106+
if (!Strings.isNullOrEmpty(pathReplacements)) {
107+
var parts = pathReplacements.split(";");
108+
linkPath = linkPath.replaceAll(parts[0], parts.length > 1 ? parts[1] : "");
109+
}
110+
111+
// Adjust link path so it suitable as part of data file path.
104112
if (PathUtils.isAbsolutePath(linkPath)) {
105-
throw new IllegalArgumentException("Link file path is absolute. Can not infer data file name: " + linkSource.getFullPath());
113+
throw new IllegalArgumentException("Link file path is absolute and gor.driver.link.common.root is not set. Can not infer data file name: " + linkSource.getFullPath());
106114
}
107115

108116
var dataFileRootPath = "";
117+
118+
// Get root from the link file
109119
var link = linkSource.exists()
110120
? load(linkSource).appendMeta(linkFileMeta)
111121
: create(linkSource, linkFileMeta);
@@ -114,6 +124,7 @@ public static String inferDataFileNameFromLinkFile(StreamSource linkSource, Stri
114124
dataFileRootPath = linkDataFileRootPath;
115125
}
116126

127+
// Get root from global const
117128
if (Strings.isNullOrEmpty(dataFileRootPath)) {
118129
dataFileRootPath = System.getenv(GorDriverConfig.GOR_DRIVER_LINK_MANAGED_DATA_FILES_URL);
119130
}
@@ -122,6 +133,7 @@ public static String inferDataFileNameFromLinkFile(StreamSource linkSource, Stri
122133
throw new IllegalArgumentException("Link file data root path is not set. Can not infer data file name from link file: " + linkSource.getFullPath());
123134
}
124135

136+
// Create file name
125137
String randomString = RandomStringUtils.random(8, true, true);
126138
var linkPathSplit = linkPath.indexOf('.');
127139
if (linkPathSplit > 0) {
@@ -135,6 +147,7 @@ public static String inferDataFileNameFromLinkFile(StreamSource linkSource, Stri
135147

136148
linkPath = linkPath.replaceAll("\\.link$", "");
137149

150+
// Insert project
138151
var project = linkSource.getSourceReference().getCommonRoot() != null
139152
? PathUtils.getFileName(linkSource.getSourceReference().getCommonRoot()) : "";
140153
if (!Strings.isNullOrEmpty(project)) {

model/src/test/java/org/gorpipe/gor/driver/linkfile/LinkFileTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.junit.Rule;
99
import org.junit.Test;
1010
import org.junit.contrib.java.lang.system.EnvironmentVariables;
11+
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
1112
import org.junit.rules.TemporaryFolder;
1213

1314
import java.io.ByteArrayInputStream;
@@ -26,6 +27,9 @@ public class LinkFileTest {
2627
@Rule
2728
public TemporaryFolder workDir = new TemporaryFolder();
2829

30+
@Rule
31+
public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties();
32+
2933
@Rule
3034
public final EnvironmentVariables environmentVariables
3135
= new EnvironmentVariables();
@@ -237,4 +241,28 @@ public void testInferDataFileNameFromLinkFile_FromMetaParam_ExistingFile() throw
237241
assertNotNull(result);
238242
assertTrue(result.matches((paramroot + "/x\\..*\\.gor").replace("/", "\\/")));
239243
}
244+
245+
@Test
246+
public void testInferDataFileNameFromLinkFile_PathReplace() throws Exception {
247+
String root = "/managed/root";
248+
environmentVariables.set(GorDriverConfig.GOR_DRIVER_LINK_MANAGED_DATA_FILES_URL, root);
249+
environmentVariables.set(GorDriverConfig.GOR_DRIVER_LINK_INFER_REPLACE, "wont;will");
250+
251+
String result = LinkFile.inferDataFileNameFromLinkFile(new FileSource("wont/x.gor.link"), null);
252+
253+
assertNotNull(result);
254+
assertTrue(result.matches((root + "/will/x\\..*\\.gor").replace("/", "\\/")));
255+
}
256+
257+
@Test
258+
public void testInferDataFileNameFromLinkFile_AbsolutePathReplace() throws Exception {
259+
String root = "/managed/root";
260+
environmentVariables.set(GorDriverConfig.GOR_DRIVER_LINK_MANAGED_DATA_FILES_URL, root);
261+
environmentVariables.set(GorDriverConfig.GOR_DRIVER_LINK_INFER_REPLACE, "\\/abs\\/");
262+
263+
String result = LinkFile.inferDataFileNameFromLinkFile(new FileSource("/abs/path/x.gor.link"), null);
264+
265+
assertNotNull(result);
266+
assertTrue(result.matches((root + "/path/x\\..*\\.gor").replace("/", "\\/")));
267+
}
240268
}

0 commit comments

Comments
 (0)