Add change_seq support

This commit is contained in:
Kira 2019-08-15 07:34:11 -07:00
parent 286e8c5c4e
commit 41e1a1a0f7
6 changed files with 108 additions and 4 deletions

View File

@ -15,6 +15,7 @@ module PostIndex
indexes :score, type: 'integer'
indexes :fav_count, type: 'integer'
indexes :tag_count, type: 'integer'
indexes :change_seq, type: 'long'
indexes :tag_count_general, type: 'integer'
indexes :tag_count_artist, type: 'integer'
@ -173,6 +174,7 @@ module PostIndex
score: score,
fav_count: fav_count,
tag_count: tag_count,
change_seq: change_seq,
tag_count_general: tag_count_general,
tag_count_artist: tag_count_artist,

View File

@ -159,6 +159,7 @@ class ElasticPostQueryBuilder
add_range_relation(q[:score], :score, must)
add_range_relation(q[:fav_count], :fav_count, must)
add_range_relation(q[:filesize], :file_size, must)
add_range_relation(q[:change_seq], :change_seq, must)
add_range_relation(q[:date], :created_at, must)
add_range_relation(q[:age], :created_at, must)
@ -442,6 +443,12 @@ class ElasticPostQueryBuilder
when "id_desc"
order.push({id: :desc})
when "change", "change_desc"
order.push({change_seq: :desc})
when "change_asc"
order.push({change_seq: :asc})
when "md5"
order.push({md5: :desc})

View File

@ -138,6 +138,7 @@ class PostQueryBuilder
relation = add_range_relation(q[:score], "posts.score", relation)
relation = add_range_relation(q[:fav_count], "posts.fav_count", relation)
relation = add_range_relation(q[:filesize], "posts.file_size", relation)
relation = add_range_relation(q[:change_seq], 'posts.change_seq', relation)
relation = add_range_relation(q[:date], "posts.created_at", relation)
relation = add_range_relation(q[:age], "posts.created_at", relation)
TagCategory.categories.each do |category|
@ -598,4 +599,4 @@ class PostQueryBuilder
relation
end
end
end

View File

@ -19,7 +19,7 @@ class Tag < ApplicationRecord
-source id -id date age order limit -status status tagcount parent -parent
child pixiv_id pixiv search upvote downvote voted filetype -filetype flagger
-flagger appealer -appealer disapproval -disapproval set -set randseed -voted
-upvote -downvote description -description
-upvote -downvote description -description change
] + TagCategory.short_name_list.map {|x| "#{x}tags"} + COUNT_METATAGS + COUNT_METATAG_SYNONYMS
SUBQUERY_METATAGS = %w[commenter comm noter noteupdater artcomm flagger -flagger appealer -appealer]
@ -38,6 +38,7 @@ class Tag < ApplicationRecord
portrait landscape
filesize filesize_asc
tagcount tagcount_asc
change change_desc change_asc
rank
random
custom
@ -686,6 +687,9 @@ class Tag < ApplicationRecord
when "filesize"
q[:filesize] = parse_helper_fudged(g2, :filesize)
when "change"
q[:change_seq] = parse_helper(g2)
when "source"
src = g2.gsub(/\A"(.*)"\Z/, '\1')
q[:source] = (src.to_escaped_for_sql_like + "%").gsub(/%+/, '%')

View File

@ -0,0 +1,31 @@
class AddChangeSeq < ActiveRecord::Migration[5.2]
def up
execute "set statement_timeout = 0"
add_column :posts, :change_seq, :bigserial, null: false
add_index :posts, :change_seq, unique: true
fields = %w[tag_string parent_id source approver_id rating description md5 is_deleted is_pending is_flagged is_rating_locked]
fields += %w[is_status_locked is_note_locked bit_flags has_active_children last_noted_at]
conditional = fields.map {|field| "NEW.#{field} != OLD.#{field}"}.join(' OR ')
execute <<SQL
CREATE OR REPLACE FUNCTION posts_trigger_change_seq()
RETURNS trigger AS
$BODY$
BEGIN
IF #{conditional}
THEN
NEW.change_seq = nextval('public.posts_change_seq_seq');
END IF;
RETURN NEW;
END;
$BODY$ LANGUAGE 'plpgsql';
SQL
execute "CREATE TRIGGER posts_update_change_seq BEFORE UPDATE ON posts FOR EACH ROW EXECUTE PROCEDURE posts_trigger_change_seq()"
end
def down
execute "set statement_timeout = 0"
execute "drop trigger posts_update_change_seq ON posts"
remove_column :posts, :change_seq
execute "drop function posts_trigger_change_seq"
end
end

View File

@ -36,6 +36,23 @@ CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA public;
COMMENT ON EXTENSION pg_trgm IS 'text similarity measurement and index searching based on trigrams';
--
-- Name: posts_trigger_change_seq(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.posts_trigger_change_seq() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
IF NEW.tag_string != OLD.tag_string OR NEW.parent_id != OLD.parent_id OR NEW.source != OLD.source OR NEW.approver_id != OLD.approver_id OR NEW.rating != OLD.rating OR NEW.description != OLD.description OR NEW.md5 != OLD.md5 OR NEW.is_deleted != OLD.is_deleted OR NEW.is_pending != OLD.is_pending OR NEW.is_flagged != OLD.is_flagged OR NEW.is_rating_locked != OLD.is_rating_locked OR NEW.is_status_locked != OLD.is_status_locked OR NEW.is_note_locked != OLD.is_note_locked OR NEW.bit_flags != OLD.bit_flags OR NEW.has_active_children != OLD.has_active_children OR NEW.last_noted_at != OLD.last_noted_at
THEN
NEW.change_seq = nextval('public.posts_change_seq_seq');
END IF;
RETURN NEW;
END;
$$;
--
-- Name: sourcepattern(text); Type: FUNCTION; Schema: public; Owner: -
--
@ -1967,10 +1984,30 @@ CREATE TABLE public.posts (
tag_count_species integer DEFAULT 0 NOT NULL,
tag_count_invalid integer DEFAULT 0 NOT NULL,
description text DEFAULT ''::text NOT NULL,
comment_count integer DEFAULT 0 NOT NULL
comment_count integer DEFAULT 0 NOT NULL,
change_seq bigint NOT NULL
);
--
-- Name: posts_change_seq_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.posts_change_seq_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: posts_change_seq_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.posts_change_seq_seq OWNED BY public.posts.change_seq;
--
-- Name: posts_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
@ -3089,6 +3126,13 @@ ALTER TABLE ONLY public.post_votes ALTER COLUMN id SET DEFAULT nextval('public.p
ALTER TABLE ONLY public.posts ALTER COLUMN id SET DEFAULT nextval('public.posts_id_seq'::regclass);
--
-- Name: posts change_seq; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.posts ALTER COLUMN change_seq SET DEFAULT nextval('public.posts_change_seq_seq'::regclass);
--
-- Name: saved_searches id; Type: DEFAULT; Schema: public; Owner: -
--
@ -4482,6 +4526,13 @@ CREATE INDEX index_post_versions_on_updater_ip_addr ON public.post_versions USIN
CREATE UNIQUE INDEX index_post_votes_on_user_id_and_post_id ON public.post_votes USING btree (user_id, post_id);
--
-- Name: index_posts_on_change_seq; Type: INDEX; Schema: public; Owner: -
--
CREATE UNIQUE INDEX index_posts_on_change_seq ON public.posts USING btree (change_seq);
--
-- Name: index_posts_on_created_at; Type: INDEX; Schema: public; Owner: -
--
@ -4902,6 +4953,13 @@ CREATE INDEX index_wiki_pages_on_title_pattern ON public.wiki_pages USING btree
CREATE INDEX index_wiki_pages_on_updated_at ON public.wiki_pages USING btree (updated_at);
--
-- Name: posts posts_update_change_seq; Type: TRIGGER; Schema: public; Owner: -
--
CREATE TRIGGER posts_update_change_seq BEFORE UPDATE ON public.posts FOR EACH ROW EXECUTE PROCEDURE public.posts_trigger_change_seq();
--
-- Name: blips trigger_blips_on_update; Type: TRIGGER; Schema: public; Owner: -
--
@ -5187,6 +5245,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20190718201354'),
('20190801210547'),
('20190804010156'),
('20190810064211');
('20190810064211'),
('20190815131908');