Class: ODBA::Storage

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/odba/storage.rb

Overview

:nodoc: all

Constant Summary collapse

BULK_FETCH_STEP =
2500
TABLES =
[
  # in table 'object', the isolated dumps of all objects are stored
  ['object', <<-'SQL'],
  ['prefetchable_index', <<-SQL],
  ['extent_index', <<-SQL],
CREATE INDEX IF NOT EXISTS extent_index ON object(extent);
  SQL
  # helper table 'object_connection'
  ['object_connection', <<-'SQL'],
  ['target_id_index', <<-SQL],
CREATE INDEX IF NOT EXISTS target_id_index ON object_connection(target_id);
  SQL
  # helper table 'collection'
  ['collection', <<-'SQL'],
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStorage

Returns a new instance of Storage.



48
49
50
# File 'lib/odba/storage.rb', line 48

def initialize
	@id_mutex = Mutex.new
end

Instance Attribute Details

#dbiObject



204
205
206
# File 'lib/odba/storage.rb', line 204

def dbi
	Thread.current[:txn] || @dbi
end

Instance Method Details

#bulk_restore(bulk_fetch_ids) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/odba/storage.rb', line 51

def bulk_restore(bulk_fetch_ids)
	if(bulk_fetch_ids.empty?)
		[]
	else
		bulk_fetch_ids = bulk_fetch_ids.uniq
		rows = []
		while(!(ids = bulk_fetch_ids.slice!(0, BULK_FETCH_STEP)).empty?)
			sql = <<-SQL
				SELECT odba_id, content FROM object 
				WHERE odba_id IN (#{ids.join(',')})
			SQL
			rows.concat(self.dbi.select_all(sql))
		end
		rows
	end
end

#collection_fetch(odba_id, key_dump) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/odba/storage.rb', line 67

def collection_fetch(odba_id, key_dump)
  sql = <<-SQL
    SELECT value FROM collection 
    WHERE odba_id = ? AND key = ?
  SQL
  row = self.dbi.select_one(sql, odba_id, key_dump)
  row.first unless row.nil?
end

#collection_remove(odba_id, key_dump) ⇒ Object



75
76
77
78
79
80
# File 'lib/odba/storage.rb', line 75

def collection_remove(odba_id, key_dump)
  self.dbi.do <<-SQL, odba_id, key_dump
    DELETE FROM collection
    WHERE odba_id = ? AND key = ?
  SQL
end

#collection_store(odba_id, key_dump, value_dump) ⇒ Object



81
82
83
84
85
86
# File 'lib/odba/storage.rb', line 81

def collection_store(odba_id, key_dump, value_dump)
  self.dbi.do <<-SQL, odba_id, key_dump, value_dump
    INSERT INTO collection (odba_id, key, value)
    VALUES (?, ?, ?)
  SQL
end

#condition_index_delete(index_name, origin_id, search_terms, target_id = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/odba/storage.rb', line 87

def condition_index_delete(index_name, origin_id, 
                           search_terms, target_id=nil)
  values = []
  sql = "DELETE FROM #{index_name}"
  if(origin_id)
    sql << " WHERE origin_id = ?"
  else
    sql << " WHERE origin_id IS ?"
  end
  search_terms.each { |key, value|
    sql << " AND %s = ?" % key
    values << value
  }
  if(target_id)
    sql << " AND target_id = ?"
    values << target_id
  end
  self.dbi.do sql, origin_id, *values
end

#condition_index_ids(index_name, id, id_name) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/odba/storage.rb', line 106

def condition_index_ids(index_name, id, id_name)
  sql = <<-SQL
    SELECT DISTINCT *
    FROM #{index_name}
    WHERE #{id_name}=?
  SQL
  self.dbi.select_all(sql, id)
end

#create_condition_index(table_name, definition) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/odba/storage.rb', line 136

def create_condition_index(table_name, definition)
  self.dbi.do <<-SQL
CREATE TABLE IF NOT EXISTS #{table_name} (
  origin_id INTEGER,
  #{definition.collect { |*pair| pair.join(' ') }.join(",\n  ") },
  target_id INTEGER
);
  SQL
  #index origin_id
  self.dbi.do <<-SQL
CREATE INDEX IF NOT EXISTS origin_id_#{table_name} ON #{table_name}(origin_id);
  SQL
  #index search_term
  definition.each { |name, datatype|
    self.dbi.do <<-SQL
CREATE INDEX IF NOT EXISTS #{name}_#{table_name} ON #{table_name}(#{name});
    SQL
  }
  #index target_id
  self.dbi.do <<-SQL
CREATE INDEX IF NOT EXISTS target_id_#{table_name} ON #{table_name}(target_id);
  SQL
end

#create_dictionary_map(language) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/odba/storage.rb', line 114

def create_dictionary_map(language)
  self.dbi.do <<-SQL
    ALTER TEXT SEARCH CONFIGURATION default_#{language}
    ALTER MAPPING FOR
      asciiword, asciihword, hword_asciipart,
      word, hword, hword_part, hword_numpart,
      numword, numhword
    WITH #{language}_ispell, #{language}_stem;
  SQL
  self.dbi.do <<-SQL
    ALTER TEXT SEARCH CONFIGURATION default_#{language}
    ALTER MAPPING FOR
      host, file, int, uint, version
    WITH simple;
  SQL
  # drop from default setting
  self.dbi.do <<-SQL
  ALTER TEXT SEARCH CONFIGURATION default_#{language}
      DROP MAPPING FOR
      email, url, url_path, sfloat, float
  SQL
end

#create_fulltext_index(table_name) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/odba/storage.rb', line 159

def create_fulltext_index(table_name)
  self.dbi.do <<-SQL
CREATE TABLE IF NOT EXISTS #{table_name} (
  origin_id INTEGER,
  search_term tsvector,
  target_id INTEGER
);
  SQL
  #index origin_id
  self.dbi.do <<-SQL
CREATE INDEX IF NOT EXISTS origin_id_#{table_name} ON #{table_name}(origin_id);
  SQL
  self.dbi.do <<-SQL
CREATE INDEX IF NOT EXISTS search_term_#{table_name}
ON #{table_name} USING gist(search_term);
  SQL
  #index target_id
  self.dbi.do <<-SQL
CREATE INDEX IF NOT EXISTS target_id_#{table_name} ON #{table_name}(target_id);
  SQL
end

#create_index(table_name) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/odba/storage.rb', line 180

def create_index(table_name)
  self.dbi.do <<-SQL
    CREATE TABLE IF NOT EXISTS #{table_name} (
      origin_id INTEGER,
      search_term TEXT,
      target_id INTEGER
    );
  SQL
  #index origin_id
  self.dbi.do <<-SQL
    CREATE INDEX IF NOT EXISTS origin_id_#{table_name}
    ON #{table_name}(origin_id)
  SQL
  #index search_term
  self.dbi.do <<-SQL
    CREATE INDEX IF NOT EXISTS search_term_#{table_name}
    ON #{table_name}(search_term)
  SQL
  #index target_id
  self.dbi.do <<-SQL
    CREATE INDEX IF NOT EXISTS target_id_#{table_name}
    ON #{table_name}(target_id)
  SQL
end

#delete_index_element(index_name, odba_id, id_name) ⇒ Object



210
211
212
213
214
# File 'lib/odba/storage.rb', line 210

def delete_index_element(index_name, odba_id, id_name)
  self.dbi.do <<-SQL, odba_id
    DELETE FROM #{index_name} WHERE #{id_name} = ?
  SQL
end

#delete_persistable(odba_id) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/odba/storage.rb', line 215

def delete_persistable(odba_id)
    # delete origin from connections
	self.dbi.do <<-SQL, odba_id
		DELETE FROM object_connection WHERE origin_id = ?
	SQL
    # delete target from connections
	self.dbi.do <<-SQL, odba_id
		DELETE FROM object_connection WHERE target_id = ?
	SQL
    # delete from collections
	self.dbi.do <<-SQL, odba_id
		DELETE FROM collection WHERE odba_id = ?
	SQL
    # delete from objects
	self.dbi.do <<-SQL, odba_id
		DELETE FROM object WHERE odba_id = ?
	SQL
end

#drop_index(index_name) ⇒ Object



207
208
209
# File 'lib/odba/storage.rb', line 207

def drop_index(index_name)
	self.dbi.do "DROP TABLE #{index_name}"
end

#ensure_object_connections(origin_id, target_ids) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/odba/storage.rb', line 233

def ensure_object_connections(origin_id, target_ids)
  sql = <<-SQL
    SELECT target_id FROM object_connection
    WHERE origin_id = ?
  SQL
  target_ids.uniq!
  update_ids = target_ids
  old_ids = []
  ## use self.dbi instead of @dbi to get information about
  ## object_connections previously stored within this transaction
  if(rows = self.dbi.select_all(sql, origin_id))
    old_ids = rows.collect { |row| row[0] }
    old_ids.uniq!
    delete_ids = old_ids - target_ids
    update_ids = target_ids - old_ids
    unless(delete_ids.empty?)
      while(!(ids = delete_ids.slice!(0, BULK_FETCH_STEP)).empty?)
        self.dbi.do <<-SQL, origin_id
          DELETE FROM object_connection
          WHERE origin_id = ? AND target_id IN (#{ids.join(',')})
        SQL
      end
    end
  end
  sth = self.dbi.prepare <<-SQL
    INSERT INTO object_connection (origin_id, target_id)
    VALUES (?, ?)
  SQL
  update_ids.each { |id|
    sth.execute(origin_id, id)
  }
  sth.finish
end

#ensure_target_id_index(table_name) ⇒ Object



266
267
268
269
270
271
272
273
# File 'lib/odba/storage.rb', line 266

def ensure_target_id_index(table_name)
  #index target_id
  self.dbi.do <<-SQL
    CREATE INDEX IF NOT EXISTS target_id_#{table_name}
    ON #{table_name}(target_id)
  SQL
rescue
end

#extent_count(klass) ⇒ Object



274
275
276
277
278
# File 'lib/odba/storage.rb', line 274

def extent_count(klass)
  self.dbi.select_one(<<-EOQ, klass.to_s).first
    SELECT COUNT(odba_id) FROM object WHERE extent = ?
  EOQ
end

#extent_ids(klass) ⇒ Object



279
280
281
282
283
# File 'lib/odba/storage.rb', line 279

def extent_ids(klass)
  self.dbi.select_all(<<-EOQ, klass.to_s).flatten
    SELECT odba_id FROM object WHERE extent = ?
  EOQ
end

#fulltext_index_delete(index_name, id, id_name) ⇒ Object



284
285
286
287
288
289
# File 'lib/odba/storage.rb', line 284

def fulltext_index_delete(index_name, id, id_name)
  self.dbi.do <<-SQL, id
    DELETE FROM #{index_name}
    WHERE #{id_name} = ?
  SQL
end

#fulltext_index_target_ids(index_name, origin_id) ⇒ Object



293
294
295
296
297
298
299
300
# File 'lib/odba/storage.rb', line 293

def fulltext_index_target_ids(index_name, origin_id)
  sql = <<-SQL
    SELECT DISTINCT target_id
    FROM #{index_name}
    WHERE origin_id=?
  SQL
  self.dbi.select_all(sql, origin_id)
end

#generate_dictionary(language) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/odba/storage.rb', line 301

def generate_dictionary(language)
  # postgres searches for the dictionary file in the directory share/tsearch_data of it installation location
  # By default under gentoo, this is /usr/share/postgresql/tsearch_data/
  # As we have no way to get the current installation path, we do not check whether the files are present or not
  file='fulltext'
  # setup configuration
  self.dbi.do <<-SQL
    CREATE TEXT SEARCH CONFIGURATION public.default_#{language} ( COPY = pg_catalog.#{language} );
  SQL
  # ispell
  self.dbi.do <<-SQL
    CREATE TEXT SEARCH DICTIONARY #{language}_ispell (
      TEMPLATE  = ispell,
      DictFile  = #{language}_#{file},
      AffFile   = #{language}_#{file},
      StopWords = #{language}_#{file}
    );
  SQL
  # stem is already there.
  create_dictionary_map(language)
end

#get_server_versionObject



290
291
292
# File 'lib/odba/storage.rb', line 290

def get_server_version
  /\s([\d\.]+)\s/.match(self.dbi.select_all("select version();").first.first)[1]
end

#index_delete_origin(index_name, odba_id, term) ⇒ Object



322
323
324
325
326
327
328
# File 'lib/odba/storage.rb', line 322

def index_delete_origin(index_name, odba_id, term)
  self.dbi.do <<-SQL, odba_id, term
    DELETE FROM #{index_name} 
    WHERE origin_id = ?
    AND search_term = ?
  SQL
end

#index_delete_target(index_name, origin_id, search_term, target_id) ⇒ Object



329
330
331
332
333
334
335
336
# File 'lib/odba/storage.rb', line 329

def index_delete_target(index_name, origin_id, search_term, target_id)
  self.dbi.do <<-SQL, origin_id, search_term, target_id
    DELETE FROM #{index_name} 
    WHERE origin_id = ?
    AND search_term = ?
    AND target_id = ?
  SQL
end

#index_fetch_keys(index_name, length = nil) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/odba/storage.rb', line 337

def index_fetch_keys(index_name, length=nil)
  expr = if(length)
           "substr(search_term, 1, #{length})"
         else
           "search_term"
         end
  sql = <<-SQL
    SELECT DISTINCT #{expr} AS key
    FROM #{index_name}
    ORDER BY key
  SQL
  self.dbi.select_all(sql).flatten
end

#index_matches(index_name, substring, limit = nil, offset = 0) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/odba/storage.rb', line 350

def index_matches(index_name, substring, limit=nil, offset=0)
  sql = <<-SQL
    SELECT DISTINCT search_term AS key
    FROM #{index_name}
    WHERE search_term LIKE ?
    ORDER BY key
  SQL
  if limit
    sql << "LIMIT #{limit}\n"
  end
  if offset > 0
    sql << "OFFSET #{offset}\n"
  end
  self.dbi.select_all(sql, substring + '%').flatten
end

#index_origin_ids(index_name, target_id) ⇒ Object



365
366
367
368
369
370
371
372
# File 'lib/odba/storage.rb', line 365

def index_origin_ids(index_name, target_id)
  sql = <<-SQL
    SELECT DISTINCT origin_id, search_term
    FROM #{index_name}
    WHERE target_id=?
  SQL
  self.dbi.select_all(sql, target_id)
end

#index_target_ids(index_name, origin_id) ⇒ Object



373
374
375
376
377
378
379
380
# File 'lib/odba/storage.rb', line 373

def index_target_ids(index_name, origin_id)
  sql = <<-SQL
    SELECT DISTINCT target_id, search_term
    FROM #{index_name}
    WHERE origin_id=?
  SQL
  self.dbi.select_all(sql, origin_id)
end

#max_idObject



381
382
383
384
385
386
# File 'lib/odba/storage.rb', line 381

def max_id
  @id_mutex.synchronize do
    ensure_next_id_set
    @next_id
  end
end

#next_idObject



387
388
389
390
391
392
# File 'lib/odba/storage.rb', line 387

def next_id
  @id_mutex.synchronize do
    ensure_next_id_set
    @next_id += 1
  end
end

#remove_dictionary(language) ⇒ Object



409
410
411
412
413
414
415
416
417
418
# File 'lib/odba/storage.rb', line 409

def remove_dictionary(language)
  # remove configuration
  self.dbi.do <<-SQL
    DROP TEXT SEARCH CONFIGURATION IF EXISTS default_#{language}
  SQL
  # remove ispell dictionaries
  self.dbi.do <<-SQL
    DROP TEXT SEARCH DICTIONARY IF EXISTS #{language}_ispell;
  SQL
end

#reserve_next_id(reserved_id) ⇒ Object



398
399
400
401
402
403
404
405
406
407
408
# File 'lib/odba/storage.rb', line 398

def reserve_next_id(reserved_id)
  @id_mutex.synchronize do
    ensure_next_id_set
    if @next_id < reserved_id
      @next_id = reserved_id
    else
      raise OdbaDuplicateIdError,
            "The id '#{reserved_id}' has already been assigned"
    end
  end
end

#restore(odba_id) ⇒ Object



419
420
421
422
# File 'lib/odba/storage.rb', line 419

def restore(odba_id)
	row = self.dbi.select_one("SELECT content FROM object WHERE odba_id = ?", odba_id)
	row.first unless row.nil?
end

#restore_collection(odba_id) ⇒ Object



503
504
505
506
507
# File 'lib/odba/storage.rb', line 503

def restore_collection(odba_id)
	self.dbi.select_all <<-EOQ
		SELECT key, value FROM collection WHERE odba_id = #{odba_id}
	EOQ
end

#restore_named(name) ⇒ Object



508
509
510
511
512
# File 'lib/odba/storage.rb', line 508

def restore_named(name)
	row = self.dbi.select_one("SELECT content FROM object WHERE name = ?", 
		name)
	row.first unless row.nil?
end

#restore_prefetchableObject



513
514
515
516
517
# File 'lib/odba/storage.rb', line 513

def restore_prefetchable
	self.dbi.select_all <<-EOQ
		SELECT odba_id, content FROM object WHERE prefetchable = true
	EOQ
end

#retrieve_connected_objects(target_id) ⇒ Object



423
424
425
426
427
428
429
# File 'lib/odba/storage.rb', line 423

def retrieve_connected_objects(target_id)
	sql = <<-SQL 
		SELECT origin_id FROM object_connection 
		WHERE target_id = ?
	SQL
	self.dbi.select_all(sql, target_id)
end

#retrieve_from_condition_index(index_name, conditions, limit = nil) ⇒ Object



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/odba/storage.rb', line 430

def retrieve_from_condition_index(index_name, conditions, limit=nil)
  sql = <<-EOQ
    SELECT target_id, COUNT(target_id) AS relevance
    FROM #{index_name}
    WHERE TRUE
  EOQ
  values = []
  lines = conditions.collect { |name, info|
    val = nil
    condition = nil
    if(info.is_a?(Hash))
      condition = info['condition']
      if(val = info['value']) 
        if(/i?like/i.match(condition))
          val += '%'
        end
        condition = "#{condition || '='} ?"
        values.push(val.to_s)
      end
    elsif(info)
      condition = "= ?"
      values.push(info.to_s)
    end
    sql << <<-EOQ
      AND #{name} #{condition || 'IS NULL'}
    EOQ
  }
  sql << "        GROUP BY target_id\n"
  if(limit)
    sql << " LIMIT #{limit}"
  end
  self.dbi.select_all(sql, *values)
end

#retrieve_from_fulltext_index(index_name, search_term, dict, limit = nil) ⇒ Object



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/odba/storage.rb', line 463

def retrieve_from_fulltext_index(index_name, search_term, dict, limit=nil)
    ## this combination of gsub statements solves the problem of 
    #  properly escaping strings of this form: "(2:1)" into 
    #  '\(2\:1\)' (see test_retrieve_from_fulltext_index)
	term = search_term.strip.gsub(/\s+/, '&').gsub(/&+/, '&')\
      .gsub(/[():]/i, '\\ \\&').gsub(/\s/, '')
   sql = <<-EOQ
		SELECT target_id, 
			max(ts_rank(search_term, to_tsquery(?, ?))) AS relevance
		FROM #{index_name} 
		WHERE search_term @@ to_tsquery(?, ?) 
		GROUP BY target_id
		ORDER BY relevance DESC
	EOQ
    if(limit)
      sql << " LIMIT #{limit}"
    end
	self.dbi.select_all(sql, dict, term, dict, term)
rescue DBI::ProgrammingError => e
	warn("ODBA::Storage.retrieve_from_fulltext_index rescued a DBI::ProgrammingError(#{e.message}). Query:")
	warn("self.dbi.select_all(#{sql}, #{dict}, #{term}, #{dict}, #{term})")
	warn("returning empty result")
	[]
end

#retrieve_from_index(index_name, search_term, exact = nil, limit = nil) ⇒ Object



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/odba/storage.rb', line 487

def retrieve_from_index(index_name, search_term, 
                        exact=nil, limit=nil)
  unless(exact)
    search_term = search_term + "%"
  end
  sql = <<-EOQ
    SELECT target_id, COUNT(target_id) AS relevance
    FROM #{index_name}
    WHERE search_term LIKE ?
    GROUP BY target_id
  EOQ
  if(limit)
    sql << " LIMIT #{limit}"
  end
  self.dbi.select_all(sql, search_term)	 
end

#setupObject



518
519
520
521
522
523
524
525
526
527
528
# File 'lib/odba/storage.rb', line 518

def setup
  TABLES.each { |name, definition|
    self.dbi.do(definition) rescue DBI::ProgrammingError
  }
  unless(self.dbi.columns('object').any? { |col| col.name == 'extent' })
    self.dbi.do <<-EOS
ALTER TABLE object ADD COLUMN extent TEXT;
CREATE INDEX IF NOT EXISTS extent_index ON object(extent);
    EOS
  end
end

#store(odba_id, dump, name, prefetchable, klass) ⇒ Object



529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'lib/odba/storage.rb', line 529

def store(odba_id, dump, name, prefetchable, klass)
	sql = "SELECT name FROM object WHERE odba_id = ?"
	if(row = self.dbi.select_one(sql, odba_id))
		name ||= row['name']
		self.dbi.do <<-SQL, dump, name, prefetchable, klass.to_s, odba_id
			UPDATE object SET 
			content = ?,
			name = ?,
			prefetchable = ?,
        extent = ?
			WHERE odba_id = ?
		SQL
	else
		self.dbi.do <<-SQL, odba_id, dump, name, prefetchable, klass.to_s
			INSERT INTO object (odba_id, content, name, prefetchable, extent)
			VALUES (?, ?, ?, ?, ?)
		SQL
	end
end

#transaction(&block) ⇒ Object



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/odba/storage.rb', line 548

def transaction(&block)
	dbi = nil
	retval = nil
	@dbi.transaction { |dbi|
      ## this should not be necessary anymore:
		#dbi['AutoCommit'] = false
		Thread.current[:txn] = dbi
		retval = block.call
	}
	retval
ensure
    ## this should not be necessary anymore:
	#dbi['AutoCommit'] = true
	Thread.current[:txn] = nil
end

#update_condition_index(index_name, origin_id, search_terms, target_id) ⇒ Object



563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'lib/odba/storage.rb', line 563

def update_condition_index(index_name, origin_id, search_terms, target_id)
  keys = []
  vals = []
  search_terms.each { |key, val|
    keys.push(key)
    vals.push(val)
  }
  if(target_id)
    self.dbi.do <<-SQL, origin_id, target_id, *vals
INSERT INTO #{index_name} (origin_id, target_id, #{keys.join(', ')})
VALUES (?, ?#{', ?' * keys.size})
    SQL
  else
    key_str = keys.collect { |key| "#{key}=?" }.join(', ')
    self.dbi.do <<-SQL, *(vals.push(origin_id))
UPDATE #{index_name} SET #{key_str}
WHERE origin_id = ?
    SQL
  end
end

#update_fulltext_index(index_name, origin_id, search_term, target_id, dict) ⇒ Object



583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/odba/storage.rb', line 583

def update_fulltext_index(index_name, origin_id, search_term, target_id, dict)
  search_term = search_term.gsub(/\s+/, ' ').strip
  if(target_id)
    self.dbi.do <<-SQL, origin_id, dict, search_term, target_id
INSERT INTO #{index_name} (origin_id, search_term, target_id)
VALUES (?, to_tsvector(?, ?), ?)
    SQL
  else
    self.dbi.do <<-SQL, dict, search_term, origin_id
UPDATE #{index_name} SET search_term=to_tsvector(?, ?)
WHERE origin_id=?
    SQL
  end
end

#update_index(index_name, origin_id, search_term, target_id) ⇒ Object



597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'lib/odba/storage.rb', line 597

def update_index(index_name, origin_id, search_term, target_id)
    if(target_id)
      self.dbi.do <<-SQL, origin_id, search_term, target_id
        INSERT INTO #{index_name} (origin_id, search_term, target_id) 
        VALUES (?, ?, ?)
      SQL
    else
      self.dbi.do <<-SQL, search_term, origin_id
        UPDATE #{index_name} SET search_term=?
        WHERE origin_id=?
      SQL
    end
end

#update_max_id(id) ⇒ Object



393
394
395
396
397
# File 'lib/odba/storage.rb', line 393

def update_max_id(id)
  @id_mutex.synchronize do
    @next_id = id
  end
end