Big cleanups and fixes:

- Groupby-Nodes with prototype now use an internal DbMapping object
with the specified prototype's DbMapping as parent mapping. This is
necessary because the groupby prototype may be used in different places
and with different relational constraints.
- Cleaned up and reduced duplcate code in Getters for Property Relations
and DbMappings. Now everything is based on getExactPropertyRelation(str)
and getPropertyRelation() methods.
- Replaced Hashtables with HashMaps for performance reasons after
noticing that they are accessed quite often within a single request.
This commit is contained in:
hns 2001-12-13 11:51:20 +00:00
parent cda86002f3
commit 15941ce4f4

View file

@ -6,7 +6,7 @@ package helma.objectmodel.db;
import helma.framework.core.Application; import helma.framework.core.Application;
import helma.util.Updatable; import helma.util.Updatable;
import helma.util.SystemProperties; import helma.util.SystemProperties;
import java.util.Hashtable; import java.util.HashMap;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.sql.*; import java.sql.*;
@ -49,9 +49,9 @@ public class DbMapping implements Updatable {
DbMapping groupbyMapping; DbMapping groupbyMapping;
// Map of property names to Relations objects // Map of property names to Relations objects
Hashtable prop2db; HashMap prop2db;
// Map of db columns to Relations objects // Map of db columns to Relations objects
Hashtable db2prop; HashMap db2prop;
// db field used as primary key // db field used as primary key
String idField; String idField;
@ -64,7 +64,6 @@ public class DbMapping implements Updatable {
String extendsProto; String extendsProto;
// dbmapping of parent prototype, if any // dbmapping of parent prototype, if any
DbMapping parentMapping; DbMapping parentMapping;
boolean inheritsMapping;
// db field that specifies the prototype of an object // db field that specifies the prototype of an object
String prototypeField; String prototypeField;
@ -92,8 +91,8 @@ public class DbMapping implements Updatable {
this.app = app; this.app = app;
prop2db = new Hashtable (); prop2db = new HashMap ();
db2prop = new Hashtable (); db2prop = new HashMap ();
parent = null; parent = null;
// subnodes = null; // subnodes = null;
@ -109,8 +108,8 @@ public class DbMapping implements Updatable {
this.app = app; this.app = app;
this.typename = typename; this.typename = typename;
prop2db = new Hashtable (); prop2db = new HashMap ();
db2prop = new Hashtable (); db2prop = new HashMap ();
parent = null; parent = null;
// subnodes = null; // subnodes = null;
@ -199,8 +198,8 @@ public class DbMapping implements Updatable {
// if (table != null && source != null) { // if (table != null && source != null) {
// app.logEvent ("set data source for "+typename+" to "+source); // app.logEvent ("set data source for "+typename+" to "+source);
Hashtable p2d = new Hashtable (); HashMap p2d = new HashMap ();
Hashtable d2p = new Hashtable (); HashMap d2p = new HashMap ();
for (Enumeration e=props.keys(); e.hasMoreElements(); ) { for (Enumeration e=props.keys(); e.hasMoreElements(); ) {
String propName = (String) e.nextElement (); String propName = (String) e.nextElement ();
@ -268,8 +267,7 @@ public class DbMapping implements Updatable {
propertiesRel = null; propertiesRel = null;
if (groupbyMapping != null) { if (groupbyMapping != null) {
groupbyMapping.subnodesRel = subnodesRel == null ? null : subnodesRel.getGroupbySubnodeRelation (); initGroupbyMapping ();
groupbyMapping.propertiesRel = propertiesRel == null ? null : propertiesRel.getGroupbyPropertyRelation ();
groupbyMapping.lastTypeChange = this.lastTypeChange; groupbyMapping.lastTypeChange = this.lastTypeChange;
} }
} }
@ -458,23 +456,12 @@ public class DbMapping implements Updatable {
public DbMapping getExactPropertyMapping (String propname) { public DbMapping getExactPropertyMapping (String propname) {
if (propname == null) Relation rel = getExactPropertyRelation (propname);
return null;
Relation rel = (Relation) prop2db.get (propname.toLowerCase());
if (rel == null && parentMapping != null)
return parentMapping.getExactPropertyMapping (propname);
return rel != null ? rel.otherType : null; return rel != null ? rel.otherType : null;
} }
public DbMapping getPropertyMapping (String propname) { public DbMapping getPropertyMapping (String propname) {
if (propname == null) { Relation rel = getPropertyRelation (propname);
if (propertiesRel != null)
return propertiesRel.otherType;
if (parentMapping != null)
return parentMapping.getPropertyMapping (null);
}
Relation rel = (Relation) prop2db.get (propname.toLowerCase());
if (rel != null) { if (rel != null) {
// if this is a virtual node, it doesn't have a dbmapping // if this is a virtual node, it doesn't have a dbmapping
if (rel.virtual && rel.prototype == null) if (rel.virtual && rel.prototype == null)
@ -482,11 +469,6 @@ public class DbMapping implements Updatable {
else else
return rel.otherType; return rel.otherType;
} }
if (propertiesRel != null)
return propertiesRel.otherType;
if (parentMapping != null)
return parentMapping.getPropertyMapping (propname);
return null; return null;
} }
@ -498,30 +480,37 @@ public class DbMapping implements Updatable {
if (subnodesRel == null || subnodesRel.groupby == null) if (subnodesRel == null || subnodesRel.groupby == null)
return null; return null;
if (groupbyMapping == null) { if (groupbyMapping == null) {
// if a prototype is defined for groupby nodes, use that initGroupbyMapping ();
// DbMapping instead of creating a new one
if (subnodesRel.groupbyprototype != null)
groupbyMapping = app.getDbMapping (subnodesRel.groupbyprototype);
// if mapping doesn' exist or isn't defined, create a new (anonymous internal) one
if (groupbyMapping == null)
groupbyMapping = new DbMapping (app);
groupbyMapping.subnodesRel = subnodesRel.getGroupbySubnodeRelation ();
if (propertiesRel != null)
groupbyMapping.propertiesRel = propertiesRel.getGroupbyPropertyRelation ();
else
groupbyMapping.propertiesRel = subnodesRel.getGroupbyPropertyRelation ();
groupbyMapping.typename = subnodesRel.groupbyprototype;
} }
return groupbyMapping; return groupbyMapping;
} }
/**
* Initialize the dbmapping used for group-by nodes.
*/
private void initGroupbyMapping () {
// if a prototype is defined for groupby nodes, use that
// if mapping doesn' exist or isn't defined, create a new (anonymous internal) one
groupbyMapping = new DbMapping (app);
// If a mapping is defined, make the internal mapping inherit from
// the defined named prototype.
if (subnodesRel.groupbyprototype != null)
groupbyMapping.parentMapping = app.getDbMapping (subnodesRel.groupbyprototype);
groupbyMapping.subnodesRel = subnodesRel.getGroupbySubnodeRelation ();
if (propertiesRel != null)
groupbyMapping.propertiesRel = propertiesRel.getGroupbyPropertyRelation ();
else
groupbyMapping.propertiesRel = subnodesRel.getGroupbyPropertyRelation ();
groupbyMapping.typename = subnodesRel.groupbyprototype;
}
/* public void setPropertyMapping (DbMapping pm) { /* public void setPropertyMapping (DbMapping pm) {
properties = pm; properties = pm;
} */ } */
public void setSubnodeRelation (Relation rel) { /* public void setSubnodeRelation (Relation rel) {
subnodesRel = rel; subnodesRel = rel;
} } */
public void setPropertyRelation (Relation rel) { public void setPropertyRelation (Relation rel) {
propertiesRel = rel; propertiesRel = rel;
@ -542,10 +531,21 @@ public class DbMapping implements Updatable {
public Relation getPropertyRelation (String propname) { public Relation getPropertyRelation (String propname) {
if (propname == null) if (propname == null)
return getPropertyRelation (); return getPropertyRelation ();
// first try finding an exact match for the property name
Relation rel = getExactPropertyRelation (propname);
// if not defined, return the generic property mapping
if (rel == null)
rel = getPropertyRelation ();
return rel;
}
public Relation getExactPropertyRelation (String propname) {
if (propname == null)
return null;
Relation rel = (Relation) prop2db.get (propname.toLowerCase()); Relation rel = (Relation) prop2db.get (propname.toLowerCase());
if (rel == null && propertiesRel == null && parentMapping != null) if (rel == null && parentMapping != null)
return parentMapping.getPropertyRelation (propname); rel = parentMapping.getExactPropertyRelation (propname);
return rel != null ? rel : propertiesRel; return rel;
} }
public String getSubnodeGroupby () { public String getSubnodeGroupby () {
@ -614,7 +614,7 @@ public class DbMapping implements Updatable {
} }
public String toString () { public String toString () {
if (app == null) if (typename == null)
return "[unspecified internal DbMapping]"; return "[unspecified internal DbMapping]";
else else
return ("["+app.getName()+"."+typename+"]"); return ("["+app.getName()+"."+typename+"]");
@ -642,13 +642,13 @@ public class DbMapping implements Updatable {
return lastID; return lastID;
} }
public Hashtable getProp2DB () { public HashMap getProp2DB () {
if (table == null && parentMapping != null) if (table == null && parentMapping != null)
return parentMapping.getProp2DB (); return parentMapping.getProp2DB ();
return prop2db; return prop2db;
} }
public Hashtable getDB2Prop () { public HashMap getDB2Prop () {
if (table == null && parentMapping != null) if (table == null && parentMapping != null)
return parentMapping.getDB2Prop (); return parentMapping.getDB2Prop ();
return db2prop; return db2prop;
@ -694,5 +694,10 @@ public class DbMapping implements Updatable {
return false; return false;
} }
public DbMapping getParentMapping () {
return parentMapping;
}
} }