You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2003/05/12 16:01:32 UTC
cvs commit: db-ojb/src/java/org/apache/ojb/broker Identity.java
arminw 2003/05/12 07:01:32
Modified: src/java/org/apache/ojb/broker/cache
ObjectCacheDefaultImpl.java
ObjectCachePerBrokerImpl.java
src/java/org/apache/ojb/broker Identity.java
Log:
improvements by Lance Eason:
3) ObjectCacheDefaultImpl and ObjectCachePerBrokerImpl both call toString()
on the Identity passed in and use that as the key to their internal maps.
In addition Identity calls toString() on itself to calculate it's hashCode.
Converting the Identity objects to Strings turns out to be much more
costly than simply comparing their contents.
Compounding matters is the fact that a lot of Identity objects get
constructed solely to do cache lookups and then are immediately
discarded but they get unnecessarily converted to String in the
process at extra cost in processing and object creation.
I changed the two caches to use the Identity object itself
as the key and changed Identity.hashCode() to calculate the
hashCode directly rather than converting to a String.
Revision Changes Path
1.12 +5 -5 db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheDefaultImpl.java
Index: ObjectCacheDefaultImpl.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheDefaultImpl.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- ObjectCacheDefaultImpl.java 13 Mar 2003 19:50:27 -0000 1.11
+++ ObjectCacheDefaultImpl.java 12 May 2003 14:01:32 -0000 1.12
@@ -120,7 +120,7 @@
if ((obj != null))
{
SoftReference ref = new SoftReference(obj);
- objectTable.put(oid.toString(), ref);
+ objectTable.put(oid, ref);
}
}
@@ -132,14 +132,14 @@
{
hitCount++;
Object obj = null;
- SoftReference ref = (SoftReference) objectTable.get(oid.toString());
+ SoftReference ref = (SoftReference) objectTable.get(oid);
if (ref != null)
{
obj = ref.get();
if (obj == null)
{
gcCount++;
- objectTable.remove(oid.toString()); // Soft-referenced Object reclaimed by GC
+ objectTable.remove(oid); // Soft-referenced Object reclaimed by GC
}
}
else
@@ -156,7 +156,7 @@
{
if (oid != null)
{
- objectTable.remove(oid.toString());
+ objectTable.remove(oid);
}
}
1.6 +6 -4 db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCachePerBrokerImpl.java
Index: ObjectCachePerBrokerImpl.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCachePerBrokerImpl.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ObjectCachePerBrokerImpl.java 12 Apr 2003 23:24:41 -0000 1.5
+++ ObjectCachePerBrokerImpl.java 12 May 2003 14:01:32 -0000 1.6
@@ -106,7 +106,7 @@
if ((obj != null))
{
SoftReference ref = new SoftReference(obj);
- objectTable.put(oid.toString(), ref);
+ objectTable.put(oid, ref);
}
}
@@ -117,13 +117,13 @@
public Object lookup(Identity oid)
{
Object obj = null;
- SoftReference ref = (SoftReference) objectTable.get(oid.toString());
+ SoftReference ref = (SoftReference) objectTable.get(oid);
if (ref != null)
{
obj = ref.get();
if (obj == null)
{
- objectTable.remove(oid.toString()); // Soft-referenced Object reclaimed by GC
+ objectTable.remove(oid); // Soft-referenced Object reclaimed by GC
}
}
return obj;
@@ -136,7 +136,7 @@
{
if (oid != null)
{
- objectTable.remove(oid.toString());
+ objectTable.remove(oid);
}
}
@@ -174,5 +174,7 @@
public void afterRollback(PBStateEvent event)
{
+ // clear to be in sync with DB
+ clear();
}
}
1.17 +22 -8 db-ojb/src/java/org/apache/ojb/broker/Identity.java
Index: Identity.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/Identity.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- Identity.java 2 Mar 2003 09:11:58 -0000 1.16
+++ Identity.java 12 May 2003 14:01:32 -0000 1.17
@@ -101,7 +101,8 @@
* The ordered list of primary key values maintaining the objects identity in the underlying RDBMS
*/
private Object[] pkValues;
- private transient String stringRepresentation = null;
+ private String stringRepresentation = null;
+ private Integer hashCode;
/**
* empty constructor is private.
@@ -192,9 +193,9 @@
// identities must be unique accross extents !
this.objectsClass = targetBroker.getTopLevelClass(objectToIdentitify.getClass());
this.objectsRealClass = objectToIdentitify.getClass();
-
+
// BRJ: definitely do NOT convertToSql
- // conversion is done when binding the sql-statement
+ // conversion is done when binding the sql-statement
this.pkValues = targetBroker.serviceBrokerHelper().getKeyValues(cld, objectToIdentitify, false);
}
@@ -351,9 +352,9 @@
result = this.getObjectsClass().equals(id.getObjectsClass());
result = result && (pkValues.length == otherPkValues.length);
for (int i = 0; result && i < pkValues.length; i++)
- {
- result = (pkValues[i] == null) ? (otherPkValues[i] == null)
- : pkValues[i].equals(otherPkValues[i]);
+ {
+ result = (pkValues[i] == null) ? (otherPkValues[i] == null)
+ : pkValues[i].equals(otherPkValues[i]);
}
}
else
@@ -373,6 +374,19 @@
*/
public int hashCode()
{
- return toString().hashCode();
+ // arminw:
+ // identity is quasi immutable, thus
+ // we could note hashCode
+ if(hashCode == null)
+ {
+ int result = this.getObjectsClass().hashCode();
+
+ for (int i = 0; i < pkValues.length; i++)
+ {
+ if (pkValues[i] != null) result ^= pkValues[i].hashCode();
+ }
+ hashCode = new Integer(result);
+ }
+ return hashCode.intValue();
}
}