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', "CREATE TABLE IF NOT EXISTS object (\n  odba_id INTEGER NOT NULL, content TEXT,\n  name TEXT, prefetchable BOOLEAN, extent TEXT,\n  PRIMARY KEY(odba_id), UNIQUE(name)\n);\n  SQL\n  ['prefetchable_index', <<-SQL],\nCREATE INDEX IF NOT EXISTS prefetchable_index ON object(prefetchable);\n  SQL\n  ['extent_index', <<-SQL],\nCREATE INDEX IF NOT EXISTS extent_index ON object(extent);\n  SQL\n  # helper table 'object_connection'\n  ['object_connection', <<-'SQL'],\nCREATE TABLE IF NOT EXISTS object_connection (\n  origin_id integer, target_id integer,\n  PRIMARY KEY(origin_id, target_id)\n);\n  SQL\n  ['target_id_index', <<-SQL],\nCREATE INDEX IF NOT EXISTS target_id_index ON object_connection(target_id);\n  SQL\n  # helper table 'collection'\n  ['collection', <<-'SQL'],\nCREATE TABLE IF NOT EXISTS collection (\n  odba_id integer NOT NULL, key text, value text,\n  PRIMARY KEY(odba_id, key)\n);\n  SQL\n]\n"],

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



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

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 = "        SELECT odba_id, content FROM object \n        WHERE odba_id IN (\#{ids.join(',')})\n      SQL\n      rows.concat(self.dbi.select_all(sql))\n    end\n    rows\n  end\nend\n"

#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 = "    SELECT value FROM collection \n    WHERE odba_id = ? AND key = ?\n  SQL\n  row = self.dbi.select_one(sql, odba_id, key_dump)\n  row.first unless row.nil?\nend\n"

#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 "    DELETE FROM collection\n    WHERE odba_id = ? AND key = ?\n  SQL\nend\n", odba_id, key_dump

#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 "    INSERT INTO collection (odba_id, key, value)\n    VALUES (?, ?, ?)\n  SQL\nend\n", odba_id, key_dump, value_dump

#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 = "    SELECT DISTINCT *\n    FROM \#{index_name}\n    WHERE \#{id_name}=?\n  SQL\n  self.dbi.select_all(sql, id)\nend\n"

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

#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 "    ALTER TEXT SEARCH CONFIGURATION default_\#{language}\n    ALTER MAPPING FOR\n      asciiword, asciihword, hword_asciipart,\n      word, hword, hword_part, hword_numpart,\n      numword, numhword\n    WITH \#{language}_ispell, \#{language}_stem;\n  SQL\n  self.dbi.do <<-SQL\n    ALTER TEXT SEARCH CONFIGURATION default_\#{language}\n    ALTER MAPPING FOR\n      host, file, int, uint, version\n    WITH simple;\n  SQL\n  # drop from default setting\n  self.dbi.do <<-SQL\n  ALTER TEXT SEARCH CONFIGURATION default_\#{language}\n      DROP MAPPING FOR\n      email, url, url_path, sfloat, float\n  SQL\nend\n"

#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
180
181
182
# File 'lib/odba/storage.rb', line 159

def create_fulltext_index(table_name)
  self.dbi.do "DROP TABLE IF EXISTS \#{table_name};\n  SQL\n  self.dbi.do <<-SQL\nCREATE TABLE IF NOT EXISTS \#{table_name}  (\n  origin_id INTEGER,\n  search_term tsvector,\n  target_id INTEGER\n) WITH OIDS ;\n  SQL\n  #index origin_id\n  self.dbi.do <<-SQL\nCREATE INDEX IF NOT EXISTS origin_id_\#{table_name} ON \#{table_name}(origin_id);\n  SQL\n  self.dbi.do <<-SQL\nCREATE INDEX IF NOT EXISTS search_term_\#{table_name}\nON \#{table_name} USING gist(search_term);\n  SQL\n  #index target_id\n  self.dbi.do <<-SQL\nCREATE INDEX IF NOT EXISTS target_id_\#{table_name} ON \#{table_name}(target_id);\n  SQL\nend\n"

#create_index(table_name) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/odba/storage.rb', line 183

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

#delete_index_element(index_name, odba_id, id_name) ⇒ Object



216
217
218
219
220
# File 'lib/odba/storage.rb', line 216

def delete_index_element(index_name, odba_id, id_name)
  self.dbi.do "    DELETE FROM \#{index_name} WHERE \#{id_name} = ?\n  SQL\nend\n", odba_id

#delete_persistable(odba_id) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/odba/storage.rb', line 221

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

#drop_index(index_name) ⇒ Object



213
214
215
# File 'lib/odba/storage.rb', line 213

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

#ensure_object_connections(origin_id, target_ids) ⇒ Object



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
266
267
268
269
270
271
# File 'lib/odba/storage.rb', line 239

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

#ensure_target_id_index(table_name) ⇒ Object



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

def ensure_target_id_index(table_name)
  #index target_id
  self.dbi.do "    CREATE INDEX IF NOT EXISTS target_id_\#{table_name}\n    ON \#{table_name}(target_id)\n  SQL\nrescue\nend\n"

#extent_count(klass) ⇒ Object



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

def extent_count(klass)
  self.dbi.select_one("    SELECT COUNT(odba_id) FROM object WHERE extent = ?\n  EOQ\nend\n", klass.to_s).first

#extent_ids(klass) ⇒ Object



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

def extent_ids(klass)
  self.dbi.select_all("    SELECT odba_id FROM object WHERE extent = ?\n  EOQ\nend\n", klass.to_s).flatten

#fulltext_index_delete(index_name, id, id_name) ⇒ Object



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

def fulltext_index_delete(index_name, id, id_name)
  self.dbi.do "    DELETE FROM \#{index_name}\n    WHERE \#{id_name} = ?\n  SQL\nend\n", id

#fulltext_index_target_ids(index_name, origin_id) ⇒ Object



299
300
301
302
303
304
305
306
# File 'lib/odba/storage.rb', line 299

def fulltext_index_target_ids(index_name, origin_id)
  sql = "    SELECT DISTINCT target_id\n    FROM \#{index_name}\n    WHERE origin_id=?\n  SQL\n  self.dbi.select_all(sql, origin_id)\nend\n"

#generate_dictionary(language) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/odba/storage.rb', line 307

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
  self.dbi.do "    DROP TEXT SEARCH DICTIONARY IF EXISTS  public.default_\#{language};\n  SQL\n  self.dbi.do <<-SQL\n    CREATE TEXT SEARCH CONFIGURATION public.default_\#{language} ( COPY = pg_catalog.\#{language} );\n  SQL\n  # ispell\n  self.dbi.do <<-SQL\n    DROP TEXT SEARCH DICTIONARY IF EXISTS  \#{language}_ispell;\n  SQL\n  self.dbi.do <<-SQL\n    CREATE TEXT SEARCH DICTIONARY \#{language}_ispell (\n      TEMPLATE  = ispell,\n      DictFile  = \#{language}_\#{file},\n      AffFile   = \#{language}_\#{file},\n      StopWords = \#{language}_\#{file}\n    );\n  SQL\n  # stem is already there.\n  create_dictionary_map(language)\nend\n"

#get_server_versionObject



296
297
298
# File 'lib/odba/storage.rb', line 296

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



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

def index_delete_origin(index_name, odba_id, term)
  self.dbi.do "    DELETE FROM \#{index_name} \n    WHERE origin_id = ?\n    AND search_term = ?\n  SQL\nend\n", odba_id, term

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



342
343
344
345
346
347
348
349
# File 'lib/odba/storage.rb', line 342

def index_delete_target(index_name, origin_id, search_term, target_id)
  self.dbi.do "    DELETE FROM \#{index_name} \n    WHERE origin_id = ?\n    AND search_term = ?\n    AND target_id = ?\n  SQL\nend\n", origin_id, search_term, target_id

#index_fetch_keys(index_name, length = nil) ⇒ Object



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

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

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



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/odba/storage.rb', line 363

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

#index_origin_ids(index_name, target_id) ⇒ Object



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

def index_origin_ids(index_name, target_id)
  sql = "    SELECT DISTINCT origin_id, search_term\n    FROM \#{index_name}\n    WHERE target_id=?\n  SQL\n  self.dbi.select_all(sql, target_id)\nend\n"

#index_target_ids(index_name, origin_id) ⇒ Object



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

def index_target_ids(index_name, origin_id)
  sql = "    SELECT DISTINCT target_id, search_term\n    FROM \#{index_name}\n    WHERE origin_id=?\n  SQL\n  self.dbi.select_all(sql, origin_id)\nend\n"

#max_idObject



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

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

#next_idObject



400
401
402
403
404
405
# File 'lib/odba/storage.rb', line 400

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

#remove_dictionary(language) ⇒ Object



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

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

#reserve_next_id(reserved_id) ⇒ Object



411
412
413
414
415
416
417
418
419
420
421
# File 'lib/odba/storage.rb', line 411

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



432
433
434
435
# File 'lib/odba/storage.rb', line 432

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



516
517
518
519
520
# File 'lib/odba/storage.rb', line 516

def restore_collection(odba_id)
  self.dbi.select_all "    SELECT key, value FROM collection WHERE odba_id = \#{odba_id}\n  EOQ\nend\n"

#restore_named(name) ⇒ Object



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

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

#restore_prefetchableObject



526
527
528
529
530
# File 'lib/odba/storage.rb', line 526

def restore_prefetchable
  self.dbi.select_all "    SELECT odba_id, content FROM object WHERE prefetchable = true\n  EOQ\nend\n"

#retrieve_connected_objects(target_id) ⇒ Object



436
437
438
439
440
441
442
# File 'lib/odba/storage.rb', line 436

def retrieve_connected_objects(target_id)
  sql = "    SELECT origin_id FROM object_connection \n    WHERE target_id = ?\n  SQL\n  self.dbi.select_all(sql, target_id)\nend\n" 

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



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/odba/storage.rb', line 443

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

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



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/odba/storage.rb', line 476

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+/, '&').gsub(/&+/, '&')\
      .gsub(/[():]/i, '\\ \\&').gsub(/\s/, '')
   sql = "    SELECT target_id, \n      max(ts_rank(search_term, to_tsquery(?))) AS relevance\n    FROM \#{index_name} \n    WHERE search_term @@ to_tsquery(?)\n    GROUP BY target_id\n    ORDER BY relevance DESC\n  EOQ\n    if(limit)\n      sql << \" LIMIT \#{limit}\"\n    end\n  self.dbi.select_all(sql, term, term)\nrescue DBI::ProgrammingError => e\n  warn(\"ODBA::Storage.retrieve_from_fulltext_index rescued a DBI::ProgrammingError(\#{e.message}). Query:\")\n  warn(\"self.dbi.select_all(\#{sql}, \#{term}, \#{term})\")\n  warn(\"returning empty result\")\n  []\nend\n"

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



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/odba/storage.rb', line 500

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

#setupObject



531
532
533
534
535
536
537
538
539
540
541
# File 'lib/odba/storage.rb', line 531

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 "ALTER TABLE object ADD COLUMN extent TEXT;\nCREATE INDEX IF NOT EXISTS extent_index ON object(extent);\n    EOS\n  end\nend\n"

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



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/odba/storage.rb', line 542

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 "      UPDATE object SET \n      content = ?,\n      name = ?,\n      prefetchable = ?,\n        extent = ?\n      WHERE odba_id = ?\n    SQL\n  else\n    self.dbi.do <<-SQL, odba_id, dump, name, prefetchable, klass.to_s\n      INSERT INTO object (odba_id, content, name, prefetchable, extent)\n      VALUES (?, ?, ?, ?, ?)\n    SQL\n  end\nend\n", dump, name, prefetchable, klass.to_s, odba_id

#transaction(&block) ⇒ Object



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/odba/storage.rb', line 561

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



576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'lib/odba/storage.rb', line 576

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 "INSERT INTO \#{index_name} (origin_id, target_id, \#{keys.join(', ')})\nVALUES (?, ?\#{', ?' * keys.size})\n    SQL\n  else\n    key_str = keys.collect { |key| \"\#{key}=?\" }.join(', ')\n    self.dbi.do <<-SQL, *(vals.push(origin_id))\nUPDATE \#{index_name} SET \#{key_str}\nWHERE origin_id = ?\n    SQL\n  end\nend\n", origin_id, target_id, *vals

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



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

def update_fulltext_index(index_name, origin_id, search_term, target_id)
search_term = search_term.gsub(/\s+/, ' ').strip
if(target_id)
                   value = "INSERT INTO \#{index_name} (origin_id, search_term, target_id)\nVALUES (?, to_tsvector(?), ?)\n  SQL\n  result = self.dbi.do <<-SQL, origin_id.to_s, search_term, target_id\nINSERT INTO \#{index_name} (origin_id, search_term, target_id)\nVALUES (?, to_tsvector(?), ?)\n  SQL\nelse\n  result = self.dbi.do <<-SQL, search_term, origin_id\nUPDATE \#{index_name} SET search_term=to_tsvector(?)\nWHERE origin_id=?\n  SQL\nend\nend\n", origin_id.to_s, search_term, target_id

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



614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/odba/storage.rb', line 614

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

#update_max_id(id) ⇒ Object



406
407
408
409
410
# File 'lib/odba/storage.rb', line 406

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