litestack 0.3.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,37 @@
1
+ module Litesearch
2
+ class Index; end
3
+ class Schema; end
4
+ end
5
+
6
+ require_relative './litesearch/index'
7
+ require_relative './litesearch/model'
8
+
9
+ module Litesearch
10
+
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
+ if block_given?
25
+ index = Index.new(self, name) do |schema|
26
+ yield schema
27
+ schema.name(name)
28
+ end
29
+ else
30
+ index = Index.new(self, name)
31
+ end
32
+ litesearch_index_cache[name] = index
33
+ end
34
+
35
+ end
36
+
37
+
@@ -7,14 +7,11 @@ require 'yaml'
7
7
  require 'pathname'
8
8
  require 'fileutils'
9
9
 
10
+ require_relative "./litescheduler"
11
+
10
12
  module Litesupport
11
13
 
12
14
  class Error < StandardError; end
13
-
14
- def self.max_contexts
15
- return 50 if scheduler == :fiber || scheduler == :polyphony
16
- 5
17
- end
18
15
 
19
16
  # Detect the Rack or Rails environment.
20
17
  def self.detect_environment
@@ -32,89 +29,11 @@ module Litesupport
32
29
  def self.environment
33
30
  @environment ||= detect_environment
34
31
  end
35
-
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
32
 
114
33
  # common db object options
115
34
  def self.create_db(path)
116
35
  db = SQLite3::Database.new(path)
117
- db.busy_handler{ switch || sleep(0.0001) }
36
+ db.busy_handler{ Litescheduler.switch || sleep(0.0001) }
118
37
  db.journal_mode = "WAL"
119
38
  db.instance_variable_set(:@stmts, {})
120
39
  class << db
@@ -153,7 +72,7 @@ module Litesupport
153
72
  end
154
73
 
155
74
  def synchronize(&block)
156
- if Litesupport.scheduler == :threaded || Litesupport.scheduler == :iodine
75
+ if Litescheduler.backend == :threaded || Litescheduler.backend == :iodine
157
76
  @mutex.synchronize{ block.call }
158
77
  else
159
78
  block.call
@@ -167,37 +86,27 @@ module Litesupport
167
86
  def initialize(count, &block)
168
87
  @count = count
169
88
  @block = block
170
- @resources = []
89
+ @resources = Thread::Queue.new
171
90
  @mutex = Litesupport::Mutex.new
172
91
  @count.times do
173
92
  resource = @mutex.synchronize{ block.call }
174
- @resources << [resource, :free]
93
+ @resources << resource
175
94
  end
176
95
  end
177
96
 
178
97
  def acquire
179
- # check for pid changes
180
- acquired = false
181
98
  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
99
+ resource = @resources.pop
100
+ begin
101
+ result = yield resource
102
+ rescue Exception => e
103
+ raise e
104
+ ensure
105
+ @resources << resource
197
106
  end
198
107
  result
199
108
  end
200
-
109
+
201
110
  end
202
111
 
203
112
  module ForkListener
@@ -215,7 +124,7 @@ module Litesupport
215
124
  def _fork(*args)
216
125
  ppid = Process.pid
217
126
  result = super
218
- if Process.pid != ppid && [:threaded, :iodine].include?(Litesupport.scheduler)
127
+ if Process.pid != ppid && [:threaded, :iodine].include?(Litescheduler.backend)
219
128
  ForkListener.listeners.each{|l| l.call }
220
129
  end
221
130
  result
@@ -331,7 +240,7 @@ module Litesupport
331
240
  # common db object options
332
241
  def create_connection(path_to_sql_file = nil)
333
242
  conn = SQLite3::Database.new(@options[:path])
334
- conn.busy_handler{ Litesupport.switch || sleep(rand * 0.002) }
243
+ conn.busy_handler{ Litescheduler.switch || sleep(rand * 0.002) }
335
244
  conn.journal_mode = "WAL"
336
245
  conn.synchronous = @options[:sync] || 1
337
246
  conn.mmap_size = @options[:mmap_size] || 0
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Litestack
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.1"
5
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"
@@ -1,10 +1,10 @@
1
1
  require_relative '../../litestack/litedb'
2
2
  require 'sequel'
3
3
  require 'sequel/adapters/sqlite'
4
- #require 'shared/litedb'
5
4
 
6
5
  module Sequel
7
6
  module Litedb
7
+
8
8
  include SQLite
9
9
 
10
10
  LITEDB_TYPES = SQLITE_TYPES
@@ -15,7 +15,7 @@ module Sequel
15
15
 
16
16
  def connect(server)
17
17
 
18
- Sequel.extension :fiber_concurrency if [:fiber, :polyphony].include? Litesupport.scheduler
18
+ Sequel.extension :fiber_concurrency if [:fiber, :polyphony].include? Litescheduler.backend
19
19
 
20
20
  opts = server_opts(server)
21
21
  opts[:database] = ':memory:' if blank_object?(opts[:database])
@@ -36,6 +36,7 @@ module Sequel
36
36
  end
37
37
 
38
38
  db.instance_variable_set(:@prepared_statements, {})
39
+ @raw_db = db
39
40
  db
40
41
  end
41
42
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: litestack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mohamed Hassan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-13 00:00:00.000000000 Z
11
+ date: 2023-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sqlite3
@@ -159,6 +159,16 @@ files:
159
159
  - lib/litestack/litemetric_collector.sql.yml
160
160
  - lib/litestack/litequeue.rb
161
161
  - lib/litestack/litequeue.sql.yml
162
+ - lib/litestack/litescheduler.rb
163
+ - lib/litestack/litesearch.rb
164
+ - lib/litestack/litesearch/index.rb
165
+ - lib/litestack/litesearch/model.rb
166
+ - lib/litestack/litesearch/schema.rb
167
+ - lib/litestack/litesearch/schema_adapters.rb
168
+ - lib/litestack/litesearch/schema_adapters/backed_adapter.rb
169
+ - lib/litestack/litesearch/schema_adapters/basic_adapter.rb
170
+ - lib/litestack/litesearch/schema_adapters/contentless_adapter.rb
171
+ - lib/litestack/litesearch/schema_adapters/standalone_adapter.rb
162
172
  - lib/litestack/litesupport.rb
163
173
  - lib/litestack/railtie.rb
164
174
  - lib/litestack/version.rb
@@ -191,7 +201,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
191
201
  - !ruby/object:Gem::Version
192
202
  version: '0'
193
203
  requirements: []
194
- rubygems_version: 3.4.17
204
+ rubygems_version: 3.4.20
195
205
  signing_key:
196
206
  specification_version: 4
197
207
  summary: A SQLite based, lightning fast, super efficient and dead simple to setup