Skip to content

Commit

Permalink
Refactor optimized relation reader
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-gordon committed Jan 7, 2025
1 parent 520bc19 commit 8bc1a0e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 30 deletions.
4 changes: 2 additions & 2 deletions core/lib/rom/plugins/relation/registry_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class RegistryReader < ::Module
EMPTY_REGISTRY = RelationRegistry.build(EMPTY_HASH).freeze

# @api private
def initialize(klass:, relation_readers_module:)
def initialize(relation_readers:)
super()
klass.include relation_readers_module
include relation_readers
end

# @api private
Expand Down
18 changes: 8 additions & 10 deletions core/lib/rom/setup/finalize/finalize_relations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ class Finalize
class FinalizeRelations
attr_reader :notifications

module BuildRelationReaders
def self.build(relations)
Module.new do
relations.each do |name|
define_method(name) do
__registry__[name]
end
end
class RelationReaders < ::Module
def initialize(relations)
super()

relations.each do |name|
define_method(name) { __registry__[name] }
end
end
end
Expand Down Expand Up @@ -44,7 +42,7 @@ def initialize(gateways, relation_classes, notifications:, mappers: nil, plugins
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def run!
relation_registry = RelationRegistry.new do |registry, relations|
relation_readers_module = BuildRelationReaders.build(relation_names)
relation_readers = RelationReaders.new(relation_names)
@relation_classes.each do |klass|
unless klass.adapter
raise MissingAdapterIdentifierError,
Expand All @@ -58,7 +56,7 @@ def run!
"Relation with name #{key.inspect} registered more than once"
end

klass.use(:registry_reader, klass: klass, relation_readers_module: relation_readers_module)
klass.use(:registry_reader, relation_readers: relation_readers)

notifications.trigger(
'configuration.relations.class.ready',
Expand Down
2 changes: 1 addition & 1 deletion repository/lib/rom/repository/class_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def new(container = nil, **options)
container ||= options.fetch(:container)

unless relation_reader
relation_reader(RelationReader.new(self, container.relations.elements.keys))
relation_reader(RelationReader.new(container.relations.elements.keys))
include(relation_reader)
end

Expand Down
34 changes: 19 additions & 15 deletions repository/lib/rom/repository/relation_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,44 @@ class RelationReader < ::Module
extend ::Dry::Core::ClassAttributes

# @api private
attr_reader :klass

# @api private
attr_reader :relations

defines :relation_readers

# @api private
defines :mutex
mutex(Mutex.new)
mutex ::Mutex.new

# @api private
defines :relation_cache
relation_cache(Concurrent::Hash.new)
relation_cache ::Concurrent::Hash.new

# @api private
attr_reader :klass

# @api private
attr_reader :relations

module InstanceMethods
# @api private
def set_relation(name) # rubocop:disable Naming/AccessorMethodName
def prepare_relation(name)
container
.relations[name]
.with(auto_struct: auto_struct, struct_namespace: struct_namespace)
.with(
auto_struct: auto_struct,
struct_namespace: struct_namespace
)
end

def relation_reader(name, relation_cache)
key = [name, auto_struct, struct_namespace]
relation_cache[key] ||= set_relation(name)
relation_cache[key] ||= prepare_relation(name)
end
end

# @api private
def mutex
ROM::Repository::RelationReader.mutex
end
def mutex = self.class.mutex

# @api private
def initialize(klass, relations)
def initialize(relations)
super()
@relations = relations
mutex.synchronize do
Expand All @@ -50,7 +54,7 @@ def initialize(klass, relations)
)
end
end
klass.include self.class.relation_readers
include self.class.relation_readers
end

# @api private
Expand Down
2 changes: 1 addition & 1 deletion repository/lib/rom/repository/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def self.inherited(klass)
# @see Repository#initialize
def initialize(*, **)
super
@root = set_relation(self.class.root)
@root = prepare_relation(self.class.root)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion repository/spec/integration/plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.apply(target, **)
target.prepend(self)
end

def set_relation(*)
def prepare_relation(*)
super.where { `1 = 0` }
end
end
Expand Down

0 comments on commit 8bc1a0e

Please sign in to comment.