From d21701afe4230d633ae685ffeee6f5fb5a76f34d Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 27 Jun 2020 11:37:27 +0200 Subject: Check for existing ids in the tree when creating a new node. --- src/messagehandler.cc | 4 +--- src/nodemanager.cc | 18 ++++++++++++------ src/nodemanager.h | 3 ++- src/nodetree.cc | 7 ++++++- src/nodetree.h | 1 + 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/messagehandler.cc b/src/messagehandler.cc index e4db5e7..9a00e46 100644 --- a/src/messagehandler.cc +++ b/src/messagehandler.cc @@ -70,10 +70,8 @@ MessageList handle_msg(MessageList msgList, clientid_t wsi) INFO(messagehandler, "Handling create command\n"); try { - nodeid_t id = m.create.id; - m.nodes = node_manager.createNode(m.create.parentid, &id, + m.nodes = node_manager.createNode(m.create.parentid, m.create.id, m.create.insertbeforeid); - m.create.id = id; outmsgs.push_back(m); } catch (std::exception& e) diff --git a/src/nodemanager.cc b/src/nodemanager.cc index 8c7cb56..e94c798 100644 --- a/src/nodemanager.cc +++ b/src/nodemanager.cc @@ -113,6 +113,11 @@ nodeid_t NodeManager::createId() return tree.createId(); } +bool NodeManager::hasId(nodeid_t id) +{ + return tree.hasId(id); +} + NodeIdListPair NodeManager::moveNode(nodeid_t id, nodeid_t to, nodeid_t beforeId) throw (std::exception) { @@ -210,19 +215,20 @@ NodeIdList NodeManager::updateNode(nodeid_t id, const std::string &name, return affectedNodes; } -NodeIdList NodeManager::createNode(nodeid_t parentid, nodeid_t *pid, nodeid_t insertbeforeid) +NodeIdList NodeManager::createNode(nodeid_t parentid, nodeid_t id, nodeid_t insertbeforeid) throw (std::exception) { + if(hasId(id)) + { + // Id alerady in tree, ignore + return {}; + } + NodeIdList affectedNodes; node_t t; - nodeid_t id = *pid;//createId(); t.attributes["title"] = ""; t.id = id; - if(pid) - { - *pid = id; - } try { diff --git a/src/nodemanager.h b/src/nodemanager.h index 97d890f..85b7955 100644 --- a/src/nodemanager.h +++ b/src/nodemanager.h @@ -45,7 +45,7 @@ public: void init(std::string filename); - NodeIdList createNode(nodeid_t parentid, nodeid_t *id, nodeid_t insertbeforeid) throw (std::exception); + NodeIdList createNode(nodeid_t parentid, nodeid_t id, nodeid_t insertbeforeid) throw (std::exception); NodeIdList updateNode(nodeid_t id, const std::string &name, const std::string &value) throw (std::exception); NodeIdList removeNode(nodeid_t id) throw (std::exception); @@ -61,6 +61,7 @@ public: void flushNodes(); nodeid_t createId(); + bool hasId(nodeid_t id); private: // NodeIdList ancestorList(nodeid_t node); diff --git a/src/nodetree.cc b/src/nodetree.cc index eac919e..a2499ea 100644 --- a/src/nodetree.cc +++ b/src/nodetree.cc @@ -101,11 +101,16 @@ nodeid_t NodeTree::createId() { nodeid = nextid++; } - while(id2node.find(nodeid) != id2node.end()); + while(hasId(nodeid)); return nodeid; } +bool NodeTree::hasId(nodeid_t nodeid) +{ + return id2node.find(nodeid) != id2node.end(); +} + static nodeid_t rootid = -1; NodeIdList NodeTree::insertAsChild(nodeid_t parentid, nodeid_t id, diff --git a/src/nodetree.h b/src/nodetree.h index 2ab9ec7..f2f4600 100644 --- a/src/nodetree.h +++ b/src/nodetree.h @@ -54,6 +54,7 @@ public: ~NodeTree(); nodeid_t createId(); + bool hasId(nodeid_t nodeid); NodeIdList insertAsChild(nodeid_t parentid, nodeid_t id, node_t data, nodeid_t insertBeforeId) throw (std::exception); -- cgit v1.2.3