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
ID_SEQUENCE_GAP =

Distance between the highest id in use and the first the sequence hands out; see the odba_id_seq entry in TABLES.

100_000
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],
    CREATE TABLE IF NOT EXISTS collection (
      odba_id integer NOT NULL, key text, value text,
      PRIMARY KEY(odba_id, key)
    );
  SQL
  # The odba_id comes from this sequence, so that several processes on
  # one database cannot hand out the same one. See #next_id.
  #
  # The start value is computed and must be: a plain CREATE SEQUENCE
  # starts at 1 and would re-issue ids that already exist. The gap on
  # top of MAX(odba_id) covers ids that processes still running with the
  # old in-memory counter hold but have not written yet. Skipped numbers
  # cost nothing - the odba_id is a surrogate key and carries no meaning.
  ["odba_id_seq", <<~SQL]
    DO $$
    BEGIN
      IF NOT EXISTS (SELECT 1 FROM pg_class
                     WHERE relkind = 'S' AND relname = 'odba_id_seq') THEN
        EXECUTE format('CREATE SEQUENCE odba_id_seq START WITH %s',
                       (SELECT COALESCE(MAX(odba_id), 0) + #{ID_SEQUENCE_GAP}
                        FROM object));
      END IF;
    END $$;
  SQL
]

Instance Method Summary collapse

Constructor Details

#initializeStorage

Returns a new instance of Storage.



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

def initialize
  @id_mutex = Mutex.new
end

Instance Method Details

#bulk_restore(bulk_fetch_ids) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/odba/storage.rb', line 79

def bulk_restore(bulk_fetch_ids)
  if bulk_fetch_ids.empty?
    []
  else
    bulk_fetch_ids = bulk_fetch_ids.uniq
    rows = []
    until (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(dbi.select_all(sql))
    end
    rows
  end
end

#collection_fetch(odba_id, key_dump) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/odba/storage.rb', line 96

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

#collection_remove(odba_id, key_dump) ⇒ Object



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

def collection_remove(odba_id, key_dump)
  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



112
113
114
115
116
117
# File 'lib/odba/storage.rb', line 112

def collection_store(odba_id, key_dump, value_dump)
  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



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

def condition_index_delete(index_name, origin_id,
  search_terms, target_id = nil)
  values = []
  sql = "DELETE FROM #{index_name}"
  sql << if origin_id
    " WHERE origin_id = ?"
  else
    " 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
  dbi.do sql, origin_id, *values
end

#condition_index_ids(index_name, id, id_name) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/odba/storage.rb', line 139

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

#create_condition_index(table_name, definition) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/odba/storage.rb', line 171

def create_condition_index(table_name, definition)
  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
  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|
    dbi.do <<~SQL
      CREATE INDEX IF NOT EXISTS #{name}_#{table_name} ON #{table_name}(#{name});
    SQL
  }
  # index target_id
  dbi.do <<~SQL
    CREATE INDEX IF NOT EXISTS target_id_#{table_name} ON #{table_name}(target_id);
  SQL
end

#create_dictionary_map(language) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/odba/storage.rb', line 148

def create_dictionary_map(language)
  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
  dbi.do <<-SQL
    ALTER TEXT SEARCH CONFIGURATION default_#{language}
    ALTER MAPPING FOR
      host, file, int, uint, version
    WITH simple;
  SQL
  # drop from default setting
  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



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/odba/storage.rb', line 195

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

#create_index(table_name) ⇒ Object



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
# File 'lib/odba/storage.rb', line 220

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

#dbiObject



248
249
250
# File 'lib/odba/storage.rb', line 248

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

#dbi=(dbi) ⇒ Object

Not attr_writer: whether the store has an id sequence is memoized, and that answer belongs to the connection it was asked on.



15
16
17
18
# File 'lib/odba/storage.rb', line 15

def dbi=(dbi)
  @id_sequence = nil
  @dbi = dbi
end

#delete_index_element(index_name, odba_id, id_name) ⇒ Object



256
257
258
259
260
# File 'lib/odba/storage.rb', line 256

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

#delete_persistable(odba_id) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/odba/storage.rb', line 262

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

#drop_index(index_name) ⇒ Object



252
253
254
# File 'lib/odba/storage.rb', line 252

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

#ensure_object_connections(origin_id, target_ids) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/odba/storage.rb', line 281

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
  ## use self.dbi instead of @dbi to get information about
  ## object_connections previously stored within this transaction
  if (rows = 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?
      until (ids = delete_ids.slice!(0, BULK_FETCH_STEP)).empty?
        dbi.do <<-SQL, origin_id
          DELETE FROM object_connection
          WHERE origin_id = ? AND target_id IN (#{ids.join(",")})
        SQL
      end
    end
  end
  sth = 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



314
315
316
317
318
319
320
321
# File 'lib/odba/storage.rb', line 314

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

#extent_count(klass) ⇒ Object



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

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

#extent_ids(klass) ⇒ Object



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

def extent_ids(klass)
  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



335
336
337
338
339
340
# File 'lib/odba/storage.rb', line 335

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

#fulltext_index_target_ids(index_name, origin_id) ⇒ Object



346
347
348
349
350
351
352
353
# File 'lib/odba/storage.rb', line 346

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

#generate_dictionary(language) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/odba/storage.rb', line 355

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/
  # Use /usr/local/pgsql-10.1/bin/pg_config --sharedir to get the current value
  # 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
  dbi.do <<-SQL
    DROP TEXT SEARCH DICTIONARY IF EXISTS  public.default_#{language};
  SQL
  dbi.do <<-SQL
    CREATE TEXT SEARCH CONFIGURATION public.default_#{language} ( COPY = pg_catalog.#{language} );
  SQL
  # ispell
  dbi.do <<-SQL
    DROP TEXT SEARCH DICTIONARY IF EXISTS  #{language}_ispell;
  SQL
  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



342
343
344
# File 'lib/odba/storage.rb', line 342

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

#id_sequence?Boolean

Returns:

  • (Boolean)


484
485
486
487
488
489
490
491
# File 'lib/odba/storage.rb', line 484

def id_sequence?
  return @id_sequence unless @id_sequence.nil?
  @id_sequence = dbi.select_one(
    "SELECT 1 FROM pg_class WHERE relkind = 'S' AND relname = 'odba_id_seq'"
  ) ? true : false
rescue
  @id_sequence = false
end

#index_delete_origin(index_name, odba_id, term) ⇒ Object



384
385
386
387
388
389
390
# File 'lib/odba/storage.rb', line 384

def index_delete_origin(index_name, odba_id, term)
  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



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

def index_delete_target(index_name, origin_id, search_term, target_id)
  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



401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/odba/storage.rb', line 401

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
  dbi.select_all(sql).flatten
end

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



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/odba/storage.rb', line 415

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
  dbi.select_all(sql, substring + "%").flatten
end

#index_origin_ids(index_name, target_id) ⇒ Object



431
432
433
434
435
436
437
438
# File 'lib/odba/storage.rb', line 431

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

#index_target_ids(index_name, origin_id) ⇒ Object



440
441
442
443
444
445
446
447
# File 'lib/odba/storage.rb', line 440

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

#max_idObject



449
450
451
452
453
454
# File 'lib/odba/storage.rb', line 449

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

#next_idObject

The id is allocated by the database, not by a counter in this process.

It used to be @next_id += 1 under this mutex, with @next_id seeded once per process from the highest odba_id in the table. That is sound for a single process and wrong for every deployment that runs more than one - web workers and import jobs on the same database each kept their own counter and handed out the same numbers, so one silently overwrote the other's row in object.

Falls back to the old behaviour where no sequence exists, so a store that was never through #setup keeps working; #setup creates it.



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/odba/storage.rb', line 467

def next_id
  if id_sequence?
    dbi.select_one("SELECT nextval('odba_id_seq')").first.to_i.tap { |id|
      # max_id and reserve_next_id read @next_id, so keep it in step.
      # Never backwards: a peer may already stand higher.
      @id_mutex.synchronize {
        @next_id = id if @next_id.nil? || @next_id < id
      }
    }
  else
    @id_mutex.synchronize do
      ensure_next_id_set
      @next_id += 1
    end
  end
end

#remove_dictionary(language) ⇒ Object



511
512
513
514
515
516
517
518
519
520
# File 'lib/odba/storage.rb', line 511

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

#reserve_next_id(reserved_id) ⇒ Object



499
500
501
502
503
504
505
506
507
508
509
# File 'lib/odba/storage.rb', line 499

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



522
523
524
525
# File 'lib/odba/storage.rb', line 522

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

#restore_collection(odba_id) ⇒ Object



611
612
613
614
615
# File 'lib/odba/storage.rb', line 611

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

#restore_named(name) ⇒ Object



617
618
619
620
621
# File 'lib/odba/storage.rb', line 617

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

#restore_prefetchableObject



623
624
625
626
627
# File 'lib/odba/storage.rb', line 623

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

#retrieve_connected_objects(target_id) ⇒ Object



527
528
529
530
531
532
533
# File 'lib/odba/storage.rb', line 527

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

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



535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/odba/storage.rb', line 535

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 = []
  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
  dbi.select_all(sql, *values)
end

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



569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# File 'lib/odba/storage.rb', line 569

def retrieve_from_fulltext_index(index_name, search_term, 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+/, "&").squeeze("&")
    .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
  dbi.select_all(sql, term, 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}, #{term}, #{term})")
  warn("returning empty result")
  []
end

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



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

def retrieve_from_index(index_name, search_term,
  exact = nil, limit = nil)
  unless exact
    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
  dbi.select_all(sql, search_term)
end

#setupObject



629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'lib/odba/storage.rb', line 629

def setup
  old_stderr = $stderr.dup # We want to silence the annoying NOTICE:  relation "object" already exists, skipping
  $stderr.reopen("/dev/null", "w")
  TABLES.each { |name, definition|
    begin
      dbi.do(definition)
    rescue
      $stderr = old_stderr
      DBI::ProgrammingError
    end
  }
  unless dbi.columns("object").any? { |col| col.name == "extent" }
    dbi.do <<~EOS
      ALTER TABLE object ADD COLUMN extent TEXT;
      CREATE INDEX IF NOT EXISTS extent_index ON object(extent);
    EOS
  end
  $stderr = old_stderr
end

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



649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# File 'lib/odba/storage.rb', line 649

def store(odba_id, dump, name, prefetchable, klass)
  sql = "SELECT name FROM object WHERE odba_id = ?"
  if (row = dbi.select_one(sql, odba_id))
    name ||= row["name"]
    dbi.do <<-SQL, dump, name, prefetchable, klass.to_s, odba_id
	UPDATE object SET 
	content = ?,
	name = ?,
	prefetchable = ?,
      extent = ?
	WHERE odba_id = ?
    SQL
  else
    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



669
670
671
672
673
674
675
676
677
678
679
680
681
682
# File 'lib/odba/storage.rb', line 669

def transaction(&block)
  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



684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
# File 'lib/odba/storage.rb', line 684

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
    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(", ")
    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) ⇒ Object



705
706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'lib/odba/storage.rb', line 705

def update_fulltext_index(index_name, origin_id, search_term, target_id)
  search_term = search_term.gsub(/\s+/, " ").strip
  if target_id
    dbi.do <<~SQL, origin_id.to_s, search_term, target_id
      INSERT INTO #{index_name} (origin_id, search_term, target_id)
      VALUES (?, to_tsvector(?), ?)
    SQL
  else
    dbi.do <<~SQL, 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



720
721
722
723
724
725
726
727
728
729
730
731
732
# File 'lib/odba/storage.rb', line 720

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

#update_max_id(id) ⇒ Object



493
494
495
496
497
# File 'lib/odba/storage.rb', line 493

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