Skip to content
Snippets Groups Projects
Commit 5ea1ef51 authored by Mariusz Trela's avatar Mariusz Trela
Browse files

Data from `hive_reblogs` is removed when a post is deleted

parent 302d433e
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !400. Comments created here will be created in the context of that merge request.
...@@ -612,7 +612,7 @@ def setup(db): ...@@ -612,7 +612,7 @@ def setup(db):
"condenser_get_discussions_by_comments.sql", "condenser_get_discussions_by_comments.sql",
"condenser_get_account_reputations.sql", "condenser_get_account_reputations.sql",
"update_follow_count.sql", "update_follow_count.sql",
"reblog_delete.sql" "delete_reblog_feed_cache.sql"
] ]
from os.path import dirname, realpath from os.path import dirname, realpath
dir_path = dirname(realpath(__file__)) dir_path = dirname(realpath(__file__))
......
DROP FUNCTION IF EXISTS delete_reblog(character varying,character varying,character varying, BOOLEAN) delete_reblog_feed_cache
DROP FUNCTION IF EXISTS delete_reblog_feed_cache(character varying,character varying,character varying, BOOLEAN)
; ;
CREATE OR REPLACE FUNCTION delete_reblog( CREATE OR REPLACE FUNCTION delete_reblog_feed_cache(
in _author hive_accounts.name%TYPE, in _author hive_accounts.name%TYPE,
in _permlink hive_permlink_data.permlink%TYPE, in _permlink hive_permlink_data.permlink%TYPE,
in _account hive_accounts.name%TYPE, in _account hive_accounts.name%TYPE,
......
...@@ -109,32 +109,45 @@ END ...@@ -109,32 +109,45 @@ END
$function$ $function$
; ;
DROP FUNCTION if exists delete_hive_post(character varying,character varying,character varying, integer) DROP FUNCTION if exists delete_hive_post(character varying,character varying,character varying, integer, BOOLEAN)
; ;
CREATE OR REPLACE FUNCTION delete_hive_post( CREATE OR REPLACE FUNCTION delete_hive_post(
in _author hive_accounts.name%TYPE, in _author hive_accounts.name%TYPE,
in _permlink hive_permlink_data.permlink%TYPE, in _permlink hive_permlink_data.permlink%TYPE,
in _block_num hive_blocks.num%TYPE) in _block_num hive_blocks.num%TYPE,
RETURNS TABLE (id hive_posts.id%TYPE, depth hive_posts.depth%TYPE) in _delete_feed_cache BOOLEAN)
RETURNS VOID
LANGUAGE plpgsql LANGUAGE plpgsql
AS AS
$function$ $function$
DECLARE
__post_id INT;
BEGIN BEGIN
RETURN QUERY UPDATE hive_posts AS hp
SET counter_deleted = __post_id = find_comment_id( _author, _permlink, False );
(
SELECT max( hps.counter_deleted ) + 1 IF __post_id = 0 THEN
FROM hive_posts hps RETURN;
INNER JOIN hive_accounts ha ON hps.author_id = ha.id END IF;
INNER JOIN hive_permlink_data hpd ON hps.permlink_id = hpd.id
WHERE ha.name = _author AND hpd.permlink = _permlink UPDATE hive_posts
) SET counter_deleted =
, block_num = _block_num (
FROM hive_posts hp1 SELECT max( counter_deleted ) + 1
INNER JOIN hive_accounts ha ON hp1.author_id = ha.id FROM hive_posts
INNER JOIN hive_permlink_data hpd ON hp1.permlink_id = hpd.id WHERE id = __post_id
WHERE hp.id = hp1.id AND ha.name = _author AND hpd.permlink = _permlink AND hp1.counter_deleted = 0 )
RETURNING hp.id, hp.depth; ,block_num = _block_num
WHERE id = __post_id AND counter_deleted = 0;
DELETE FROM hive_reblogs
WHERE post_id = __post_id;
IF _delete_feed_cache THEN
DELETE FROM hive_feed_cache
WHERE post_id = __post_id;
END IF;
END END
$function$ $function$
; ;
"""Maintains feed cache (blogs + reblogs)"""
import logging
from hive.db.adapter import Db
from hive.indexer.db_adapter_holder import DbAdapterHolder
log = logging.getLogger(__name__)
DB = Db.instance()
class FeedCache(DbAdapterHolder):
"""Maintains `hive_feed_cache`, which merges posts and reports.
The feed cache allows for efficient querying of posts + reblogs,
savings us from expensive queries. Effectively a materialized view.
"""
@classmethod
def delete(cls, post_id, account_id=None):
"""Remove a post from feed cache.
If `account_id` is specified, we remove a single entry (e.g. a
singular un-reblog). Otherwise, we remove all instances of the
post (e.g. a post was deleted; its entry and all reblogs need
to be removed.
"""
sql = "DELETE FROM hive_feed_cache WHERE post_id = :id"
if account_id:
sql = sql + " AND account_id = :account_id"
DB.query(sql, account_id=account_id, id=post_id)
...@@ -10,7 +10,7 @@ from diff_match_patch import diff_match_patch ...@@ -10,7 +10,7 @@ from diff_match_patch import diff_match_patch
from hive.db.adapter import Db from hive.db.adapter import Db
from hive.db.db_state import DbState from hive.db.db_state import DbState
from hive.indexer.feed_cache import FeedCache from hive.indexer.reblog import Reblog
from hive.indexer.community import Community from hive.indexer.community import Community
from hive.indexer.notify import Notify from hive.indexer.notify import Notify
from hive.indexer.post_data_cache import PostDataCache from hive.indexer.post_data_cache import PostDataCache
...@@ -400,21 +400,10 @@ class Posts(DbAdapterHolder): ...@@ -400,21 +400,10 @@ class Posts(DbAdapterHolder):
def delete(cls, op): def delete(cls, op):
"""Marks a post record as being deleted.""" """Marks a post record as being deleted."""
sql = """ delete_feed_cache = not DbState.is_initial_sync()
SELECT id, depth
FROM delete_hive_post((:author)::varchar, (:permlink)::varchar, (:block_num)::int);
"""
row = DB.query_row(sql, author=op['author'], permlink = op['permlink'], block_num=op['block_num'])
result = dict(row)
pid = result['id']
depth = result['depth']
if depth == 0 and not DbState.is_initial_sync(): sql = "SELECT delete_hive_post((:author)::varchar, (:permlink)::varchar, (:block_num)::int, (:delete_feed_cache)::BOOLEAN );"
# TODO: delete from hive_reblogs -- otherwise feed cache gets DB.query_no_return(sql, author=op['author'], permlink = op['permlink'], block_num=op['block_num'], delete_feed_cache=delete_feed_cache)
# populated with deleted posts somwrimas
FeedCache.delete(pid)
@classmethod @classmethod
def _verify_post_against_community(cls, op, community_id, is_valid, is_muted): def _verify_post_against_community(cls, op, community_id, is_valid, is_muted):
......
...@@ -5,7 +5,6 @@ import logging ...@@ -5,7 +5,6 @@ import logging
from hive.db.db_state import DbState from hive.db.db_state import DbState
from hive.indexer.accounts import Accounts from hive.indexer.accounts import Accounts
from hive.indexer.feed_cache import FeedCache
from hive.indexer.db_adapter_holder import DbAdapterHolder from hive.indexer.db_adapter_holder import DbAdapterHolder
from hive.utils.normalize import escape_characters from hive.utils.normalize import escape_characters
...@@ -54,7 +53,7 @@ class Reblog(DbAdapterHolder): ...@@ -54,7 +53,7 @@ class Reblog(DbAdapterHolder):
""" """
delete_feed_cache = not DbState.is_initial_sync() delete_feed_cache = not DbState.is_initial_sync()
sql = "SELECT delete_reblog( (:author)::VARCHAR, (:permlink)::VARCHAR, (:permlink)::VARCHAR, (:delete_feed_cache)::BOOLEAN );" sql = "SELECT delete_reblog_feed_cache( (:author)::VARCHAR, (:permlink)::VARCHAR, (:permlink)::VARCHAR, (:delete_feed_cache)::BOOLEAN );"
status = await db.query_col(sql, author=author, permlink=permlink, account=account, delete_feed_cache=delete_feed_cache); status = await db.query_col(sql, author=author, permlink=permlink, account=account, delete_feed_cache=delete_feed_cache);
assert status is not None assert status is not None
if status == 0: if status == 0:
......
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