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.
This commit is contained in:
Coconut
2013-03-06 12:19:20 -05:00
parent 31927b54d7
commit 3fd375b23a
3 changed files with 33 additions and 7 deletions

View File

@@ -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")

View File

@@ -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

View File

@@ -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