Skip to content

A platform for community discussion. Free, open, simple.

PERF: Avoid resolving unrelated JSON:API default sorts (#43791)

Loïc Guitaut9ef8d9d38c02
diff --git a/lib/json_api_kit/default_sorts.rb b/lib/json_api_kit/default_sorts.rb
index f5bb33f90e7..a9d8b07dd62 100644
--- a/lib/json_api_kit/default_sorts.rb
+++ b/lib/json_api_kit/default_sorts.rb
@@ -7,33 +7,10 @@ module JsonApiKit
@orderings = {}
end
- def for(resource) = orderings[resource] ||= ordering_for(resource)
+ def for(resource) = orderings[resource] ||= History.new(resource:, changes:).ordering
private
attr_reader :changes, :orderings
-
- def ordering_for(resource)
- defaults
- .fetch(resource.type) { return resource.default_ordering }
- .then { resource.default_ordering(it.ordering) }
- end
-
- def defaults
- @defaults ||= changes.reduce({}) { |defaults, change| advance(defaults, through: change) }
- end
-
- def advance(defaults, through:)
- translated_defaults(defaults, through:).merge(
- through.current_default_sorts.index_by(&:type),
- ) { |_type, earlier_default, _later_default| earlier_default }
- end
-
- def translated_defaults(defaults, through:)
- defaults
- .values
- .map { |default| default.convert_names { through.current_names(it).sole } }
- .index_by(&:type)
- end
end
end
diff --git a/lib/json_api_kit/default_sorts/history.rb b/lib/json_api_kit/default_sorts/history.rb
new file mode 100644
index 00000000000..8f72c96d081
--- /dev/null
+++ b/lib/json_api_kit/default_sorts/history.rb
@@ -0,0 +1,53 @@
+# frozen_string_literal: true
+
+module JsonApiKit
+ class DefaultSorts
+ class History
+ def initialize(resource:, changes:)
+ @resource = resource
+ @changes = changes
+ end
+
+ def ordering
+ each_historical_default do |default, later_changes|
+ return resource.default_ordering(current_ordering(default, through: later_changes))
+ end
+ resource.default_ordering
+ end
+
+ private
+
+ attr_reader :resource, :changes
+
+ def each_historical_default
+ return if changes.empty?
+
+ changes
+ .each_with_index
+ .zip(types_after_changes(changes.each)) do |(change, index), type|
+ change.each_current_default_sort(type) do |default|
+ yield(default, changes.drop(index + 1))
+ end
+ end
+ end
+
+ def types_after_changes(changes)
+ Enumerator.produce(changes.next.current_resource_type(type_at_pin)) do |type|
+ changes.next.current_resource_type(type)
+ end
+ end
+
+ def type_at_pin
+ changes
+ .reverse_each
+ .reduce(resource.type) { |name, change| change.previous_resource_type(name) }
+ end
+
+ def current_ordering(historical_default, through:)
+ through
+ .reduce(historical_default) { |default, change| change.current_default_sort(default) }
+ .ordering
+ end
+ end
+ end
+end
diff --git a/lib/json_api_kit/version_change.rb b/lib/json_api_kit/version_change.rb
index 724b1df2e24..eaf72a39440 100644
--- a/lib/json_api_kit/version_change.rb
+++ b/lib/json_api_kit/version_change.rb
@@ -64,6 +64,10 @@ module JsonApiKit
raise ArgumentError, "#{source} #{conflict.message}"
end
+ def current_resource_type(type) = type_renames.current(type)
+
+ def previous_resource_type(type) = type_renames.previous(type)
+
def current_names(name) = transformations.current_names(current_type(name))
def current_attributes(attributes)
@@ -78,16 +82,20 @@ module JsonApiKit
transformations.previous_values(attributes).transform_keys { previous_type(it) }
end
- def current_default_sorts
- default_sorts.convert_names { transformations.current_names(it).sole }
+ def current_default_sort(default) = default.convert_names { current_names(it).sole }
+
+ def each_current_default_sort(type)
+ default_sorts.each_for(type) do |default|
+ yield default.convert_names { transformations.current_names(it).sole }
+ end
end
private
attr_reader :transformations, :type_renames, :default_sorts
- def current_type(name) = name.convert_type { type_renames.current(it) }
+ def current_type(name) = name.convert_type { current_resource_type(it) }
- def previous_type(name) = name.convert_type { type_renames.previous(it) }
+ def previous_type(name) = name.convert_type { previous_resource_type(it) }
end
end
diff --git a/lib/json_api_kit/version_change/default_sorts.rb b/lib/json_api_kit/version_change/default_sorts.rb
index 503ab226172..b50e62107b5 100644
--- a/lib/json_api_kit/version_change/default_sorts.rb
+++ b/lib/json_api_kit/version_change/default_sorts.rb
@@ -6,18 +6,19 @@ module JsonApiKit
Conflict = Class.new(StandardError)
def initialize(defaults)
- @defaults = defaults.dup
+ @defaults = defaults.group_by(&:type)
+ @defaults.default = [].freeze
end
def verify!
defaults
- .map(&:type)
- .tally
- .detect { |_type, count| count > 1 }
- .try { |type, _count| raise Conflict, "changes the default sort for #{type} twice." }
+ .detect { |_type, declarations| declarations.many? }
+ .try do |type, _declarations|
+ raise Conflict, "changes the default sort for #{type} twice."
+ end
end
- def convert_names(&) = defaults.map { it.convert_names(&) }
+ def each_for(type, &) = defaults[type].each(&)
private
diff --git a/spec/lib/json_api_kit/default_sorts/history_spec.rb b/spec/lib/json_api_kit/default_sorts/history_spec.rb
new file mode 100644
index 00000000000..472f6c4a273
--- /dev/null
+++ b/spec/lib/json_api_kit/default_sorts/history_spec.rb
@@ -0,0 +1,293 @@
+# frozen_string_literal: true
+
+RSpec.describe JsonApiKit::DefaultSorts::History do
+ subject(:history) { described_class.new(resource:, changes:) }
+
+ let(:resource) do
+ Class.new(JsonApiKit::Resource) do
+ model Topic
+ type :topics
+ sort :created_at
+ sort :title
+ default_sort title: :asc
+ end
+ end
+ let(:changes) { [] }
+
+ describe "#ordering" do
+ subject(:ordering) { history.ordering }
+
+ it "returns the current default without version changes" do
+ expect(ordering).to eq("title" => :asc)
+ end
+
+ context "when the default changed after the pin" do
+ let(:changes) do
+ [
+ Class
+ .new(JsonApiKit::VersionChange) do
+ resource(:topics) { changed_default_sort from: { created_at: :desc } }
+ end
+ .new(__FILE__),
+ ]
+ end
+
+ it "returns the historical default" do
+ expect(ordering).to eq("created_at" => :desc)
+ end
+ end
+
+ context "when the default changed several times" do
+ let(:changes) do
+ [
+ Class
+ .new(JsonApiKit::VersionChange) do
+ resource(:topics) { changed_default_sort from: { created_at: :asc } }
+ end
+ .new(__FILE__),
+ Class
+ .new(JsonApiKit::VersionChange) do
+ resource(:topics) { changed_default_sort from: { created_at: :desc } }
+ end
+ .new(__FILE__),
+ ]
+ end
+
+ it "uses the earliest default after the pin" do
+ expect(ordering).to eq("created_at" => :asc)
+ end
+ end
+
+ context "when the historical default is empty" do
+ let(:changes) do
+ [
+ Class
+ .new(JsonApiKit::VersionChange) { resource(:topics) { changed_default_sort from: {} } }
+ .new(__FILE__),
+ ]
+ end
+
+ it "preserves the empty ordering" do
+ expect(ordering).to be_empty
+ end
+ end
+
+ context "when the same change renames a sort and its resource type" do
+ let(:changes) do
+ [
+ Class
+ .new(JsonApiKit::VersionChange) do
+ renamed_type from: :discussions, to: :topics
+ resource :topics do
+ renamed_sort from: :posted_at, to: :created_at
+ changed_default_sort from: { posted_at: :desc }
+ end
+ end
+ .new(__FILE__),
+ ]
+ end
+
+ it "uses the current sort name" do
+ expect(ordering).to eq("created_at" => :desc)
+ end
+ end
+
+ context "when sorts and types change before and after the default declaration" do
+ let(:changes) do
+ [
+ Class
+ .new(JsonApiKit::VersionChange) do
+ resource(:discussions) { renamed_sort from: :posted_at, to: :published_at }
+ end
+ .new(__FILE__),
+ Class
+ .new(JsonApiKit::VersionChange) do
+ resource(:discussions) { changed_default_sort from: { published_at: :desc } }
+ end
+ .new(__FILE__),
+ Class
+ .new(JsonApiKit::VersionChange) do
+ renamed_type from: :discussions, to: :topics
+ resource(:topics) { renamed_sort from: :published_at, to: :created_at }
+ end
+ .new(__FILE__),
+ ]
+ end
+
+ it "translates from the declaration's version" do
+ expect(ordering).to eq("created_at" => :desc)
+ end
+ end
+
+ context "when an earlier rename reuses the historical default's spelling" do
+ let(:changes) do
+ [
+ Class
+ .new(JsonApiKit::VersionChange) do
+ resource :topics do
+ renamed_sort from: :created_at, to: :title
+ renamed_sort from: :posted_at, to: :created_at
+ end
+ end
+ .new(__FILE__),
+ Class
+ .new(JsonApiKit::VersionChange) do
+ resource(:topics) { changed_default_sort from: { created_at: :desc } }
+ end
+ .new(__FILE__),
+ ]
+ end
+
+ it "does not apply changes preceding the default declaration" do
+ expect(ordering).to eq("created_at" => :desc)
+ end
+ end
+
+ context "when a later change merges two default sort names" do
+ let(:changes) do
+ [
+ Class
+ .new(JsonApiKit::VersionChange) do
+ resource(:topics) do
+ changed_default_sort from: { posted_date: :desc, posted_time: :desc }
+ end
+ end
+ .new(__FILE__),
+ Class
+ .new(JsonApiKit::VersionChange) do
+ resource :topics do
+ merged_attributes from: %i[posted_date posted_time],
+ to: :created_at,
+ up: ->(date, time) { "#{date} #{time}" },
+ down: ->(time) { time.split(" ") }
+ end
+ end
+ .new(__FILE__),
+ ]
+ end
+
+ it "uses the merged sort once" do
+ expect(ordering).to eq("created_at" => :desc)
+ end
+ end
+
+ context "when the historical sort is undeclared" do
+ let(:changes) do
+ [
+ Class
+ .new(JsonApiKit::VersionChange) do
+ resource(:topics) { changed_default_sort from: { unknown: :asc } }
+ end
+ .new(__FILE__),
+ ]
+ end
+
+ it "raises a declaration error" do
+ expect { ordering }.to raise_error(
+ JsonApiKit::Resource::Sorting::UndeclaredDefault,
+ /unknown/,
+ )
+ end
+ end
+
+ context "when the change belongs to another resource" do
+ let(:changes) do
+ [
+ Class
+ .new(JsonApiKit::VersionChange) do
+ resource(:users) { changed_default_sort from: { username: :desc } }
+ end
+ .new(__FILE__),
+ ]
+ end
+
+ it "uses the resource's current default" do
+ expect(ordering).to eq("title" => :asc)
+ end
+ end
+
+ context "when two resources swap type names" do
+ subject(:orderings) do
+ resources.to_h { [it.type, described_class.new(resource: it, changes:).ordering] }
+ end
+
+ let(:resources) { [resource, Class.new(resource) { type :users }] }
+ let(:changes) do
+ [
+ Class
+ .new(JsonApiKit::VersionChange) do
+ resource(:topics) { changed_default_sort from: { created_at: :desc } }
+ resource(:users) { changed_default_sort from: { title: :desc } }
+ end
+ .new(__FILE__),
+ Class
+ .new(JsonApiKit::VersionChange) do
+ renamed_type from: :topics, to: :users
+ renamed_type from: :users, to: :topics
+ resource(:topics) { renamed_sort from: :title, to: :created_at }
+ resource(:users) { renamed_sort from: :created_at, to: :title }
+ end
+ .new(__FILE__),
+ Class
+ .new(JsonApiKit::VersionChange) do
+ resource(:topics) { changed_default_sort from: { title: :asc } }
+ resource(:users) { changed_default_sort from: { created_at: :asc } }
+ end
+ .new(__FILE__),
+ ]
+ end
+
+ it "keeps the earliest default with each resource" do
+ expect(orderings).to eq(
+ "topics" => {
+ "created_at" => :desc,
+ },
+ "users" => {
+ "title" => :desc,
+ },
+ )
+ end
+ end
+
+ context "when another resource acquires an earlier type name" do
+ let(:changes) do
+ [
+ Class
+ .new(JsonApiKit::VersionChange) do
+ resource(:topics) { changed_default_sort from: { title: :desc } }
+ resource(:discussions) { changed_default_sort from: { created_at: :desc } }
+ end
+ .new(__FILE__),
+ Class
+ .new(JsonApiKit::VersionChange) { renamed_type from: :topics, to: :archives }
+ .new(__FILE__),
+ Class
+ .new(JsonApiKit::VersionChange) { renamed_type from: :discussions, to: :topics }
+ .new(__FILE__),
+ ]
+ end
+
+ it "selects the default belonging to the resource before the rename" do
+ expect(ordering).to eq("created_at" => :desc)
+ end
+ end
+
+ context "when a type rename and a default change share a version change" do
+ let(:changes) do
+ [
+ Class
+ .new(JsonApiKit::VersionChange) do
+ renamed_type from: :discussions, to: :topics
+ resource(:topics) { changed_default_sort from: { created_at: :desc } }
+ resource(:users) { changed_default_sort from: { title: :desc } }
+ end
+ .new(__FILE__),
+ ]
+ end
+
+ it "looks up the declaration under the type after the rename" do
+ expect(ordering).to eq("created_at" => :desc)
+ end
+ end
+ end
+end
diff --git a/spec/lib/json_api_kit/default_sorts_spec.rb b/spec/lib/json_api_kit/default_sorts_spec.rb
index 4fad27de5ed..cca9a486368 100644
--- a/spec/lib/json_api_kit/default_sorts_spec.rb
+++ b/spec/lib/json_api_kit/default_sorts_spec.rb
@@ -3,206 +3,43 @@
RSpec.describe JsonApiKit::DefaultSorts do
subject(:defaults) { described_class.new(changes) }
+ let(:changes) do
+ [
+ Class
+ .new(JsonApiKit::VersionChange) do
+ resource(:topics) { changed_default_sort from: { created_at: :desc } }
+ end
+ .new(__FILE__),
+ ]
+ end
let(:resource) do
Class.new(JsonApiKit::Resource) do
model Topic
type :topics
sort :created_at
- sort :title
- default_sort title: :asc
+ default_sort created_at: :asc
end
end
- let(:changes) { [] }
describe "#for" do
subject(:ordering) { defaults.for(resource) }
- it "returns the current default without version changes" do
- expect(ordering).to eq("title" => :asc)
- end
-
- context "when the default changed after the pin" do
- let(:changes) do
- [
- Class
- .new(JsonApiKit::VersionChange) do
- resource(:topics) { changed_default_sort from: { created_at: :desc } }
- end
- .new(__FILE__),
- ]
- end
-
- it "returns the historical default" do
- expect(ordering).to eq("created_at" => :desc)
- end
- end
-
- context "when the default changed several times" do
- let(:changes) do
- [
- Class
- .new(JsonApiKit::VersionChange) do
- resource(:topics) { changed_default_sort from: { created_at: :asc } }
- end
- .new(__FILE__),
- Class
- .new(JsonApiKit::VersionChange) do
- resource(:topics) { changed_default_sort from: { created_at: :desc } }
- end
- .new(__FILE__),
- ]
- end
-
- it "uses the earliest default after the pin" do
- expect(ordering).to eq("created_at" => :asc)
- end
- end
-
- context "when the historical default is empty" do
- let(:changes) do
- [
- Class
- .new(JsonApiKit::VersionChange) { resource(:topics) { changed_default_sort from: {} } }
- .new(__FILE__),
- ]
- end
-
- it "preserves the empty ordering" do
- expect(ordering).to be_empty
- end
+ it "returns the ordering from the resource's history" do
+ expect(ordering).to eq("created_at" => :desc)
end
- context "when the same change renames a sort and its resource type" do
- let(:changes) do
- [
- Class
- .new(JsonApiKit::VersionChange) do
- renamed_type from: :discussions, to: :topics
- resource :topics do
- renamed_sort from: :posted_at, to: :created_at
- changed_default_sort from: { posted_at: :desc }
- end
- end
- .new(__FILE__),
- ]
- end
-
- it "uses the current sort name" do
- expect(ordering).to eq("created_at" => :desc)
- end
+ it "reuses the resolved ordering for the resource" do
+ expect(defaults.for(resource)).to equal(ordering)
end
- context "when sorts and types change before and after the default declaration" do
- let(:changes) do
- [
- Class
- .new(JsonApiKit::VersionChange) do
- resource(:discussions) { renamed_sort from: :posted_at, to: :published_at }
- end
- .new(__FILE__),
- Class
- .new(JsonApiKit::VersionChange) do
- resource(:discussions) { changed_default_sort from: { published_at: :desc } }
- end
- .new(__FILE__),
- Class
- .new(JsonApiKit::VersionChange) do
- renamed_type from: :discussions, to: :topics
- resource(:topics) { renamed_sort from: :published_at, to: :created_at }
- end
- .new(__FILE__),
- ]
- end
+ context "when two resource classes share a type" do
+ let(:changes) { [] }
+ let(:other_resource) { Class.new(resource) { default_sort created_at: :desc } }
- it "translates from the declaration's version" do
- expect(ordering).to eq("created_at" => :desc)
- end
- end
-
- context "when an earlier rename reuses the historical default's spelling" do
- let(:changes) do
- [
- Class
- .new(JsonApiKit::VersionChange) do
- resource :topics do
- renamed_sort from: :created_at, to: :title
- renamed_sort from: :posted_at, to: :created_at
- end
- end
- .new(__FILE__),
- Class
- .new(JsonApiKit::VersionChange) do
- resource(:topics) { changed_default_sort from: { created_at: :desc } }
- end
- .new(__FILE__),
- ]
- end
-
- it "does not apply changes preceding the default declaration" do
- expect(ordering).to eq("created_at" => :desc)
- end
- end
-
- context "when a later change merges two default sort names" do
- let(:changes) do
- [
- Class
- .new(JsonApiKit::VersionChange) do
- resource(:topics) do
- changed_default_sort from: { posted_date: :desc, posted_time: :desc }
- end
- end
- .new(__FILE__),
- Class
- .new(JsonApiKit::VersionChange) do
- resource :topics do
- merged_attributes from: %i[posted_date posted_time],
- to: :created_at,
- up: ->(date, time) { "#{date} #{time}" },
- down: ->(time) { time.split(" ") }
- end
- end
- .new(__FILE__),
- ]
- end
-
- it "uses the merged sort once" do
- expect(ordering).to eq("created_at" => :desc)
- end
- end
-
- context "when the historical sort is undeclared" do
- let(:changes) do
- [
- Class
- .new(JsonApiKit::VersionChange) do
- resource(:topics) { changed_default_sort from: { unknown: :asc } }
- end
- .new(__FILE__),
- ]
- end
-
- it "raises a declaration error" do
- expect { ordering }.to raise_error(
- JsonApiKit::Resource::Sorting::UndeclaredDefault,
- /unknown/,
- )
- end
- end
-
- context "when the change belongs to another resource" do
- let(:changes) do
- [
- Class
- .new(JsonApiKit::VersionChange) do
- resource(:users) { changed_default_sort from: { username: :desc } }
- end
- .new(__FILE__),
- ]
- end
+ before { ordering }
- it "uses the resource's current default" do
- expect(ordering).to eq("title" => :asc)
+ it "resolves each resource's own default" do
+ expect(defaults.for(other_resource)).to eq("created_at" => :desc)
end
end
end
diff --git a/spec/lib/json_api_kit/version_change/default_sorts_spec.rb b/spec/lib/json_api_kit/version_change/default_sorts_spec.rb
index 09b3c86bb7a..7678a25058a 100644
--- a/spec/lib/json_api_kit/version_change/default_sorts_spec.rb
+++ b/spec/lib/json_api_kit/version_change/default_sorts_spec.rb
@@ -34,23 +34,28 @@ RSpec.describe JsonApiKit::VersionChange::DefaultSorts do
end
end
- describe "#convert_names" do
- subject(:converted_defaults) { default_sorts.convert_names { it.with(value: it.value.upcase) } }
-
- it "converts the names in every default" do
- expect(converted_defaults).to match(
- [
- have_attributes(type: "TOPICS", ordering: { "POSTED_AT" => :desc }),
- have_attributes(type: "USERS", ordering: { "USERNAME" => :asc }),
- ],
- )
+ describe "#each_for" do
+ subject(:resource_defaults) { default_sorts.each_for(type).to_a }
+
+ let(:type) { "topics" }
+
+ it "yields only the requested resource's default" do
+ expect(resource_defaults).to eq([topic_default])
+ end
+
+ context "when the resource has no default declaration" do
+ let(:type) { "posts" }
+
+ it "returns no defaults" do
+ expect(resource_defaults).to be_empty
+ end
end
context "when the collection is empty" do
let(:defaults) { [] }
it "returns no defaults" do
- expect(converted_defaults).to be_empty
+ expect(resource_defaults).to be_empty
end
end
end
@@ -83,8 +88,39 @@ RSpec.describe JsonApiKit::VersionChange do
end
end
- describe "#current_default_sorts" do
- subject(:default_sorts) { version_change.current_default_sorts }
+ describe "#current_default_sort" do
+ subject(:current_default) { version_change.current_default_sort(default_sort) }
+
+ let(:default_sort) { JsonApiKit::DefaultSort.new(type, posted_at: :desc) }
+ let(:type) { "topics" }
+
+ it "translates an existing default's sort names" do
+ expect(current_default).to have_attributes(
+ type: "topics",
+ ordering: {
+ "created_at" => :desc,
+ },
+ )
+ end
+
+ context "when the resource type also changes" do
+ let(:type) { "discussions" }
+
+ before { change_class.renamed_type from: :discussions, to: :topics }
+
+ it "translates the type before the sort names" do
+ expect(current_default).to have_attributes(
+ type: "topics",
+ ordering: {
+ "created_at" => :desc,
+ },
+ )
+ end
+ end
+ end
+
+ describe "#each_current_default_sort" do
+ subject(:default_sorts) { version_change.enum_for(:each_current_default_sort, "topics").to_a }
it "translates sort names within the declaring change" do
expect(default_sorts.sole).to have_attributes(
@@ -102,6 +138,22 @@ RSpec.describe JsonApiKit::VersionChange do
expect(default_sorts).to be_empty
end
end
+
+ context "when type names are reused in the same change" do
+ before do
+ change_class.renamed_type from: :topics, to: :archives
+ change_class.renamed_type from: :discussions, to: :topics
+ end
+
+ it "preserves the declaration's current type" do
+ expect(default_sorts.sole).to have_attributes(
+ type: "topics",
+ ordering: {
+ "created_at" => :desc,
+ },
+ )
+ end
+ end
end
describe "#verify!" do
diff --git a/spec/lib/json_api_kit/version_change/type_renames_spec.rb b/spec/lib/json_api_kit/version_change/type_renames_spec.rb
index 2ac9843acf9..c06cb07145d 100644
--- a/spec/lib/json_api_kit/version_change/type_renames_spec.rb
+++ b/spec/lib/json_api_kit/version_change/type_renames_spec.rb
@@ -125,6 +125,42 @@ RSpec.describe JsonApiKit::VersionChange do
end
end
+ describe "#current_resource_type" do
+ subject(:type) { version_change.current_resource_type(previous_type) }
+
+ let(:previous_type) { "things" }
+
+ it "returns the type after the change" do
+ expect(type).to eq("items")
+ end
+
+ context "when the resource has no type rename" do
+ let(:previous_type) { "people" }
+
+ it "preserves the type" do
+ expect(type).to eq("people")
+ end
+ end
+ end
+
+ describe "#previous_resource_type" do
+ subject(:type) { version_change.previous_resource_type(current_type) }
+
+ let(:current_type) { "items" }
+
+ it "returns the type before the change" do
+ expect(type).to eq("things")
+ end
+
+ context "when the resource has no type rename" do
+ let(:current_type) { "people" }
+
+ it "preserves the type" do
+ expect(type).to eq("people")
+ end
+ end
+ end
+
describe "#current_names" do
subject(:current_names) { version_change.current_names(name) }