From 92da97550c27567c860cb1eef203838858a10283 Mon Sep 17 00:00:00 2001 From: hns Date: Wed, 23 Mar 2005 14:57:28 +0000 Subject: [PATCH] Split IDatabase.saveNode() into insertNode() and updateNode(). Fixes bug 418. --- src/helma/objectmodel/IDatabase.java | 15 ++++++-- src/helma/objectmodel/db/NodeManager.java | 4 +-- src/helma/objectmodel/dom/XmlDatabase.java | 42 +++++++++++++++------- 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/helma/objectmodel/IDatabase.java b/src/helma/objectmodel/IDatabase.java index 7691fccf..c09be3f0 100644 --- a/src/helma/objectmodel/IDatabase.java +++ b/src/helma/objectmodel/IDatabase.java @@ -60,14 +60,25 @@ public interface IDatabase { throws IOException, ObjectNotFoundException; /** - * Save a node with the given key + * Insert a node with the given key * * @param transaction * @param key * @param node * @throws IOException */ - public void saveNode(ITransaction transaction, String key, INode node) + public void insertNode(ITransaction transaction, String key, INode node) + throws IOException; + + /** + * Update a node with the given key + * + * @param transaction + * @param key + * @param node + * @throws IOException + */ + public void updateNode(ITransaction transaction, String key, INode node) throws IOException; /** diff --git a/src/helma/objectmodel/db/NodeManager.java b/src/helma/objectmodel/db/NodeManager.java index b64d601c..0bc239d1 100644 --- a/src/helma/objectmodel/db/NodeManager.java +++ b/src/helma/objectmodel/db/NodeManager.java @@ -401,7 +401,7 @@ public final class NodeManager { DbMapping dbm = node.getDbMapping(); if ((dbm == null) || !dbm.isRelational()) { - db.saveNode(txn, node.getID(), node); + db.insertNode(txn, node.getID(), node); } else { insertRelationalNode(node, dbm, dbm.getConnection()); } @@ -534,7 +534,7 @@ public final class NodeManager { boolean markMappingAsUpdated = false; if ((dbm == null) || !dbm.isRelational()) { - db.saveNode(txn, node.getID(), node); + db.updateNode(txn, node.getID(), node); } else { Hashtable propMap = node.getPropMap(); Property[] props; diff --git a/src/helma/objectmodel/dom/XmlDatabase.java b/src/helma/objectmodel/dom/XmlDatabase.java index 34c43dd3..f145bfc7 100644 --- a/src/helma/objectmodel/dom/XmlDatabase.java +++ b/src/helma/objectmodel/dom/XmlDatabase.java @@ -19,9 +19,6 @@ package helma.objectmodel.dom; import helma.objectmodel.*; import helma.objectmodel.db.NodeManager; import helma.objectmodel.db.Node; -import helma.objectmodel.dom.XmlIDGenerator; -import helma.objectmodel.dom.XmlDatabaseReader; -import helma.objectmodel.dom.XmlWriter; import helma.framework.core.Application; import java.io.*; @@ -101,7 +98,7 @@ public final class XmlDatabase implements IDatabase { } catch (ObjectNotFoundException notfound) { node = new Node("root", "0", "Root", nmgr.safe); node.setDbMapping(app.getDbMapping("root")); - saveNode(txn, node.getID(), node); + insertNode(txn, node.getID(), node); // register node with nodemanager cache nmgr.registerNode(node); } @@ -111,7 +108,7 @@ public final class XmlDatabase implements IDatabase { } catch (ObjectNotFoundException notfound) { node = new Node("users", "1", null, nmgr.safe); node.setDbMapping(app.getDbMapping("__userroot__")); - saveNode(txn, node.getID(), node); + insertNode(txn, node.getID(), node); // register node with nodemanager cache nmgr.registerNode(node); } @@ -279,17 +276,38 @@ public final class XmlDatabase implements IDatabase { throw new IOException(x.toString()); } } + /** + * Save a node with the given key. Writes the node to a temporary file + * which is copied to its final name when the transaction is committed. + * + * @param txn + * @param kstr + * @param node + * @throws java.io.IOException + */ + public void insertNode(ITransaction txn, String kstr, INode node) + throws IOException { + File f = new File(dbHomeDir, kstr + ".xml"); + + if (f.exists()) { + throw new IOException("Object already exists for key " + kstr); + } + + // apart from the above check insertNode() is equivalent to updateNode() + updateNode(txn, kstr, node); + } /** - * Write the node to a temporary file. + * Update a node with the given key. Writes the node to a temporary file + * which is copied to its final name when the transaction is committed. * - * @param txn the transaction we're in - * @param kstr the node's key - * @param node the node to save - * @throws IOException + * @param txn + * @param kstr + * @param node + * @throws java.io.IOException */ - public void saveNode(ITransaction txn, String kstr, INode node) - throws IOException { + public void updateNode(ITransaction txn, String kstr, INode node) + throws IOException { XmlWriter writer = null; File tmp = File.createTempFile(kstr + ".xml.", ".tmp", dbHomeDir);