Skip to content

Commit

Permalink
Performance optimizations, reduce number of queries performed on the …
Browse files Browse the repository at this point in the history
…database
  • Loading branch information
aaime committed Oct 4, 2018
1 parent 92f7583 commit 68191c3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.Objects;
import org.geoserver.config.GeoServer;

public class ProductClass implements Serializable {
public class ProductClass implements Serializable, Cloneable {

/** The generic product class */
public static final ProductClass GENERIC =
Expand Down Expand Up @@ -205,4 +205,9 @@ public static List<ProductClass> getProductClasses(OSEOInfo oseo) {
return oseo.getProductClasses();
}
}

@Override
public Object clone() throws CloneNotSupportedException {
return new ProductClass(this.name, this.prefix, this.namespace);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,8 @@ public List<FeatureId> addFeatures(FeatureCollection<FeatureType, Feature> featu
}
}

featuresModified();

return result;
}

Expand All @@ -640,6 +642,8 @@ public void removeFeatures(Filter filter) throws IOException {
// finally drop the collections themselves
SimpleFeatureStore store = getDelegateCollectionStore();
store.removeFeatures(mappedFilter);

featuresModified();
}

/**
Expand Down Expand Up @@ -843,8 +847,16 @@ public void modifyFeatures(Name[] attributeNames, Object[] attributeValues, Filt
Object[] valueArray = (Object[]) localValues.toArray(new Object[localValues.size()]);
getDelegateCollectionStore().modifyFeatures(nameArray, valueArray, mappedFilter);
}

featuresModified();
}

/**
* Hooks for subclasses that need to track feature modification and deletion. By default it does
* nothing.
*/
protected void featuresModified() {}

/**
* Allows subclasses to handle other attributes mapped in secondary tables
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ protected String getLinkForeignKey() {
protected String getThumbnailTable() {
return "collection_thumb";
}

@Override
protected void featuresModified() {
openSearchAccess.clearFeatureSourceCaches();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.geotools.jdbc.JDBCDataStore;
import org.geotools.jdbc.SQLDialect;
import org.geotools.jdbc.VirtualTable;
import org.geotools.util.SoftValueHashMap;
import org.locationtech.jts.geom.Polygon;
import org.opengis.feature.Feature;
import org.opengis.feature.Property;
Expand Down Expand Up @@ -103,6 +104,10 @@ public class JDBCOpenSearchAccess implements OpenSearchAccess {

private GeoServer geoServer;

private LowercasingDataStore delegateStoreCache;
private SoftValueHashMap<Name, SimpleFeatureSource> featureSourceCache =
new SoftValueHashMap<>();

public JDBCOpenSearchAccess(
Repository repository, Name delegateStoreName, String namespaceURI, GeoServer geoServer)
throws IOException {
Expand Down Expand Up @@ -317,8 +322,12 @@ private List<String> getMissingRequiredTables(DataStore delegate, String... tabl
*/
DataStore getDelegateStore() throws IOException {
DataStore store = getRawDelegateStore();
LowercasingDataStore ds = new LowercasingDataStore(store);
return ds;
if (delegateStoreCache != null && delegateStoreCache.wraps(store)) {
return delegateStoreCache;
}
LowercasingDataStore result = new LowercasingDataStore(store);
this.delegateStoreCache = result;
return result;
}

JDBCDataStore getRawDelegateStore() {
Expand Down Expand Up @@ -420,10 +429,13 @@ public FeatureSource<FeatureType, Feature> getFeatureSource(Name typeName) throw
} else if (productFeatureType.getName().equals(typeName)) {
return getProductSource();
}
if (Objects.equal(namespaceURI, typeName.getNamespaceURI())
&& getNames().contains(typeName)) {
// silly generics...
return (FeatureSource) getCollectionGranulesSource(typeName.getLocalPart());
if (Objects.equal(namespaceURI, typeName.getNamespaceURI())) {
SimpleFeatureSource result = featureSourceCache.get(typeName);
if (result == null && getNames().contains(typeName)) {
result = getCollectionGranulesSource(typeName.getLocalPart());
featureSourceCache.put(typeName, result);
}
return (FeatureSource) result;
}

throw new IOException("Schema '" + typeName + "' does not exist.");
Expand Down Expand Up @@ -540,7 +552,7 @@ public SimpleFeatureSource getCollectionGranulesSource(String typeName) throws I
sb.append(" JOIN ");
encodeTableName(dialect, dbSchema, collectionTableName, sb);
sb.append(" as collection ON product.\"eoParentIdentifier\" = collection.\"eoIdentifier\"");
// comparing with false on purpose, allows to defaul to true if primary is null or empty
// comparing with false on purpose, allows to default to true if primary is null or empty
boolean primaryTable = !Boolean.FALSE.equals(collectionFeature.getAttribute("primary"));
if (primaryTable || band != null) {
sb.append(" WHERE ");
Expand Down Expand Up @@ -738,4 +750,8 @@ public java.util.List<org.opengis.filter.identity.FeatureId> addFeatures(
throw new IOException(e);
}
}

void clearFeatureSourceCaches() {
featureSourceCache.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@
*/
class LowercasingDataStore extends RetypingDataStore {

DataStore wrapped;

public LowercasingDataStore(DataStore wrapped) throws IOException {
super(wrapped);
this.wrapped = wrapped;
}

@Override
protected String transformFeatureTypeName(String originalName) {
return originalName.toLowerCase();
}

public boolean wraps(DataStore ds) {
return wrapped == ds;
}
}

0 comments on commit 68191c3

Please sign in to comment.