Skip to content

Commit 85c455e

Browse files
committed
More standard fixes
1 parent 1438e1b commit 85c455e

17 files changed

+546
-614
lines changed

lib/litestack/litedb.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
require_relative "litemetric"
66

77
# litedb in particular gets access to litesearch
8-
require_relative 'litesearch'
8+
require_relative "litesearch"
99

1010
# Litedb inherits from the SQLite3::Database class and adds a few initialization options
1111
class Litedb < ::SQLite3::Database
1212
# add litemetric support
1313
include Litemetric::Measurable
14-
14+
1515
# add litesearch support
1616
include Litesearch
17-
17+
1818
# overrride the original initilaizer to allow for connection configuration
1919
def initialize(file, options = {}, zfs = nil)
2020
if block_given?

lib/litestack/litesearch.rb

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module Litesearch
22
class Index; end
3+
34
class Schema; end
45
end
56

6-
require_relative './litesearch/index'
7-
require_relative './litesearch/model'
7+
require_relative "./litesearch/index"
8+
require_relative "./litesearch/model"
89

910
module Litesearch
10-
1111
def litesearch_index_cache
1212
@litesearch_index_cache ||= {}
1313
end
@@ -21,17 +21,14 @@ def search_index(name)
2121
return index if index && !block_given?
2222
# if either there is no index in the cache or a block is given
2323
# create a new index instance and then place it in the cache and return
24-
if block_given?
25-
index = Index.new(self, name) do |schema|
24+
index = if block_given?
25+
Index.new(self, name) do |schema|
2626
yield schema
2727
schema.name(name)
2828
end
2929
else
30-
index = Index.new(self, name)
30+
Index.new(self, name)
3131
end
3232
litesearch_index_cache[name] = index
3333
end
34-
3534
end
36-
37-

lib/litestack/litesearch/index.rb

+51-52
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
require 'oj'
2-
require_relative './schema.rb'
1+
require "oj"
2+
require_relative "./schema"
33

44
class Litesearch::Index
5-
65
DEFAULT_SEARCH_OPTIONS = {limit: 25, offset: 0}
76

87
def initialize(db, name)
@@ -17,7 +16,7 @@ def initialize(db, name)
1716
# if they differ in tokenizer then rebuild if auto-rebuild is on (error otherwise)
1817
# if they differ in both then update the structure and rebuild if auto-rebuild is on (error otherwise)
1918
load_index(name) if exists?(name)
20-
19+
2120
if block_given?
2221
schema = Litesearch::Schema.new
2322
schema.schema[:name] = name
@@ -33,54 +32,56 @@ def initialize(db, name)
3332
end
3433
prepare_statements
3534
end
35+
elsif exists?(name)
36+
load_index(name)
37+
prepare_statements
38+
# an index already exists, load it from the database and return the index instance to the caller
3639
else
37-
if exists?(name)
38-
# an index already exists, load it from the database and return the index instance to the caller
39-
load_index(name)
40-
prepare_statements
41-
else
42-
raise "index does not exist and no schema was supplied"
43-
end
40+
raise "index does not exist and no schema was supplied"
4441
end
4542
end
46-
43+
4744
def load_index(name)
4845
# we cannot use get_config_value here since the schema object is not created yet, should we allow something here?
49-
@schema = Litesearch::Schema.new(Oj.load(@db.get_first_value("SELECT v from #{name}_config where k = ?", :litesearch_schema.to_s))) rescue nil
46+
@schema = begin
47+
Litesearch::Schema.new(Oj.load(@db.get_first_value("SELECT v from #{name}_config where k = ?", :litesearch_schema.to_s)))
48+
rescue
49+
nil
50+
end
5051
raise "index configuration not found, either corrupted or not a litesearch index!" if @schema.nil?
5152
self
5253
end
53-
54+
5455
def modify
5556
schema = Litesearch::Schema.new
5657
yield schema
5758
schema.schema[:name] = @schema.schema[:name]
5859
do_modify(schema)
5960
end
60-
61+
6162
def rebuild!
6263
@db.transaction(:immediate) do
6364
do_rebuild
6465
end
6566
end
66-
67+
6768
def add(document)
6869
@stmts[:insert].execute!(document)
69-
return @db.last_insert_row_id
70+
@db.last_insert_row_id
7071
end
71-
72+
7273
def remove(id)
7374
@stmts[:delete].execute!(id)
7475
end
75-
76+
7677
def count(term = nil)
7778
if term
7879
@stmts[:count].execute!(term)[0][0]
7980
else
80-
@stmts[:count_all].execute!()[0][0]
81+
@stmts[:count_all].execute![0][0]
8182
end
8283
end
83-
84+
8485
# search options include
8586
# limit: how many records to return
8687
# offset: start from which record
@@ -90,31 +91,30 @@ def search(term, options = {})
9091
rs = @stmts[:search].execute(term, options[:limit], options[:offset])
9192
if @db.results_as_hash
9293
rs.each_hash do |hash|
93-
result << hash
94+
result << hash
9495
end
9596
else
9697
result = rs.to_a
97-
end
98+
end
9899
result
99100
end
100-
101+
101102
def clear!
102103
@stmts[:delete_all].execute!(id)
103104
end
104-
105+
105106
def drop!
106107
if @schema.get(:type) == :backed
107108
@db.execute_batch(@schema.sql_for(:drop_primary_triggers))
108-
if secondary_triggers_sql = @schema.sql_for(:create_secondary_triggers)
109+
if @schema.sql_for(:create_secondary_triggers)
109110
@db.execute_batch(@schema.sql_for(:drop_secondary_triggers))
110111
end
111112
end
112113
@db.execute(@schema.sql_for(:drop))
113114
end
114-
115-
115+
116116
private
117-
117+
118118
def exists?(name)
119119
@db.get_first_value("SELECT count(*) FROM SQLITE_MASTER WHERE name = ? AND type = 'table' AND (sql like '%fts5%' OR sql like '%FTS5%')", name.to_s) == 1
120120
end
@@ -123,11 +123,11 @@ def prepare_statements
123123
stmt_names = [:insert, :delete, :delete_all, :drop, :count, :count_all, :search]
124124
stmt_names.each do |stmt_name|
125125
@stmts[stmt_name] = @db.prepare(@schema.sql_for(stmt_name))
126-
end
126+
end
127127
end
128-
129-
def do_create(schema)
130-
@schema = schema
128+
129+
def do_create(schema)
130+
@schema = schema
131131
@schema.clean
132132
# create index
133133
@db.execute(schema.sql_for(:create_index, true))
@@ -136,7 +136,7 @@ def do_create(schema)
136136
# create triggers (if any)
137137
if @schema.get(:type) == :backed
138138
@db.execute_batch(@schema.sql_for(:create_primary_triggers))
139-
if secondary_triggers_sql = @schema.sql_for(:create_secondary_triggers)
139+
if (secondary_triggers_sql = @schema.sql_for(:create_secondary_triggers))
140140
@db.execute_batch(secondary_triggers_sql)
141141
end
142142
@db.execute(@schema.sql_for(:rebuild)) if @schema.get(:rebuild_on_create)
@@ -152,11 +152,11 @@ def do_modify(new_schema)
152152
requires_schema_change = false
153153
requires_trigger_change = false
154154
requires_rebuild = false
155-
if changes[:fields] || changes[:table] || changes[:tokenizer] || changes[:filter_column] || changes[:removed_fields_count] > 0# any change here will require a schema change
155+
if changes[:fields] || changes[:table] || changes[:tokenizer] || changes[:filter_column] || changes[:removed_fields_count] > 0 # any change here will require a schema change
156156
requires_schema_change = true
157157
# only a change in tokenizer
158158
requires_rebuild = changes[:tokenizer] || new_schema.get(:rebuild_on_modify)
159-
requires_trigger_change = (changes[:table] || changes[:fields] || changes[:filter_column]) && @schema.get(:type) == :backed
159+
requires_trigger_change = (changes[:table] || changes[:fields] || changes[:filter_column]) && @schema.get(:type) == :backed
160160
end
161161
if requires_schema_change
162162
# 1. enable schema editing
@@ -169,12 +169,12 @@ def do_modify(new_schema)
169169
@db.execute(new_schema.sql_for(:expand_data), changes[:extra_fields_count])
170170
@db.execute(new_schema.sql_for(:expand_docsize), changes[:extra_fields_count])
171171
@db.execute("PRAGMA WRITABLE_SCHEMA = RESET")
172-
# need to reprepare statements
172+
# need to reprepare statements
173173
end
174174
if requires_trigger_change
175175
@db.execute_batch(new_schema.sql_for(:drop_primary_triggers))
176176
@db.execute_batch(new_schema.sql_for(:create_primary_triggers))
177-
if secondary_triggers_sql = new_schema.sql_for(:create_secondary_triggers)
177+
if (secondary_triggers_sql = new_schema.sql_for(:create_secondary_triggers))
178178
@db.execute_batch(new_schema.sql_for(:drop_secondary_triggers))
179179
@db.execute_batch(secondary_triggers_sql)
180180
end
@@ -183,48 +183,47 @@ def do_modify(new_schema)
183183
@schema = new_schema
184184
set_config_value(:litesearch_schema, @schema.schema)
185185
prepare_statements
186-
#save_schema
186+
# save_schema
187187
end
188188
do_rebuild if requires_rebuild
189-
# update the weights if they changed
189+
# update the weights if they changed
190190
@db.execute(@schema.sql_for(:ranks)) if changes[:weights]
191191
end
192192

193193
def do_rebuild
194194
# remove any zero weight columns
195195
if @schema.get(:type) == :backed
196196
@db.execute_batch(@schema.sql_for(:drop_primary_triggers))
197-
if secondary_triggers_sql = @schema.sql_for(:create_secondary_triggers)
197+
if (secondary_triggers_sql = @schema.sql_for(:create_secondary_triggers))
198198
@db.execute_batch(@schema.sql_for(:drop_secondary_triggers))
199199
end
200200
@db.execute(@schema.sql_for(:drop))
201201
@db.execute(@schema.sql_for(:create_index, true))
202202
@db.execute_batch(@schema.sql_for(:create_primary_triggers))
203203
@db.execute_batch(secondary_triggers_sql) if secondary_triggers_sql
204-
@db.execute(@schema.sql_for(:rebuild))
204+
@db.execute(@schema.sql_for(:rebuild))
205205
elsif @schema.get(:type) == :standalone
206206
removables = []
207-
@schema.get(:fields).each_with_index{|f, i| removables << [f[0], i] if f[1][:weight] == 0 }
207+
@schema.get(:fields).each_with_index { |f, i| removables << [f[0], i] if f[1][:weight] == 0 }
208208
removables.each do |col|
209209
@db.execute(@schema.sql_for(:drop_content_col, col[1]))
210-
@schema.get(:fields).delete(col[0])
210+
@schema.get(:fields).delete(col[0])
211211
end
212212
@db.execute("PRAGMA WRITABLE_SCHEMA = TRUE")
213213
@db.execute(@schema.sql_for(:update_index), @schema.sql_for(:create_index, true))
214-
@db.execute(@schema.sql_for(:update_content_table), @schema.sql_for(:create_content_table, @schema.schema[:fields].count))
214+
@db.execute(@schema.sql_for(:update_content_table), @schema.sql_for(:create_content_table, @schema.schema[:fields].count))
215215
@db.execute("PRAGMA WRITABLE_SCHEMA = RESET")
216-
@db.execute(@schema.sql_for(:rebuild))
216+
@db.execute(@schema.sql_for(:rebuild))
217217
end
218218
set_config_value(:litesearch_schema, @schema.schema)
219-
@db.execute(@schema.sql_for(:ranks, true))
220-
end
221-
219+
@db.execute(@schema.sql_for(:ranks, true))
220+
end
221+
222222
def get_config_value(key)
223-
Oj.load(@db.get_first_value(@schema.sql_for(:get_config_value), key.to_s)) #rescue nil
223+
Oj.load(@db.get_first_value(@schema.sql_for(:get_config_value), key.to_s)) # rescue nil
224224
end
225-
225+
226226
def set_config_value(key, value)
227227
@db.execute(@schema.sql_for(:set_config_value), key.to_s, Oj.dump(value))
228228
end
229-
230229
end

0 commit comments

Comments
 (0)