-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Transmitter.update_fields when full indexing
By default Sphinx selects only 20 rows. Therefor Transmitter updates not all rows when full reindex running. This commit will fix this. And adds new method ThinkingSphinx.find_in_batches. https://jira.railsc.ru/browse/PC4-15139
- Loading branch information
Showing
5 changed files
with
179 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
lib/sphinx/integration/extensions/thinking_sphinx/statements.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
module Sphinx | ||
module Integration | ||
module Extensions | ||
module ThinkingSphinx | ||
module Statements | ||
def replace(index_name, data) | ||
query = ::Riddle::Query::Insert.new(index_name, data.keys, data.values).replace!.to_sql | ||
execute(query, on_slaves: replication?) | ||
end | ||
|
||
def update(index_name, data, where) | ||
query = ::Sphinx::Integration::Extensions::Riddle::Query::Update.new(index_name, data, where).to_sql | ||
execute(query) | ||
end | ||
|
||
def delete(index_name, document_id) | ||
query = ::Riddle::Query::Delete.new(index_name, document_id).to_sql | ||
execute(query) | ||
end | ||
|
||
def soft_delete(index_name, document_id) | ||
update(index_name, {sphinx_deleted: 1}, {id: document_id}) | ||
end | ||
|
||
def select(values, index_name, where) | ||
query = ::Riddle::Query::Select. | ||
new. | ||
reset_values. | ||
values(values). | ||
from(index_name). | ||
where(where). | ||
to_sql | ||
execute(query).to_a | ||
end | ||
|
||
def find_in_batches(index_name, where, options = {}) | ||
bound = select("min(sphinx_internal_id) as min_id, max(sphinx_internal_id) as max_id", | ||
index_name, | ||
where).first | ||
return unless bound | ||
|
||
min = bound["min_id"].to_i | ||
max = bound["max_id"].to_i | ||
return if max.zero? | ||
batch_size = options.fetch(:batch_size, 1_000) | ||
|
||
while min <= max | ||
where[:sphinx_internal_id] = Range.new(min, min + batch_size - 1) | ||
ids = select("sphinx_internal_id", index_name, where).map { |row| row["sphinx_internal_id"].to_i } | ||
yield ids if ids.any? | ||
min += batch_size | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
spec/sphinx/integration/extensions/thinking_sphinx/statements_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require "spec_helper" | ||
|
||
describe ThinkingSphinx do | ||
describe ".replace" do | ||
it do | ||
expect(ThinkingSphinx). | ||
to receive(:execute).with("REPLACE INTO product (`company_id`) VALUES (1)", on_slaves: false) | ||
ThinkingSphinx.replace("product", company_id: 1) | ||
end | ||
end | ||
|
||
describe ".update" do | ||
it do | ||
expect(ThinkingSphinx). | ||
to receive(:execute).with("UPDATE product SET company_id = 1 WHERE `id` = 1") | ||
|
||
ThinkingSphinx.update("product", {company_id: 1}, id: 1) | ||
end | ||
end | ||
|
||
describe ".delete" do | ||
it do | ||
expect(ThinkingSphinx). | ||
to receive(:execute).with("DELETE FROM product WHERE id = 1") | ||
|
||
ThinkingSphinx.delete("product", 1) | ||
end | ||
end | ||
|
||
describe ".soft_delete" do | ||
it do | ||
expect(ThinkingSphinx). | ||
to receive(:execute).with("UPDATE product SET sphinx_deleted = 1 WHERE `id` = 1") | ||
|
||
ThinkingSphinx.soft_delete("product", 1) | ||
end | ||
end | ||
|
||
describe ".select" do | ||
it do | ||
expect(ThinkingSphinx). | ||
to receive(:execute).with("SELECT company_id FROM product WHERE `id` = 1") | ||
|
||
ThinkingSphinx.select("company_id", "product", id: 1) | ||
end | ||
end | ||
|
||
describe ".find_in_batches" do | ||
it do | ||
expect(ThinkingSphinx).to( | ||
receive(:execute). | ||
with("SELECT min(sphinx_internal_id) as min_id, max(sphinx_internal_id) as max_id " + | ||
"FROM product WHERE `company_id` = 1"). | ||
and_return([{"min_id" => "1", "max_id" => "2"}]) | ||
) | ||
|
||
expect(ThinkingSphinx).to( | ||
receive(:execute). | ||
with("SELECT sphinx_internal_id " + | ||
"FROM product WHERE `company_id` = 1 AND `sphinx_internal_id` BETWEEN 1 AND 1000"). | ||
and_return([{"sphinx_internal_id" => "1"}, {"sphinx_internal_id" => "2"}]) | ||
) | ||
|
||
result = [] | ||
ThinkingSphinx.find_in_batches("product", company_id: 1) { |ids| result += ids } | ||
expect(result).to eq [1, 2] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters