Split IDatabase.saveNode() into insertNode() and updateNode(). Fixes bug 418.

This commit is contained in:
hns 2005-03-23 14:57:28 +00:00
parent 11257b4765
commit 92da97550c
3 changed files with 45 additions and 16 deletions

View file

@ -60,14 +60,25 @@ public interface IDatabase {
throws IOException, ObjectNotFoundException; throws IOException, ObjectNotFoundException;
/** /**
* Save a node with the given key * Insert a node with the given key
* *
* @param transaction * @param transaction
* @param key * @param key
* @param node * @param node
* @throws IOException * @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; throws IOException;
/** /**

View file

@ -401,7 +401,7 @@ public final class NodeManager {
DbMapping dbm = node.getDbMapping(); DbMapping dbm = node.getDbMapping();
if ((dbm == null) || !dbm.isRelational()) { if ((dbm == null) || !dbm.isRelational()) {
db.saveNode(txn, node.getID(), node); db.insertNode(txn, node.getID(), node);
} else { } else {
insertRelationalNode(node, dbm, dbm.getConnection()); insertRelationalNode(node, dbm, dbm.getConnection());
} }
@ -534,7 +534,7 @@ public final class NodeManager {
boolean markMappingAsUpdated = false; boolean markMappingAsUpdated = false;
if ((dbm == null) || !dbm.isRelational()) { if ((dbm == null) || !dbm.isRelational()) {
db.saveNode(txn, node.getID(), node); db.updateNode(txn, node.getID(), node);
} else { } else {
Hashtable propMap = node.getPropMap(); Hashtable propMap = node.getPropMap();
Property[] props; Property[] props;

View file

@ -19,9 +19,6 @@ package helma.objectmodel.dom;
import helma.objectmodel.*; import helma.objectmodel.*;
import helma.objectmodel.db.NodeManager; import helma.objectmodel.db.NodeManager;
import helma.objectmodel.db.Node; 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 helma.framework.core.Application;
import java.io.*; import java.io.*;
@ -101,7 +98,7 @@ public final class XmlDatabase implements IDatabase {
} catch (ObjectNotFoundException notfound) { } catch (ObjectNotFoundException notfound) {
node = new Node("root", "0", "Root", nmgr.safe); node = new Node("root", "0", "Root", nmgr.safe);
node.setDbMapping(app.getDbMapping("root")); node.setDbMapping(app.getDbMapping("root"));
saveNode(txn, node.getID(), node); insertNode(txn, node.getID(), node);
// register node with nodemanager cache // register node with nodemanager cache
nmgr.registerNode(node); nmgr.registerNode(node);
} }
@ -111,7 +108,7 @@ public final class XmlDatabase implements IDatabase {
} catch (ObjectNotFoundException notfound) { } catch (ObjectNotFoundException notfound) {
node = new Node("users", "1", null, nmgr.safe); node = new Node("users", "1", null, nmgr.safe);
node.setDbMapping(app.getDbMapping("__userroot__")); node.setDbMapping(app.getDbMapping("__userroot__"));
saveNode(txn, node.getID(), node); insertNode(txn, node.getID(), node);
// register node with nodemanager cache // register node with nodemanager cache
nmgr.registerNode(node); nmgr.registerNode(node);
} }
@ -279,16 +276,37 @@ public final class XmlDatabase implements IDatabase {
throw new IOException(x.toString()); 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 txn
* @param kstr the node's key * @param kstr
* @param node the node to save * @param node
* @throws IOException * @throws java.io.IOException
*/ */
public void saveNode(ITransaction txn, String kstr, INode node) public void updateNode(ITransaction txn, String kstr, INode node)
throws IOException { throws IOException {
XmlWriter writer = null; XmlWriter writer = null;
File tmp = File.createTempFile(kstr + ".xml.", ".tmp", dbHomeDir); File tmp = File.createTempFile(kstr + ".xml.", ".tmp", dbHomeDir);