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:
# fifo queue
_dirty = UniqueFIFO()
# in-mem id->rank map
_ranks = {}
# account core methods
# --------------------
......@@ -116,15 +119,11 @@ class Accounts:
return count
@classmethod
def update_ranks(cls):
"""Rebuild `hive_accounts` table rank-by-vote-weight column."""
sql = """
UPDATE hive_accounts
SET rank = r.rnk
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)
def fetch_ranks(cls):
"""Rebuild account ranks and store in memory for next update."""
sql = "SELECT id FROM hive_accounts ORDER BY vote_weight DESC"
for rank, _id in enumerate(DB.query_col(sql)):
cls._ranks[_id] = rank + 1
@classmethod
def _cache_accounts(cls, accounts, steem, trx=True):
......@@ -186,5 +185,10 @@ class Accounts:
'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:])
return ("UPDATE hive_accounts SET %s WHERE name = :name" % bind, values)
......@@ -39,8 +39,9 @@ class Sync:
# ensure db schema up to date, check app status
DbState.initialize()
# prefetch id->name memory map
# prefetch id->name and id->rank memory maps
Accounts.load_ids()
Accounts.fetch_ranks()
if DbState.is_initial_sync():
# resume initial sync
......@@ -189,8 +190,8 @@ class Sync:
cnt['insert'], cnt['update'], cnt['payout'], cnt['upvote'],
cnt['recount'], accts, follows, ms, ' SLOW' if ms > 1000 else '')
#if num % 1200 == 0: #1hr
# Accounts.update_ranks() #144
if num % 1200 == 0: #1hr
Accounts.fetch_ranks()
if num % 100 == 0: #5min
Accounts.dirty_oldest(500)
Accounts.flush(steemd, trx=True)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment