From 6236eba83e8f81778bdb7cba6d534470ffd407bc Mon Sep 17 00:00:00 2001 From: hns Date: Thu, 3 Jul 2003 13:42:41 +0000 Subject: [PATCH] Implement readObject() and writeObject() on some key classes to make sure storageNames are properly internalized. --- src/helma/objectmodel/db/DbKey.java | 29 ++++++++++++++++++++-- src/helma/objectmodel/db/Key.java | 8 +++--- src/helma/objectmodel/db/MultiKey.java | 29 ++++++++++++++++++++-- src/helma/objectmodel/db/SyntheticKey.java | 6 +++++ 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/helma/objectmodel/db/DbKey.java b/src/helma/objectmodel/db/DbKey.java index bf7bb5a3..b8d7ddd1 100644 --- a/src/helma/objectmodel/db/DbKey.java +++ b/src/helma/objectmodel/db/DbKey.java @@ -17,6 +17,10 @@ package helma.objectmodel.db; import java.io.Serializable; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.IOException; + /** * This is the internal representation of a database key. It is constructed @@ -27,14 +31,16 @@ public final class DbKey implements Key, Serializable { // the name of the prototype which defines the storage of this object. // this is the name of the object's prototype, or one of its ancestors. // If null, the object is stored in the embedded db. - private final String storageName; + private String storageName; // the id that defines this key's object within the above storage space - private final String id; + private String id; // lazily initialized hashcode private transient int hashcode = 0; + static final long serialVersionUID = 1618863960930966588L; + /** * make a key for a persistent Object, describing its datasource and id. */ @@ -116,4 +122,23 @@ public final class DbKey implements Key, Serializable { public String toString() { return (storageName == null) ? ("[" + id + "]") : (storageName + "[" + id + "]"); } + + // We implement write/readObject to set storageName + // to the interned version of the string. + + private void writeObject(ObjectOutputStream stream) throws IOException { + stream.writeObject(storageName); + stream.writeObject(id); + } + + private void readObject(ObjectInputStream stream) + throws IOException, ClassNotFoundException { + storageName = (String) stream.readObject(); + id = (String) stream.readObject(); + // if storageName is not null, set it to the interned version + if (storageName != null) { + storageName = storageName.intern(); + } + } + } diff --git a/src/helma/objectmodel/db/Key.java b/src/helma/objectmodel/db/Key.java index 850450eb..185eadf4 100644 --- a/src/helma/objectmodel/db/Key.java +++ b/src/helma/objectmodel/db/Key.java @@ -22,24 +22,26 @@ package helma.objectmodel.db; * */ public interface Key { + /** - * + * Get the key's parent key * * @return ... */ public Key getParentKey(); /** - * + * Get the key's ID part * * @return ... */ public String getID(); /** - * + * Get the key's storage id * * @return ... */ public String getStorageName(); + } diff --git a/src/helma/objectmodel/db/MultiKey.java b/src/helma/objectmodel/db/MultiKey.java index 5e72f03e..aece2c58 100644 --- a/src/helma/objectmodel/db/MultiKey.java +++ b/src/helma/objectmodel/db/MultiKey.java @@ -17,6 +17,9 @@ package helma.objectmodel.db; import java.io.Serializable; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.IOException; import java.util.Map; /** @@ -31,14 +34,17 @@ public final class MultiKey implements Key, Serializable { // the name of the prototype which defines the storage of this object. // this is the name of the object's prototype, or one of its ancestors. // If null, the object is stored in the embedded db. - private final String storageName; + private String storageName; // the id that defines this key's object within the above storage space - private final Map parts; + private Map parts; // lazily initialized hashcode private transient int hashcode = 0; + static final long serialVersionUID = -9173409137561990089L; + + /** * make a key for a persistent Object, describing its datasource and key parts. */ @@ -121,4 +127,23 @@ public final class MultiKey implements Key, Serializable { public String toString() { return (storageName == null) ? ("[" + parts + "]") : (storageName + "[" + parts + "]"); } + + // We implement write/readObject to set storageName + // to the interned version of the string. + + private void writeObject(ObjectOutputStream stream) throws IOException { + stream.writeObject(storageName); + stream.writeObject(parts); + } + + private void readObject(ObjectInputStream stream) + throws IOException, ClassNotFoundException { + storageName = (String) stream.readObject(); + parts = (Map) stream.readObject(); + // if storageName is not null, set it to the interned version + if (storageName != null) { + storageName = storageName.intern(); + } + } + } diff --git a/src/helma/objectmodel/db/SyntheticKey.java b/src/helma/objectmodel/db/SyntheticKey.java index a8c88c3f..7704e6dd 100644 --- a/src/helma/objectmodel/db/SyntheticKey.java +++ b/src/helma/objectmodel/db/SyntheticKey.java @@ -25,12 +25,18 @@ import java.io.Serializable; * virtual nodes and groupby nodes. */ public final class SyntheticKey implements Key, Serializable { + + // the parent key private final Key parentKey; + + // the name relative to the parent key private final String name; // lazily initialized hashcode private transient int hashcode = 0; + static final long serialVersionUID = -693454133259421857L; + /** * make a key for a persistent Object, describing its datasource and id. */