@@ -0,0 +1,2399 @@
+/* $This file is distributed under the terms of the license in /doc/license.txt$ */%0A%0Apackage edu.cornell.mannlib.vitro.webapp.modelaccess.ontmodels;%0A%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0Aimport java.util.SortedSet;%0Aimport java.util.TreeSet;%0A%0Aimport org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A%0Aimport com.hp.hpl.jena.ontology.OntModel;%0A%0Aimport edu.cornell.mannlib.vitro.webapp.utils.logging.ToString;%0A%0A/**%0A * Use two OntModelCaches as one.%0A * %0A * If both caches contain models with the same name, a warning will be written%0A * to the log, and the model from the primary cache will be used.%0A * %0A * Any new models will be created on the primary cache.%0A */%0Apublic class JoinedOntModelCache implements OntModelCache %7B%0A%09private static final Log log = LogFactory.getLog(JoinedOntModelCache.class);%0A%0A%09private final OntModelCache primary;%0A%09private final OntModelCache secondary;%0A%0A%09public JoinedOntModelCache(OntModelCache primary, OntModelCache secondary) %7B%0A%09%09this.primary = primary;%0A%09%09this.secondary = secondary;%0A%0A// extremely expensive with TDB : see below%0A//%09%09Set%3CString%3E duplicateNames = new HashSet%3CString%3E(primary.getModelNames());%0A//%09%09duplicateNames.retainAll(secondary.getModelNames());%0A//%09%09if (!duplicateNames.isEmpty()) %7B%0A//%09%09%09log.warn(%22These model names appear in both caches: %22%0A//%09%09%09%09%09+ duplicateNames);%0A//%09%09%7D%0A%09%7D%0A%0A%0A    /*%0A%09 * Local mod: Calling getModelNames() under TDB is very expensive%0A%09 */%0A%09@Override%0A%09public OntModel getOntModel(String name) %7B%0A%09    OntModel sec = secondary.getOntModel(name);%0A%09    if(sec.isEmpty()) %7B%0A%09        return primary.getOntModel(name);%0A%09    %7D else %7B%0A%09        OntModel pri = primary.getOntModel(name);%0A%09        if(!pri.isEmpty()) %7B%0A%09            return pri;%0A%09        %7D else %7B%0A%09            return sec;%0A%09        %7D%0A%09    %7D%0A%09%09//if (primary.getModelNames().contains(name)) %7B%0A%09%09//%09return primary.getOntModel(name);%0A%09%09//%7D%0A%09%09//if (secondary.getModelNames().contains(name)) %7B%0A%09%09//%09return secondary.getOntModel(name);%0A%09%09//%7D%0A%09%09//return primary.getOntModel(name);%0A%09%7D%0A%0A%09@Override%0A%09public SortedSet%3CString%3E getModelNames() %7B%0A%09%09SortedSet%3CString%3E allNames = new TreeSet%3CString%3E(primary.getModelNames());%0A%09%09allNames.addAll(secondary.getModelNames());%0A%09%09return allNames;%0A%09%7D%0A%0A%09@Override%0A%09public String toString() %7B%0A%09%09return %22JoinedOntModelCache%5B%22 + ToString.hashHex(this) + %22, primary=%22%0A%09%09%09%09+ primary + %22, secondary=%22 + secondary + %22%5D%22;%0A%09%7D%0A%0A%7D%0A

Change JoinedOntModelCache to avoid expensive queries on every page request