mirror of
https://github.com/Icinga/icinga2.git
synced 2025-09-23 17:57:54 +02:00
Dictionary: store data in a HybridMap
to allow O(1) reads for the sake of DSL, config load speed.
This commit is contained in:
parent
1849a03835
commit
f7ce32aa14
@ -16,18 +16,20 @@ REGISTER_PRIMITIVE_TYPE(Dictionary, Object, Dictionary::GetPrototype());
|
|||||||
Dictionary::Dictionary(const DictionaryData& other)
|
Dictionary::Dictionary(const DictionaryData& other)
|
||||||
{
|
{
|
||||||
for (const auto& kv : other)
|
for (const auto& kv : other)
|
||||||
m_Data.insert(kv);
|
m_Data.Set(kv.first, kv.second);
|
||||||
}
|
}
|
||||||
|
|
||||||
Dictionary::Dictionary(DictionaryData&& other)
|
Dictionary::Dictionary(DictionaryData&& other)
|
||||||
{
|
{
|
||||||
for (auto& kv : other)
|
for (auto& kv : other)
|
||||||
m_Data.insert(std::move(kv));
|
m_Data.Set(std::move(kv.first), std::move(kv.second));
|
||||||
}
|
}
|
||||||
|
|
||||||
Dictionary::Dictionary(std::initializer_list<Dictionary::Pair> init)
|
Dictionary::Dictionary(std::initializer_list<Dictionary::Pair> init)
|
||||||
: m_Data(init)
|
{
|
||||||
{ }
|
for (auto& kv : init)
|
||||||
|
m_Data.Set(kv.first, kv.second);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves a value from a dictionary.
|
* Retrieves a value from a dictionary.
|
||||||
@ -37,14 +39,11 @@ Dictionary::Dictionary(std::initializer_list<Dictionary::Pair> init)
|
|||||||
*/
|
*/
|
||||||
Value Dictionary::Get(const String& key) const
|
Value Dictionary::Get(const String& key) const
|
||||||
{
|
{
|
||||||
|
Value result;
|
||||||
ObjectLock olock(this);
|
ObjectLock olock(this);
|
||||||
|
|
||||||
auto it = m_Data.find(key);
|
m_Data.Get(key, result);
|
||||||
|
return result;
|
||||||
if (it == m_Data.end())
|
|
||||||
return Empty;
|
|
||||||
|
|
||||||
return it->second;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -58,13 +57,7 @@ bool Dictionary::Get(const String& key, Value *result) const
|
|||||||
{
|
{
|
||||||
ObjectLock olock(this);
|
ObjectLock olock(this);
|
||||||
|
|
||||||
auto it = m_Data.find(key);
|
return m_Data.Get(key, *result);
|
||||||
|
|
||||||
if (it == m_Data.end())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
*result = it->second;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -81,7 +74,7 @@ void Dictionary::Set(const String& key, Value value, bool overrideFrozen)
|
|||||||
if (m_Frozen && !overrideFrozen)
|
if (m_Frozen && !overrideFrozen)
|
||||||
BOOST_THROW_EXCEPTION(std::invalid_argument("Value in dictionary must not be modified."));
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Value in dictionary must not be modified."));
|
||||||
|
|
||||||
m_Data[key] = std::move(value);
|
m_Data.Set(key, std::move(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -93,7 +86,7 @@ size_t Dictionary::GetLength() const
|
|||||||
{
|
{
|
||||||
ObjectLock olock(this);
|
ObjectLock olock(this);
|
||||||
|
|
||||||
return m_Data.size();
|
return m_Data.GetLength();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -106,7 +99,7 @@ bool Dictionary::Contains(const String& key) const
|
|||||||
{
|
{
|
||||||
ObjectLock olock(this);
|
ObjectLock olock(this);
|
||||||
|
|
||||||
return (m_Data.find(key) != m_Data.end());
|
return m_Data.Contains(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -150,7 +143,7 @@ void Dictionary::Remove(Dictionary::Iterator it, bool overrideFrozen)
|
|||||||
if (m_Frozen && !overrideFrozen)
|
if (m_Frozen && !overrideFrozen)
|
||||||
BOOST_THROW_EXCEPTION(std::invalid_argument("Dictionary must not be modified."));
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Dictionary must not be modified."));
|
||||||
|
|
||||||
m_Data.erase(it);
|
m_Data.Remove(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -166,13 +159,7 @@ void Dictionary::Remove(const String& key, bool overrideFrozen)
|
|||||||
if (m_Frozen && !overrideFrozen)
|
if (m_Frozen && !overrideFrozen)
|
||||||
BOOST_THROW_EXCEPTION(std::invalid_argument("Dictionary must not be modified."));
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Dictionary must not be modified."));
|
||||||
|
|
||||||
Dictionary::Iterator it;
|
m_Data.Remove(key);
|
||||||
it = m_Data.find(key);
|
|
||||||
|
|
||||||
if (it == m_Data.end())
|
|
||||||
return;
|
|
||||||
|
|
||||||
m_Data.erase(it);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -187,14 +174,14 @@ void Dictionary::Clear(bool overrideFrozen)
|
|||||||
if (m_Frozen && !overrideFrozen)
|
if (m_Frozen && !overrideFrozen)
|
||||||
BOOST_THROW_EXCEPTION(std::invalid_argument("Dictionary must not be modified."));
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Dictionary must not be modified."));
|
||||||
|
|
||||||
m_Data.clear();
|
m_Data.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dictionary::CopyTo(const Dictionary::Ptr& dest) const
|
void Dictionary::CopyTo(const Dictionary::Ptr& dest) const
|
||||||
{
|
{
|
||||||
ObjectLock olock(this);
|
ObjectLock olock(this);
|
||||||
|
|
||||||
for (const Dictionary::Pair& kv : m_Data) {
|
for (auto& kv : m_Data) {
|
||||||
dest->Set(kv.first, kv.second);
|
dest->Set(kv.first, kv.second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -226,7 +213,7 @@ Object::Ptr Dictionary::Clone() const
|
|||||||
|
|
||||||
dict.reserve(GetLength());
|
dict.reserve(GetLength());
|
||||||
|
|
||||||
for (const Dictionary::Pair& kv : m_Data) {
|
for (auto& kv : m_Data) {
|
||||||
dict.emplace_back(kv.first, kv.second.Clone());
|
dict.emplace_back(kv.first, kv.second.Clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -246,7 +233,7 @@ std::vector<String> Dictionary::GetKeys() const
|
|||||||
|
|
||||||
std::vector<String> keys;
|
std::vector<String> keys;
|
||||||
|
|
||||||
for (const Dictionary::Pair& kv : m_Data) {
|
for (auto& kv : m_Data) {
|
||||||
keys.push_back(kv.first);
|
keys.push_back(kv.first);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#ifndef DICTIONARY_H
|
#ifndef DICTIONARY_H
|
||||||
#define DICTIONARY_H
|
#define DICTIONARY_H
|
||||||
|
|
||||||
|
#include "base/hybrid-map.hpp"
|
||||||
#include "base/i2-base.hpp"
|
#include "base/i2-base.hpp"
|
||||||
#include "base/object.hpp"
|
#include "base/object.hpp"
|
||||||
#include "base/value.hpp"
|
#include "base/value.hpp"
|
||||||
@ -28,7 +29,7 @@ public:
|
|||||||
/**
|
/**
|
||||||
* An iterator that can be used to iterate over dictionary elements.
|
* An iterator that can be used to iterate over dictionary elements.
|
||||||
*/
|
*/
|
||||||
typedef std::map<String, Value>::iterator Iterator;
|
typedef HybridMap<String, Value>::Iterator Iterator;
|
||||||
|
|
||||||
typedef std::map<String, Value>::size_type SizeType;
|
typedef std::map<String, Value>::size_type SizeType;
|
||||||
|
|
||||||
@ -74,7 +75,7 @@ public:
|
|||||||
bool GetOwnField(const String& field, Value *result) const override;
|
bool GetOwnField(const String& field, Value *result) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::map<String, Value> m_Data; /**< The data for the dictionary. */
|
HybridMap<String, Value> m_Data; /**< The data for the dictionary. */
|
||||||
bool m_Frozen{false};
|
bool m_Frozen{false};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user