void SimpleConfig::delKey(std::string const §ion, std::string const &key) { std::string index = _hash_index(section, key); ConfigDictionary::iterator i = m_dict.find(index); if (i != m_dict.end()) { ElementList::iterator at; at = std::find(m_elements.begin(), m_elements.end(), i->second); assert(at != m_elements.end()); m_elements.erase(at); m_dict.erase(i); delete *at; } }
/* Tries to find a node fast using the hash table * * Used by: slurmctld */ static sw_gen_node_info_t * _find_node(char *node_name) { int i; sw_gen_node_info_t *n; struct node_record *node_ptr; if (node_name == NULL) { error("%s: _find_node node name is NULL", plugin_type); return NULL; } if (libstate->node_count == 0) return NULL; xassert(libstate->magic == SW_GEN_LIBSTATE_MAGIC); if (libstate->hash_table) { i = _hash_index(node_name); n = libstate->hash_table[i]; while (n) { xassert(n->magic == SW_GEN_NODE_INFO_MAGIC); if (!strcmp(n->node_name, node_name)) return n; n = n->next; } } /* This code is only needed if NodeName and NodeHostName differ */ node_ptr = find_node_record(node_name); if (node_ptr && libstate->hash_table) { i = _hash_index(node_ptr->node_hostname); n = libstate->hash_table[i]; while (n) { xassert(n->magic == SW_GEN_NODE_INFO_MAGIC); if (!strcmp(n->node_name, node_name)) return n; n = n->next; } } return NULL; }
/* * _find_alias_node_record - find a record for node with the alias of * the specified name supplied * input: name - name to be aliased of the desired node * output: return pointer to node record or NULL if not found * global: node_record_table_ptr - pointer to global node table * node_hash_table - table of hash indexes */ static struct node_record *_find_alias_node_record (char *name) { int i; char *alias = NULL; if ((name == NULL) || (name[0] == '\0')) { info("_find_alias_node_record: passed NULL name"); return NULL; } /* Get the alias we have just to make sure the user isn't * trying to use the real hostname to run on something that has * been aliased. */ alias = slurm_conf_get_nodename(name); if (!alias) return NULL; /* try to find via hash table, if it exists */ if (node_hash_table) { struct node_record *node_ptr; i = _hash_index (alias); node_ptr = node_hash_table[i]; while (node_ptr) { xassert(node_ptr->magic == NODE_MAGIC); if (!strcmp(node_ptr->name, alias)) { xfree(alias); return node_ptr; } node_ptr = node_ptr->node_next; } error ("_find_alias_node_record: lookup failure for %s", name); } /* revert to sequential search */ else { for (i = 0; i < node_record_count; i++) { if (!strcmp (alias, node_record_table_ptr[i].name)) { xfree(alias); return (&node_record_table_ptr[i]); } } } xfree(alias); return (struct node_record *) NULL; }
/* Add the hash entry for a newly created node record */ static void _hash_add_nodeinfo(sw_gen_node_info_t *new_node_info) { int index; xassert(libstate); xassert(libstate->hash_table); xassert(libstate->hash_max >= libstate->node_count); xassert(libstate->magic == SW_GEN_LIBSTATE_MAGIC); if (!new_node_info->node_name || !new_node_info->node_name[0]) return; index = _hash_index(new_node_info->node_name); new_node_info->next = libstate->hash_table[index]; libstate->hash_table[index] = new_node_info; libstate->node_count++; }
/* * _find_node_record - find a record for node with specified name * IN: name - name of the desired node * IN: test_alias - if set, also test NodeHostName value * RET: pointer to node record or NULL if not found */ static struct node_record *_find_node_record (char *name, bool test_alias) { int i; if ((name == NULL) || (name[0] == '\0')) { info("find_node_record passed NULL name"); return NULL; } /* try to find via hash table, if it exists */ if (node_hash_table) { struct node_record *node_ptr; i = _hash_index (name); node_ptr = node_hash_table[i]; while (node_ptr) { xassert(node_ptr->magic == NODE_MAGIC); if (!strcmp(node_ptr->name, name)) { return node_ptr; } node_ptr = node_ptr->node_next; } if ((node_record_count == 1) && (strcmp(node_record_table_ptr[0].name, "localhost") == 0)) return (&node_record_table_ptr[0]); error ("find_node_record: lookup failure for %s", name); } /* revert to sequential search */ else { for (i = 0; i < node_record_count; i++) { if (!strcmp (name, node_record_table_ptr[i].name)) { return (&node_record_table_ptr[i]); } } } if (test_alias) { /* look for the alias node record if the user put this in * instead of what slurm sees the node name as */ return _find_alias_node_record (name); } return NULL; }
void SimpleConfig::_addValue(const std::string §ion, const std::string &key, const std::string &value) { if (key != "") { ConfigElement *element = new ConfigValue(section, key, value); ConfigDictionary::iterator last = m_last.find(section); if (last != m_last.end()) { ElementList::iterator at; at = std::find(m_elements.begin(), m_elements.end(), last->second); if (at != m_elements.end()) ++at; m_elements.insert(at, element); } else { _addComment(""); _addSection(section); m_elements.push_back(element); } std::string index = _hash_index(section, key); m_dict[index] = element; m_last[section] = element; } }
/* * rehash_node - build a hash table of the node_record entries. * NOTE: manages memory for node_hash_table */ extern void rehash_node (void) { int i, inx; struct node_record *node_ptr = node_record_table_ptr; xfree (node_hash_table); node_hash_table = xmalloc (sizeof (struct node_record *) * node_record_count); for (i = 0; i < node_record_count; i++, node_ptr++) { if ((node_ptr->name == NULL) || (node_ptr->name[0] == '\0')) continue; /* vestigial record */ inx = _hash_index (node_ptr->name); node_ptr->node_next = node_hash_table[inx]; node_hash_table[inx] = node_ptr; } #if _DEBUG _dump_hash(); #endif return; }
// This is a rather ugly and slow way to delete a section. It can leave stray // comments in the middle of sections, but will remove the section entirely, // even if it is multiply declared. This whole function was a bit of an // afterthought, since it isn't very likely to be used in the application this // class was written for. Sorry to all the purists out there. :-( void SimpleConfig::delSection(std::string const §ion_name) { ConfigDictionary::iterator i = m_last.find(section_name); if (i != m_last.end()) { ElementList rebuild; ElementList::iterator at; bool blank = false; for (at = m_elements.begin(); at != m_elements.end(); at++) { ConfigSection *section = dynamic_cast<ConfigSection *>(*at); if (section && section->getName() == section_name) { delete *at; continue; } ConfigValue *value = dynamic_cast<ConfigValue *>(*at); if (value && value->getSection() == section_name) { std::string index = _hash_index(section_name, value->getKey()); ConfigDictionary::iterator i = m_dict.find(index); if (i != m_dict.end()) m_dict.erase(i); delete *at; continue; } ConfigComment *comment = dynamic_cast<ConfigComment *>(*at); if (comment && comment->getComment() == "") { if (blank) { delete *at; continue; } blank = true; } else blank = false; rebuild.push_back(*at); } m_elements = rebuild; m_modified = true; if (m_autosave) save(); m_last.erase(i); } }
bool SimpleConfig::hasKey(std::string const §ion, std::string const &key) const { std::string index = _hash_index(section, key); ConfigDictionary::const_iterator i = m_dict.find(index); return (i != m_dict.end()); }
ConfigValue *SimpleConfig::_find(const std::string §ion, const std::string &key) { std::string index = _hash_index(section, key); ConfigDictionary::iterator i = m_dict.find(index); if (i == m_dict.end()) return 0; return dynamic_cast<ConfigValue*> ((*i).second); }