litedb 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 23fe5303539c84fa0323b9b1d4f875c9fee899f2c8cedf9abfe37ed18ee6d17e
4
- data.tar.gz: baad8c74278a8d2efc7781c7692789abeeef52b5e37917a844c4bef335fa621b
3
+ metadata.gz: '03940873fc6d049cf80790e80b04325ad29d0b51af711ad5dc4442df01396cb5'
4
+ data.tar.gz: ce364a86c99bc2ad9211163f606a850b9f943838579ec3d53225de7fb7fcd41d
5
5
  SHA512:
6
- metadata.gz: 5842f4d166945c9c8ef0e3a41c7c767378cb4b7386b4cfa98758e6796419d3e7b042698f02762ae3cc7a1df0feced037db75a7569d3cc48c6acdba9d747b5e6e
7
- data.tar.gz: be7703fecba593e2e5342ae59a5bd31d2ab9934c5d6678484dfb5c6b83f24fc9e4268938ba3d05eb8ca0bf6ba11f7f13dfd1a22427d882d630b68caa107e2bbf
6
+ metadata.gz: 2392eabd26b22e60b87559d984481c7b1d029df091a22994cbabedd42bc8fc5700423c840c749eee0e72d1b8a5c5658d91df262bfdd7124601014cff0cb128db
7
+ data.tar.gz: 104675e33444e702ffaba66e1908464800f5432ee00bbc717bb0514ad8f983b308ed9355650193f4ef790d5f36a6a7cc5bf5372ac6add652f85a4c95b0f00306
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.1] - 2023-08-11
4
+
5
+ - Move the `SQLite3::Database` wrapper to `Litedb::Connection` class. This both makes it easier to extend the gem to wrap other SQLite3 classes (like `Statement`) and also makes dealing with the VERSION reasonable (and possible). Keeping VERSION in a separate file proved impossible when having Litedb be a class that inherited from SQLite3::Database, because the gemspec needed the version before sqlite3 was required.
6
+
7
+ ## [0.2.0] - 2023-08-10
8
+
9
+ - Initial release of usable code
10
+
3
11
  ## [0.1.0] - 2023-08-09
4
12
 
5
- - Initial release
13
+ - Initial release to reserve gem name
data/Gemfile.lock CHANGED
@@ -1,15 +1,19 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- litedb (0.1.0)
4
+ litedb (0.2.1)
5
+ litescheduler (>= 0.2.0)
6
+ sqlite3 (>= 1.5.0)
5
7
 
6
8
  GEM
7
9
  remote: https://rubygems.org/
8
10
  specs:
9
11
  ast (2.4.2)
12
+ docile (1.4.0)
10
13
  json (2.6.3)
11
14
  language_server-protocol (3.17.0.3)
12
15
  lint_roller (1.1.0)
16
+ litescheduler (0.2.1)
13
17
  minitest (5.19.0)
14
18
  parallel (1.23.0)
15
19
  parser (3.2.2.3)
@@ -36,6 +40,14 @@ GEM
36
40
  rubocop (>= 1.7.0, < 2.0)
37
41
  rubocop-ast (>= 0.4.0)
38
42
  ruby-progressbar (1.13.0)
43
+ simplecov (0.22.0)
44
+ docile (~> 1.1)
45
+ simplecov-html (~> 0.11)
46
+ simplecov_json_formatter (~> 0.1)
47
+ simplecov-html (0.12.3)
48
+ simplecov_json_formatter (0.1.4)
49
+ sqlite3 (1.6.3-arm64-darwin)
50
+ sqlite3 (1.6.3-x86_64-linux)
39
51
  standard (1.30.1)
40
52
  language_server-protocol (~> 3.17.0.2)
41
53
  lint_roller (~> 1.0)
@@ -52,11 +64,13 @@ GEM
52
64
 
53
65
  PLATFORMS
54
66
  arm64-darwin-21
67
+ x86_64-linux
55
68
 
56
69
  DEPENDENCIES
57
70
  litedb!
58
71
  minitest (~> 5.0)
59
72
  rake (~> 13.0)
73
+ simplecov
60
74
  standard (~> 1.3)
61
75
 
62
76
  BUNDLED WITH
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sqlite3"
4
+ require "litescheduler"
5
+
6
+ module Litedb
7
+ class Connection < SQLite3::Database
8
+ DEFAULT_FILE = ":memory:"
9
+ DEFAULT_OPTIONS = {
10
+ synchronous: :NORMAL,
11
+ mmap_size: 32 * 1024, # 32 kilobytes
12
+ journal_size_limit: 64 * 1024 * 1024 # 64 megabytes
13
+ }.freeze
14
+
15
+ attr_reader :config
16
+
17
+ def initialize(file = nil, options = {}, zvfs = nil)
18
+ @scheduler = Litescheduler.instance
19
+ file ||= DEFAULT_FILE
20
+ options = options.transform_keys { |key|
21
+ begin
22
+ key.to_sym
23
+ rescue
24
+ key
25
+ end
26
+ }
27
+ @config = DEFAULT_OPTIONS.merge(options)
28
+
29
+ super(file, @config, zvfs)
30
+
31
+ set_pragmas
32
+ run_migrations
33
+ prepare_statements
34
+ end
35
+
36
+ def run_statement(statement, *args)
37
+ statements[statement].execute!(*args)
38
+ end
39
+
40
+ private
41
+
42
+ def set_pragmas
43
+ # set a custom busy handler to override the `busy_timeout`
44
+ # this ensures we either switch to another execution context or try again to connect
45
+ # https://www.sqlite.org/pragma.html#pragma_busy_timeout
46
+ busy_handler { @scheduler.switch || sleep(rand * 0.001) }
47
+ # Journal mode WAL allows for greater concurrency (many readers + one writer)
48
+ # https://www.sqlite.org/pragma.html#pragma_journal_mode
49
+ self.journal_mode = :WAL
50
+ # level of database durability
51
+ # 2 = "FULL" (sync on every write)
52
+ # 1 = "NORMAL" (sync every 1000 written pages)
53
+ # 0 = "OFF" (don't sync)
54
+ # https://www.sqlite.org/pragma.html#pragma_synchronous
55
+ self.synchronous = @config[:synchronous]
56
+ # set the global memory map so all processes can share data
57
+ # https://www.sqlite.org/pragma.html#pragma_mmap_size
58
+ # https://www.sqlite.org/mmap.html
59
+ self.mmap_size = @config[:mmap_size]
60
+ # impose a limit on the WAL file to prevent unlimited growth (with a negative impact on read performance as well)
61
+ # https://www.sqlite.org/pragma.html#pragma_journal_size_limit
62
+ self.journal_size_limit = @config[:journal_size_limit]
63
+ end
64
+
65
+ def run_migrations
66
+ return if @config[:migrations].nil?
67
+
68
+ migrations = if @config[:migrations].is_a?(Hash)
69
+ @config[:migrations].values
70
+ elsif @config[:migrations].is_a?(Array)
71
+ @config[:migrations]
72
+ else
73
+ raise Error.new("`migrations` option must be either a Hash or an Array")
74
+ end
75
+
76
+ transaction(:immediate) do
77
+ migrations.each do |sql|
78
+ execute(clean_sql(sql))
79
+ end
80
+ end
81
+ end
82
+
83
+ def prepare_statements
84
+ instance_variable_set(:@statements, {})
85
+ self.class.attr_reader(:statements) unless respond_to?(:statements)
86
+
87
+ return if @config[:statements].nil?
88
+
89
+ @config[:statements].each do |key, sql|
90
+ statements[key.to_sym] = prepare(clean_sql(sql))
91
+ end
92
+ end
93
+
94
+ def clean_sql(sql)
95
+ sql.gsub(/[[:space:]]+/, " ").strip
96
+ end
97
+ end
98
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Litedb
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.1"
5
5
  end
data/lib/litedb.rb CHANGED
@@ -1,8 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "litedb/version"
4
+ require_relative "litedb/connection"
5
+
6
+ require "sqlite3"
7
+ require "litescheduler"
4
8
 
5
9
  module Litedb
6
10
  class Error < StandardError; end
7
- # Your code goes here...
8
11
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: litedb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mohamed Hassan
@@ -9,8 +9,50 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2023-08-09 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2023-08-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sqlite3
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 1.5.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 1.5.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: litescheduler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 0.2.0
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 0.2.0
42
+ - !ruby/object:Gem::Dependency
43
+ name: simplecov
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
14
56
  description:
15
57
  email:
16
58
  - oldmoe@gmail.com
@@ -28,6 +70,7 @@ files:
28
70
  - README.md
29
71
  - Rakefile
30
72
  - lib/litedb.rb
73
+ - lib/litedb/connection.rb
31
74
  - lib/litedb/version.rb
32
75
  - sig/litedb.rbs
33
76
  homepage: https://github.com/litestack-ruby/litedb