Skip to content
Snippets Groups Projects
Commit ce150a56 authored by roadscape's avatar roadscape
Browse files

maintain accounts.rank field, close #144

parent 6ed5ace5
No related branches found
No related tags found
No related merge requests found
...@@ -26,6 +26,9 @@ class Accounts: ...@@ -26,6 +26,9 @@ class Accounts:
# fifo queue # fifo queue
_dirty = UniqueFIFO() _dirty = UniqueFIFO()
# in-mem id->rank map
_ranks = {}
# account core methods # account core methods
# -------------------- # --------------------
...@@ -116,15 +119,11 @@ class Accounts: ...@@ -116,15 +119,11 @@ class Accounts:
return count return count
@classmethod @classmethod
def update_ranks(cls): def fetch_ranks(cls):
"""Rebuild `hive_accounts` table rank-by-vote-weight column.""" """Rebuild account ranks and store in memory for next update."""
sql = """ sql = "SELECT id FROM hive_accounts ORDER BY vote_weight DESC"
UPDATE hive_accounts for rank, _id in enumerate(DB.query_col(sql)):
SET rank = r.rnk cls._ranks[_id] = rank + 1
FROM (SELECT id, ROW_NUMBER() OVER (ORDER BY vote_weight DESC) as rnk FROM hive_accounts) r
WHERE hive_accounts.id = r.id AND rank != r.rnk;
"""
DB.query(sql)
@classmethod @classmethod
def _cache_accounts(cls, accounts, steem, trx=True): def _cache_accounts(cls, accounts, steem, trx=True):
...@@ -186,5 +185,10 @@ class Accounts: ...@@ -186,5 +185,10 @@ class Accounts:
'raw_json': json.dumps(account)} 'raw_json': json.dumps(account)}
# update rank field, if present
_id = cls.get_id(account['name'])
if _id in cls._ranks:
values['rank'] = cls._ranks[_id]
bind = ', '.join([k+" = :"+k for k in list(values.keys())][1:]) bind = ', '.join([k+" = :"+k for k in list(values.keys())][1:])
return ("UPDATE hive_accounts SET %s WHERE name = :name" % bind, values) return ("UPDATE hive_accounts SET %s WHERE name = :name" % bind, values)
...@@ -39,8 +39,9 @@ class Sync: ...@@ -39,8 +39,9 @@ class Sync:
# ensure db schema up to date, check app status # ensure db schema up to date, check app status
DbState.initialize() DbState.initialize()
# prefetch id->name memory map # prefetch id->name and id->rank memory maps
Accounts.load_ids() Accounts.load_ids()
Accounts.fetch_ranks()
if DbState.is_initial_sync(): if DbState.is_initial_sync():
# resume initial sync # resume initial sync
...@@ -189,8 +190,8 @@ class Sync: ...@@ -189,8 +190,8 @@ class Sync:
cnt['insert'], cnt['update'], cnt['payout'], cnt['upvote'], cnt['insert'], cnt['update'], cnt['payout'], cnt['upvote'],
cnt['recount'], accts, follows, ms, ' SLOW' if ms > 1000 else '') cnt['recount'], accts, follows, ms, ' SLOW' if ms > 1000 else '')
#if num % 1200 == 0: #1hr if num % 1200 == 0: #1hr
# Accounts.update_ranks() #144 Accounts.fetch_ranks()
if num % 100 == 0: #5min if num % 100 == 0: #5min
Accounts.dirty_oldest(500) Accounts.dirty_oldest(500)
Accounts.flush(steemd, trx=True) Accounts.flush(steemd, trx=True)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment