Skip to content

Commit

Permalink
add support for updating cache from local archive
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpoelen committed Mar 20, 2020
1 parent 9b256f0 commit 6548204
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public void run() {

List<DatasetRegistry> registries = new ArrayList<>();
for (String registryName : registryNames) {
DatasetRegistryFactoryImpl datasetRegistryFactory = new DatasetRegistryFactoryImpl(inputStreamFactory);
DatasetRegistryFactoryImpl datasetRegistryFactory
= new DatasetRegistryFactoryImpl(
getWorkDir(), getCacheDir(), inputStreamFactory);
try {
DatasetRegistry registry = datasetRegistryFactory.createRegistryByName(registryName);
registries.add(registry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,30 @@
import org.globalbioticinteractions.dataset.DatasetRegistry;
import org.globalbioticinteractions.dataset.DatasetRegistryGitHubArchive;
import org.globalbioticinteractions.dataset.DatasetRegistryZenodo;
import org.globalbioticinteractions.elton.util.DatasetRegistrySingleDir;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;

public class DatasetRegistryFactoryImpl implements DatasetRegistryFactory {
private static final Map<String, Class<? extends DatasetRegistry>> REGISTRY_LOOKUP = MapUtils.unmodifiableMap(new TreeMap<String, Class<? extends DatasetRegistry>>() {{
put("local", DatasetRegistrySingleDir.class);
put("zenodo", DatasetRegistryZenodo.class);
put("github", DatasetRegistryGitHubArchive.class);
}});

private final InputStreamFactory inputStreamFactory;
private final URI workDir;
private final String cacheDir;

public DatasetRegistryFactoryImpl(InputStreamFactory inputStreamFactory) {
public DatasetRegistryFactoryImpl(URI workDir, String cacheDir, InputStreamFactory inputStreamFactory) {
this.workDir = workDir;
this.cacheDir = cacheDir;
this.inputStreamFactory = inputStreamFactory;
}

Expand All @@ -32,14 +40,40 @@ public DatasetRegistry createRegistryByName(String name) throws DatasetFinderExc
throw new DatasetFinderException("failed to create registry for [" + name + "]: not supported");
}
try {
Constructor<? extends DatasetRegistry> constructor = registryClass.getConstructor(InputStreamFactory.class);
return constructor.newInstance(inputStreamFactory);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
Class<?>[] paramTypes = {URI.class, String.class, InputStreamFactory.class};
Optional<Constructor<? extends DatasetRegistry>> constructor = constructorFor(registryClass, paramTypes);
if (!constructor.isPresent()) {
Class<?>[] paramTypesShort = {InputStreamFactory.class};
Optional<Constructor<? extends DatasetRegistry>> constructor2 = constructorFor(registryClass, paramTypesShort);
return constructor2
.orElseThrow(() -> new DatasetFinderException("failed to create registry for [" + name + "]")
).newInstance(inputStreamFactory);
} else {
return constructor.get().newInstance(getWorkDir(), getCacheDir(), inputStreamFactory);
}
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new DatasetFinderException("failed to create registry for [" + name + "]", e);
}
}

private Optional<Constructor<? extends DatasetRegistry>> constructorFor(Class<? extends DatasetRegistry> registryClass, Class<?>[] paramTypes) {
try {
Constructor<? extends DatasetRegistry> constructor = registryClass.getConstructor(paramTypes);
return Optional.of(constructor);
} catch(NoSuchMethodException ex) {
return Optional.empty();
}
}

public static Set<String> getSupportedRegistries() {
return REGISTRY_LOOKUP.keySet();
}

public URI getWorkDir() {
return workDir;
}

public String getCacheDir() {
return cacheDir;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.globalbioticinteractions.elton.util;

import org.eol.globi.util.InputStreamFactory;
import org.globalbioticinteractions.cache.CachePullThrough;
import org.globalbioticinteractions.dataset.Dataset;
import org.globalbioticinteractions.dataset.DatasetFinderException;
import org.globalbioticinteractions.dataset.DatasetImpl;
import org.globalbioticinteractions.dataset.DatasetRegistry;
import org.globalbioticinteractions.dataset.DatasetWithCache;

import java.net.URI;
import java.util.Collection;
import java.util.Collections;

public class DatasetRegistrySingleDir implements DatasetRegistry {
private final URI localArchiveDir;
private final InputStreamFactory streamFactory;
private final String cacheDir;

public DatasetRegistrySingleDir(URI localArchiveDir, String cacheDir, InputStreamFactory streamFactory) {
this.localArchiveDir = localArchiveDir;
this.cacheDir = cacheDir;
this.streamFactory = streamFactory;
}

@Override
public Collection<String> findNamespaces() throws DatasetFinderException {
return Collections.singletonList(DatasetRegistryUtil.NAMESPACE_LOCAL);
}

@Override
public Dataset datasetFor(String namespace) throws DatasetFinderException {
DatasetImpl local = new DatasetImpl(DatasetRegistryUtil.NAMESPACE_LOCAL, localArchiveDir, streamFactory);

return new DatasetWithCache(local,
new CachePullThrough(DatasetRegistryUtil.NAMESPACE_LOCAL, cacheDir, streamFactory));
}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,21 @@
package org.globalbioticinteractions.elton.util;

import org.globalbioticinteractions.dataset.Dataset;
import org.globalbioticinteractions.dataset.DatasetFinderException;
import org.globalbioticinteractions.dataset.DatasetImpl;
import org.globalbioticinteractions.dataset.DatasetRegistry;
import org.eol.globi.util.InputStreamFactory;
import org.globalbioticinteractions.cache.CacheFactory;
import org.globalbioticinteractions.cache.CacheLocalReadonly;
import org.globalbioticinteractions.cache.CachePullThrough;
import org.globalbioticinteractions.dataset.DatasetRegistryLocal;
import org.globalbioticinteractions.dataset.DatasetWithCache;

import java.net.URI;
import java.util.Collection;
import java.util.Collections;

public class DatasetRegistryUtil {

public static final String NAMESPACE_LOCAL = "local";

public static DatasetRegistry forLocalDir(final URI localDir, final String localCacheDir, InputStreamFactory streamFactory) {
return new DatasetRegistry() {
private final String localStaticNamespace = NAMESPACE_LOCAL;

@Override
public Collection<String> findNamespaces() throws DatasetFinderException {
return Collections.singletonList(localStaticNamespace);
}

@Override
public Dataset datasetFor(String namespace) throws DatasetFinderException {
DatasetImpl local = new DatasetImpl(NAMESPACE_LOCAL, localDir, streamFactory);

return new DatasetWithCache(local,
new CachePullThrough(NAMESPACE_LOCAL, localCacheDir, streamFactory));
}
};
public static DatasetRegistry forLocalDir(final URI localArchiveDir, final String cacheDir, InputStreamFactory streamFactory) {
return new DatasetRegistrySingleDir(localArchiveDir, cacheDir, streamFactory);
}

private static CacheFactory getCacheFactoryLocal(String cacheDir, InputStreamFactory streamFactory) {
Expand Down Expand Up @@ -65,4 +45,5 @@ public static DatasetRegistry forCacheDirOrLocalDir(String cacheDir, URI workDir
}
return registry;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.globalbioticinteractions.elton.cmd;

import org.globalbioticinteractions.dataset.DatasetFinderException;
import org.globalbioticinteractions.dataset.DatasetRegistry;
import org.hamcrest.core.Is;
import org.junit.Test;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static org.hamcrest.MatcherAssert.assertThat;

public class DatasetRegistryFactoryImplTest {

@Test
public void list() throws DatasetFinderException {
List<DatasetRegistry> registries = new ArrayList<>();
Set<String> supportedRegistries = DatasetRegistryFactoryImpl.getSupportedRegistries();
for (String supportedRegistry : supportedRegistries) {
DatasetRegistry registry = new DatasetRegistryFactoryImpl(
URI.create("some:uri"), "someCacheDir", in -> in)
.createRegistryByName(supportedRegistry);
registries.add(registry);
}
assertThat(registries.size(), Is.is(supportedRegistries.size()));
}

}

0 comments on commit 6548204

Please sign in to comment.