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

mix[RW]
model[R]

Public Class Methods

new(model = nil, &block) click to toggle source
# 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

<<(other) click to toggle source

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
==(other) click to toggle source

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
Also aliased as: eql?
add_statement(statement, mixes = {}, group: nil, label: nil) click to toggle source

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
eql?(other)
Alias for: ==
fields_exist?() click to toggle source

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_with_tag(tag) click to toggle source

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
queries() click to toggle source

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_updates() click to toggle source

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
source_code() click to toggle source

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
statement_weights() click to toggle source

Retrieve the weights for the current mix @return [Hash]

# File lib/nose/workload.rb, line 81
def statement_weights
  @statement_weights[@mix]
end
statements() click to toggle source

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
updates() click to toggle source

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