class NoSE::Workload
A representation of a query workload over a given set of entities
Constants
- LOAD_PATH
The subdirectory workloads are loaded from
Attributes
Public Class Methods
# File lib/nose/workload.rb, line 18 def initialize(model = nil, &block) @statement_weights = { default: {} } @model = model || Model.new @mix = :default # Apply the DSL WorkloadDSL.new(self).instance_eval(&block) if block_given? end
Public Instance Methods
Add a new {Entity} or {Statement} to the workload @return [self] the current workload to allow chaining
# File lib/nose/workload.rb, line 37 def <<(other) if other.is_a? Entity @model.add_entity other.freeze elsif other.is_a? Statement add_statement other.freeze else fail TypeError, 'can only add queries and entities to a workload' end self end
Compare models and statements @return [Boolean]
# File lib/nose/workload.rb, line 29 def ==(other) other.is_a?(Workload) && @model == other.model && statement_weights == other.statement_weights end
Add a new {Statement} to the workload or parse a string @return [void]
# File lib/nose/workload.rb, line 51 def add_statement(statement, mixes = {}, group: nil, label: nil) statement = Statement.parse(statement, @model, group: group, label: label) \ if statement.is_a? String statement.freeze mixes = { default: mixes } if mixes.is_a? Numeric mixes = { default: 1.0 } if mixes.empty? mixes.each do |mix, weight| @statement_weights[mix] = {} unless @statement_weights.key? mix @statement_weights[mix][statement] = weight end end
Check if all the fields used by queries in the workload exist @return [Boolean]
# File lib/nose/workload.rb, line 117 def fields_exist? @statement_weights[@mix].each_key do |query| # Projected fields and fields in the where clause exist fields = query.where.map(&:field) + query.fields fields.each do |field| return false unless @model.find_field field.value end end true end
Find a statement in the workload with the provided tag @return [Statement]
# File lib/nose/workload.rb, line 95 def find_with_tag(tag) statements.find do |s| s.text.end_with? "-- #{tag}" end end
Strip the weights from the query dictionary and return a list of queries @return [Array<Statement>]
# File lib/nose/workload.rb, line 67 def queries @statement_weights[@mix].keys.select do |statement| statement.is_a? Query end end
Remove any updates from the workload @return [void]
# File lib/nose/workload.rb, line 103 def remove_updates @statement_weights[@mix].select! { |stmt, _| stmt.is_a? Query } end
Produce the source code used to define this workload @return [String]
# File lib/nose/workload.rb, line 131 def source_code return @source_code unless @source_code.nil? ns = OpenStruct.new(workload: self) tmpl = File.read File.join(File.dirname(__FILE__), '../../templates/workload.erb') tmpl = ERB.new(tmpl, nil, '>') @source_code = tmpl.result(ns.instance_eval { binding }) end
Retrieve the weights for the current mix @return [Hash]
# File lib/nose/workload.rb, line 81 def statement_weights @statement_weights[@mix] end
Strip the weights and return a list of statements @return [Array<Statement>]
# File lib/nose/workload.rb, line 75 def statements (@statement_weights[@mix] || {}).keys end
Get all the support queries for updates in the workload @
# File lib/nose/workload.rb, line 109
def support_queries(indexes)
updates.map do |update|
indexes.map { |index| update.support_queries(index) }
end.flatten(2)
end
Strip the weights from the query dictionary and return a list of updates @return [Array<Statement>]
# File lib/nose/workload.rb, line 87 def updates @statement_weights[@mix].keys.reject do |statement| statement.is_a? Query end end