omf_oml 1.1.6 → 1.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,7 +20,8 @@ module OMF::OML
20
20
  # @param opts
21
21
  #
22
22
  def self.create(tname, file_name, opts = {}, &on_before_row_added)
23
- self.new(tname, file_name, opts, &on_before_row_added)
23
+ opts[:file_name] = file_name
24
+ self.new(tname, opts[:schema], opts, &on_before_row_added)
24
25
  end
25
26
 
26
27
  #
@@ -28,27 +29,29 @@ module OMF::OML
28
29
  # schema - OmlSchema or Array containing [name, type*] for every column in table
29
30
  # Table adds a '__id__' column at the beginning which keeps track of the rows unique id
30
31
  # @params opts
32
+ # @opts :file_name - Name of file to read data from
31
33
  # @opts :max_size - keep table to that size by dropping older rows
32
34
  # @opts :index - only keep the latest inserted row for a unique col value - messes with row order
33
35
  # @opts :has_csv_header If true use first row in file as schema descriptor
34
36
  # @opts :schema Schema associated with this table
35
37
  #
36
- def initialize(tname, file_name, opts = {}, &on_before_row_added)
38
+ def initialize(tname, schema, opts = {}, &on_before_row_added)
39
+ file_name = opts[:file_name]
37
40
  unless File.readable?(file_name)
38
41
  raise "Can't read CSV file '#{file_name}'"
39
42
  end
40
43
  csv_opts = {}
41
- csv_opts[:headers] = (opts.delete(:has_csv_header) == true)
44
+ csv_opts[:headers] = (opts[:has_csv_header] == true) #(opts.delete(:has_csv_header) == true)
42
45
  unless csv_opts[:headers]
43
46
  raise "Current implementation only works with CSV files which inlcude a schema description in the first line"
44
47
  end
45
48
 
46
- encoding = opts.delete(:encoding)
49
+ encoding = opts[:encoding] #opts.delete(:encoding)
47
50
  mode = "rb"
48
51
  mode << ":#{encoding}" if encoding
49
52
  csv = CSV.open(file_name, mode, csv_opts)
50
53
 
51
- unless schema = opts.delete(:schema)
54
+ unless schema = opts[:schema]
52
55
  unless csv_opts[:headers]
53
56
  raise "No schema given and ':has_csv_header' not set to capture schema from file header"
54
57
  end
@@ -79,6 +82,29 @@ module OMF::OML
79
82
 
80
83
  end
81
84
 
85
+ # Return a new table which only contains the rows of this
86
+ # table whose value in column 'col_name' is equal to 'col_value'
87
+ #
88
+ def create_sliced_table(col_name, col_value, table_opts = {})
89
+ sname = "#{@name}_slice_#{Kernel.rand(100000)}"
90
+ st = OmlTable.new(sname, @schema, table_opts)
91
+ index = @schema.index_for_col(col_name)
92
+ first_row = true
93
+ @rows.each do |row|
94
+ if row[index] == col_value
95
+ #row = row[1 .. -1] # remove the row_id
96
+ debug "Add first row '#{row.inspect}'" if first_row
97
+ st.add_row(row)
98
+ first_row = false
99
+ end
100
+ end
101
+ def st.release
102
+ # do nothing
103
+ end
104
+
105
+ debug "Created sliced table '#{sname}' from '#{@name}' (rows: #{st.rows.length} from #{@rows.length})"
106
+ st
107
+ end
82
108
 
83
109
 
84
110
  end # class
@@ -31,6 +31,7 @@ module OMF::OML
31
31
  # - query_interval: Interval between consecutive queries when processing large result set.
32
32
  #
33
33
  def initialize(sql_table_name, schema, query, opts = {})
34
+ debug "query: #{query.sql}"
34
35
  @sname = sql_table_name
35
36
  @schema = schema
36
37
  raise "Expected OmlSchema but got '#{schema.class}'" unless schema.is_a? OmlSchema
data/lib/omf_oml/table.rb CHANGED
@@ -58,6 +58,7 @@ module OMF::OML
58
58
  @schema.insert_column_at(0, [:__id__, 'int'])
59
59
  end
60
60
  end
61
+ @col_count = @schema.columns.length
61
62
  @opts = opts
62
63
  if (index = opts[:index])
63
64
  throw "No longer supported, use IndexedTable instead"
@@ -163,7 +164,7 @@ module OMF::OML
163
164
  def create_sliced_table(col_name, col_value, table_opts = {})
164
165
  sname = "#{@name}_slice_#{Kernel.rand}"
165
166
 
166
- st = self.class.new(name, @schema, table_opts)
167
+ st = self.class.new(name, @schema, @opts.merge(table_opts))
167
168
  st.instance_variable_set(:@sname, sname)
168
169
  st.instance_variable_set(:@master_ds, self)
169
170
  st.instance_variable_set(:@add_index, true)
@@ -230,6 +231,9 @@ module OMF::OML
230
231
  # was ultimately added.
231
232
  #
232
233
  def _add_row_finally(row)
234
+ unless row.length == @col_count
235
+ raise "Unexpected col count for row '#{row}' - schema: #{@schema}"
236
+ end
233
237
  # if @indexed_rows
234
238
  # @indexed_rows[row[@index_col]] = row
235
239
  # return
@@ -6,6 +6,6 @@
6
6
  #-------------------------------------------------------------------------------
7
7
  module OMF
8
8
  module OML
9
- VERSION = '1.1.6'
9
+ VERSION = '1.1.7'
10
10
  end
11
11
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omf_oml
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.6
4
+ version: 1.1.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-06-01 00:00:00.000000000 Z
12
+ date: 2014-07-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: omf_base