lookup of account by name and id work

This commit is contained in:
Daniel Larimer 2015-07-14 09:46:38 -04:00
parent 98e761c9c2
commit 957381968b
4 changed files with 132 additions and 127 deletions

View file

@ -12,118 +12,102 @@ ChainDataModel::ChainDataModel( fc::thread& t, QObject* parent )
Account* ChainDataModel::getAccount(qint64 id) Account* ChainDataModel::getAccount(qint64 id)
{ {
auto itr = m_accounts.find( id ); auto& by_id_idx = m_accounts.get<::by_id>();
if( itr != m_accounts.end() ) auto itr = by_id_idx.find(id);
return *itr; if( itr == by_id_idx.end() )
{
auto tmp = new Account;
tmp->id = id; --m_account_query_num;
tmp->name = QString::number( --m_account_query_num);
auto result = m_accounts.insert( tmp );
assert( result.second );
auto acct = new Account(this); /** execute in app thread */
acct->setProperty("id", id); m_thread->async( [this,id](){
acct->setProperty("accountName", "LOADING"); try {
auto insert_result = m_accounts.insert( acct ); ilog( "look up names.." );
auto result = m_db_api->get_accounts( {account_id_type(id)} );
/** execute in main */
Q_EMIT queueExecute( [this,result,id](){
wlog( "process result" );
auto& by_id_idx = this->m_accounts.get<::by_id>();
auto itr = by_id_idx.find(id);
assert( itr != by_id_idx.end() );
/** execute in app thread */ if( result.size() == 0 || !result.front() )
m_thread->async( [=](){ {
try { elog( "delete later" );
auto result = m_db_api->get_accounts( {account_id_type(id)} ); (*itr)->deleteLater();
if( result.size() && result.front().valid() ) by_id_idx.erase( itr );
{ }
QString name = QString::fromStdString( result.front()->name ); else
/** execute in main */ {
Q_EMIT queueExecute( [=](){ by_id_idx.modify( itr,
this->m_accounts.modify( insert_result.first, [=]( Account* a ){
[=]( Account* a ){ a->setProperty("accountName", name ); } a->setProperty("name", QString::fromStdString(result.front()->name) );
); }
}); );
} }
else });
{ }
/** execute in main */ catch ( const fc::exception& e )
Q_EMIT queueExecute( [=](){ {
acct->deleteLater(); Q_EMIT exceptionThrown( QString::fromStdString(e.to_string()) );
m_accounts.erase( insert_result.first ); }
}); });
} return *result.first;
} }
catch ( const fc::exception& e ) return *itr;
{
edump((e.to_detail_string()));
Q_EMIT exceptionThrown( QString::fromStdString(e.to_string()) );
}
});
return acct;
} }
Account* ChainDataModel::getAccount(QString name) Account* ChainDataModel::getAccount(QString name)
{ {
auto& by_name_idx = m_accounts.get<by_account_name>();
auto itr = by_name_idx.find(name);
if( itr == by_name_idx.end() )
{ {
auto itr = m_accounts.get<by_account_name>().begin(); auto tmp = new Account;
while( itr != m_accounts.get<by_account_name>().end() ) tmp->id = --m_account_query_num;
{ tmp->name = name;
edump(( (*itr)->getAccountName().toStdString())); auto result = m_accounts.insert( tmp );
if((*itr)->name() == name.toStdString() ) assert( result.second );
wlog( "THEY ARE THE SAME" );
++itr;
}
}
auto& by_name_index = m_accounts.get<by_account_name>();
{
for( auto itr = by_name_index.begin(); itr != by_name_index.end(); ++itr )
wdump(( (*itr)->getAccountName().toStdString()));
}
auto itr = by_name_index.find(name.toStdString());
idump((itr != by_name_index.end()));
if( itr != by_name_index.end() )
{
return *itr;
}
auto acct = new Account(this); /** execute in app thread */
acct->setProperty("id", --m_account_query_num ); m_thread->async( [this,name](){
acct->setProperty("accountName", name); try {
auto insert_result = m_accounts.insert( acct ); ilog( "look up names.." );
edump( (insert_result.second) ); auto result = m_db_api->lookup_account_names( {name.toStdString()} );
edump( (int64_t(*insert_result.first))(int64_t(acct)) ); /** execute in main */
auto itr2 = by_name_index.find(name.toStdString()); Q_EMIT queueExecute( [this,result,name](){
assert( itr2 != by_name_index.end() ); wlog( "process result" );
auto& by_name_idx = this->m_accounts.get<by_account_name>();
auto itr = by_name_idx.find(name);
assert( itr != by_name_idx.end() );
/** execute in app thread */ if( result.size() == 0 || !result.front() )
m_thread->async( [=](){ {
try { elog( "delete later" );
ilog( "look up names.." ); (*itr)->deleteLater();
auto result = m_db_api->lookup_account_names( {name.toStdString()} ); by_name_idx.erase( itr );
idump((result)); }
if( result.size() && result.front().valid() ) else
{ {
/** execute in main */ by_name_idx.modify( itr,
Q_EMIT queueExecute( [=](){ [=]( Account* a ){
this->m_accounts.modify( insert_result.first, a->setProperty("id", result.front()->id.instance() );
[=]( Account* a ){ }
ilog( "setting id ${i}", ("i",result.front()->id.instance()) ); );
idump((int64_t(a))); }
a->setProperty("id", result.front()->id.instance() ); });
} }
); catch ( const fc::exception& e )
}); {
} Q_EMIT exceptionThrown( QString::fromStdString(e.to_string()) );
else }
{ });
/** execute in main */ return *result.first;
Q_EMIT queueExecute( [=](){ }
acct->deleteLater(); return *itr;
this->m_accounts.erase( insert_result.first );
});
}
}
catch ( const fc::exception& e )
{
edump((e.to_detail_string()));
Q_EMIT exceptionThrown( QString::fromStdString(e.to_string()) );
}
});
idump((int64_t(acct)));
return acct;
} }
QQmlListProperty<Balance> Account::balances() QQmlListProperty<Balance> Account::balances()

View file

@ -11,25 +11,14 @@
#include <fc/thread/thread.hpp> #include <fc/thread/thread.hpp>
#include <graphene/app/api.hpp> #include <graphene/app/api.hpp>
#include <QCryptographicHash>
#include <QObject> #include <QObject>
#include <QQmlListProperty> #include <QQmlListProperty>
#include <QtQml>
using boost::multi_index_container; using boost::multi_index_container;
using namespace boost::multi_index; using namespace boost::multi_index;
Q_DECLARE_METATYPE(std::function<void()>) Q_DECLARE_METATYPE(std::function<void()>)
class Crypto {
Q_GADGET
public:
Q_INVOKABLE QString sha256(QByteArray data) {
return QCryptographicHash::hash(data, QCryptographicHash::Sha256).toHex();
}
};
QML_DECLARE_TYPE(Crypto)
class Asset : public QObject { class Asset : public QObject {
Q_OBJECT Q_OBJECT
@ -58,26 +47,26 @@ class Balance : public QObject {
class Account : public QObject { class Account : public QObject {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString accountName MEMBER accountName NOTIFY accountNameChanged) Q_PROPERTY(QString name MEMBER name NOTIFY nameChanged)
Q_PROPERTY(qint64 id MEMBER id NOTIFY idChanged) Q_PROPERTY(qint64 id MEMBER id NOTIFY idChanged)
Q_PROPERTY(QQmlListProperty<Balance> balances READ balances) Q_PROPERTY(QQmlListProperty<Balance> balances READ balances)
QString accountName;
qint64 id;
QList<Balance*> m_balances; QList<Balance*> m_balances;
public: public:
Account(QObject* parent = nullptr) // Account(QObject* parent = nullptr)
: QObject(parent){} // : QObject(parent){}
const QString& getAccountName()const { return accountName; } const QString& getName()const { return name; }
qint64 getId()const { return id; } qint64 getId()const { return id; }
std::string name()const { return accountName.toStdString(); }
QQmlListProperty<Balance> balances(); QQmlListProperty<Balance> balances();
QString name;
qint64 id;
signals: signals:
void accountNameChanged(); void nameChanged();
void idChanged(); void idChanged();
}; };
@ -90,8 +79,7 @@ typedef multi_index_container<
Account*, Account*,
indexed_by< indexed_by<
hashed_unique< tag<by_id>, const_mem_fun<Account, qint64, &Account::getId > >, hashed_unique< tag<by_id>, const_mem_fun<Account, qint64, &Account::getId > >,
hashed_unique< tag<by_account_name>, const_mem_fun<Account, std::string, &Account::name> > ordered_unique< tag<by_account_name>, const_mem_fun<Account, const QString&, &Account::getName> >
// ordered_non_unique< tag<by_account_name>, const_mem_fun<Account, std::string, &Account::name> >
> >
> account_multi_index_type; > account_multi_index_type;

View file

@ -21,9 +21,11 @@ int main(int argc, char *argv[])
qmlRegisterType<GrapheneApplication>("Graphene.Client", 0, 1, "GrapheneApplication"); qmlRegisterType<GrapheneApplication>("Graphene.Client", 0, 1, "GrapheneApplication");
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
/*
QVariant crypto; QVariant crypto;
crypto.setValue(Crypto()); crypto.setValue(Crypto());
engine.rootContext()->setContextProperty("Crypto", crypto); engine.rootContext()->setContextProperty("Crypto", crypto);
*/
#ifdef NDEBUG #ifdef NDEBUG
engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
#else #else

View file

@ -55,12 +55,12 @@ ApplicationWindow {
} }
TextField { TextField {
id: nameField id: nameField
onAccepted: lookupButton.clicked() onAccepted: lookupNameButton.clicked()
focus: true focus: true
} }
Button { Button {
id: lookupButton id: lookupNameButton
text: "Lookup" text: "Lookup Name"
onClicked: { onClicked: {
var acct = app.model.getAccount(nameField.text) var acct = app.model.getAccount(nameField.text)
console.log(JSON.stringify(acct)) console.log(JSON.stringify(acct))
@ -83,6 +83,37 @@ ApplicationWindow {
} }
} }
TextField {
id: idField
onAccepted: lookupIdButton.clicked()
focus: true
}
Button {
id: lookupIdButton
text: "Lookup ID"
onClicked: {
var acct = app.model.getAccount(parseInt(idField.text))
console.log(JSON.stringify(acct))
// @disable-check M126
if (acct == null)
console.log("Got back null account")
else if ( !(parseInt(acct.name) <= 0) )
{
console.log("NAME ALREADY SET" );
console.log(JSON.stringify(acct))
}
else
{
console.log("Waiting for result...")
acct.nameChanged.connect(function(loadedAcct) {
console.log( "NAME CHANGED" );
console.log(JSON.stringify(acct))
})
}
}
}
} }
FormBox { FormBox {