Skip to content

Commit

Permalink
[api] Allows to use relative jar uri for cache folder name
Browse files Browse the repository at this point in the history
Fixes: #3025
  • Loading branch information
frankfliu committed Mar 12, 2024
1 parent 0fa0db2 commit ed28bc7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
17 changes: 13 additions & 4 deletions api/src/main/java/ai/djl/repository/JarRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ public class JarRepository extends AbstractRepository {
private String artifactId;
private String modelName;
private String queryString;
private String originalUri;

private Metadata metadata;
private boolean resolved;

JarRepository(String name, URI uri, String fileName, String queryString) {
JarRepository(String name, URI uri, String fileName, URI realUri) {
super(name, uri);
this.queryString = queryString;
this.uri = realUri;
queryString = uri.getRawQuery();
originalUri = uri.toString();
modelName = arguments.get("model_name");
artifactId = arguments.get("artifact_id");
if (artifactId == null) {
Expand Down Expand Up @@ -123,8 +126,14 @@ private synchronized Metadata getMetadata() {
metadata = new Metadata.MatchAllMetadata();
metadata.setArtifactId(artifactId);
metadata.setArtifacts(Collections.singletonList(artifact));
String hash =
Utils.hash(queryString == null ? uri.toString() : uri.toString() + queryString);
String hashKey;
if (Boolean.parseBoolean(arguments.get("ignore_real_uri"))) {
hashKey = originalUri;
} else {
hashKey = queryString == null ? uri.toString() : uri.toString() + queryString;
}

String hash = Utils.hash(hashKey);
MRL mrl = model(Application.UNDEFINED, DefaultModelZoo.GROUP_ID, hash);
metadata.setRepositoryUri(mrl.toURI());

Expand Down
10 changes: 6 additions & 4 deletions api/src/main/java/ai/djl/repository/RepositoryFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,27 +144,29 @@ private static final class JarRepositoryFactory implements RepositoryFactory {
@Override
public Repository newInstance(String name, URI uri) {
String p = uri.getPath();
String queryString = uri.getRawQuery();
if (p.startsWith("/")) {
p = p.substring(1);
}
URL u = ClassLoaderUtils.getContextClassLoader().getResource(p);
if (u == null) {
throw new IllegalArgumentException("Resource not found: " + uri);
}

URI realUri;
try {
uri = u.toURI();
// resolve real uri: jar:file:/path/my_lib.jar!/model.zip
realUri = u.toURI();
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Resource not found: " + uri, e);
}

Path path = Paths.get(parseFilePath(uri));
Path path = Paths.get(parseFilePath(realUri));
String fileName = path.toFile().getName();
if (FilenameUtils.isArchiveFile(fileName)) {
fileName = FilenameUtils.getNamePart(fileName);
}

return new JarRepository(name, uri, fileName, queryString);
return new JarRepository(name, uri, fileName, realUri);
}

/** {@inheritDoc} */
Expand Down
8 changes: 7 additions & 1 deletion api/src/test/java/ai/djl/repository/JarRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testResource() throws IOException {
URL[] url = {jarFile.toUri().toURL()};
try {
Thread.currentThread().setContextClassLoader(new URLClassLoader(url));
Repository repo = Repository.newInstance("test", "jar:///test.zip?hash=1");
Repository repo = Repository.newInstance("test", "jar:///test.zip");
Assert.assertEquals("test", repo.getName());
Assert.assertTrue(repo.isRemote());

Expand All @@ -55,6 +55,12 @@ public void testResource() throws IOException {
Artifact artifact = repo.resolve(list.get(0), null);
repo.prepare(artifact);
Assert.assertEquals(1, artifact.getFiles().size());

repo = Repository.newInstance("test", "jar:///test.zip?ignore_real_uri=true");
list = repo.getResources();
artifact = repo.resolve(list.get(0), null);
Path p = repo.getResourceDirectory(artifact);
Assert.assertFalse(Files.exists(p));
} finally {
Thread.currentThread().setContextClassLoader(null);
}
Expand Down

0 comments on commit ed28bc7

Please sign in to comment.