Skip to content

Commit

Permalink
keyfile_singleton::get_logs update
Browse files Browse the repository at this point in the history
  • Loading branch information
sinev-valentine committed Mar 5, 2019
1 parent 6d45fae commit 52aceaa
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 62 deletions.
16 changes: 7 additions & 9 deletions keychain_lib/include/keychain_lib/keychain_commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,13 @@ struct keychain_command<command_te::sign_trx> : keychain_command_base
fc_light::variant json;
dev::Secret private_key;
auto& keyfiles = keyfile_singleton::instance();

// auto logs = keyfiles.get_logs(params.public_key);
// int n = std::distance(logs.begin(), logs.end());
// for (int i = 0 ; i < n ; i++)
// {
// auto &a = logs[i];
// auto b = a.blockchain_type;
// }

/* auto &logs = keyfiles.get_logs(params.public_key);
int n = std::distance(logs.begin(), logs.end());
for (int i = 0 ; i < n ; i++)
{
auto &a = logs[i];
auto b = a.blockchain_type;
}*/
//NOTE: using vector instead array because move semantic is implemented in the vector
auto trans_len = keychain_app::from_hex(params.transaction, raw.data(), raw.size());
raw.resize(trans_len);
Expand Down
6 changes: 2 additions & 4 deletions keychain_lib/include/keychain_lib/keyfile_singleton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class keyfile_singleton

using value_t = keyfile_map_t::value_type;
keyfile_map_t m_keydata_map;
// signlog_map_t m_signlog_map;
signlog_map_t m_signlog_map;

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;
Expand All @@ -105,9 +105,7 @@ class keyfile_singleton
using iterator = keyfile_map_t::iterator; //primary_index
using const_iterator = keyfile_map_t::const_iterator; //primary_index

std::vector<keychain_app::keyfile_format::log_record> get_logs(const dev::Public& );

// const log_random_access_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);

Expand Down
6 changes: 2 additions & 4 deletions keychain_lib/include/keychain_lib/sql_singleton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
struct sql_singleton
{
static sql_singleton& instance();
std::vector<keychain_app::keyfile_format::log_record> select(const dev::Public& );

// const keychain_app::keyfiles_map::log_records_t select(const dev::Public& );
int insert(const dev::Public&, const keychain_app::keyfile_format::log_record& );
const std::vector<keychain_app::keyfile_format::log_record> select_log(const dev::Public& );
int insert_log(const dev::Public&, const keychain_app::keyfile_format::log_record& );

private:
sql_singleton();
Expand Down
27 changes: 13 additions & 14 deletions keychain_lib/src/keyfile_singleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,21 +351,20 @@ void keyfile_singleton::flush_all() const
});*/
}

std::vector<keychain_app::keyfile_format::log_record> keyfile_singleton::get_logs(const dev::Public& pkey)
//const keyfile_singleton::log_random_access_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_load();//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>();
*/
auto& sql = sql_singleton::instance();
return sql.select(pkey);
// auto records = sql.select(pkey);
// return records.get<keyfiles_map::log_random_access_tag>();
auto records = sql.select_log(pkey);

if (records.size() == 0)
FC_LIGHT_THROW_EXCEPTION(fc_light::file_not_found_exception, "Public_key: ${PKEY}", ("PKEY", pkey));

m_signlog_map.clear();
m_signlog_map.insert(signlog_map_t::value_type(pkey, log_records_t()));
auto it = m_signlog_map.begin();
auto& logmap = it->second;
std::copy(records.begin(), records.end(), std::inserter(logmap, logmap.begin()));
return logmap.get<keyfiles_map::log_random_access_tag>();
}

/*
Expand All @@ -383,7 +382,7 @@ const keyfile_singleton::log_date_index_type& keyfile_singleton::get_logs_date_o
void keyfile_singleton::add_log_record(const dev::Public& pkey, const keyfile_format::log_record& record)
{
auto& sql = sql_singleton::instance();
sql.insert(pkey, record);
sql.insert_log(pkey, record);
/*
auto it = m_signlog_map.find(pkey);
if (it == m_signlog_map.end())
Expand Down
42 changes: 11 additions & 31 deletions keychain_lib/src/sql_singleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ sql_singleton& sql_singleton::instance()
return instance;
}

//const keychain_app::keyfiles_map::log_records_t sql_singleton::select(const dev::Public& pkey)
std::vector<keychain_app::keyfile_format::log_record> sql_singleton::select(const dev::Public& pkey)
const std::vector<keychain_app::keyfile_format::log_record> sql_singleton::select_log(const dev::Public& pkey)
{
sqlite3_stmt * stmt;
std::vector<keychain_app::keyfile_format::log_record> set;
std::vector<keychain_app::keyfile_format::log_record> records;
const char * statement = "select trx, sign_time, blockchain_type from signlog where public_key=?";

auto res = sqlite3_prepare_v2(db, statement, -1, &stmt, NULL);
Expand All @@ -75,43 +74,24 @@ std::vector<keychain_app::keyfile_format::log_record> sql_singleton::select(cons
std::string time((const char *) sqlite3_column_text(stmt, 1));
std::string chain((const char *) sqlite3_column_text(stmt, 2));

dev::bytes trx_(trx.length());
auto len = keychain_app::from_hex(trx, trx_.data(), trx_.size() );
trx_.resize(len);

fc_light::variant v_time (time);
auto time_ = v_time.as<fc_light::time_point>();

fc_light::variant v_chain (chain);
auto chain_ = v_chain.as<keychain_app::blockchain_te>();

set.push_back(keychain_app::keyfile_format::log_record(trx_, time_, chain_));
keychain_app::keyfile_format::log_record rec;
rec.transaction.resize(trx.length());
auto len = keychain_app::from_hex(trx, rec.transaction.data(), rec.transaction.size() );
rec.transaction.resize(len);
rec.sign_time = fc_light::variant(time).as<fc_light::time_point>();
rec.blockchain_type = fc_light::variant(chain).as<keychain_app::blockchain_te>();
records.push_back(rec);
}
else break;
}

if (sqlite3_finalize(stmt) != SQLITE_OK )
FC_LIGHT_THROW_EXCEPTION(fc_light::internal_error_exception, "sqlite3_finalize");

return set;
// auto log_rec_t = keychain_app::keyfiles_map::log_records_t();
// for (auto& s: set)
// log_rec_t.insert(s);

// return log_rec_t;

// keychain_app::keyfiles_map::signlog_map_t m_signlog_map;
// auto res1 = m_signlog_map.insert(keychain_app::keyfiles_map::signlog_map_t::value_type(pkey,
// keychain_app::keyfiles_map::log_records_t() ));
// auto it = m_signlog_map.begin();
//
// auto& logmap = it->second;
//// std::copy(set.begin(), set.end(), std::inserter(logmap, logmap.begin()));
//
// return logmap.get<keychain_app::keyfiles_map::log_random_access_tag>();
return records;
}

int sql_singleton::insert(const dev::Public& pkey, const keychain_app::keyfile_format::log_record& record)
int sql_singleton::insert_log(const dev::Public& pkey, const keychain_app::keyfile_format::log_record& record)
{
sqlite3_stmt * stmt;
fc_light::variant vtime(record.sign_time);
Expand Down

0 comments on commit 52aceaa

Please sign in to comment.