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
- DEFAULT_TIMEOUT =
10000
- 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.
21 22 23 24 25 26 27 |
# File 'lib/fts_lite/index.rb', line 21 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
12 13 14 |
# File 'lib/fts_lite/index.rb', line 12 def self.have_ft4_replace SQLITE_HAVE_FT4_REPLACE end |
.open(path, options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/fts_lite/index.rb', line 28 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
15 16 17 |
# File 'lib/fts_lite/index.rb', line 15 def self.sqlite3_version SQLite3.libversion end |
Instance Method Details
#close ⇒ Object
41 42 43 |
# File 'lib/fts_lite/index.rb', line 41 def close @db.close end |
#count ⇒ Object
96 97 98 |
# File 'lib/fts_lite/index.rb', line 96 def count @db.execute("SELECT COUNT(*) FROM #{@table_name} ;").first.first end |
#delete(docid) ⇒ Object
70 71 72 |
# File 'lib/fts_lite/index.rb', line 70 def delete(docid) @db.execute("DELETE FROM #{@table_name} WHERE docid = ?;", [docid]) end |
#delete_all ⇒ Object
99 100 101 |
# File 'lib/fts_lite/index.rb', line 99 def delete_all @db.execute("DELETE FROM #{@table_name} ;") end |
#drop_table! ⇒ Object
102 103 104 105 106 |
# File 'lib/fts_lite/index.rb', line 102 def drop_table! if (table_exist?) @db.execute("DROP TABLE #{@table_name};") end end |
#search(text, options = {}) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/fts_lite/index.rb', line 73 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.query(text, )]).flatten end |
#set(docid, text, sort_value = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/fts_lite/index.rb', line 52 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
44 45 46 |
# File 'lib/fts_lite/index.rb', line 44 def tokenize(text) @tokenizer.vector(text).split(" ") end |
#transaction(&block) ⇒ Object
47 48 49 50 51 |
# File 'lib/fts_lite/index.rb', line 47 def transaction(&block) @db.transaction do block.call end end |
#update_sort_value(docid, sort_value) ⇒ Object
66 67 68 69 |
# File 'lib/fts_lite/index.rb', line 66 def update_sort_value(docid, sort_value) @db.execute("UPDATE #{@table_name} SET sort_value = ? WHERE docid = ?;", [sort_value, docid]) end |