slugdb-sqlite3 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b98942641ec0a274414e634c976fd74a11991065763437a1407788f2e1334ae8
4
+ data.tar.gz: 4d9b261b4b324b4578f285807468562885fec2419d41544b82f755cf497a4454
5
+ SHA512:
6
+ metadata.gz: 722493175782ea598c4de52b80e5410058325b7a1326b942d4952d2fa08571c79cff08edd983e4ef258200797647421602af718498ac81f9b792248b669defff
7
+ data.tar.gz: 96761296797eb615727de17381d2de9432a14b0c110b9766ff11c11ad30b3e3a75eb0fa98429fe70cbe38c338c5022efa36182506a03ac4f5097d9cd46218bb2
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,16 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ NewCops: enable
4
+
5
+ Metrics/ClassLength:
6
+ Max: 150
7
+
8
+ Metrics/MethodLength:
9
+ Max: 15
10
+
11
+ Layout/LineLength:
12
+ Max: 120
13
+
14
+ Metrics/BlockLength:
15
+ Exclude:
16
+ - '**/*_spec.rb'
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 3.0.0
6
+ before_install: gem install bundler -v 2.2.3
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at TODO: Write your email address. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in slugdb-sqlite3.gemspec
6
+ gemspec
7
+
8
+ gem 'gem-release'
9
+ gem 'pry'
10
+ gem 'rake', '~> 13.0'
11
+ gem 'rspec', '~> 3.0'
12
+ gem 'rubocop', '~> 0.80'
data/Gemfile.lock ADDED
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ slugdb-sqlite3 (0.1.0)
5
+ sqlite3
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ coderay (1.1.3)
12
+ diff-lcs (1.4.4)
13
+ gem-release (2.2.1)
14
+ method_source (1.0.0)
15
+ parallel (1.20.1)
16
+ parser (3.0.0.0)
17
+ ast (~> 2.4.1)
18
+ pry (0.14.0)
19
+ coderay (~> 1.1)
20
+ method_source (~> 1.0)
21
+ rainbow (3.0.0)
22
+ rake (13.0.3)
23
+ regexp_parser (2.1.1)
24
+ rexml (3.2.4)
25
+ rspec (3.10.0)
26
+ rspec-core (~> 3.10.0)
27
+ rspec-expectations (~> 3.10.0)
28
+ rspec-mocks (~> 3.10.0)
29
+ rspec-core (3.10.1)
30
+ rspec-support (~> 3.10.0)
31
+ rspec-expectations (3.10.1)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.10.0)
34
+ rspec-mocks (3.10.2)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.10.0)
37
+ rspec-support (3.10.2)
38
+ rubocop (0.93.1)
39
+ parallel (~> 1.10)
40
+ parser (>= 2.7.1.5)
41
+ rainbow (>= 2.2.2, < 4.0)
42
+ regexp_parser (>= 1.8)
43
+ rexml
44
+ rubocop-ast (>= 0.6.0)
45
+ ruby-progressbar (~> 1.7)
46
+ unicode-display_width (>= 1.4.0, < 2.0)
47
+ rubocop-ast (1.4.1)
48
+ parser (>= 2.7.1.5)
49
+ ruby-progressbar (1.11.0)
50
+ sqlite3 (1.4.2)
51
+ unicode-display_width (1.7.0)
52
+
53
+ PLATFORMS
54
+ x86_64-linux
55
+
56
+ DEPENDENCIES
57
+ gem-release
58
+ pry
59
+ rake (~> 13.0)
60
+ rspec (~> 3.0)
61
+ rubocop (~> 0.80)
62
+ slugdb-sqlite3!
63
+
64
+ BUNDLED WITH
65
+ 2.2.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # SlugDB::SQLite3
2
+
3
+ A lightweight, NoSQL, file based database backed by SQLite3.
4
+
5
+ I wanted a tiny database for an embedded project that could follow the advanced data modelling techniques for NoSQL.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'slugdb-sqlite3'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install slugdb-sqlite3
22
+
23
+ ## Usage
24
+
25
+ Checkout the regular SlugDB then come back here. This is the same interface using SQLite3 instead of PStore with the caveat that index names must be strings, cannot be symbols.
26
+
27
+ Basic usage
28
+
29
+ ```ruby
30
+ require 'slugdb/sqlite3'
31
+ # => true
32
+
33
+ # Create a database
34
+
35
+ sdb = SlugDB::SQLite3.new('./data.slug')
36
+ # => #<SlugDB::SQLite3:0x000055dbc5eb72a0
37
+ # @sdb=
38
+ # #<SQLite3::Database:0x000055dbc5eb7138
39
+ # @authorizer=nil,
40
+ # @busy_handler=nil,
41
+ # @collations={},
42
+ # @encoding=#<Encoding:UTF-8>,
43
+ # @functions={},
44
+ # @readonly=false,
45
+ # @results_as_hash=nil,
46
+ # @tracefunc=nil,
47
+ # @type_translation=nil,
48
+ # @type_translator=#<Proc:0x000055dbc5ca5188 /home/john/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:722 (lambda)>>>
49
+
50
+ # Put some data in it
51
+
52
+ sdb.put_item(pk: 'awesome#partition#1#', sk: 'metadata#')
53
+ # => {:pk=>"awesome#partition#1#", :sk=>"metadata#"}
54
+
55
+ # Read it back
56
+
57
+ sdb.get_item(pk: 'awesome#partition#1#', sk: 'metadata#')
58
+ # => {:pk=>"awesome#partition#1#", :sk=>"metadata#"}
59
+
60
+ # Put in some more data
61
+
62
+ sdb.put_item(pk: 'awesome#partition#1#', sk: 'something#useful#')
63
+ # => {:pk=>"awesome#partition#1#", :sk=>"something#useful#"}
64
+
65
+ # Query our records
66
+
67
+ sdb.query(pk: 'awesome#partition#1#')
68
+ # => [{:pk=>"awesome#partition#1#", :sk=>"metadata#"},
69
+ # {:pk=>"awesome#partition#1#", :sk=>"something#useful#"}]
70
+ ```
71
+
72
+ ## Performance
73
+
74
+ Using Ubuntu 20.04 on an i7-10710U with Ruby `3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]` I got the following:
75
+
76
+ ```
77
+ john@devbox:~/work/ruby-slugdb-sqlite3$ bundle exec bin/benchmark
78
+ Rehearsal -------------------------------------------------------------------------------
79
+ put_item 5 partitions, 1000 items 3.257310 2.628153 5.885463 ( 17.955709)
80
+ put_item 50 partitions, 100 items 2.448559 2.243897 4.692456 ( 16.230796)
81
+ put_item 500 partitions, 10 items 2.918192 2.362401 5.280593 ( 16.944482)
82
+ put_item 5000 partitions, 1 items 2.493245 2.292205 4.785450 ( 16.391882)
83
+ 2 indexes put_item 5 partitions, 1000 items 3.839494 3.019077 6.858571 ( 19.569620)
84
+ 2 indexes put_item 50 partitions, 100 items 4.034768 3.155123 7.189891 ( 19.707652)
85
+ 2 indexes put_item 500 partitions, 10 items 4.653889 3.610218 8.264107 ( 22.149808)
86
+ 2 indexes put_item 5000 partitions, 1 items 4.130269 3.220645 7.350914 ( 20.107618)
87
+ put_item, get_item 5 partitions, 1000 items 2.060286 1.840930 3.901216 ( 14.719933)
88
+ put_item, get_item 50 partitions, 100 items 2.461780 2.057571 4.519351 ( 15.747244)
89
+ put_item, get_item 500 partitions, 10 items 3.235899 2.735682 5.971581 ( 17.945475)
90
+ put_item, get_item 5000 partitions, 1 items 2.970254 2.439286 5.409540 ( 17.297003)
91
+ --------------------------------------------------------------------- total: 70.109133sec
92
+
93
+ user system total real
94
+ put_item 5 partitions, 1000 items 2.547680 2.312878 4.860558 ( 16.370065)
95
+ put_item 50 partitions, 100 items 3.387639 3.059782 6.447421 ( 18.399558)
96
+ put_item 500 partitions, 10 items 3.105934 2.880162 5.986096 ( 18.292644)
97
+ put_item 5000 partitions, 1 items 2.998857 2.688083 5.686940 ( 17.614748)
98
+ 2 indexes put_item 5 partitions, 1000 items 3.712199 2.853161 6.565360 ( 19.003753)
99
+ 2 indexes put_item 50 partitions, 100 items 4.180586 3.321287 7.501873 ( 20.203936)
100
+ 2 indexes put_item 500 partitions, 10 items 4.342980 3.487185 7.830165 ( 20.940714)
101
+ 2 indexes put_item 5000 partitions, 1 items 4.306121 3.332199 7.638320 ( 20.226231)
102
+ put_item, get_item 5 partitions, 1000 items 2.431277 2.235503 4.666780 ( 16.193609)
103
+ put_item, get_item 50 partitions, 100 items 2.636079 2.477241 5.113320 ( 16.677008)
104
+ put_item, get_item 500 partitions, 10 items 2.267650 1.782565 4.050215 ( 15.191621)
105
+ put_item, get_item 5000 partitions, 1 items 2.691626 2.369711 5.061337 ( 16.854689)
106
+ ```
107
+
108
+ This is staggeringly better than SlugDB's PStore implementation. If you want performance and a small extra dep of SQLite3, then use this.
109
+
110
+ ## Development
111
+
112
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
113
+
114
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
115
+
116
+ ## Contributing
117
+
118
+ Bug reports and pull requests are welcome on GitHub at https://github.com/watmin/Ruby-slugdb-sqlite3. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/watmin/Ruby-slugdb-sqlite3/blob/master/CODE_OF_CONDUCT.md).
119
+
120
+ ## License
121
+
122
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
123
+
124
+ ## Code of Conduct
125
+
126
+ Everyone interacting in the SlugDB project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/watmin/Ruby-slugdb-sqlite3/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ RuboCop::RakeTask.new
9
+
10
+ task default: %i[spec rubocop]
data/bin/benchmark ADDED
@@ -0,0 +1,159 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'benchmark'
6
+ require 'slugdb/sqlite3'
7
+ require 'pry'
8
+ require 'tempfile'
9
+
10
+ ##
11
+ # Factory to make benchmark inputs
12
+ module BenchmarkFactory
13
+ module_function
14
+
15
+ def partitions(count, size)
16
+ Array.new(count) { |p| items("p#{p}#", size) }
17
+ end
18
+
19
+ def items(partition, size)
20
+ Array.new(size) do |x|
21
+ { pk: partition, sk: "s#{x}#", ipk: partition, isk: "s#{x}#" }
22
+ end
23
+ end
24
+ end
25
+
26
+ five_partitions = BenchmarkFactory.partitions(5, 1000)
27
+ fifty_partitions = BenchmarkFactory.partitions(50, 100)
28
+ five_hundred_partitions = BenchmarkFactory.partitions(500, 10)
29
+ five_thousand_partitions = BenchmarkFactory.partitions(5000, 1)
30
+
31
+ Benchmark.bmbm do |reporter| # rubocop:disable Metrics/BlockLength
32
+ reporter.report('put_item 5 partitions, 1000 items') do
33
+ Tempfile.create do |db_file|
34
+ db = SlugDB::SQLite3.new(db_file.path)
35
+ five_partitions.each do |partition|
36
+ partition.each { |item| db.put_item(**item) }
37
+ end
38
+ end
39
+ end
40
+
41
+ reporter.report('put_item 50 partitions, 100 items') do
42
+ Tempfile.create do |db_file|
43
+ db = SlugDB::SQLite3.new(db_file.path)
44
+ fifty_partitions.each do |partition|
45
+ partition.each { |item| db.put_item(**item) }
46
+ end
47
+ end
48
+ end
49
+
50
+ reporter.report('put_item 500 partitions, 10 items') do
51
+ Tempfile.create do |db_file|
52
+ db = SlugDB::SQLite3.new(db_file.path)
53
+ five_hundred_partitions.each do |partition|
54
+ partition.each { |item| db.put_item(**item) }
55
+ end
56
+ end
57
+ end
58
+
59
+ reporter.report('put_item 5000 partitions, 1 items') do
60
+ Tempfile.create do |db_file|
61
+ db = SlugDB::SQLite3.new(db_file.path)
62
+ five_thousand_partitions.each do |partition|
63
+ partition.each { |item| db.put_item(**item) }
64
+ end
65
+ end
66
+ end
67
+
68
+ reporter.report('2 indexes put_item 5 partitions, 1000 items') do
69
+ Tempfile.create do |db_file|
70
+ db = SlugDB::SQLite3.new(db_file.path)
71
+ db.add_index(name: 'ski', pk: :sk, sk: :pk)
72
+ db.add_index(name: 'i', pk: :ipk, sk: :isk)
73
+ five_partitions.each do |partition|
74
+ partition.each { |item| db.put_item(**item) }
75
+ end
76
+ end
77
+ end
78
+
79
+ reporter.report('2 indexes put_item 50 partitions, 100 items') do
80
+ Tempfile.create do |db_file|
81
+ db = SlugDB::SQLite3.new(db_file.path)
82
+ db.add_index(name: 'ski', pk: :sk, sk: :pk)
83
+ db.add_index(name: 'i', pk: :ipk, sk: :isk)
84
+ fifty_partitions.each do |partition|
85
+ partition.each { |item| db.put_item(**item) }
86
+ end
87
+ end
88
+ end
89
+
90
+ reporter.report('2 indexes put_item 500 partitions, 10 items') do
91
+ Tempfile.create do |db_file|
92
+ db = SlugDB::SQLite3.new(db_file.path)
93
+ db.add_index(name: 'ski', pk: :sk, sk: :pk)
94
+ db.add_index(name: 'i', pk: :ipk, sk: :isk)
95
+ five_hundred_partitions.each do |partition|
96
+ partition.each { |item| db.put_item(**item) }
97
+ end
98
+ end
99
+ end
100
+
101
+ reporter.report('2 indexes put_item 5000 partitions, 1 items') do
102
+ Tempfile.create do |db_file|
103
+ db = SlugDB::SQLite3.new(db_file.path)
104
+ db.add_index(name: 'ski', pk: :sk, sk: :pk)
105
+ db.add_index(name: 'i', pk: :ipk, sk: :isk)
106
+ five_thousand_partitions.each do |partition|
107
+ partition.each { |item| db.put_item(**item) }
108
+ end
109
+ end
110
+ end
111
+
112
+ reporter.report('put_item, get_item 5 partitions, 1000 items') do
113
+ Tempfile.create do |db_file|
114
+ db = SlugDB::SQLite3.new(db_file.path)
115
+ five_partitions.each do |partition|
116
+ partition.each { |item| db.put_item(**item) }
117
+ end
118
+ five_partitions.each do |partition| # rubocop:disable Style/CombinableLoops
119
+ partition.each { |item| db.get_item(**item) }
120
+ end
121
+ end
122
+ end
123
+
124
+ reporter.report('put_item, get_item 50 partitions, 100 items') do
125
+ Tempfile.create do |db_file|
126
+ db = SlugDB::SQLite3.new(db_file.path)
127
+ fifty_partitions.each do |partition|
128
+ partition.each { |item| db.put_item(**item) }
129
+ end
130
+ fifty_partitions.each do |partition| # rubocop:disable Style/CombinableLoops
131
+ partition.each { |item| db.get_item(**item) }
132
+ end
133
+ end
134
+ end
135
+
136
+ reporter.report('put_item, get_item 500 partitions, 10 items') do
137
+ Tempfile.create do |db_file|
138
+ db = SlugDB::SQLite3.new(db_file.path)
139
+ five_hundred_partitions.each do |partition|
140
+ partition.each { |item| db.put_item(**item) }
141
+ end
142
+ five_hundred_partitions.each do |partition| # rubocop:disable Style/CombinableLoops
143
+ partition.each { |item| db.get_item(**item) }
144
+ end
145
+ end
146
+ end
147
+
148
+ reporter.report('put_item, get_item 5000 partitions, 1 items') do
149
+ Tempfile.create do |db_file|
150
+ db = SlugDB::SQLite3.new(db_file.path)
151
+ five_thousand_partitions.each do |partition|
152
+ partition.each { |item| db.put_item(**item) }
153
+ end
154
+ five_thousand_partitions.each do |partition| # rubocop:disable Style/CombinableLoops
155
+ partition.each { |item| db.get_item(**item) }
156
+ end
157
+ end
158
+ end
159
+ end
data/bin/console ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'slugdb/sqlite3'
6
+
7
+ require 'tempfile'
8
+
9
+ # You can add fixtures and/or initialization code here to make experimenting
10
+ # with your gem easier. You can also use a different console, if you like.
11
+
12
+ Tempfile.create do |db_file|
13
+ @db = SlugDB::SQLite3.new(db_file.path)
14
+ require 'pry'
15
+ Pry.start
16
+ end
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,176 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sqlite3'
4
+ require_relative 'sqlite3/version'
5
+
6
+ class SlugDB
7
+ ##
8
+ # SlugDB backed by SQLite3
9
+ class SQLite3
10
+ def initialize(file) # rubocop:disable Metrics/MethodLength
11
+ @sdb = ::SQLite3::Database.new(file)
12
+ @sdb.execute(
13
+ <<~SQL
14
+ CREATE TABLE IF NOT EXISTS main(
15
+ pk varchar(2048) NOT NULL,
16
+ sk varchar(1024) NOT NULL,
17
+ item varchar(409600) NOT NULL,
18
+ PRIMARY KEY(pk, sk)
19
+ )
20
+ SQL
21
+ )
22
+ @sdb.execute(
23
+ <<~SQL
24
+ CREATE TABLE IF NOT EXISTS indexes(
25
+ name varchar(255) NOT NULL,
26
+ schema varchar(4096) NOT NULL,
27
+ PRIMARY KEY(name)
28
+ )
29
+ SQL
30
+ )
31
+ end
32
+
33
+ def list_partitions
34
+ @sdb.execute('SELECT pk FROM main').map(&:first).uniq
35
+ end
36
+
37
+ def list_indexes
38
+ @sdb.execute('SELECT * FROM indexes')
39
+ .reduce({}) { |memo, (name, schema)| memo.merge(name => to_item(schema)) }
40
+ end
41
+
42
+ def add_index(name:, pk:, sk:, reindex: false) # rubocop:disable Metrics/MethodLength,Naming/MethodParameterName
43
+ index = { name => { pk: pk, sk: sk } }
44
+
45
+ if @sdb.execute('SELECT name FROM indexes WHERE name = ?', name).empty?
46
+ @sdb.execute(
47
+ <<~SQL
48
+ CREATE TABLE IF NOT EXISTS index_#{name}(
49
+ ipk varchar(2048) NOT NULL,
50
+ isk varchar(1024) NOT NULL,
51
+ pk varchar(2048) NOT NULL,
52
+ sk varchar(1024) NOT NULL,
53
+ item varchar(409600) NOT NULL,
54
+ PRIMARY KEY(ipk, isk, pk, sk)
55
+ )
56
+ SQL
57
+ )
58
+ @sdb.execute(
59
+ 'INSERT INTO indexes (name, schema) VALUES (?, ?)',
60
+ [name, to_raw(pk: pk, sk: sk)]
61
+ )
62
+ end
63
+
64
+ reindex!(index) if reindex
65
+
66
+ index
67
+ end
68
+
69
+ def reindex!(index)
70
+ @sdb.execute('SELECT item FROM main').each do |raw_item,|
71
+ item = to_item(raw_item)
72
+ @sdb.execute('BEGIN TRANSACTION')
73
+ index_delete_statements(index, item).each { |s, v| @sdb.execute(s, v) }
74
+ index_insert_statements(index, item, raw_item).each { |s, v| @sdb.execute(s, v) }
75
+ @sdb.execute('COMMIT TRANSACTION')
76
+ end
77
+ rescue ::SQLite3::SQLException => e
78
+ @sdb.execute('ABORT TRANSACTION')
79
+ raise e
80
+ end
81
+
82
+ def get_item(pk:, sk:, **_) # rubocop:disable Naming/MethodParameterName
83
+ @sdb.execute(
84
+ 'SELECT item FROM main WHERE pk = ? AND sk = ?',
85
+ [pk, sk]
86
+ ).flatten.map(&method(:to_item)).first
87
+ end
88
+
89
+ def put_item(pk:, sk:, **attributes) # rubocop:disable Metrics/AbcSize,Naming/MethodParameterName
90
+ new_item = attributes.merge(pk: pk, sk: sk)
91
+ raw_item = to_raw(new_item)
92
+ old_item = get_item(**new_item)
93
+ indexes = list_indexes
94
+
95
+ @sdb.execute('BEGIN TRANSACTION')
96
+ @sdb.execute('DELETE FROM main WHERE pk = ? AND sk = ?', [pk, sk])
97
+ index_delete_statements(indexes, old_item).each { |s, v| @sdb.execute(s, v) }
98
+ index_insert_statements(indexes, new_item, raw_item).each { |s, v| @sdb.execute(s, v) }
99
+ @sdb.execute('INSERT INTO main (pk, sk, item) VALUES (?, ?, ?)', [pk, sk, raw_item])
100
+ @sdb.execute('COMMIT TRANSACTION')
101
+
102
+ new_item
103
+ rescue SQLite3::SQLException => e
104
+ @sdb.execute('ABORT TRANSACTION')
105
+ raise e
106
+ end
107
+
108
+ def delete_item(pk:, sk:, **_) # rubocop:disable Naming/MethodParameterName
109
+ item = get_item(pk: pk, sk: sk)
110
+ return if item.nil?
111
+
112
+ indexes = list_indexes
113
+ @sdb.execute('BEGIN TRANSACTION')
114
+ @sdb.execute('DELETE FROM main WHERE pk = ? AND sk = ?', [pk, sk])
115
+ index_delete_statements(indexes, item).each { |s, v| @sdb.execute(s, v) }
116
+ @sdb.execute('COMMIT TRANSACTION')
117
+
118
+ item
119
+ rescue SQLite3::SQLException => e
120
+ @sdb.execute('ABORT TRANSACTION')
121
+ raise e
122
+ end
123
+
124
+ def query(pk:, index: 'main', select: nil, filter: nil) # rubocop:disable Naming/MethodParameterName
125
+ results =
126
+ if index == 'main'
127
+ raw_query('SELECT sk, item FROM main WHERE pk = ?', [pk])
128
+ else
129
+ raw_query("SELECT isk, item FROM index_#{index} WHERE ipk = ?", [pk])
130
+ end
131
+
132
+ results = results.select { |sk,| select[sk] } if select
133
+ results = results.filter { |_, item| filter[item] } if filter
134
+
135
+ results.map(&:last)
136
+ end
137
+
138
+ private
139
+
140
+ def to_raw(item)
141
+ Marshal.dump(item)
142
+ end
143
+
144
+ def to_item(raw)
145
+ Marshal.load(raw) # rubocop:disable Security/MarshalLoad
146
+ end
147
+
148
+ def index_delete_statements(indexes, item)
149
+ indexes.map do |name, schema|
150
+ next if item.nil?
151
+
152
+ [
153
+ "DELETE FROM index_#{name} WHERE #{schema[:pk]} = ? AND #{schema[:sk]} = ?",
154
+ [item[schema[:pk]], item[schema[:sk]]]
155
+ ]
156
+ end.compact
157
+ end
158
+
159
+ def index_insert_statements(indexes, item, raw_item)
160
+ indexes.map do |name, schema|
161
+ next unless item.key?(schema[:pk]) && item.key?(schema[:sk])
162
+
163
+ [
164
+ "INSERT INTO index_#{name} (ipk, isk, pk, sk, item) VALUES(?, ?, ?, ?, ?)",
165
+ [item[schema[:pk]], item[schema[:sk]], item[:pk], item[:sk], raw_item]
166
+ ]
167
+ end.compact
168
+ end
169
+
170
+ def raw_query(statement, values)
171
+ return enum_for(:raw_query, statement, values) unless block_given?
172
+
173
+ @sdb.execute(statement, values).each { |sk, raw_item| yield [sk, to_item(raw_item)] }
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SlugDB
4
+ class SQLite3
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/slugdb/sqlite3/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'slugdb-sqlite3'
7
+ spec.version = SlugDB::SQLite3::VERSION
8
+ spec.authors = ['John Shields <watmin>']
9
+ spec.email = ['john@shields.wtf']
10
+
11
+ spec.summary = 'A lightweight, opinionated, file based NoSQL database backed by SQLite3'
12
+ spec.description = 'I wanted sqlite with a document store.'
13
+ spec.homepage = 'https://github.com/watmin/Ruby-slugdb-sqlite3'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
16
+
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_dependency 'sqlite3'
29
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slugdb-sqlite3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Shields <watmin>
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-03-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sqlite3
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: I wanted sqlite with a document store.
28
+ email:
29
+ - john@shields.wtf
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".rubocop.yml"
37
+ - ".travis.yml"
38
+ - CODE_OF_CONDUCT.md
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - bin/benchmark
45
+ - bin/console
46
+ - bin/setup
47
+ - lib/slugdb/sqlite3.rb
48
+ - lib/slugdb/sqlite3/version.rb
49
+ - slugdb-sqlite3.gemspec
50
+ homepage: https://github.com/watmin/Ruby-slugdb-sqlite3
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ allowed_push_host: https://rubygems.org
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.5.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.2.3
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: A lightweight, opinionated, file based NoSQL database backed by SQLite3
74
+ test_files: []