Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api] Allows to use relative jar uri for cache folder name #3026

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading