Class: FullSearch::Index
- Inherits:
-
Object
- Object
- FullSearch::Index
- Defined in:
- lib/full_search/index.rb
Class Method Summary collapse
- .create_triggers!(model) ⇒ Object
- .drop!(model) ⇒ Object
-
.drop_fts_table_safely!(table_name) ⇒ Object
Drops an FTS5 virtual table, recovering from corruption where a bare DROP TABLE IF EXISTS raises "invalid fts5 file format" because the shadow tables were tampered with out-of-band.
-
.drop_shadow_tables!(table_name) ⇒ Object
FTS5 stores its index data in a fixed set of shadow tables named
_content,
_data,
_idx,
_docsize and
_config.
- .drop_triggers!(model) ⇒ Object
- .ensure_table!(model) ⇒ Object
- .fts_table_name(model) ⇒ Object
- .healthy?(model) ⇒ Boolean
- .missing_table?(model) ⇒ Boolean
- .optimize!(model) ⇒ Object
- .rebuild!(model) ⇒ Object
- .rebuild_if_needed!(model) ⇒ Object
- .reindex_source_fields!(model) ⇒ Object
- .sqlite!(model) ⇒ Object
- .sqlite?(model = nil) ⇒ Boolean
- .stored_config_hash(model) ⇒ Object
- .trigram_table_exists?(model) ⇒ Boolean
- .trigram_table_name(model) ⇒ Object
- .trigram_table_needed?(model) ⇒ Boolean
- .truncate!(model) ⇒ Object
- .upsert_source_row!(model, rowid, values) ⇒ Object
- .verified_tables ⇒ Object
Class Method Details
.create_triggers!(model) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153
# File 'lib/full_search/index.rb', line 141 def create_triggers!(model) connection.execute(insert_trigger_sql(model)) connection.execute(delete_trigger_sql(model)) connection.execute(update_trigger_sql(model)) if model.full_search_dsl.soft_delete_column connection.execute(soft_delete_removal_trigger_sql(model)) end if trigram_table_needed?(model) connection.execute(insert_trigram_trigger_sql(model)) connection.execute(delete_trigram_trigger_sql(model)) connection.execute(update_trigram_trigger_sql(model)) end end
.drop!(model) ⇒ Object
179 180 181 182 183 184 185 186
# File 'lib/full_search/index.rb', line 179 def drop!(model) IndexCache.clear! verified_tables.delete(model.table_name) sqlite!(model) drop_triggers!(model) drop_fts_table_safely!(fts_table_name(model)) drop_fts_table_safely!(trigram_table_name(model)) end
.drop_fts_table_safely!(table_name) ⇒ Object
Drops an FTS5 virtual table, recovering from corruption where a bare DROP TABLE IF EXISTS raises "invalid fts5 file format" because the shadow tables were tampered with out-of-band. When that happens we drop the shadow tables directly, then drop the (now-empty) virtual table wrapper, so the caller can recreate a clean index.
192 193 194 195 196 197 198 199
# File 'lib/full_search/index.rb', line 192 def drop_fts_table_safely!(table_name) connection.execute("DROP TABLE IF EXISTS #{qt(table_name)};") rescue ActiveRecord::StatementInvalid => e raise unless e..match?(/fts5: corruption|invalid fts5 file format|malformed database schema/i) drop_shadow_tables!(table_name) connection.execute("DROP TABLE IF EXISTS #{qt(table_name)};") end
.drop_shadow_tables!(table_name) ⇒ Object
FTS5 stores its index data in a fixed set of shadow tables named
_content,
_data,
_idx,
_docsize and
_config. Dropping these lets SQLite recover a virtual table that is otherwise undroppable due to internal corruption.
205 206 207 208 209 210
# File 'lib/full_search/index.rb', line 205 def drop_shadow_tables!(table_name) %w[content data idx docsize config].each do |suffix| shadow = "#{table_name}_#{suffix}" connection.execute("DROP TABLE IF EXISTS #{qt(shadow)};") end end
.drop_triggers!(model) ⇒ Object
155 156 157 158 159
# File 'lib/full_search/index.rb', line 155 def drop_triggers!(model) (trigger_names(model) + trigram_trigger_names(model)).each do |name| connection.execute("DROP TRIGGER IF EXISTS #{qt(name)};") end end
.ensure_table!(model) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
# File 'lib/full_search/index.rb', line 21 def ensure_table!(model) return if model.respond_to?(:table_name) && verified_tables.include?(model.table_name) sqlite!(model) conn = connection dsl = model.full_search_dsl return unless dsl if table_exists?(model) && !healthy?(model) rebuild!(model) end fts_was_created = false unless table_exists?(model) conn.execute(create_virtual_table_sql(model)) fts_was_created = true end trigram_was_created = false if trigram_table_needed?(model) && !trigram_table_exists?(model) FullSearch::Typo.warn_unsupported!(model) unless FullSearch::Typo.supported?(model) conn.execute(create_trigram_virtual_table_sql(model)) trigram_was_created = true end if fts_was_created || trigram_was_created conn.execute(backfill_sql(model)) if fts_was_created conn.execute(backfill_trigram_sql(model)) if trigram_was_created reindex_source_fields!(model) if dsl.fields.any?(&:source) store_config_hash!(model) end ensure_triggers!(model) if model_table_exists?(model) verified_tables.add(model.table_name) end
.fts_table_name(model) ⇒ Object
212 213 214
# File 'lib/full_search/index.rb', line 212 def fts_table_name(model) "#{model.table_name}_fts" end
.healthy?(model) ⇒ Boolean
248 249 250 251 252 253 254 255 256 257
# File 'lib/full_search/index.rb', line 248 def healthy?(model) return false unless table_exists?(model) connection.execute("SELECT count(*) FROM #{qt(fts_table_name(model))}") true rescue ActiveRecord::StatementInvalid => e raise unless e..match?(/fts5: corruption|invalid fts5 file format|malformed database schema/i) false end
.missing_table?(model) ⇒ Boolean
244 245 246
# File 'lib/full_search/index.rb', line 244 def missing_table?(model) !table_exists?(model) end
.optimize!(model) ⇒ Object
111 112 113 114 115 116
# File 'lib/full_search/index.rb', line 111 def optimize!(model) FullSearch::Instrumentation.instrument("optimize", model: model.name) do sqlite!(model) connection.execute("INSERT INTO #{qt(fts_table_name(model))}(#{qt(fts_table_name(model))}) VALUES('optimize');") end end
.rebuild!(model) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
# File 'lib/full_search/index.rb', line 61 def rebuild!(model) IndexCache.clear! sqlite!(model) dsl = model.full_search_dsl return unless dsl conn = connection with_rebuild_lock(model) do FullSearch::Instrumentation.instrument("rebuild", model: model.name) do drop_triggers!(model) drop_fts_table_safely!(fts_table_name(model)) drop_fts_table_safely!(trigram_table_name(model)) conn.execute(create_virtual_table_sql(model)) if trigram_table_needed?(model) FullSearch::Typo.warn_unsupported!(model) unless FullSearch::Typo.supported?(model) conn.execute(create_trigram_virtual_table_sql(model)) end conn.execute(backfill_sql(model)) conn.execute(backfill_trigram_sql(model)) if trigram_table_needed?(model) reindex_source_fields!(model) if dsl.fields.any?(&:source) create_triggers!(model) optimize!(model) store_config_hash!(model, rebuilt_at: Time.current) verified_tables.add(model.table_name) end end end
.rebuild_if_needed!(model) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
# File 'lib/full_search/index.rb', line 92 def rebuild_if_needed!(model) IndexCache.clear! sqlite!(model) dsl = model.full_search_dsl return false unless dsl ensure_table!(model) stored = stored_config_hash(model) if stored && stored == dsl.config_hash false else rebuild!(model) true end end
.reindex_source_fields!(model) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130
# File 'lib/full_search/index.rb', line 118 def reindex_source_fields!(model) dsl = model.full_search_dsl return unless dsl source_fields = dsl.fields.select(&:source) return if source_fields.empty? model.find_each(batch_size: 500) do |record| pairs = source_fields.to_h { |f| [column_name(f), FullSearch::Model.evaluate_source(record, f).to_s] } next if pairs.empty? upsert_source_row!(model, record.id, pairs) end end
.sqlite!(model) ⇒ Object
230 231 232 233
# File 'lib/full_search/index.rb', line 230 def sqlite!(model) adapter = model ? model.connection.adapter_name : connection.adapter_name raise UnsupportedDatabaseError, "full_search requires SQLite, but #{adapter} is configured" unless sqlite?(model) end
.sqlite?(model = nil) ⇒ Boolean
220 221 222 223 224 225 226 227 228
# File 'lib/full_search/index.rb', line 220 def sqlite?(model = nil) if model model.connection.adapter_name.downcase.include?("sqlite") else connection.adapter_name.downcase.include?("sqlite") end rescue ActiveRecord::ConnectionNotEstablished, NoMethodError connection.adapter_name.downcase.include?("sqlite") end
.stored_config_hash(model) ⇒ Object
235 236 237 238 239 240 241 242
# File 'lib/full_search/index.rb', line 235 def stored_config_hash(model) IndexCache.fetch("stored_config_hash:#{model.table_name}") do row = connection.execute( "SELECT config_hash FROM full_search_index_versions WHERE table_name=#{q(model.table_name)}" ).first row&.[]("config_hash") end end
.trigram_table_exists?(model) ⇒ Boolean
263 264 265 266 267
# File 'lib/full_search/index.rb', line 263 def trigram_table_exists?(model) connection.execute( "SELECT name FROM sqlite_master WHERE type='table' AND name=#{q(trigram_table_name(model))} LIMIT 1" ).any? end
.trigram_table_name(model) ⇒ Object
216 217 218
# File 'lib/full_search/index.rb', line 216 def trigram_table_name(model) "#{fts_table_name(model)}_trigram" end
.trigram_table_needed?(model) ⇒ Boolean
259 260 261
# File 'lib/full_search/index.rb', line 259 def trigram_table_needed?(model) model.full_search_dsl&.typo_tolerance? && model.full_search_dsl.tokenize != "trigram" end
.truncate!(model) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
# File 'lib/full_search/index.rb', line 161 def truncate!(model) IndexCache.clear! sqlite!(model) dsl = model.full_search_dsl return unless dsl return unless table_exists?(model) conn = connection begin conn.execute("DELETE FROM #{qt(fts_table_name(model))};") conn.execute("DELETE FROM #{qt(trigram_table_name(model))};") if trigram_table_needed?(model) && trigram_table_exists?(model) rescue ActiveRecord::StatementInvalid => e raise unless e..match?(/fts5: corruption|invalid fts5 file format|malformed database schema/i) rebuild!(model) end end
.upsert_source_row!(model, rowid, values) ⇒ Object
132 133 134 135 136 137 138 139
# File 'lib/full_search/index.rb', line 132 def upsert_source_row!(model, rowid, values) table = qt(fts_table_name(model)) sets = values.map { |name, value| "#{qc(name)} = #{q(value)}" }.join(", ") connection.execute(<<~SQL) UPDATE #{table} SET #{sets} WHERE rowid = #{q(rowid)}; SQL end
.verified_tables ⇒ Object
17 18 19
# File 'lib/full_search/index.rb', line 17 def verified_tables @verified_tables ||= Set.new end