Class: Poros::Query
Instance Attribute Summary collapse
-
#object ⇒ Object
Returns the value of attribute object.
-
#queries ⇒ Object
Returns the value of attribute queries.
Instance Method Summary collapse
- #each(&block) ⇒ Object
-
#initialize(object) ⇒ Query
constructor
A new instance of Query.
- #log(count:, query:, load:) ⇒ Object
- #results ⇒ Object
- #to_s ⇒ Object
- #where(query) ⇒ Object
Constructor Details
#initialize(object) ⇒ Query
Returns a new instance of Query.
6 7 8 9 |
# File 'lib/poros/query.rb', line 6 def initialize(object) @object = object @queries = {} end |
Instance Attribute Details
#object ⇒ Object
Returns the value of attribute object.
4 5 6 |
# File 'lib/poros/query.rb', line 4 def object @object end |
#queries ⇒ Object
Returns the value of attribute queries.
4 5 6 |
# File 'lib/poros/query.rb', line 4 def queries @queries end |
Instance Method Details
#each(&block) ⇒ Object
16 17 18 |
# File 'lib/poros/query.rb', line 16 def each(&block) results.each { |result| block.call(result) } end |
#log(count:, query:, load:) ⇒ Object
38 39 40 |
# File 'lib/poros/query.rb', line 38 def log(count:, query:, load:) Poros::Config.logger.info("#{self} [#{count} records] (Querying: #{(query).round(2)}s Loading: #{(load).round(2)}s Total: #{(query + load).round(3)}s)") end |
#results ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/poros/query.rb', line 42 def results start = Time.now indexed, table_scan = @queries.partition { |index, key| @object.poro_indexes.include?(index) } indexed_results = indexed.map { |key, value| case value when Regexp @object.index_data[key].keys.flat_map { |value_name| @object.index_data[key][value_name] if value =~ value_name }.compact when Array value.flat_map { |value_name| @object.index_data[key][value_name] } when Proc @object.index_data[key].keys.flat_map { |value_name| @object.index_data[key][value_name] if value.call(value_name) }.compact else @object.index_data[key].has_key?(value) ? @object.index_data[key][value] : [] end }.inject(:&) if table_scan.size > 0 scanned_results = Dir.glob(File.join(@object.data_directory, '*.yml')).map { |file| next if file == @object.index_file data = YAML.safe_load( File.read(file), permitted_classes: Poros::Config.configuration[:permitted_classes], ) data[:uuid] if table_scan.all? { |key, value| case value when Regexp value =~ data[key] when Array value.include?(data[key]) when Proc value.call(data[key]) else data[key] == value end } }.compact end if indexed.size > 0 && table_scan.size > 0 results = indexed_results & scanned_results elsif indexed.size > 0 results = indexed_results else results = scanned_results end query_time = Time.now - start start = Time.now records = results.map { |uuid| @object.find(uuid) } load_time = Time.now - start log(count: records.count, query: query_time, load: load_time) records end |
#to_s ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/poros/query.rb', line 20 def to_s str = [@object] if @queries.any? str << "WHERE" @queries.each do |field, query| if query.is_a?(Proc) str << "#{field} = -> {...}" else str << "#{field} = #{query}" end end end str.join(" ") end |
#where(query) ⇒ Object
11 12 13 14 |
# File 'lib/poros/query.rb', line 11 def where(query) @queries.merge!(query) self end |