Browse Source

Add jsonb_path_ops index on activities

pull/63/head
Toromino 4 months ago
parent
commit
9b94176086

+ 1
- 0
migrations/2019-06-09-220843_activitypub_data_indices/down.sql View File

@@ -0,0 +1 @@
1
+DROP INDEX IF EXISTS activities_ap_data;

+ 1
- 0
migrations/2019-06-09-220843_activitypub_data_indices/up.sql View File

@@ -0,0 +1 @@
1
+CREATE INDEX activities_ap_data ON activities USING GIN (data jsonb_path_ops);

+ 6
- 5
src/activity.rs View File

@@ -21,10 +21,9 @@ pub fn count_ap_object_replies_by_id(
21 21
     object_id: &str,
22 22
 ) -> Result<usize, diesel::result::Error> {
23 23
     match sql_query(format!(
24
-        "SELECT * FROM activities WHERE data->'object'->>'inReplyTo' = '{}';",
24
+        "SELECT * FROM activities WHERE data @> '{{\"object\": {{\"inReplyTo\": \"{}\"}}}}';",
25 25
         runtime_escape(object_id)
26 26
     ))
27
-    .clone()
28 27
     .load::<QueryActivity>(db_connection)
29 28
     {
30 29
         Ok(activity_arr) => Ok(activity_arr.len()),
@@ -37,7 +36,9 @@ pub fn count_ap_object_reactions_by_id(
37 36
     object_id: &str,
38 37
     reaction: &str,
39 38
 ) -> Result<usize, diesel::result::Error> {
40
-    match sql_query(format!("SELECT * FROM activities WHERE data->>'type' = '{reaction_type}' AND data->>'object'= '{id}';",
39
+    match sql_query(format!(
40
+        "SELECT * FROM activities WHERE data @> '{{\"type\": \"{reaction_type}\"}}' \
41
+        AND data @> '{{\"object\": \"{id}\"}}';",
41 42
                             reaction_type = runtime_escape(reaction),
42 43
                             id = runtime_escape(object_id)))
43 44
         .load::<QueryActivity>(db_connection)
@@ -107,7 +108,7 @@ pub fn get_ap_activity_by_id(
107 108
     activity_id: &str,
108 109
 ) -> Result<Activity, diesel::result::Error> {
109 110
     match sql_query(format!(
110
-        "SELECT * FROM activities WHERE data->>'id' = '{}' LIMIT 1;",
111
+        "SELECT * FROM activities WHERE data @> '{{\"id\": \"{}\"}}' LIMIT 1;",
111 112
         runtime_escape(activity_id)
112 113
     ))
113 114
     .clone()
@@ -130,7 +131,7 @@ pub fn get_ap_object_by_id(
130 131
     object_id: &str,
131 132
 ) -> Result<Activity, diesel::result::Error> {
132 133
     match sql_query(format!(
133
-        "SELECT * FROM activities WHERE data->'object'->>'id' = '{}' LIMIT 1;",
134
+        "SELECT * FROM activities WHERE data @> '{{\"object\": {{\"id\": \"{}\"}}}}' LIMIT 1;",
134 135
         runtime_escape(object_id)
135 136
     ))
136 137
     .clone()

Loading…
Cancel
Save