Skip to content

Commit

Permalink
Fix GeoGig plugin to align with GeoGig core changes
Browse files Browse the repository at this point in the history
 - Fix GeoGig repository name on Init with PG backend
 - Add geogig profile to web/app/pom.xml
 - Let RepositoryManager cache RepositoryInfo instances
 - Simplified RepositoryInfo.getRepoName()
 - It simply delegates to RepositoryResolver.getName(URI),
   the logic to resolve the name has been moved to geogig.
 - Make sure RepositoryCache returns an open repository if it exists
 - Follow up from upstream: upgrade web api to use Repository instead of GeoGIG
 - Don't let DataStore.dispose() close the geogig repository.
 - Repository instances are cached as it can be resource
   intensive to continuously create and destroy them.
  • Loading branch information
Erik Merkle committed Aug 3, 2016
1 parent cb07d63 commit 8050c9b
Show file tree
Hide file tree
Showing 15 changed files with 350 additions and 113 deletions.
Expand Up @@ -21,6 +21,8 @@
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Level; import java.util.logging.Level;
Expand Down Expand Up @@ -73,6 +75,22 @@ public class ConfigStore {


private final ReadWriteLock lock; private final ReadWriteLock lock;


private static class CachedInfo {
final RepositoryInfo info;

final long lastModified;

CachedInfo(RepositoryInfo info, long lastModified) {
this.info = info;
this.lastModified = lastModified;
}
}

/**
* Map of cached {@link RepositoryInfo} instances key'ed by id
*/
private ConcurrentMap<String, CachedInfo> cache = new ConcurrentHashMap<>();

public ConfigStore(ResourceStore resourceLoader) { public ConfigStore(ResourceStore resourceLoader) {
checkNotNull(resourceLoader, "resourceLoader"); checkNotNull(resourceLoader, "resourceLoader");
this.resourceLoader = resourceLoader; this.resourceLoader = resourceLoader;
Expand All @@ -97,8 +115,11 @@ public RepositoryInfo save(RepositoryInfo info) {
checkNotNull(info.getLocation(), "null location URI: %s", info); checkNotNull(info.getLocation(), "null location URI: %s", info);


lock.writeLock().lock(); lock.writeLock().lock();
try (OutputStream out = resource(info.getId()).out()) { Resource resource = resource(info.getId());
try (OutputStream out = resource.out()) {
getConfigredXstream().toXML(info, new OutputStreamWriter(out, Charsets.UTF_8)); getConfigredXstream().toXML(info, new OutputStreamWriter(out, Charsets.UTF_8));
long lastmodified = resource.lastmodified();
cache.put(info.getId(), new CachedInfo(info, lastmodified));
} catch (IOException e) { } catch (IOException e) {
throw Throwables.propagate(e); throw Throwables.propagate(e);
} finally { } finally {
Expand All @@ -112,6 +133,7 @@ public boolean delete(final String id) {
checkIdFormat(id); checkIdFormat(id);
lock.writeLock().lock(); lock.writeLock().lock();
try { try {
cache.remove(id);
return resource(id).delete(); return resource(id).delete();
} finally { } finally {
lock.writeLock().unlock(); lock.writeLock().unlock();
Expand Down Expand Up @@ -216,13 +238,22 @@ public List<WhitelistRule> saveWhitelist(List<WhitelistRule> whitelist) {
* Loads a {@link RepositoryInfo} by {@link RepositoryInfo#getId() id} from its xml file under * Loads a {@link RepositoryInfo} by {@link RepositoryInfo#getId() id} from its xml file under
* {@code <data-dir>/geogig/config/repos/} * {@code <data-dir>/geogig/config/repos/}
*/ */
public RepositoryInfo load(final String id) throws IOException { public RepositoryInfo get(final String id) throws IOException {
checkNotNull(id, "provided a null id"); checkNotNull(id, "provided a null id");
checkIdFormat(id); checkIdFormat(id);
lock.readLock().lock(); lock.readLock().lock();
try { try {
CachedInfo cached = cache.get(id);
Resource resource = resource(id); Resource resource = resource(id);
return load(resource); final long lastmodified = resource.lastmodified();
RepositoryInfo info;
if (cached == null || cached.lastModified < lastmodified) {
info = load(resource);
cache.put(id, new CachedInfo(info, lastmodified));
} else {
info = cached.info;
}
return info;
} finally { } finally {
lock.readLock().unlock(); lock.readLock().unlock();
} }
Expand Down Expand Up @@ -262,12 +293,19 @@ public boolean apply(Resource input) {
} }
}; };


private static final Function<Resource, RepositoryInfo> LOADER = new Function<Resource, RepositoryInfo>() { private final Function<Resource, RepositoryInfo> LOADER = new Function<Resource, RepositoryInfo>() {


@Override @Override
public RepositoryInfo apply(Resource input) { public RepositoryInfo apply(final Resource resource) {
try { try {
return load(input); RepositoryInfo loaded = load(resource);
CachedInfo cached = cache.get(loaded.getId());
if (cached == null) {
long lastModified = resource.lastmodified();
cached = new CachedInfo(loaded, lastModified);
cache.put(loaded.getId(), cached);
}
return cached.info;
} catch (IOException e) { } catch (IOException e) {
LOGGER.log(Level.WARNING, "Error loading RepositoryInfo", e); LOGGER.log(Level.WARNING, "Error loading RepositoryInfo", e);
return null; return null;
Expand Down
Expand Up @@ -4,17 +4,18 @@
*/ */
package org.geogig.geoserver.config; package org.geogig.geoserver.config;


import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;

import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;


import org.locationtech.geogig.repository.Context; import org.locationtech.geogig.repository.Context;
import org.locationtech.geogig.repository.GeoGIG;
import org.locationtech.geogig.repository.Repository; import org.locationtech.geogig.repository.Repository;
import org.locationtech.geogig.repository.RepositoryConnectionException; import org.locationtech.geogig.repository.RepositoryConnectionException;
import org.locationtech.geogig.repository.RepositoryResolver; import org.locationtech.geogig.repository.RepositoryResolver;
import org.locationtech.geogig.storage.ConfigDatabase; import org.locationtech.geogig.storage.ConfigDatabase;


import com.google.common.base.Preconditions;
import com.google.common.base.Strings; import com.google.common.base.Strings;


/** /**
Expand All @@ -23,6 +24,7 @@
public class GeoServerGeoGigRepositoryResolver extends RepositoryResolver { public class GeoServerGeoGigRepositoryResolver extends RepositoryResolver {


public static final String GEOSERVER_URI_SCHEME = "geoserver"; public static final String GEOSERVER_URI_SCHEME = "geoserver";

public static final int SCHEME_LENGTH = GEOSERVER_URI_SCHEME.length() + "://".length(); public static final int SCHEME_LENGTH = GEOSERVER_URI_SCHEME.length() + "://".length();


public static String getURI(String repoName) { public static String getURI(String repoName) {
Expand Down Expand Up @@ -52,14 +54,12 @@ public boolean repoExists(URI repoURI) throws IllegalArgumentException {


@Override @Override
public String getName(URI repoURI) { public String getName(URI repoURI) {
Preconditions.checkArgument(canHandle(repoURI), "Not a GeoServer GeoGig repository URI: %s", checkArgument(canHandle(repoURI), "Not a GeoServer GeoGig repository URI: %s", repoURI);
repoURI);
// valid looking URI, strip the name part out and get everything after the scheme // valid looking URI, strip the name part out and get everything after the scheme
// "geoserver" and the "://" // "geoserver" and the "://"
String name = repoURI.toString().substring(SCHEME_LENGTH); String name = repoURI.toString().substring(SCHEME_LENGTH);
// if it's empty, they didn't provide a name or Id // if it's empty, they didn't provide a name or Id
Preconditions.checkArgument(!Strings.isNullOrEmpty(name), checkArgument(!Strings.isNullOrEmpty(name), "No GeoGig repository Name or ID specified");
"No GeoGig repository Name or ID specified");
return name; return name;
} }


Expand All @@ -82,11 +82,9 @@ public Repository open(URI repositoryLocation) throws RepositoryConnectionExcept
try { try {
RepositoryInfo info = repoMgr.getByRepoName(name); RepositoryInfo info = repoMgr.getByRepoName(name);
String repositoryId = info.getId(); String repositoryId = info.getId();
GeoGIG gig = repoMgr.getRepository(repositoryId); Repository repo = repoMgr.getRepository(repositoryId);
Repository repo = gig.getRepository(); checkState(repo.isOpen(), "RepositoryManager returned a closed repository for %s",
if (!repo.isOpen()) { name);
repo.open();
}
return repo; return repo;
} catch (IOException ioe) { } catch (IOException ioe) {
// didn't find a repo // didn't find a repo
Expand Down

0 comments on commit 8050c9b

Please sign in to comment.