Skip to content

Commit

Permalink
#119 add random_access index for sign logs, add random access operator
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-modelist-dev committed Feb 13, 2019
1 parent 6ac40bf commit 605abe8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
23 changes: 15 additions & 8 deletions keychain_lib/include/keychain_lib/keyfile_singleton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ namespace keyfiles_map {
struct prim_pubkey_tag {};
struct second_keyname_tag {};
struct third_date_tag {};
struct log_record_tag {};
struct random_access_tag {};
struct log_date_tag {};
struct key_random_access_tag {};
struct log_random_access_tag {};


using keyfile_map_t = boost::multi_index::multi_index_container<
Expand All @@ -44,7 +45,7 @@ using keyfile_map_t = boost::multi_index::multi_index_container<
boost::multi_index::const_mem_fun<keyfile_format::keyfile_t, const fc_light::time_point&, &keyfile_format::keyfile_t::last_date>
>,
boost::multi_index::random_access<
boost::multi_index::tag<random_access_tag>
boost::multi_index::tag<key_random_access_tag>
>
>
>;
Expand All @@ -53,8 +54,11 @@ using log_records_t = boost::multi_index::multi_index_container<
keyfile_format::log_record,
boost::multi_index::indexed_by<
boost::multi_index::ordered_non_unique<
boost::multi_index::tag<log_record_tag>,
boost::multi_index::tag<log_date_tag>,
boost::multi_index::member<keyfile_format::log_record, fc_light::time_point, &keyfile_format::log_record::sign_time>
>,
boost::multi_index::random_access<
boost::multi_index::tag<log_random_access_tag>
>
>
>;
Expand All @@ -79,8 +83,9 @@ class keyfile_singleton
using prim_index_type = keyfile_map_t::index<keyfiles_map::prim_pubkey_tag>::type;
using second_index_type = keyfile_map_t::index<keyfiles_map::second_keyname_tag>::type;
using third_index_type = keyfile_map_t::index<keyfiles_map::third_date_tag>::type;
using log_index_type = log_records_t::index<keyfiles_map::log_record_tag>::type;
using random_access_index_type = keyfile_map_t::index<keyfiles_map::random_access_tag>::type;
using log_random_access_index_type = log_records_t::index<keyfiles_map::log_random_access_tag>::type;
using log_date_index_type = log_records_t::index<keyfiles_map::log_date_tag>::type;
using key_random_access_index_type = keyfile_map_t::index<keyfiles_map::key_random_access_tag>::type;

using prim_key_type = keyfile_map_t::key_type;
using second_key_type = second_index_type::key_type;
Expand All @@ -99,7 +104,8 @@ class keyfile_singleton
using iterator = keyfile_map_t::iterator; //primary_index
using const_iterator = keyfile_map_t::const_iterator; //primary_index

const log_index_type& get_logs(const dev::Public& pkey);
const log_random_access_index_type& get_logs(const dev::Public& pkey);
const log_date_index_type& get_logs_date_ordered(const dev::Public& pkey);
void add_log_record(const dev::Public& pkey, const keyfile_format::log_record& record);

const_iterator begin() const; //primary_index
Expand All @@ -122,10 +128,11 @@ class keyfile_singleton
const prim_index_type& prim_index() const;
const second_index_type& second_index() const;
const third_index_type& third_index() const;
const random_access_index_type& random_access_index() const;
const key_random_access_index_type& random_access_index() const;

const keyfile_format::keyfile_t& operator[](const prim_key_type& key); //NOTE: use for searching by public key
const keyfile_format::keyfile_t& operator[](const second_key_type& key); //NOTE: use for searchin by keyname
const keyfile_format::keyfile_t& operator[](size_t index); //NOTE: random_access_index for key table
void insert(keyfile_format::keyfile_t&& keyfile_data); //NOTE: keyfile_t (fc_light::variant) is not support move semantic

template <typename modifier_f>
Expand Down
41 changes: 33 additions & 8 deletions keychain_lib/src/keyfile_singleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ const keyfile_singleton::third_index_type& keyfile_singleton::third_index() cons
return m_keydata_map.get<keyfiles_map::third_date_tag>();
}

const keyfile_singleton::random_access_index_type& keyfile_singleton::random_access_index() const
const keyfile_singleton::key_random_access_index_type& keyfile_singleton::random_access_index() const
{
return m_keydata_map.get<keyfiles_map::random_access_tag>();
return m_keydata_map.get<keyfiles_map::key_random_access_tag>();
}

void keyfile_singleton::keydata_initialize()
Expand Down Expand Up @@ -129,21 +129,36 @@ keyfile_singleton::~keyfile_singleton()
flush_all();
}

const keyfile_format::keyfile_t& keyfile_singleton::operator[](size_t index)
{
bool stop = false;
do{
auto& ra_ind = random_access_index();
if( index >= ra_ind.size() )
{
if(stop == true)
FC_LIGHT_THROW_EXCEPTION(fc_light::out_of_range_exception, "index = ${ind_}", ("ind_", index));
keydata_initialize();
bool stop = false;
}
return ra_ind[index];
} while (true);
}

const keyfile_format::keyfile_t& keyfile_singleton::operator[](const keyfile_singleton::prim_key_type& key)
{
bool stop = false;
do{
auto it = m_keydata_map.find(key);
if (it == m_keydata_map.end())
{
keydata_initialize();
if(stop == true)
FC_LIGHT_THROW_EXCEPTION(fc_light::key_not_found_exception, "Private key is not exist public = ${key_}", ("key_", key));
keydata_initialize();
stop = true;
}
return *it;
} while (true);

}

const keyfile_format::keyfile_t& keyfile_singleton::operator[](const keyfile_singleton::second_key_type& key)
Expand All @@ -154,10 +169,10 @@ const keyfile_format::keyfile_t& keyfile_singleton::operator[](const keyfile_sin
auto it = second_index.find(key);
if (it == second_index.end())
{
keydata_initialize();
if(stop == true)
FC_LIGHT_THROW_EXCEPTION(fc_light::key_not_found_exception, "Private key is not found by keyname = ${name_}",
("name_", key));
("name_", key));
keydata_initialize();
stop = true;
}
return *it;
Expand Down Expand Up @@ -250,14 +265,24 @@ void keyfile_singleton::flush_all() const
});
}

const keyfile_singleton::log_index_type& keyfile_singleton::get_logs(const dev::Public& pkey)
const keyfile_singleton::log_random_access_index_type& keyfile_singleton::get_logs(const dev::Public& pkey)
{
signlog_initialize();//NOTE: it may be slowly, using sqlite and triggers is more preferable
auto it = m_signlog_map.find(pkey);
if (it == m_signlog_map.end())
FC_LIGHT_THROW_EXCEPTION(fc_light::file_not_found_exception, "Public_key: ${PKEY}", ("PKEY", pkey));
auto& records = it->second;
return records.get<keyfiles_map::log_random_access_tag>();
}

const keyfile_singleton::log_date_index_type& keyfile_singleton::get_logs_date_ordered(const dev::Public& pkey)
{
signlog_initialize();//NOTE: it may be slowly, using sqlite and triggers is more preferable
auto it = m_signlog_map.find(pkey);
if (it == m_signlog_map.end())
FC_LIGHT_THROW_EXCEPTION(fc_light::file_not_found_exception, "Public_key: ${PKEY}", ("PKEY", pkey));
auto& records = it->second;
return records.get<keyfiles_map::log_record_tag>();
return records.get<keyfiles_map::log_date_tag>();
}

void keyfile_singleton::add_log_record(const dev::Public& pkey, const keyfile_format::log_record& record)
Expand Down

0 comments on commit 605abe8

Please sign in to comment.