Rewrote parent fetching for relational DBs
This commit is contained in:
parent
a16f2133c7
commit
5dab540dce
2 changed files with 54 additions and 33 deletions
|
@ -25,10 +25,10 @@ public class DbMapping {
|
||||||
DbSource source;
|
DbSource source;
|
||||||
String table;
|
String table;
|
||||||
|
|
||||||
DbMapping parent;
|
String[] parent; // list of properties to try for parent
|
||||||
|
Boolean[] anonymous; // are parent relations anonymous or not?
|
||||||
DbMapping subnodes;
|
DbMapping subnodes;
|
||||||
DbMapping properties;
|
DbMapping properties;
|
||||||
private Relation parentRel;
|
|
||||||
private Relation subnodesRel;
|
private Relation subnodesRel;
|
||||||
private Relation propertiesRel;
|
private Relation propertiesRel;
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ public class DbMapping {
|
||||||
/**
|
/**
|
||||||
* Read the mapping from the Properties. Return true if the properties were changed.
|
* Read the mapping from the Properties. Return true if the properties were changed.
|
||||||
*/
|
*/
|
||||||
public boolean read () {
|
public synchronized boolean read () {
|
||||||
|
|
||||||
long lastmod = props.lastModified ();
|
long lastmod = props.lastModified ();
|
||||||
if (lastmod == lastTypeChange)
|
if (lastmod == lastTypeChange)
|
||||||
|
@ -98,7 +98,7 @@ public class DbMapping {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void rewire () {
|
public synchronized void rewire () {
|
||||||
|
|
||||||
// if (table != null && source != null) {
|
// if (table != null && source != null) {
|
||||||
// IServer.getLogger().log ("set data source for "+typename+" to "+source);
|
// IServer.getLogger().log ("set data source for "+typename+" to "+source);
|
||||||
|
@ -116,25 +116,34 @@ public class DbMapping {
|
||||||
d2p.put (rel.localField, rel);
|
d2p.put (rel.localField, rel);
|
||||||
// IServer.getLogger().log ("Mapping "+propName+" -> "+dbField);
|
// IServer.getLogger().log ("Mapping "+propName+" -> "+dbField);
|
||||||
|
|
||||||
} else if ("_id".equalsIgnoreCase (propName)) {
|
|
||||||
idField = props.getProperty (propName);
|
|
||||||
|
|
||||||
} else if ("_name".equalsIgnoreCase (propName)) {
|
|
||||||
nameField = props.getProperty (propName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
prop2db = p2d;
|
prop2db = p2d;
|
||||||
db2prop = d2p;
|
db2prop = d2p;
|
||||||
|
|
||||||
|
idField = props.getProperty ("_id");
|
||||||
|
|
||||||
|
nameField = props.getProperty ("_name");
|
||||||
|
|
||||||
|
String ano = props.getProperty ("_anonymous");
|
||||||
|
if (ano != null) {
|
||||||
|
// comma-separated list of true/false values
|
||||||
|
StringTokenizer st = new StringTokenizer (ano, ",; ");
|
||||||
|
anonymous = new Boolean[st.countTokens()];
|
||||||
|
for (int i=0; i<anonymous.length; i++)
|
||||||
|
anonymous[i] = "false".equalsIgnoreCase (st.nextToken().trim()) ? Boolean.FALSE : Boolean.TRUE;
|
||||||
|
} else
|
||||||
|
anonymous = null;
|
||||||
|
|
||||||
String parentMapping = props.getProperty ("_parent");
|
String parentMapping = props.getProperty ("_parent");
|
||||||
if (parentMapping != null) {
|
if (parentMapping != null) {
|
||||||
parentRel = new Relation (parentMapping, "_parent", this, props);
|
// comma-separated list of properties to be used as parent
|
||||||
if (parentRel.isReference ())
|
StringTokenizer st = new StringTokenizer (parentMapping, ",; ");
|
||||||
parent = parentRel.other;
|
parent = new String[st.countTokens()];
|
||||||
else
|
for (int i=0; i<parent.length; i++)
|
||||||
parent = (DbMapping) app.getDbMapping (parentMapping);
|
parent[i] = st.nextToken().trim();
|
||||||
} else
|
} else
|
||||||
parentRel = null;
|
parent = null;
|
||||||
|
|
||||||
String subnodeMapping = props.getProperty ("_subnodes");
|
String subnodeMapping = props.getProperty ("_subnodes");
|
||||||
if (subnodeMapping != null) {
|
if (subnodeMapping != null) {
|
||||||
|
@ -222,10 +231,14 @@ public class DbMapping {
|
||||||
return (Relation) prop2db.get (propName);
|
return (Relation) prop2db.get (propName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DbMapping getParentMapping () {
|
public synchronized String[] getParentPropNames () {
|
||||||
return parent;
|
return parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized Boolean[] getAnonymous () {
|
||||||
|
return anonymous;
|
||||||
|
}
|
||||||
|
|
||||||
public DbMapping getSubnodeMapping () {
|
public DbMapping getSubnodeMapping () {
|
||||||
return subnodes;
|
return subnodes;
|
||||||
}
|
}
|
||||||
|
@ -282,6 +295,7 @@ public class DbMapping {
|
||||||
return rel != null ? rel : propertiesRel;
|
return rel != null ? rel : propertiesRel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String getIDgen () {
|
public String getIDgen () {
|
||||||
return idgen;
|
return idgen;
|
||||||
}
|
}
|
||||||
|
@ -299,7 +313,7 @@ public class DbMapping {
|
||||||
/**
|
/**
|
||||||
* Return a Village Schema object for this DbMapping.
|
* Return a Village Schema object for this DbMapping.
|
||||||
*/
|
*/
|
||||||
public Schema getSchema () throws ClassNotFoundException, SQLException, DataSetException {
|
public synchronized Schema getSchema () throws ClassNotFoundException, SQLException, DataSetException {
|
||||||
if (!isRelational ())
|
if (!isRelational ())
|
||||||
throw new SQLException ("Can't get Schema for non-relational data mapping");
|
throw new SQLException ("Can't get Schema for non-relational data mapping");
|
||||||
// Use local variable s to avoid synchronization (schema may be nulled elsewhere)
|
// Use local variable s to avoid synchronization (schema may be nulled elsewhere)
|
||||||
|
@ -313,7 +327,7 @@ public class DbMapping {
|
||||||
/**
|
/**
|
||||||
* Return a Village Schema object for this DbMapping.
|
* Return a Village Schema object for this DbMapping.
|
||||||
*/
|
*/
|
||||||
public KeyDef getKeyDef () {
|
public synchronized KeyDef getKeyDef () {
|
||||||
if (!isRelational ())
|
if (!isRelational ())
|
||||||
throw new RuntimeException ("Can't get KeyDef for non-relational data mapping");
|
throw new RuntimeException ("Can't get KeyDef for non-relational data mapping");
|
||||||
// Use local variable s to avoid synchronization (keydef may be nulled elsewhere)
|
// Use local variable s to avoid synchronization (keydef may be nulled elsewhere)
|
||||||
|
|
|
@ -546,24 +546,31 @@ public class Node implements INode, Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Key getParentKey () {
|
|
||||||
if (parentID == null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
DbMapping pm = parentmap;
|
|
||||||
if (pm == null && dbmap != null)
|
|
||||||
pm = dbmap.getParentMapping ();
|
|
||||||
return Key.makeKey (pm, parentID);
|
|
||||||
}
|
|
||||||
|
|
||||||
public INode getParent () {
|
public INode getParent () {
|
||||||
|
|
||||||
|
// check what's specified in the type.properties for this node.
|
||||||
|
String[] parentProps = null;
|
||||||
|
if (dbmap != null && dbmap.isRelational ())
|
||||||
|
parentProps = dbmap.getParentPropNames ();
|
||||||
|
|
||||||
|
// check if current parent candidate matches presciption, if not, try to get it
|
||||||
|
if (parentProps != null) {
|
||||||
|
for (int i=0; i<parentProps.length; i++) {
|
||||||
|
INode pn = getNode (parentProps[i], false);
|
||||||
|
if (pn != null) {
|
||||||
|
// see if dbmapping specifies anonymity for this node
|
||||||
|
Boolean[] ano = dbmap.getAnonymous ();
|
||||||
|
if (ano != null && ano.length > i)
|
||||||
|
anonymous = ano[i].booleanValue();
|
||||||
|
return pn;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fall back to heuristic parent (the node that fetched this one from db)
|
||||||
if (parentID == null)
|
if (parentID == null)
|
||||||
return null;
|
return null;
|
||||||
|
return nmgr.getNode (parentID, parentmap);
|
||||||
DbMapping pm = parentmap;
|
|
||||||
if (pm == null && dbmap != null)
|
|
||||||
pm = dbmap.getParentMapping ();
|
|
||||||
return nmgr.getNode (parentID, pm);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue