From f9adf50cbcf06c260e71e3fbe157cd1e01f7d59e Mon Sep 17 00:00:00 2001 From: Arto Bendiken Date: Sun, 12 Dec 2010 02:50:08 +0100 Subject: [PATCH] Implemented the RDF::Query#optimize and #optimize! methods. --- lib/rdf/query.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/rdf/query.rb b/lib/rdf/query.rb index 42745b02..11eeab7f 100644 --- a/lib/rdf/query.rb +++ b/lib/rdf/query.rb @@ -153,6 +153,33 @@ def pattern(pattern, options = {}) self end + ## + # Returns an optimized copy of this query. + # + # @param [Hash{Symbol => Object}] options + # any additional options for optimization + # @return [RDF::Query] a copy of `self` + # @since 0.3.0 + def optimize(options = {}) + self.dup.optimize!(options) + end + + ## + # Optimizes this query by reordering its constituent triple patterns + # according to their cost estimates. + # + # @param [Hash{Symbol => Object}] options + # any additional options for optimization + # @return [void] `self` + # @see RDF::Query::Pattern#cost + # @since 0.3.0 + def optimize!(options = {}) + @patterns.sort! do |a, b| + (a.cost || 0) <=> (b.cost || 0) + end + self + end + ## # Executes this query on the given `queryable` graph or repository. #