Key is now an interface with two implementing classes:

DbKey for primary database keys, and SyntheticKey (might be
renamed to "DerivedKey" for virtual nodes, groupby nodes, and
nodes accessed via a property name instead of the primary key.
SyntheticKey consist of a parent key and a (property) name to
derive the object from the parent object.
This commit is contained in:
hns 2001-08-03 14:44:00 +00:00
parent 2b4dec4e34
commit 33c9f4cc8c
3 changed files with 243 additions and 74 deletions

View file

@ -0,0 +1,119 @@
// DbKey.java
// Copyright (c) Hannes Wallnöfer 1998-2000
package helma.objectmodel;
import java.io.Serializable;
/**
* This is the internal representation of a database key. It is constructed
* out of the database URL, the table name, the user name and the database
* key of the node and unique within each Helma application. Currently only
* single keys are supported.
*/
public final class DbKey implements Key, Serializable {
private final String storageName;
private final String id;
private final int hash;
/**
* make a key for a persistent Object, describing its datasource and id.
*/
public DbKey (DbMapping dbmap, String id) {
this.id = id;
this.storageName = dbmap == null ? null : dbmap.getStorageTypeName ();
hash = id.hashCode ();
}
public boolean equals (Object what) {
if (what == this)
return true;
try {
DbKey k = (DbKey) what;
return (storageName == k.storageName || storageName.equals (k.storageName)) &&
(id == k.id || id.equals (k.id));
} catch (Exception x) {
return false;
}
}
public int hashCode () {
return hash;
}
public Key getParentKey () {
return null;
}
public String getStorageName () {
return storageName;
}
public String getID () {
return id;
}
public String toString () {
return storageName+"["+id+"]";
}
}

View file

@ -4,86 +4,18 @@
package helma.objectmodel;
import helma.util.CacheMap;
import java.io.Serializable;
/**
* This is the internal representation of a database key. It is constructed
* out of the database URL, the table name, the user name and the database
* key of the node and unique within each Helma application. Currently only
* single keys are supported.
* This is the interface for the internal representation of an object key.
*
*/
public class Key implements Serializable {
private String type;
private String id;
private int hash;
private boolean virtual;
public interface Key {
public Key (DbMapping dbmap, String id) {
this.type = dbmap == null ? null : dbmap.getStorageTypeName ();
this.id = id;
hash = id.hashCode ();
virtual = false;
}
public Key getParentKey ();
public Key (String type, String id) {
this.type = type;
this.id = id;
hash = id.hashCode ();
virtual = false;
}
public boolean equals (Object what) {
try {
Key k = (Key) what;
return (type == k.type || type.equals (k.type)) && (id == k.id || id.equals (k.id));
} catch (Exception x) {
return false;
}
}
public int hashCode () {
return hash;
}
/**
* Get the Key for a virtual node contained by this node, that is, a node that does
* not represent a record in the database. The main objective here is to generate
* a key that can't be mistaken for a relational db key.
*/
public Key getVirtualKey (String sid) {
Key k = new Key ((String) null, makeVirtualID (type, id, sid));
k.virtual = true;
return k;
}
public String getVirtualID (String sid) {
return makeVirtualID (type, id, sid);
}
public static String makeVirtualID (DbMapping pmap, String pid, String sid) {
return makeVirtualID (pmap == null ? (String) null : pmap.typename, pid, sid);
}
public static String makeVirtualID (String ptype, String pid, String sid) {
return ptype+"/"+pid + "~" + sid;
}
public String getType () {
return type;
}
public String getID () {
return id;
}
public String toString () {
return type+"["+id+"]";
}
public String getID ();
public String getStorageName ();
}

View file

@ -0,0 +1,118 @@
// SyntheticKey.java
// Copyright (c) Hannes Wallnöfer 1998-2000
package helma.objectmodel;
import java.io.Serializable;
/**
* This is the internal key for an object that is not stored in a db, but generated
* on the fly. Currently there are two kinds of such objects: virtual nodes, which are used
* as utility containers for objects in the database, and groupby nodes, which are used
* to group a certain kind of relational objects according to some property.
*/
public final class SyntheticKey implements Key, Serializable {
private final Key parentKey;
private final String name;
private final int hash;
/**
* make a key for a persistent Object, describing its datasource and id.
*/
public SyntheticKey (Key key, String name) {
this.parentKey = key;
this.name = name;
hash = name.hashCode () + key.hashCode ();
}
public boolean equals (Object what) {
try {
SyntheticKey k = (SyntheticKey) what;
return parentKey.equals (k.parentKey) &&
(name == k.name || name.equals (k.name));
} catch (Exception x) {
System.err.println ("SYNTHETIC NOT EQUAL: "+what+" - "+this);
return false;
}
}
public int hashCode () {
return hash;
}
public Key getParentKey () {
return parentKey;
}
public String getID () {
return name;
}
public String getStorageName () {
return null;
}
public String toString () {
return parentKey+"/"+name;
}
}