From 5ea1ef51f745a160b7751a14f12b5ccff5b15ddd Mon Sep 17 00:00:00 2001
From: mtrela <mtrela@syncad.com>
Date: Tue, 24 Nov 2020 12:22:22 +0100
Subject: [PATCH] Data from `hive_reblogs` is removed when a post is deleted

---
 hive/db/schema.py                             |  2 +-
 ...elete.sql => delete_reblog_feed_cache.sql} |  6 ++-
 hive/db/sql_scripts/hive_post_operations.sql  | 49 ++++++++++++-------
 hive/indexer/feed_cache.py                    | 29 -----------
 hive/indexer/posts.py                         | 19 ++-----
 hive/indexer/reblog.py                        |  3 +-
 6 files changed, 41 insertions(+), 67 deletions(-)
 rename hive/db/sql_scripts/{reblog_delete.sql => delete_reblog_feed_cache.sql} (72%)
 delete mode 100644 hive/indexer/feed_cache.py

diff --git a/hive/db/schema.py b/hive/db/schema.py
index 9709fd39d..082ee6784 100644
--- a/hive/db/schema.py
+++ b/hive/db/schema.py
@@ -612,7 +612,7 @@ def setup(db):
       "condenser_get_discussions_by_comments.sql",
       "condenser_get_account_reputations.sql",
       "update_follow_count.sql",
-      "reblog_delete.sql"
+      "delete_reblog_feed_cache.sql"
     ]
     from os.path import dirname, realpath
     dir_path = dirname(realpath(__file__))
diff --git a/hive/db/sql_scripts/reblog_delete.sql b/hive/db/sql_scripts/delete_reblog_feed_cache.sql
similarity index 72%
rename from hive/db/sql_scripts/reblog_delete.sql
rename to hive/db/sql_scripts/delete_reblog_feed_cache.sql
index 332027849..6b26e2314 100644
--- a/hive/db/sql_scripts/reblog_delete.sql
+++ b/hive/db/sql_scripts/delete_reblog_feed_cache.sql
@@ -1,7 +1,9 @@
-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 _permlink hive_permlink_data.permlink%TYPE,
   in _account hive_accounts.name%TYPE,
diff --git a/hive/db/sql_scripts/hive_post_operations.sql b/hive/db/sql_scripts/hive_post_operations.sql
index 2bf71e82f..800b28b11 100644
--- a/hive/db/sql_scripts/hive_post_operations.sql
+++ b/hive/db/sql_scripts/hive_post_operations.sql
@@ -109,32 +109,45 @@ END
 $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(
   in _author hive_accounts.name%TYPE,
   in _permlink hive_permlink_data.permlink%TYPE,
-  in _block_num hive_blocks.num%TYPE)
-RETURNS TABLE (id hive_posts.id%TYPE, depth hive_posts.depth%TYPE)
+  in _block_num hive_blocks.num%TYPE,
+  in _delete_feed_cache BOOLEAN)
+RETURNS VOID
 LANGUAGE plpgsql
 AS
 $function$
+DECLARE
+  __post_id INT;
 BEGIN
-  RETURN QUERY UPDATE hive_posts AS hp
-    SET counter_deleted =
-    (
-      SELECT max( hps.counter_deleted ) + 1
-      FROM hive_posts hps
-      INNER JOIN hive_accounts ha ON hps.author_id = ha.id
-      INNER JOIN hive_permlink_data hpd ON hps.permlink_id = hpd.id
-      WHERE ha.name = _author AND hpd.permlink = _permlink
-    )
-    , block_num = _block_num
-  FROM hive_posts hp1
-  INNER JOIN hive_accounts ha ON hp1.author_id = ha.id
-  INNER JOIN hive_permlink_data hpd ON hp1.permlink_id = hpd.id
-  WHERE hp.id = hp1.id AND ha.name = _author AND hpd.permlink = _permlink AND hp1.counter_deleted = 0
-  RETURNING hp.id, hp.depth;
+
+  __post_id = find_comment_id( _author, _permlink, False );
+
+  IF __post_id = 0 THEN
+    RETURN;
+  END IF;
+
+  UPDATE hive_posts
+  SET counter_deleted =
+  (
+    SELECT max( counter_deleted ) + 1
+    FROM hive_posts
+    WHERE id = __post_id
+  )
+  ,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
 $function$
 ;
diff --git a/hive/indexer/feed_cache.py b/hive/indexer/feed_cache.py
deleted file mode 100644
index 3dc4d3b32..000000000
--- a/hive/indexer/feed_cache.py
+++ /dev/null
@@ -1,29 +0,0 @@
-"""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)
diff --git a/hive/indexer/posts.py b/hive/indexer/posts.py
index 08edcbb58..732d205f2 100644
--- a/hive/indexer/posts.py
+++ b/hive/indexer/posts.py
@@ -10,7 +10,7 @@ from diff_match_patch import diff_match_patch
 from hive.db.adapter import Db
 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.notify import Notify
 from hive.indexer.post_data_cache import PostDataCache
@@ -400,21 +400,10 @@ class Posts(DbAdapterHolder):
     def delete(cls, op):
         """Marks a post record as being deleted."""
 
-        sql = """
-              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']
+        delete_feed_cache = not DbState.is_initial_sync()
 
-        if depth == 0 and not DbState.is_initial_sync():
-            # TODO: delete from hive_reblogs -- otherwise feed cache gets
-            # populated with deleted posts somwrimas
-            FeedCache.delete(pid)
+        sql = "SELECT delete_hive_post((:author)::varchar, (:permlink)::varchar, (:block_num)::int, (:delete_feed_cache)::BOOLEAN );"
+        DB.query_no_return(sql, author=op['author'], permlink = op['permlink'], block_num=op['block_num'], delete_feed_cache=delete_feed_cache)
 
     @classmethod
     def _verify_post_against_community(cls, op, community_id, is_valid, is_muted):
diff --git a/hive/indexer/reblog.py b/hive/indexer/reblog.py
index a0023fe63..0647e29ec 100644
--- a/hive/indexer/reblog.py
+++ b/hive/indexer/reblog.py
@@ -5,7 +5,6 @@ import logging
 from hive.db.db_state import DbState
 
 from hive.indexer.accounts import Accounts
-from hive.indexer.feed_cache import FeedCache
 from hive.indexer.db_adapter_holder import DbAdapterHolder
 from hive.utils.normalize import escape_characters
 
@@ -54,7 +53,7 @@ class Reblog(DbAdapterHolder):
         """
         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);
         assert status is not None
         if status == 0:
-- 
GitLab