litestack 0.3.0 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.standard.yml +3 -0
  3. data/BENCHMARKS.md +34 -7
  4. data/CHANGELOG.md +21 -0
  5. data/Gemfile +1 -5
  6. data/Gemfile.lock +92 -0
  7. data/README.md +120 -6
  8. data/ROADMAP.md +45 -0
  9. data/Rakefile +3 -1
  10. data/WHYLITESTACK.md +1 -1
  11. data/assets/litecache_metrics.png +0 -0
  12. data/assets/litedb_metrics.png +0 -0
  13. data/assets/litemetric_logo_teal.png +0 -0
  14. data/assets/litesearch_logo_teal.png +0 -0
  15. data/bench/bench.rb +17 -10
  16. data/bench/bench_cache_rails.rb +10 -13
  17. data/bench/bench_cache_raw.rb +17 -22
  18. data/bench/bench_jobs_rails.rb +19 -13
  19. data/bench/bench_jobs_raw.rb +17 -10
  20. data/bench/bench_queue.rb +4 -6
  21. data/bench/rails_job.rb +5 -7
  22. data/bench/skjob.rb +4 -4
  23. data/bench/uljob.rb +6 -6
  24. data/lib/action_cable/subscription_adapter/litecable.rb +5 -8
  25. data/lib/active_job/queue_adapters/litejob_adapter.rb +6 -8
  26. data/lib/active_record/connection_adapters/litedb_adapter.rb +65 -75
  27. data/lib/active_support/cache/litecache.rb +38 -41
  28. data/lib/generators/litestack/install/install_generator.rb +3 -3
  29. data/lib/generators/litestack/install/templates/database.yml +7 -1
  30. data/lib/litestack/liteboard/liteboard.rb +269 -149
  31. data/lib/litestack/litecable.rb +44 -40
  32. data/lib/litestack/litecable.sql.yml +22 -11
  33. data/lib/litestack/litecache.rb +80 -89
  34. data/lib/litestack/litecache.sql.yml +81 -22
  35. data/lib/litestack/litecache.yml +1 -1
  36. data/lib/litestack/litedb.rb +39 -38
  37. data/lib/litestack/litejob.rb +31 -31
  38. data/lib/litestack/litejobqueue.rb +107 -106
  39. data/lib/litestack/litemetric.rb +83 -95
  40. data/lib/litestack/litemetric.sql.yml +244 -234
  41. data/lib/litestack/litemetric_collector.sql.yml +38 -41
  42. data/lib/litestack/litequeue.rb +39 -41
  43. data/lib/litestack/litequeue.sql.yml +39 -31
  44. data/lib/litestack/litescheduler.rb +84 -0
  45. data/lib/litestack/litesearch/index.rb +260 -0
  46. data/lib/litestack/litesearch/model.rb +179 -0
  47. data/lib/litestack/litesearch/schema.rb +190 -0
  48. data/lib/litestack/litesearch/schema_adapters/backed_adapter.rb +143 -0
  49. data/lib/litestack/litesearch/schema_adapters/basic_adapter.rb +137 -0
  50. data/lib/litestack/litesearch/schema_adapters/contentless_adapter.rb +14 -0
  51. data/lib/litestack/litesearch/schema_adapters/standalone_adapter.rb +31 -0
  52. data/lib/litestack/litesearch/schema_adapters.rb +4 -0
  53. data/lib/litestack/litesearch.rb +34 -0
  54. data/lib/litestack/litesupport.rb +85 -186
  55. data/lib/litestack/railtie.rb +1 -1
  56. data/lib/litestack/version.rb +2 -2
  57. data/lib/litestack.rb +7 -4
  58. data/lib/railties/rails/commands/dbconsole.rb +11 -15
  59. data/lib/sequel/adapters/litedb.rb +18 -22
  60. data/lib/sequel/adapters/shared/litedb.rb +168 -168
  61. data/scripts/build_metrics.rb +91 -0
  62. data/scripts/test_cable.rb +30 -0
  63. data/scripts/test_job_retry.rb +33 -0
  64. data/scripts/test_metrics.rb +60 -0
  65. data/template.rb +2 -2
  66. metadata +112 -7
@@ -0,0 +1,34 @@
1
+ module Litesearch
2
+ class Index; end
3
+
4
+ class Schema; end
5
+ end
6
+
7
+ require_relative "./litesearch/index"
8
+ require_relative "./litesearch/model"
9
+
10
+ module Litesearch
11
+ def litesearch_index_cache
12
+ @litesearch_index_cache ||= {}
13
+ end
14
+
15
+ def search_index(name)
16
+ # normalize the index name
17
+ # find the index in the db cache
18
+ name = name.to_s.downcase.to_sym
19
+ index = litesearch_index_cache[name]
20
+ # if the index is in the cache and no block is given then return it
21
+ return index if index && !block_given?
22
+ # if either there is no index in the cache or a block is given
23
+ # create a new index instance and then place it in the cache and return
24
+ index = if block_given?
25
+ Index.new(self, name) do |schema|
26
+ yield schema
27
+ schema.name(name)
28
+ end
29
+ else
30
+ Index.new(self, name)
31
+ end
32
+ litesearch_index_cache[name] = index
33
+ end
34
+ end
@@ -1,20 +1,16 @@
1
1
  # frozen_stringe_literal: true
2
2
 
3
- require 'sqlite3'
4
- require 'logger'
5
- require 'oj'
6
- require 'yaml'
7
- require 'pathname'
8
- require 'fileutils'
3
+ require "sqlite3"
4
+ require "logger"
5
+ require "oj"
6
+ require "yaml"
7
+ require "pathname"
8
+ require "fileutils"
9
9
 
10
- module Litesupport
10
+ require_relative "./litescheduler"
11
11
 
12
+ module Litesupport
12
13
  class Error < StandardError; end
13
-
14
- def self.max_contexts
15
- return 50 if scheduler == :fiber || scheduler == :polyphony
16
- 5
17
- end
18
14
 
19
15
  # Detect the Rack or Rails environment.
20
16
  def self.detect_environment
@@ -33,88 +29,10 @@ module Litesupport
33
29
  @environment ||= detect_environment
34
30
  end
35
31
 
36
- # cache the scheduler we are running in
37
- # it is an error to change the scheduler for a process
38
- # or for a child forked from that process
39
- def self.scheduler
40
- @scehduler ||= detect_scheduler
41
- end
42
-
43
- # identify which scheduler we are running in
44
- # we currently support :fiber, :polyphony, :iodine & :threaded
45
- # in the future we might want to expand to other schedulers
46
- def self.detect_scheduler
47
- return :fiber if Fiber.scheduler
48
- return :polyphony if defined? Polyphony
49
- return :iodine if defined? Iodine
50
- return :threaded # fall back for all other schedulers
51
- end
52
-
53
- # spawn a new execution context
54
- def self.spawn(&block)
55
- if self.scheduler == :fiber
56
- Fiber.schedule(&block)
57
- elsif self.scheduler == :polyphony
58
- spin(&block)
59
- elsif self.scheduler == :threaded or self.scheduler == :iodine
60
- Thread.new(&block)
61
- end
62
- # we should never reach here
63
- end
64
-
65
- def self.context
66
- if scheduler == :fiber || scheduler == :poylphony
67
- Fiber.current.storage
68
- else
69
- Thread.current
70
- end
71
- end
72
-
73
- def self.current_context
74
- if scheduler == :fiber || scheduler == :poylphony
75
- Fiber.current
76
- else
77
- Thread.current
78
- end
79
- end
80
-
81
- # switch the execution context to allow others to run
82
- def self.switch
83
- if self.scheduler == :fiber
84
- Fiber.scheduler.yield
85
- true
86
- elsif self.scheduler == :polyphony
87
- Fiber.current.schedule
88
- Thread.current.switch_fiber
89
- true
90
- else
91
- #Thread.pass
92
- false
93
- end
94
- end
95
-
96
- # mutex initialization
97
- def self.mutex
98
- # a single mutex per process (is that ok?)
99
- @@mutex ||= Mutex.new
100
- end
101
-
102
- # bold assumption, we will only synchronize threaded code!
103
- # If some code explicitly wants to synchronize a fiber
104
- # they must send (true) as a parameter to this method
105
- # else it is a no-op for fibers
106
- def self.synchronize(fiber_sync = false, &block)
107
- if self.scheduler == :fiber or self.scheduler == :polyphony
108
- yield # do nothing, just run the block as is
109
- else
110
- self.mutex.synchronize(&block)
111
- end
112
- end
113
-
114
32
  # common db object options
115
33
  def self.create_db(path)
116
34
  db = SQLite3::Database.new(path)
117
- db.busy_handler{ switch || sleep(0.0001) }
35
+ db.busy_handler { Litescheduler.switch || sleep(0.0001) }
118
36
  db.journal_mode = "WAL"
119
37
  db.instance_variable_set(:@stmts, {})
120
38
  class << db
@@ -124,12 +42,12 @@ module Litesupport
124
42
  end
125
43
 
126
44
  # Databases will be stored by default at this path.
127
- def self.root
128
- @root ||= ensure_root_volume detect_root
45
+ def self.root(env = Litesupport.environment)
46
+ ensure_root_volume detect_root(env)
129
47
  end
130
48
 
131
49
  # Default path where we'll store all of the databases.
132
- def self.detect_root
50
+ def self.detect_root(env)
133
51
  path = if ENV["LITESTACK_DATA_PATH"]
134
52
  ENV["LITESTACK_DATA_PATH"]
135
53
  elsif defined? Rails
@@ -138,7 +56,7 @@ module Litesupport
138
56
  "."
139
57
  end
140
58
 
141
- Pathname.new(path).join(Litesupport.environment)
59
+ Pathname.new(path).join(env)
142
60
  end
143
61
 
144
62
  def self.ensure_root_volume(path)
@@ -147,125 +65,106 @@ module Litesupport
147
65
  end
148
66
 
149
67
  class Mutex
150
-
151
68
  def initialize
152
69
  @mutex = Thread::Mutex.new
153
70
  end
154
-
71
+
155
72
  def synchronize(&block)
156
- if Litesupport.scheduler == :threaded || Litesupport.scheduler == :iodine
157
- @mutex.synchronize{ block.call }
73
+ if Litescheduler.backend == :threaded || Litescheduler.backend == :iodine
74
+ @mutex.synchronize { block.call }
158
75
  else
159
76
  block.call
160
77
  end
161
78
  end
162
-
163
79
  end
164
-
80
+
165
81
  class Pool
166
-
167
82
  def initialize(count, &block)
168
83
  @count = count
169
84
  @block = block
170
- @resources = []
85
+ @resources = Thread::Queue.new
171
86
  @mutex = Litesupport::Mutex.new
172
87
  @count.times do
173
- resource = @mutex.synchronize{ block.call }
174
- @resources << [resource, :free]
88
+ resource = @mutex.synchronize { block.call }
89
+ @resources << resource
175
90
  end
176
91
  end
177
-
92
+
178
93
  def acquire
179
- # check for pid changes
180
- acquired = false
181
94
  result = nil
182
- while !acquired do
183
- @mutex.synchronize do
184
- if resource = @resources.find{|r| r[1] == :free }
185
- resource[1] = :busy
186
- begin
187
- result = yield resource[0]
188
- rescue Exception => e
189
- raise e
190
- ensure
191
- resource[1] = :free
192
- acquired = true
193
- end
194
- end
195
- end
196
- sleep 0.001 unless acquired
95
+ resource = @resources.pop
96
+ begin
97
+ result = yield resource
98
+ ensure
99
+ @resources << resource
197
100
  end
198
101
  result
199
102
  end
200
-
201
103
  end
202
-
104
+
203
105
  module ForkListener
204
106
  def self.listeners
205
107
  @listeners ||= []
206
108
  end
207
-
109
+
208
110
  def self.listen(&block)
209
111
  listeners << block
210
112
  end
211
113
  end
212
114
 
213
115
  module Forkable
214
-
215
116
  def _fork(*args)
216
117
  ppid = Process.pid
217
118
  result = super
218
- if Process.pid != ppid && [:threaded, :iodine].include?(Litesupport.scheduler)
219
- ForkListener.listeners.each{|l| l.call }
119
+ if Process.pid != ppid && [:threaded, :iodine].include?(Litescheduler.backend)
120
+ ForkListener.listeners.each { |l| l.call }
220
121
  end
221
122
  result
222
123
  end
223
-
224
124
  end
225
-
125
+
226
126
  module Liteconnection
227
-
228
127
  include Forkable
229
128
 
230
129
  # close, setup, run_stmt and run_sql assume a single connection was created
231
-
130
+
232
131
  def options
233
132
  @options
234
133
  end
235
-
134
+
236
135
  def close
237
136
  @running = false
238
- @conn.acquire do |q|
239
- q.stmts.each_pair {|k, v| q.stmts[k].close }
137
+ @conn.acquire do |q|
138
+ q.stmts.each_pair { |k, v| q.stmts[k].close }
240
139
  q.close
241
140
  end
242
141
  end
243
142
 
244
143
  def size
245
- run_sql("SELECT size.page_size * count.page_count FROM pragma_page_size() AS size, pragma_page_count() AS count")[0][0].to_f / (1024*1024)
144
+ run_sql("SELECT size.page_size * count.page_count FROM pragma_page_size() AS size, pragma_page_count() AS count")[0][0].to_f / (1024 * 1024)
246
145
  end
247
146
 
248
147
  def journal_mode
249
148
  run_method(:journal_mode)
250
149
  end
251
-
150
+
252
151
  def synchronous
253
152
  run_method(:synchronous)
254
153
  end
255
-
154
+
256
155
  def path
257
156
  run_method(:filename)
258
157
  end
259
-
158
+
260
159
  private # all methods are private
261
-
160
+
262
161
  def init(options = {})
263
- #c configure the object, loading options from the appropriate location
264
- configure(options)
162
+ # c configure the object, loading options from the appropriate location
163
+ configure(options)
265
164
  # setup connections and background threads
266
- setup
165
+ setup
267
166
  # handle process exiting
268
- at_exit do
167
+ at_exit do
269
168
  exit_callback
270
169
  end
271
170
  # handle forking (restart connections and background threads)
@@ -276,9 +175,17 @@ module Litesupport
276
175
 
277
176
  def configure(options = {})
278
177
  # detect enviornment (production, development, etc.)
279
- defaults = self.class::DEFAULT_OPTIONS rescue {}
178
+ defaults = begin
179
+ self.class::DEFAULT_OPTIONS
180
+ rescue
181
+ {}
182
+ end
280
183
  @options = defaults.merge(options)
281
- config = YAML.load_file(@options[:config_path]) rescue {} # an empty hash won't hurt
184
+ config = begin
185
+ YAML.load_file(@options[:config_path])
186
+ rescue
187
+ {}
188
+ end # an empty hash won't hurt
282
189
  config = config[Litesupport.environment] if config[Litesupport.environment] # if there is a config for the current enviornment defined then use it, otherwise use the top level declaration
283
190
  config.keys.each do |k| # symbolize keys
284
191
  config[k.to_sym] = config[k]
@@ -287,51 +194,50 @@ module Litesupport
287
194
  @options.merge!(config)
288
195
  @options.merge!(options) # make sure options passed to initialize trump everything else
289
196
  end
290
-
197
+
291
198
  def setup
292
199
  @conn = create_pooled_connection
293
200
  @logger = create_logger
294
201
  @running = true
295
202
  end
296
-
203
+
297
204
  def create_logger
298
205
  @options[:logger] = nil unless @options[:logger]
299
206
  return @options[:logger] if @options[:logger].respond_to? :info
300
- return Logger.new(STDOUT) if @options[:logger] == 'STDOUT'
301
- return Logger.new(STDERR) if @options[:logger] == 'STDERR'
302
- return Logger.new(@options[:logger]) if @options[:logger].is_a? String
303
- return Logger.new(IO::NULL)
207
+ return Logger.new($stdout) if @options[:logger] == "STDOUT"
208
+ return Logger.new($stderr) if @options[:logger] == "STDERR"
209
+ return Logger.new(@options[:logger]) if @options[:logger].is_a? String
210
+ Logger.new(IO::NULL)
304
211
  end
305
-
212
+
306
213
  def exit_callback
307
214
  close
308
215
  end
309
-
216
+
310
217
  def run_stmt(stmt, *args)
311
- @conn.acquire{|q| q.stmts[stmt].execute!(*args) }
218
+ @conn.acquire { |q| q.stmts[stmt].execute!(*args) }
312
219
  end
313
220
 
314
221
  def run_sql(sql, *args)
315
- @conn.acquire{|q| q.execute(sql, *args) }
222
+ @conn.acquire { |q| q.execute(sql, args) }
316
223
  end
317
-
224
+
318
225
  def run_method(method, *args)
319
- @conn.acquire{|q| q.send(method, *args)}
226
+ @conn.acquire { |q| q.send(method, *args) }
320
227
  end
321
228
 
322
229
  def run_stmt_method(stmt, method, *args)
323
- @conn.acquire{|q| q.stmts[stmt].send(method, *args)}
230
+ @conn.acquire { |q| q.stmts[stmt].send(method, *args) }
324
231
  end
325
232
 
326
-
327
233
  def create_pooled_connection(count = 1)
328
- Litesupport::Pool.new(1){create_connection}
234
+ Litesupport::Pool.new(1) { create_connection }
329
235
  end
330
236
 
331
237
  # common db object options
332
238
  def create_connection(path_to_sql_file = nil)
333
239
  conn = SQLite3::Database.new(@options[:path])
334
- conn.busy_handler{ Litesupport.switch || sleep(rand * 0.002) }
240
+ conn.busy_handler { Litescheduler.switch || sleep(rand * 0.002) }
335
241
  conn.journal_mode = "WAL"
336
242
  conn.synchronous = @options[:sync] || 1
337
243
  conn.mmap_size = @options[:mmap_size] || 0
@@ -344,38 +250,31 @@ module Litesupport
344
250
  unless path_to_sql_file.nil?
345
251
  sql = YAML.load_file(path_to_sql_file)
346
252
  version = conn.get_first_value("PRAGMA user_version")
347
- sql["schema"].each_pair do |v, obj|
253
+ sql["schema"].each_pair do |v, obj|
348
254
  if v > version
349
- conn.transaction do
350
- obj.each do |k, s|
351
- begin
352
- conn.execute(s)
353
- rescue Exception => e
354
- STDERR.puts "Error parsing #{k}"
355
- STDERR.puts s
356
- raise e
357
- end
255
+ conn.transaction do
256
+ obj.each do |k, s|
257
+ conn.execute(s)
258
+ rescue Exception => e # standard:disable Lint/RescueException
259
+ warn "Error parsing #{k}"
260
+ warn s
261
+ raise e
358
262
  end
359
263
  conn.user_version = v
360
264
  end
361
265
  end
362
- end
363
- sql["stmts"].each do |k, v|
364
- begin
365
- conn.stmts[k.to_sym] = conn.prepare(v)
366
- rescue Exception => e
367
- STDERR.puts "Error parsing #{k}"
368
- STDERR.puts v
369
- raise e
370
- end
266
+ end
267
+ sql["stmts"].each do |k, v|
268
+ conn.stmts[k.to_sym] = conn.prepare(v)
269
+ rescue Exception => e # standard:disable Lint/RescueException
270
+ warn "Error parsing #{k}"
271
+ warn v
272
+ raise e
371
273
  end
372
274
  end
373
275
  conn
374
276
  end
375
-
376
277
  end
377
-
378
- end
278
+ end
379
279
 
380
280
  Process.singleton_class.prepend(Litesupport::Forkable)
381
-
@@ -7,4 +7,4 @@ module Litestack
7
7
  app.config.active_record.sqlite3_production_warning = false
8
8
  end
9
9
  end
10
- end
10
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Litestack
4
- VERSION = "0.3.0"
5
- end
4
+ VERSION = "0.4.2"
5
+ end
data/lib/litestack.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  # load core classes
4
4
  require_relative "./litestack/version"
5
+ require_relative "./litestack/litescheduler"
5
6
  require_relative "./litestack/litesupport"
6
7
  require_relative "./litestack/litemetric"
7
8
  require_relative "./litestack/litedb"
@@ -12,14 +13,16 @@ require_relative "./litestack/litecable"
12
13
  # conditionally load integration with other libraries
13
14
  require_relative "./sequel/adapters/litedb" if defined? Sequel
14
15
  require_relative "./active_record/connection_adapters/litedb_adapter" if defined? ActiveRecord
15
- require_relative "./railties/rails/commands/dbconsole" if defined? Rails && defined? ActiveRecord
16
+ require_relative "./railties/rails/commands/dbconsole" if defined?(Rails) && defined?(ActiveRecord)
16
17
  require_relative "./active_support/cache/litecache" if defined? ActiveSupport
17
18
  require_relative "./active_job/queue_adapters/litejob_adapter" if defined? ActiveJob
18
19
  require_relative "./action_cable/subscription_adapter/litecable" if defined? ActionCable
19
20
  require_relative "./litestack/railtie" if defined? Rails::Railtie
20
21
 
21
22
  module Litestack
22
- class NotImplementedError < Exception; end
23
- class TimeoutError < Exception; end
24
- class DeadlockError < Exception; end
23
+ class NotImplementedError < RuntimeError; end
24
+
25
+ class TimeoutError < RuntimeError; end
26
+
27
+ class DeadlockError < RuntimeError; end
25
28
  end
@@ -1,6 +1,5 @@
1
1
  module Rails
2
2
  class DBConsole
3
-
4
3
  def start
5
4
  ENV["RAILS_ENV"] ||= @options[:environment] || environment
6
5
  config = db_config.configuration_hash
@@ -31,14 +30,14 @@ module Rails
31
30
  find_cmd_and_exec(["mysql", "mysql5"], *args)
32
31
 
33
32
  when /^postgres|^postgis/
34
- ENV["PGUSER"] = config[:username] if config[:username]
35
- ENV["PGHOST"] = config[:host] if config[:host]
36
- ENV["PGPORT"] = config[:port].to_s if config[:port]
37
- ENV["PGPASSWORD"] = config[:password].to_s if config[:password] && @options[:include_password]
38
- ENV["PGSSLMODE"] = config[:sslmode].to_s if config[:sslmode]
39
- ENV["PGSSLCERT"] = config[:sslcert].to_s if config[:sslcert]
40
- ENV["PGSSLKEY"] = config[:sslkey].to_s if config[:sslkey]
41
- ENV["PGSSLROOTCERT"] = config[:sslrootcert].to_s if config[:sslrootcert]
33
+ ENV["PGUSER"] = config[:username] if config[:username]
34
+ ENV["PGHOST"] = config[:host] if config[:host]
35
+ ENV["PGPORT"] = config[:port].to_s if config[:port]
36
+ ENV["PGPASSWORD"] = config[:password].to_s if config[:password] && @options[:include_password]
37
+ ENV["PGSSLMODE"] = config[:sslmode].to_s if config[:sslmode]
38
+ ENV["PGSSLCERT"] = config[:sslcert].to_s if config[:sslcert]
39
+ ENV["PGSSLKEY"] = config[:sslkey].to_s if config[:sslkey]
40
+ ENV["PGSSLROOTCERT"] = config[:sslrootcert].to_s if config[:sslrootcert]
42
41
  find_cmd_and_exec("psql", db_config.database)
43
42
 
44
43
  when "sqlite3", "litedb"
@@ -50,7 +49,6 @@ module Rails
50
49
 
51
50
  find_cmd_and_exec("sqlite3", *args)
52
51
 
53
-
54
52
  when "oracle", "oracle_enhanced"
55
53
  logon = ""
56
54
 
@@ -65,9 +63,9 @@ module Rails
65
63
  when "sqlserver"
66
64
  args = []
67
65
 
68
- args += ["-d", "#{db_config.database}"] if db_config.database
69
- args += ["-U", "#{config[:username]}"] if config[:username]
70
- args += ["-P", "#{config[:password]}"] if config[:password]
66
+ args += ["-d", db_config.database.to_s] if db_config.database
67
+ args += ["-U", config[:username].to_s] if config[:username]
68
+ args += ["-P", config[:password].to_s] if config[:password]
71
69
 
72
70
  if config[:host]
73
71
  host_arg = +"tcp:#{config[:host]}"
@@ -81,7 +79,5 @@ module Rails
81
79
  abort "Unknown command-line client for #{db_config.database}."
82
80
  end
83
81
  end
84
-
85
-
86
82
  end
87
83
  end
@@ -1,48 +1,44 @@
1
- require_relative '../../litestack/litedb'
2
- require 'sequel'
3
- require 'sequel/adapters/sqlite'
4
- #require 'shared/litedb'
1
+ require_relative "../../litestack/litedb"
2
+ require "sequel"
3
+ require "sequel/adapters/sqlite"
5
4
 
6
5
  module Sequel
7
6
  module Litedb
8
- include SQLite
7
+ include SQLite
9
8
 
10
9
  LITEDB_TYPES = SQLITE_TYPES
11
-
10
+
12
11
  class Database < Sequel::SQLite::Database
13
-
14
12
  set_adapter_scheme :litedb
15
-
13
+
16
14
  def connect(server)
17
-
18
- Sequel.extension :fiber_concurrency if [:fiber, :polyphony].include? Litesupport.scheduler
19
-
15
+ Sequel.extension :fiber_concurrency if [:fiber, :polyphony].include? Litescheduler.backend
16
+
20
17
  opts = server_opts(server)
21
- opts[:database] = ':memory:' if blank_object?(opts[:database])
18
+ opts[:database] = ":memory:" if blank_object?(opts[:database])
22
19
  sqlite3_opts = {}
23
20
  sqlite3_opts[:readonly] = typecast_value_boolean(opts[:readonly]) if opts.has_key?(:readonly)
24
21
  db = ::Litedb.new(opts[:database].to_s, sqlite3_opts)
25
22
 
26
23
  self.transaction_mode = :immediate
27
-
28
- if sqlite_version >= 104
24
+
25
+ if sqlite_version >= 104
29
26
  db.extended_result_codes = true
30
27
  end
31
-
32
- connection_pragmas.each{|s| log_connection_yield(s, db){db.execute_batch(s)}}
33
-
28
+
29
+ connection_pragmas.each { |s| log_connection_yield(s, db) { db.execute_batch(s) } }
30
+
34
31
  class << db
35
32
  attr_reader :prepared_statements
36
33
  end
37
-
34
+
38
35
  db.instance_variable_set(:@prepared_statements, {})
36
+ @raw_db = db
39
37
  db
40
38
  end
41
-
42
39
  end
43
-
44
- class Dataset < Sequel::SQLite::Dataset
40
+
41
+ class Dataset < Sequel::SQLite::Dataset
45
42
  end
46
-
47
43
  end
48
44
  end