From 3fd375b23a2c6dc8b212eea2c5bf39dcf517d833 Mon Sep 17 00:00:00 2001 From: Coconut Date: Wed, 6 Mar 2013 12:19:20 -0500 Subject: [PATCH] Added pixiv: metatag and Pixiv source indexes Adds a pixiv: metatag to quickly find posts by Pixiv ID. Adds source:pixiv/* searches to allow left-anchored source searches for Pixiv sources. Added relevant indexes to make these queries efficient. --- app/logical/post_query_builder.rb | 16 ++++++++++++---- app/models/tag.rb | 9 ++++++--- .../20130302214500_add_index_pixiv_on_posts.rb | 15 +++++++++++++++ 3 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20130302214500_add_index_pixiv_on_posts.rb diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index 49018c7a5..78a6aff3f 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -124,8 +124,9 @@ class PostQueryBuilder relation = add_range_relation(q[:artist_tag_count], "posts.tag_count_artist", relation) relation = add_range_relation(q[:copyright_tag_count], "posts.tag_count_copyright", relation) relation = add_range_relation(q[:character_tag_count], "posts.tag_count_character", relation) - relation = add_range_relation(q[:tag_count], "posts.tag_count", relation) - + relation = add_range_relation(q[:tag_count], "posts.tag_count", relation) + relation = add_range_relation(q[:pixiv], "substring(posts.source, 'pixiv.net/img.*/([0-9]+)[^/]*$')::integer", relation) + if q[:md5] relation = relation.where(["posts.md5 IN (?)", q[:md5]]) has_constraints! @@ -146,6 +147,13 @@ class PostQueryBuilder if q[:source] if q[:source] == "none%" relation = relation.where("(posts.source = '' OR posts.source IS NULL)") + elsif q[:source] =~ /^(pixiv\/|%\.?pixiv(\.net(\/img)?)?(%\/|(?=%$)))(.+)$/ + if $5 == "%" + relation = relation.where("substring(posts.source, 'pixiv.net/img.*/([^/]*/[^/]*)$') IS NOT NULL") + else + relation = relation.where("substring(posts.source, 'pixiv.net/img.*/([^/]*/[^/]*)$') LIKE ? ESCAPE E'\\\\'", $5) + end + has_constraints! else relation = relation.where("posts.source LIKE ? ESCAPE E'\\\\'", q[:source]) has_constraints! @@ -241,8 +249,8 @@ class PostQueryBuilder when "filesize_asc" relation = relation.order("posts.file_size ASC") - when "rank" - relation = relation.order("log(3, posts.score) + (extract(epoch from posts.created_at) - extract(epoch from timestamp '2005-05-24')) / 45000 DESC") + when "rank" + relation = relation.order("log(3, posts.score) + (extract(epoch from posts.created_at) - extract(epoch from timestamp '2005-05-24')) / 45000 DESC") else relation = relation.order("posts.id DESC") diff --git a/app/models/tag.rb b/app/models/tag.rb index 917f339c4..a8a473af3 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -1,5 +1,5 @@ class Tag < ActiveRecord::Base - METATAGS = "-user|user|-approver|approver|-pool|pool|-fav|fav|sub|md5|-rating|rating|width|height|mpixels|score|filesize|source|id|date|order|status|tagcount|gentags|arttags|chartags|copytags|parent" + METATAGS = "-user|user|-approver|approver|-pool|pool|-fav|fav|sub|md5|-rating|rating|width|height|mpixels|score|filesize|source|id|date|order|status|tagcount|gentags|arttags|chartags|copytags|parent|pixiv" attr_accessible :category after_save :update_category_cache_for_all has_one :wiki_page, :foreign_key => "name", :primary_key => "title" @@ -308,8 +308,8 @@ class Tag < ActiveRecord::Base q[:filesize] = parse_helper($2, :filesize) when "source" - q[:source] = $2.to_escaped_for_sql_like + "%" - + q[:source] = ($2.to_escaped_for_sql_like + "%").gsub(/%+/, '%') + when "date" q[:date] = parse_helper($2, :date) @@ -337,6 +337,9 @@ class Tag < ActiveRecord::Base when "status" q[:status] = $2 + when "pixiv" + q[:pixiv] = parse_helper($2) + end else diff --git a/db/migrate/20130302214500_add_index_pixiv_on_posts.rb b/db/migrate/20130302214500_add_index_pixiv_on_posts.rb new file mode 100644 index 000000000..cf833e5e8 --- /dev/null +++ b/db/migrate/20130302214500_add_index_pixiv_on_posts.rb @@ -0,0 +1,15 @@ +class AddIndexPixivOnPosts < ActiveRecord::Migration + def up + execute "set statement_timeout = 0" + execute "CREATE INDEX index_posts_on_pixiv_suffix ON posts USING btree + ((substring(source, 'pixiv.net/img.*/([^/]*/[^/]*)$')) text_pattern_ops);" + execute "CREATE INDEX index_posts_on_pixiv_id ON posts USING btree + ((substring(source, 'pixiv.net/img.*/([0-9]+)[^/]*$')::integer));" + end + + def down + execute "set statement_timeout = 0" + execute "DROP INDEX index_posts_on_pixiv_suffix;" + execute "DROP INDEX index_posts_on_pixiv_id;" + end +end