Skip to content

Commit 789d21c

Browse files
committed
Added more geo-options (srs and spatial index file) and added support to resolve globs against the classpath.
1 parent 21322ee commit 789d21c

File tree

5 files changed

+105
-12
lines changed

5 files changed

+105
-12
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@
334334
<version>0.0.1-3-SNAPSHOT</version>
335335
</dependency>
336336

337+
<dependency>
338+
<groupId>io.github.classgraph</groupId>
339+
<artifactId>classgraph</artifactId>
340+
<version>4.8.129</version>
341+
</dependency>
342+
337343
<dependency>
338344
<groupId>org.apache.commons</groupId>
339345
<artifactId>commons-vfs2</artifactId>

rdf-processing-toolkit-cli/pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@
6565
<groupId>org.aksw.jenax</groupId>
6666
<artifactId>jenax-arq-connection-qlever</artifactId>
6767
</dependency>
68-
68+
6969
<dependency>
7070
<groupId>org.aksw.jenax</groupId>
7171
<artifactId>jenax-graphql-v2-schemagen</artifactId>
7272
</dependency>
73-
73+
7474
<dependency>
7575
<groupId>org.aksw.jenax</groupId>
7676
<artifactId>jenax-io-core</artifactId>
@@ -89,6 +89,12 @@
8989
<groupId>org.aksw.rmltk</groupId>
9090
<artifactId>rml-jena-arq</artifactId>
9191
</dependency>
92+
93+
<dependency>
94+
<groupId>io.github.classgraph</groupId>
95+
<artifactId>classgraph</artifactId>
96+
</dependency>
97+
9298
<dependency>
9399
<groupId>net.sansa-stack</groupId>
94100
<artifactId>sansa-cmds-picocli_2.13</artifactId>

rdf-processing-toolkit-cli/src/main/java/org/aksw/sparql_integrate/cli/cmd/CmdSparqlIntegrateMain.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public class CmdSparqlIntegrateMain
6060
@Option(names = { "--read-only" }, description="Disable SPARQL update on the server")
6161
public boolean readOnlyMode = false;
6262

63+
@Option(names = { "--db-loader-set" }, description="Set an engine loader option")
64+
public Map<String, String> dbLoaderOptions = new LinkedHashMap<>();
6365

6466
@Option(names = { "--env" }, description="Set property that can be accessed using the SPARQL function sys:getenv(key).")
6567
public Map<String, String> env;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.aksw.sparql_integrate.cli.main;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.regex.PatternSyntaxException;
7+
8+
import org.apache.commons.lang3.StringUtils;
9+
10+
import io.github.classgraph.ClassGraph;
11+
import io.github.classgraph.Resource;
12+
import io.github.classgraph.ResourceList;
13+
import io.github.classgraph.ScanResult;
14+
15+
public class ClassPathResourceResolver {
16+
17+
/** Try to resolve the glob to a list of resources on the class path. */
18+
public static List<String> resolve(String glob) {
19+
// Replace all unescaped backslashes
20+
// TODO Fix pattern
21+
String normalized = glob.replace('\\', '/');
22+
23+
String prefix = longestLiteralPrefix(normalized);
24+
// Remove leading slashes from the prefix because ClassGraph resources don't start with a slash.
25+
normalized = normalized.replaceAll("^//+", "");
26+
27+
List<String> result = new ArrayList<>();
28+
try (ScanResult scanResult = new ClassGraph().acceptPaths(prefix).scan()) {
29+
ResourceList resourceList;
30+
try {
31+
resourceList = scanResult.getResourcesMatchingWildcard(normalized);
32+
//ResourceList resourceList = scanResult.getAllResources();
33+
for (Resource r : resourceList) {
34+
result.add(r.getPath());
35+
}
36+
} catch (PatternSyntaxException e) {
37+
// Invalid glob pattern - don't match anything.
38+
}
39+
}
40+
Collections.sort(result);
41+
return result;
42+
}
43+
44+
/** Longest literal (meta-free) prefix of a Unix-style glob. */
45+
public static String longestLiteralPrefix(String glob) {
46+
if (glob == null) {
47+
throw new NullPointerException("glob");
48+
}
49+
50+
int cut = StringUtils.indexOfAny(glob, '*', '?', '[');
51+
if (cut == -1) {
52+
cut = glob.length();
53+
}
54+
55+
// Back up over any trailing slashes.
56+
int end = cut;
57+
while (end > 0 && glob.charAt(end - 1) == '/') {
58+
end--;
59+
}
60+
String prefix = glob.substring(0, end);
61+
return prefix;
62+
}
63+
64+
}

rdf-processing-toolkit-cli/src/main/java/org/aksw/sparql_integrate/cli/main/SparqlIntegrateCmdImpls.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
import org.aksw.jenax.dataaccess.sparql.connection.common.RDFConnectionUtils;
5656
import org.aksw.jenax.dataaccess.sparql.creator.RDFDatabase;
5757
import org.aksw.jenax.dataaccess.sparql.creator.RDFDatabaseFactory;
58-
import org.aksw.jenax.dataaccess.sparql.creator.RdfDatabaseBuilder;
58+
import org.aksw.jenax.dataaccess.sparql.creator.RDFDatabaseBuilder;
5959
import org.aksw.jenax.dataaccess.sparql.datasource.RDFDataSource;
6060
import org.aksw.jenax.dataaccess.sparql.engine.RDFEngine;
6161
import org.aksw.jenax.dataaccess.sparql.engine.RDFEngines;
@@ -269,7 +269,19 @@ public static int sparqlIntegrate(CmdSparqlIntegrateMain cmd) throws Exception {
269269
});
270270
}
271271

272-
List<String> args = cmd.nonOptionArgs;
272+
List<String> rawArgs = cmd.nonOptionArgs;
273+
274+
List<String> args = new ArrayList<>();
275+
276+
for (String rawArg : rawArgs) {
277+
List<String> resolved = ClassPathResourceResolver.resolve(rawArg);
278+
if (resolved.isEmpty()) {
279+
args.add(rawArg);
280+
} else {
281+
logger.info("Resolved " + rawArg + " to " + resolved);
282+
args.addAll(resolved);
283+
}
284+
}
273285

274286
// If an in/out file is given then prepend it to the arguments
275287
Path outFile = null;
@@ -437,7 +449,10 @@ public static int sparqlIntegrate(CmdSparqlIntegrateMain cmd) throws Exception {
437449
if (databaseFactory != null) {
438450
closeablePath = setupDbFolder(cmd);
439451

440-
RdfDatabaseBuilder<?> databaseBuilder = databaseFactory.newBuilder();
452+
RDFDatabaseBuilder<?> databaseBuilder = databaseFactory.newBuilder();
453+
454+
databaseBuilder.setProperties(cmd.dbLoaderOptions);
455+
441456
Path outputFolder = closeablePath.path();
442457
databaseBuilder.setOutputFolder(outputFolder);
443458

@@ -681,7 +696,7 @@ public static int sparqlIntegrate(CmdSparqlIntegrateMain cmd) throws Exception {
681696
Document metaDoc = parser.parseDocument(metaSchemaRawStr);
682697

683698
String graphQlSchemaRawStr = toStringUtf8(streamMgr, cmd.graphQlSchema);
684-
Document schemaDoc = parser.parseDocument(graphQlSchemaRawStr);
699+
Document schemaDoc = GraphQlUtils.parseUnrestricted(graphQlSchemaRawStr);
685700

686701
List<Definition> mergedDefinitions = new ArrayList<>();
687702
mergedDefinitions.addAll(metaDoc.getDefinitions());
@@ -795,7 +810,7 @@ public void beforeExec() {
795810
public void beforeExec() {
796811
Context finalDatasetCxt = finalDataset.getContext();
797812
if (finalDatasetCxt != null && finalDatasetCxt.isFalseOrUndef(SPATIAL_INDEX_IS_CLEAN)) {
798-
updateSpatialIndex(finalDataset);
813+
updateSpatialIndex(finalDataset, cmd.arqConfig.geoindexSrs, cmd.arqConfig.geoindexFile);
799814

800815
// The the spatial index symbol is in the dataset's context
801816
// copy it into the query exec's context.
@@ -851,7 +866,7 @@ public void beforeExec() {
851866
if (finalDatasetCxt != null && finalDatasetCxt.isFalseOrUndef(SPATIAL_INDEX_IS_CLEAN)) {
852867
spatialUpdateNeeded = isSpatialIndexUpdateImmediatelyRequired(ur);
853868
if (spatialUpdateNeeded) {
854-
updateSpatialIndex(finalDataset);
869+
updateSpatialIndex(finalDataset, cmd.arqConfig.geoindexSrs, cmd.arqConfig.geoindexFile);
855870

856871
// The the spatial index symbol is in the dataset's context
857872
// copy it into the query exec's context.
@@ -875,7 +890,7 @@ public void afterExec() {
875890
// Defer update
876891
if (false && spatialUpdateNeeded) {
877892
// Note: The update runs in the same transaction as the update!
878-
updateSpatialIndex(finalDataset);
893+
updateSpatialIndex(finalDataset, cmd.arqConfig.geoindexSrs, cmd.arqConfig.geoindexFile);
879894
}
880895
}
881896
};
@@ -1112,11 +1127,11 @@ private static void loadMacros(Set<String> macroProfiles, Map<String, UserDefine
11121127
}
11131128

11141129
/** Be careful not to call within a read transaction! */
1115-
public static void updateSpatialIndex(Dataset dataset) {
1130+
public static void updateSpatialIndex(Dataset dataset, String srs, Path file) {
11161131
Context cxt = dataset.getContext();
1117-
logger.info("(Re-)computing geo index");
1132+
// logger.info("(Re-)computing geo index");
11181133
try {
1119-
GeoSPARQLConfig.setupSpatialIndex(dataset);
1134+
GeoSPARQLConfig.setupSpatialIndex(dataset, srs, file);
11201135
cxt.setTrue(SPATIAL_INDEX_IS_CLEAN);
11211136
} catch (Exception e) {
11221137
if (e.getMessage().toLowerCase().contains("no srs found")) {

0 commit comments

Comments
 (0)