Class: FtsLite::Index
- Inherits:
-
Object
- Object
- FtsLite::Index
- Defined in:
- lib/fts_lite/index.rb
Defined Under Namespace
Classes: RuntimeError
Constant Summary collapse
- DEFAULT_TOKENIZER =
:bigram
- DEFAULT_JURNAL_MODE =
"MEMORY"
- DEFAULT_TEMP_STORE =
"MEMORY"
- DEFAULT_CACHE_SIZE =
32000
- SQLITE_HAVE_FT4_REPLACE =
SQLite3.libversion >= 3007007
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #count ⇒ Object
- #delete(docid) ⇒ Object
- #delete_all ⇒ Object
- #drop_table! ⇒ Object
-
#initialize(path, options = {}) ⇒ Index
constructor
A new instance of Index.
- #search(text, options = {}) ⇒ Object
- #set(docid, text, sort_value = nil) ⇒ Object
- #tokenize(text) ⇒ Object
- #transaction(&block) ⇒ Object
- #update_sort_value(docid, sort_value) ⇒ Object
Constructor Details
#initialize(path, options = {}) ⇒ Index
Returns a new instance of Index.
20 21 22 23 24 25 26 |
# File 'lib/fts_lite/index.rb', line 20 def initialize(path, = {}) @db = SQLite3::Database.new(path) @table_name = [:table_name] || "fts_lite" create_table!() set_db_param() @tokenizer = Tokenizer.create([:tokenizer] || DEFAULT_TOKENIZER) end |
Class Method Details
.have_ft4_replace ⇒ Object
11 12 13 |
# File 'lib/fts_lite/index.rb', line 11 def self.have_ft4_replace SQLITE_HAVE_FT4_REPLACE end |
.open(path, options = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/fts_lite/index.rb', line 27 def self.open(path, = {}) if (block_given?) index = Index.new(path, ) begin yield(index) ensure index.close end else Index.new(path, ) end end |
.sqlite3_version ⇒ Object
14 15 16 |
# File 'lib/fts_lite/index.rb', line 14 def self.sqlite3_version SQLite3.libversion end |
Instance Method Details
#close ⇒ Object
40 41 42 |
# File 'lib/fts_lite/index.rb', line 40 def close @db.close end |
#count ⇒ Object
95 96 97 |
# File 'lib/fts_lite/index.rb', line 95 def count @db.execute("SELECT COUNT(*) FROM #{@table_name} ;").first.first end |
#delete(docid) ⇒ Object
69 70 71 |
# File 'lib/fts_lite/index.rb', line 69 def delete(docid) @db.execute("DELETE FROM #{@table_name} WHERE docid = ?;", [docid]) end |
#delete_all ⇒ Object
98 99 100 |
# File 'lib/fts_lite/index.rb', line 98 def delete_all @db.execute("DELETE FROM #{@table_name} ;") end |
#drop_table! ⇒ Object
101 102 103 104 105 |
# File 'lib/fts_lite/index.rb', line 101 def drop_table! if (table_exist?) @db.execute("DROP TABLE #{@table_name};") end end |
#search(text, options = {}) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/fts_lite/index.rb', line 72 def search(text, = {}) limit = [:limit] order = nil if ([:order]) case [:order].to_sym when :desc order = :desc when :asc order = :asc end end sql = "SELECT docid FROM #{@table_name} WHERE text MATCH ?" if (order) sql += sprintf(" ORDER BY sort_value %s", order == :desc ? "DESC" : "ASC") else sql += sprintf(" ORDER BY docid ASC") end if (limit) sql += sprintf(" LIMIT %d", limit) end sql += ";" @db.execute(sql, [@tokenizer.vector(text)]).flatten end |
#set(docid, text, sort_value = nil) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/fts_lite/index.rb', line 51 def set(docid, text, sort_value = nil) if (SQLITE_HAVE_FT4_REPLACE) @db.execute("INSERT OR REPLACE INTO #{@table_name} (docid, text, sort_value) VALUES(?, ?, ?);", [docid, @tokenizer.vector(text), sort_value]) else begin @db.execute("INSERT INTO #{@table_name} (docid, text, sort_value) VALUES(?, ?, ?);", [docid, @tokenizer.vector(text), sort_value]) rescue SQLite3::ConstraintException @db.execute("UPDATE #{@table_name} SET text = ?, sort_value = ? WHERE docid = ?;", [@tokenizer.vector(text), sort_value, docid]) end end end |
#tokenize(text) ⇒ Object
43 44 45 |
# File 'lib/fts_lite/index.rb', line 43 def tokenize(text) @tokenizer.vector(text).split(" ") end |
#transaction(&block) ⇒ Object
46 47 48 49 50 |
# File 'lib/fts_lite/index.rb', line 46 def transaction(&block) @db.transaction do block.call end end |
#update_sort_value(docid, sort_value) ⇒ Object
65 66 67 68 |
# File 'lib/fts_lite/index.rb', line 65 def update_sort_value(docid, sort_value) @db.execute("UPDATE #{@table_name} SET sort_value = ? WHERE docid = ?;", [sort_value, docid]) end |