Class: MoSQL::Schema

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/mosql/schema.rb

Instance Method Summary collapse

Methods included from Logging

#log

Constructor Details

#initialize(map) ⇒ Schema

Returns a new instance of Schema.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mosql/schema.rb', line 60

def initialize(map)
  @map = {}
  map.each do |dbname, db|
    @map[dbname] = { :meta => parse_meta(db[:meta]) }
    db.each do |cname, spec|
      next unless cname.is_a?(String)
      begin
        @map[dbname][cname] = parse_spec("#{dbname}.#{cname}", spec)
      rescue KeyError => e
        raise SchemaError.new("In spec for #{dbname}.#{cname}: #{e}")
      end
    end
  end

  # Lurky way to force Sequel force all timestamps to use UTC.
  Sequel.default_timezone = :utc
end

Instance Method Details

#all_columns(schema, copy = false) ⇒ Object



275
276
277
278
279
280
281
282
283
284
# File 'lib/mosql/schema.rb', line 275

def all_columns(schema, copy=false)
  cols = []
  schema[:columns].each do |col|
    cols << col[:name] unless copy && !copy_column?(col)
  end
  if schema[:meta][:extra_props]
    cols << "_extra_props"
  end
  cols
end

#all_columns_for_copy(schema) ⇒ Object



286
287
288
# File 'lib/mosql/schema.rb', line 286

def all_columns_for_copy(schema)
  all_columns(schema, true)
end

#all_mongo_dbsObject



335
336
337
# File 'lib/mosql/schema.rb', line 335

def all_mongo_dbs
  @map.keys
end

#check_columns!(ns, spec) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/mosql/schema.rb', line 35

def check_columns!(ns, spec)
  seen = Set.new
  spec[:columns].each do |col|
    if seen.include?(col[:source])
      raise SchemaError.new("Duplicate source #{col[:source]} in column definition #{col[:name]} for #{ns}.")
    end
    seen.add(col[:source])
  end
end

#collections_for_mongo_db(db) ⇒ Object



339
340
341
# File 'lib/mosql/schema.rb', line 339

def collections_for_mongo_db(db)
  (@map[db]||{}).keys
end

#copy_column?(col) ⇒ Boolean

Returns:

  • (Boolean)


271
272
273
# File 'lib/mosql/schema.rb', line 271

def copy_column?(col)
  col[:source] != '$timestamp'
end

#copy_data(db, ns, objs) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/mosql/schema.rb', line 290

def copy_data(db, ns, objs)
  schema = find_ns!(ns)
  db.synchronize do |pg|
    sql = "COPY \"#{schema[:meta][:table]}\" " +
      "(#{all_columns_for_copy(schema).map {|c| "\"#{c}\""}.join(",")}) FROM STDIN"
    pg.execute(sql)
    objs.each do |o|
      pg.put_copy_data(transform_to_copy(ns, o, schema) + "\n")
    end
    pg.put_copy_end
    begin
      pg.get_result.check
    rescue PGError => e
      db.send(:raise_error, e)
    end
  end
end

#create_schema(db, clobber = false) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/mosql/schema.rb', line 78

def create_schema(db, clobber=false)
  @map.values.each do |dbspec|
    dbspec.each do |n, collection|
      next unless n.is_a?(String)
      meta = collection[:meta]
      composite_key = meta[:composite_key]
      keys = []
      log.info("Creating table '#{meta[:table]}'...")
      db.send(clobber ? :create_table! : :create_table?, meta[:table]) do
        collection[:columns].each do |col|
          opts = {}
          if col[:source] == '$timestamp'
            opts[:default] = Sequel.function(:now)
          end
          column col[:name], col[:type], opts

          if composite_key and composite_key.include?(col[:name])
            keys << col[:name].to_sym
          elsif not composite_key and col[:source].to_sym == :_id
            keys << col[:name].to_sym
          end
        end

        primary_key keys
        if meta[:extra_props]
          type =
            case meta[:extra_props]
            when 'JSON'
              'JSON'
            when 'JSONB'
              'JSONB'
            else
              'TEXT'
            end
          column '_extra_props', type
        end
      end
    end
  end
end

#fetch_and_delete_dotted(obj, dotted) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/mosql/schema.rb', line 146

def fetch_and_delete_dotted(obj, dotted)
  pieces = dotted.split(".")
  breadcrumbs = []
  while pieces.length > 1
    key = pieces.shift
    breadcrumbs << [obj, key]
    obj = obj[key]
    return nil unless obj.is_a?(Hash)
  end

  val = obj.delete(pieces.first)

  breadcrumbs.reverse.each do |obj, key|
    obj.delete(key) if obj[key].empty?
  end

  val
end

#fetch_exists(obj, dotted) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'lib/mosql/schema.rb', line 165

def fetch_exists(obj, dotted)
  pieces = dotted.split(".")
  while pieces.length > 1
    key = pieces.shift
    obj = obj[key]
    return false unless obj.is_a?(Hash)
  end
  obj.has_key?(pieces.first)
end

#fetch_special_source(obj, source, original) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/mosql/schema.rb', line 175

def fetch_special_source(obj, source, original)
  case source
  when "$timestamp"
    Sequel.function(:now)
  when /^\$exists (.+)/
    # We need to look in the cloned original object, not in the version that
    # has had some fields deleted.
    fetch_exists(original, $1)
  else
    raise SchemaError.new("Unknown source: #{source}")
  end
end

#find_db(db) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/mosql/schema.rb', line 119

def find_db(db)
  unless @map.key?(db)
    @map[db] = @map.values.find do |spec|
      spec && spec[:meta][:alias].any? { |a| a.match(db) }
    end
  end
  @map[db]
end

#find_ns(ns) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/mosql/schema.rb', line 128

def find_ns(ns)
  db, collection = ns.split(".", 2)
  unless spec = find_db(db)
    return nil
  end
  unless schema = spec[collection]
    log.debug("No mapping for ns: #{ns}")
    return nil
  end
  schema
end

#find_ns!(ns) ⇒ Object

Raises:



140
141
142
143
144
# File 'lib/mosql/schema.rb', line 140

def find_ns!(ns)
  schema = find_ns(ns)
  raise SchemaError.new("No mapping for namespace: #{ns}") if schema.nil?
  schema
end

#parse_meta(meta) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/mosql/schema.rb', line 52

def parse_meta(meta)
  meta = {} if meta.nil?
  meta[:alias] = [] unless meta.key?(:alias)
  meta[:alias] = [meta[:alias]] unless meta[:alias].is_a?(Array)
  meta[:alias] = meta[:alias].map { |r| Regexp.new(r) }
  meta
end

#parse_spec(ns, spec) ⇒ Object



45
46
47
48
49
50
# File 'lib/mosql/schema.rb', line 45

def parse_spec(ns, spec)
  out = spec.dup
  out[:columns] = to_array(spec.fetch(:columns))
  check_columns!(ns, out)
  out
end

#primary_sql_key_for_ns(ns) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
# File 'lib/mosql/schema.rb', line 343

def primary_sql_key_for_ns(ns)
  ns = find_ns!(ns)
  keys = []
  if ns[:meta][:composite_key]
    keys = ns[:meta][:composite_key]
  else
    keys << ns[:columns].find {|c| c[:source] == '_id'}[:name]
  end

  return keys
end

#quote_copy(val) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/mosql/schema.rb', line 308

def quote_copy(val)
  case val
  when nil
    "\\N"
  when true
    't'
  when false
    'f'
  when Sequel::SQL::Function
    nil
  when DateTime, Time
    val.strftime("%FT%T.%6N %z")
  when Sequel::SQL::Blob
    "\\\\x" + [val].pack("h*")
  else
    val.to_s.gsub(/([\\\t\n\r])/, '\\\\\\1')
  end
end

#sanitize(value) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/mosql/schema.rb', line 251

def sanitize(value)
  # Base64-encode binary blobs from _extra_props -- they may
  # contain invalid UTF-8, which to_json will not properly encode.
  case value
  when Hash
    ret = {}
    value.each {|k, v| ret[k] = sanitize(v)}
    ret
  when Array
    value.map {|v| sanitize(v)}
  when BSON::Binary
    Base64.encode64(value.to_s)
  when Float
    # NaN is illegal in JSON. Translate into null.
    value.nan? ? nil : value
  else
    value
  end
end

#table_for_ns(ns) ⇒ Object



331
332
333
# File 'lib/mosql/schema.rb', line 331

def table_for_ns(ns)
  find_ns!(ns)[:meta][:table]
end

#to_array(lst) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mosql/schema.rb', line 7

def to_array(lst)
  lst.map do |ent|
    col = nil
    if ent.is_a?(Hash) && ent[:source].is_a?(String) && ent[:type].is_a?(String)
      # new configuration format
      col = {
        :source => ent.fetch(:source),
        :type   => ent.fetch(:type),
        :name   => (ent.keys - [:source, :type]).first,
      }
    elsif ent.is_a?(Hash) && ent.keys.length == 1 && ent.values.first.is_a?(String)
      col = {
        :source => ent.first.first,
        :name   => ent.first.first,
        :type   => ent.first.last
      }
    else
      raise SchemaError.new("Invalid ordered hash entry #{ent.inspect}")
    end

    if !col.key?(:array_type) && /\A(.+)\s+array\z/i.match(col[:type])
      col[:array_type] = $1
    end

    col
  end
end

#transform(ns, obj, schema = nil) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/mosql/schema.rb', line 205

def transform(ns, obj, schema=nil)
  schema ||= find_ns!(ns)

  original = obj

  # Do a deep clone, because we're potentially going to be
  # mutating embedded objects.
  obj = BSON.deserialize(BSON.serialize(obj))

  row = []
  schema[:columns].each do |col|

    source = col[:source]
    type = col[:type]

    if source.start_with?("$")
      v = fetch_special_source(obj, source, original)
    else
      v = fetch_and_delete_dotted(obj, source)
      case v
      when Hash
        v = JSON.dump(Hash[v.map { |k,v| [k, transform_primitive(v)] }])
      when Array
        v = v.map { |it| transform_primitive(it) }
        if col[:array_type]
          v = Sequel.pg_array(v, col[:array_type])
        else
          v = JSON.dump(v)
        end
      else
        v = transform_primitive(v, type)
      end
    end
    row << v
  end

  if schema[:meta][:extra_props]
    extra = sanitize(obj)
    row << JSON.dump(extra)
  end

  log.debug { "Transformed: #{row.inspect}" }

  row
end

#transform_primitive(v, type = nil) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/mosql/schema.rb', line 188

def transform_primitive(v, type=nil)
  case v
  when BSON::ObjectId, Symbol
    v.to_s
  when BSON::Binary
    if type.downcase == 'uuid'
      v.to_s.unpack("H*").first
    else
      Sequel::SQL::Blob.new(v.to_s)
    end
  when BSON::DBRef
    v.object_id.to_s
  else
    v
  end
end

#transform_to_copy(ns, row, schema = nil) ⇒ Object



327
328
329
# File 'lib/mosql/schema.rb', line 327

def transform_to_copy(ns, row, schema=nil)
  row.map { |c| quote_copy(c) }.compact.join("\t")
end