litedb 0.1.0 → 0.2.0

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: 8e1097139b537cf11185aff048fe8a748c5310a39035a064592c7ee2f6630d3b
4
+ data.tar.gz: 7ff2fac632767ed67aa3ab838d8b731f4af5b5a870e087e8eeae42976823b7cd
5
5
  SHA512:
6
- metadata.gz: 5842f4d166945c9c8ef0e3a41c7c767378cb4b7386b4cfa98758e6796419d3e7b042698f02762ae3cc7a1df0feced037db75a7569d3cc48c6acdba9d747b5e6e
7
- data.tar.gz: be7703fecba593e2e5342ae59a5bd31d2ab9934c5d6678484dfb5c6b83f24fc9e4268938ba3d05eb8ca0bf6ba11f7f13dfd1a22427d882d630b68caa107e2bbf
6
+ metadata.gz: 46abb2fff25ef4498fff3700204d4dc72364c670e6a5b59d7ca0e419e154be0e016e8c88b4ec0a08d08ea548317823238f8b6398a448c9431bf74fe9c6dd80c7
7
+ data.tar.gz: 736164fddc5851d19cf256b0a58640e4610e4231114bf46c5d8803380d050444dfc14ccd017698c2982ddba9947e146f3b0a44fa74905f9b5fda09b8d8414715
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2023-08-10
4
+
5
+ - Initial release of usable code
6
+
3
7
  ## [0.1.0] - 2023-08-09
4
8
 
5
- - Initial release
9
+ - Initial release to reserve gem name
data/Gemfile.lock CHANGED
@@ -1,7 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- litedb (0.1.0)
4
+ litedb (0.2.0)
5
+ litescheduler (>= 0.2.0)
6
+ sqlite3 (>= 1.5.0)
5
7
 
6
8
  GEM
7
9
  remote: https://rubygems.org/
@@ -10,6 +12,7 @@ GEM
10
12
  json (2.6.3)
11
13
  language_server-protocol (3.17.0.3)
12
14
  lint_roller (1.1.0)
15
+ litescheduler (0.2.1)
13
16
  minitest (5.19.0)
14
17
  parallel (1.23.0)
15
18
  parser (3.2.2.3)
@@ -36,6 +39,8 @@ GEM
36
39
  rubocop (>= 1.7.0, < 2.0)
37
40
  rubocop-ast (>= 0.4.0)
38
41
  ruby-progressbar (1.13.0)
42
+ sqlite3 (1.6.3-arm64-darwin)
43
+ sqlite3 (1.6.3-x86_64-linux)
39
44
  standard (1.30.1)
40
45
  language_server-protocol (~> 3.17.0.2)
41
46
  lint_roller (~> 1.0)
@@ -52,6 +57,7 @@ GEM
52
57
 
53
58
  PLATFORMS
54
59
  arm64-darwin-21
60
+ x86_64-linux
55
61
 
56
62
  DEPENDENCIES
57
63
  litedb!
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Litedb
4
- VERSION = "0.1.0"
3
+ require "sqlite3"
4
+
5
+ class Litedb < SQLite3::Database
6
+ VERSION = "0.2.0"
5
7
  end
data/lib/litedb.rb CHANGED
@@ -2,7 +2,99 @@
2
2
 
3
3
  require_relative "litedb/version"
4
4
 
5
- module Litedb
5
+ require "sqlite3"
6
+ require "litescheduler"
7
+
8
+ class Litedb < SQLite3::Database
6
9
  class Error < StandardError; end
7
- # Your code goes here...
10
+
11
+ DEFAULT_FILE = ":memory:"
12
+ DEFAULT_OPTIONS = {
13
+ synchronous: :NORMAL,
14
+ mmap_size: 32 * 1024, # 32 kilobytes
15
+ journal_size_limit: 64 * 1024 * 1024 # 64 megabytes
16
+ }.freeze
17
+
18
+ attr_reader :config
19
+
20
+ def initialize(file = nil, options = {}, zvfs = nil)
21
+ @scheduler = Litescheduler.instance
22
+ file ||= DEFAULT_FILE
23
+ options = options.transform_keys { |key|
24
+ begin
25
+ key.to_sym
26
+ rescue
27
+ key
28
+ end
29
+ }
30
+ @config = DEFAULT_OPTIONS.merge(options)
31
+
32
+ super(file, @config, zvfs)
33
+
34
+ set_pragmas
35
+ run_migrations
36
+ prepare_statements
37
+ end
38
+
39
+ def run_statement(statement, *args)
40
+ statements[statement].execute!(*args)
41
+ end
42
+
43
+ private
44
+
45
+ def set_pragmas
46
+ # set a custom busy handler to override the `busy_timeout`
47
+ # this ensures we either switch to another execution context or try again to connect
48
+ # https://www.sqlite.org/pragma.html#pragma_busy_timeout
49
+ busy_handler { @scheduler.switch || sleep(rand * 0.001) }
50
+ # Journal mode WAL allows for greater concurrency (many readers + one writer)
51
+ # https://www.sqlite.org/pragma.html#pragma_journal_mode
52
+ self.journal_mode = :WAL
53
+ # level of database durability
54
+ # 2 = "FULL" (sync on every write)
55
+ # 1 = "NORMAL" (sync every 1000 written pages)
56
+ # 0 = "OFF" (don't sync)
57
+ # https://www.sqlite.org/pragma.html#pragma_synchronous
58
+ self.synchronous = @config[:synchronous]
59
+ # set the global memory map so all processes can share data
60
+ # https://www.sqlite.org/pragma.html#pragma_mmap_size
61
+ # https://www.sqlite.org/mmap.html
62
+ self.mmap_size = @config[:mmap_size]
63
+ # impose a limit on the WAL file to prevent unlimited growth (with a negative impact on read performance as well)
64
+ # https://www.sqlite.org/pragma.html#pragma_journal_size_limit
65
+ self.journal_size_limit = @config[:journal_size_limit]
66
+ end
67
+
68
+ def run_migrations
69
+ return if @config[:migrations].nil?
70
+
71
+ migrations = if @config[:migrations].is_a?(Hash)
72
+ @config[:migrations].values
73
+ elsif @config[:migrations].is_a?(Array)
74
+ @config[:migrations]
75
+ else
76
+ raise Error.new("`migrations` option must be either a Hash or an Array")
77
+ end
78
+
79
+ transaction(:immediate) do
80
+ migrations.each do |sql|
81
+ execute(clean_sql(sql))
82
+ end
83
+ end
84
+ end
85
+
86
+ def prepare_statements
87
+ instance_variable_set(:@statements, {})
88
+ self.class.attr_reader(:statements) unless respond_to?(:statements)
89
+
90
+ return if @config[:statements].nil?
91
+
92
+ @config[:statements].each do |key, sql|
93
+ statements[key.to_sym] = prepare(clean_sql(sql))
94
+ end
95
+ end
96
+
97
+ def clean_sql(sql)
98
+ sql.gsub(/[[:space:]]+/, " ").strip
99
+ end
8
100
  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.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mohamed Hassan
@@ -9,8 +9,36 @@ 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-10 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
14
42
  description:
15
43
  email:
16
44
  - oldmoe@gmail.com