postgres_key_value 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data/.env.template +6 -0
- data/.github/workflows/main.yml +39 -0
- data/.gitignore +9 -0
- data/.rubocop.yml +3 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +48 -0
- data/LICENSE.txt +22 -0
- data/README.md +219 -0
- data/Rakefile +21 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/postgres_key_value/errors.rb +7 -0
- data/lib/postgres_key_value/store.rb +80 -0
- data/lib/postgres_key_value/utils.rb +24 -0
- data/lib/postgres_key_value/version.rb +5 -0
- data/lib/postgres_key_value.rb +10 -0
- data/postgres_key_value.gemspec +29 -0
- data.tar.gz.sig +0 -0
- metadata +87 -0
- metadata.gz.sig +0 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2d46cdd5eefe2180f231e0696f3e9849fbd6d61b91f5b2a02066f5ef82ccbfc0
|
4
|
+
data.tar.gz: cdbf6755b5cba7283c47e351a8393123f9d3e4e6a0a437c521d51f734aec2739
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 97a3f67306f9bf1008cdf014e3625638b280a693ef5bfe9aeeb38e86e7a60ce9ab865b58e2a77a62a6c79a32e133cce93c1afdc93bb57cfbf4eafc0ccde38b9d
|
7
|
+
data.tar.gz: f10a3162d5459bf1c3c636f01fc5a37d4ebf5ae56c43fd43c795c017519917285923a790f1f45fd6a333429d7730d9539a4266fb8f098d003101c9cfe4cd7a6c
|
checksums.yaml.gz.sig
ADDED
Binary file
|
data/.env.template
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
---
|
2
|
+
name: PostgresKeyValue
|
3
|
+
|
4
|
+
on: [push, pull_request] # yamllint disable-line rule:truthy
|
5
|
+
|
6
|
+
jobs:
|
7
|
+
build:
|
8
|
+
runs-on: ubuntu-latest
|
9
|
+
env:
|
10
|
+
DB_NAME: postgres
|
11
|
+
DB_USER: postgres
|
12
|
+
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
|
13
|
+
DB_HOST: localhost
|
14
|
+
DB_PORT: 5432
|
15
|
+
services:
|
16
|
+
postgres:
|
17
|
+
image: postgres
|
18
|
+
env:
|
19
|
+
POSTGRES_USER: postgres
|
20
|
+
POSTGRES_PASSWORD: ${{ secrets.DB_PASSWORD }}
|
21
|
+
ports:
|
22
|
+
- 5432:5432
|
23
|
+
options: >-
|
24
|
+
--health-cmd pg_isready
|
25
|
+
--health-interval 10s
|
26
|
+
--health-timeout 5s
|
27
|
+
--health-retries 5
|
28
|
+
steps:
|
29
|
+
- uses: actions/checkout@v2
|
30
|
+
- name: Set up Ruby
|
31
|
+
uses: ruby/setup-ruby@v1
|
32
|
+
with:
|
33
|
+
ruby-version: 3.0.0
|
34
|
+
- name: Run the default task
|
35
|
+
run: |
|
36
|
+
gem install bundler -v 2.2.3
|
37
|
+
bundle install
|
38
|
+
bundle exec rake
|
39
|
+
bundle exec rake benchmark
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 ber@berk.es. 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,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in postgres_key_value.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem 'byebug'
|
9
|
+
gem 'dotenv', '~> 2.7.6'
|
10
|
+
gem 'minitest', '~> 5.0'
|
11
|
+
gem 'rake', '~> 13.0'
|
12
|
+
gem 'rubocop', '~> 0.80'
|
13
|
+
|
14
|
+
gem 'pg', '~> 1'
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
postgres_key_value (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
byebug (11.1.3)
|
11
|
+
dotenv (2.7.6)
|
12
|
+
minitest (5.15.0)
|
13
|
+
parallel (1.21.0)
|
14
|
+
parser (3.1.1.0)
|
15
|
+
ast (~> 2.4.1)
|
16
|
+
pg (1.3.3)
|
17
|
+
rainbow (3.1.1)
|
18
|
+
rake (13.0.6)
|
19
|
+
regexp_parser (2.2.1)
|
20
|
+
rexml (3.2.5)
|
21
|
+
rubocop (0.93.1)
|
22
|
+
parallel (~> 1.10)
|
23
|
+
parser (>= 2.7.1.5)
|
24
|
+
rainbow (>= 2.2.2, < 4.0)
|
25
|
+
regexp_parser (>= 1.8)
|
26
|
+
rexml
|
27
|
+
rubocop-ast (>= 0.6.0)
|
28
|
+
ruby-progressbar (~> 1.7)
|
29
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
30
|
+
rubocop-ast (1.16.0)
|
31
|
+
parser (>= 3.1.1.0)
|
32
|
+
ruby-progressbar (1.11.0)
|
33
|
+
unicode-display_width (1.8.0)
|
34
|
+
|
35
|
+
PLATFORMS
|
36
|
+
x86_64-linux
|
37
|
+
|
38
|
+
DEPENDENCIES
|
39
|
+
byebug
|
40
|
+
dotenv (~> 2.7.6)
|
41
|
+
minitest (~> 5.0)
|
42
|
+
pg (~> 1)
|
43
|
+
postgres_key_value!
|
44
|
+
rake (~> 13.0)
|
45
|
+
rubocop (~> 0.80)
|
46
|
+
|
47
|
+
BUNDLED WITH
|
48
|
+
2.2.3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Bèr Kessels
|
4
|
+
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
14
|
+
all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,219 @@
|
|
1
|
+
# PostgresKeyValue
|
2
|
+
|
3
|
+
[![Ruby](https://github.com/berkes/postgres_key_value/actions/workflows/main.yml/badge.svg)](https://github.com/berkes/postgres_key_value/actions/workflows/main.yml)
|
4
|
+
|
5
|
+
**Key-Value storage for Posgresql**
|
6
|
+
|
7
|
+
Performant and simple key-value storage in Posgresql. With a Hash-like
|
8
|
+
interface. Only dependency is pg gem.
|
9
|
+
|
10
|
+
PostgresKeyValue tries to get out of your way, by being unopiniated small, and
|
11
|
+
simple.
|
12
|
+
|
13
|
+
It works similar, but not compatible to, Hash. Some features from Hash are implemented, others
|
14
|
+
deliberately omitted when they don't make sense or would make leaky abstractions.
|
15
|
+
|
16
|
+
PostgresKeyValue depends on the [pg gem](https://rubygems.org/gems/pg), but
|
17
|
+
doesn't add this as requirement, so that you can provide your own, your
|
18
|
+
version, fork or compatible gem instead.
|
19
|
+
|
20
|
+
Configuration and usage is done through dependency injection, which makes it
|
21
|
+
easy for you to test, and to replace with mocks. The design aims to decouple as
|
22
|
+
much as possible, allowing to integrate in the right place (and only there).
|
23
|
+
|
24
|
+
A few tools are included to prepare and optimize the database. Usable in e.g.
|
25
|
+
your migrations or a deploy script.
|
26
|
+
|
27
|
+
## PostgresKeyValue is not finished!
|
28
|
+
|
29
|
+
Work in Progress. Here are some evident TODOs (will be moved into github issues later)
|
30
|
+
|
31
|
+
* [x] Fix glaring SQL injection holes. Use prepared statement or params to ensure clean input.
|
32
|
+
* [ ] Determine locking and transactional behaviour: who wins on a conflict?
|
33
|
+
* [x] Add proper index to key. Introduce some benchmark tests.
|
34
|
+
* [ ] Allow read-only setup so that e.g. workers can read but never write.
|
35
|
+
* [ ] Allow "connection" to be passed in from ActiveRecord (and sequel?) so that users can re-use it.
|
36
|
+
* [x] Add tools to use in migrations or deploy scripts to setup database like we do in tests.
|
37
|
+
* [x] Add `key?()` api to check if a key exists.
|
38
|
+
* [x] Add `fetch()` api to provide a default and/or raise exception similar to ENV and hash.
|
39
|
+
* [x] Add a default to initializer for the entire store. Maybe with a block, to mimic Hash.new signature?
|
40
|
+
* [ ] Add sanitizers and protection for the JSON de- serializers e.g. storage size or formats.
|
41
|
+
* [ ] Allow JSON de- serializers to be dependency-injected instead of using `JSON.parse` and `x.to_json`.
|
42
|
+
* [ ] Check for more robust SQL injection protection. e.g. by force-escaping before use? See: https://stackoverflow.com/a/42281333/73673
|
43
|
+
* [ ] Use prepared statement or params to improve performance.
|
44
|
+
|
45
|
+
## Installation
|
46
|
+
|
47
|
+
Add this line to your application's Gemfile:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
gem 'postgres_key_value'
|
51
|
+
```
|
52
|
+
|
53
|
+
And then execute:
|
54
|
+
|
55
|
+
$ bundle install
|
56
|
+
|
57
|
+
Or install it yourself as:
|
58
|
+
|
59
|
+
$ gem install postgres_key_value
|
60
|
+
|
61
|
+
## Usage
|
62
|
+
|
63
|
+
Steps are as follows:
|
64
|
+
|
65
|
+
1. Make a connection to a postgresql database.
|
66
|
+
2. Instantiate a PostgresKeyValue object by passing in this connection.
|
67
|
+
3. Write to-, and read from this database.
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
require 'pg'
|
71
|
+
require 'postgres_key_value'
|
72
|
+
|
73
|
+
connection = PG::Connection.open(:dbname => 'test')
|
74
|
+
greetings = PostgresKeyValue::Store.new(connection)
|
75
|
+
|
76
|
+
greetings[:en] = "Hello World"
|
77
|
+
greetings[:nl] = "Hallo Wereld"
|
78
|
+
|
79
|
+
greetings[:en] #=> Hello World
|
80
|
+
greetings['DE-de'] #=> nil
|
81
|
+
greetings.fetch('DE-de', 'No greeting') #=> No greeting
|
82
|
+
greetings.key?(:nl) #=> true
|
83
|
+
|
84
|
+
# Can be another process on another machine entirely.
|
85
|
+
Thread.new do
|
86
|
+
other_greetings = PostgresKeyValue::Store.new(connection)
|
87
|
+
other_greetings[:en] = "Hello Mars!"
|
88
|
+
end.join
|
89
|
+
|
90
|
+
greetings[:en] #=> Hello Mars!
|
91
|
+
```
|
92
|
+
|
93
|
+
## Utils
|
94
|
+
|
95
|
+
Utils to create and prepare the table are provided. For example in your migrations:
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
class CreateKVTableForCursors < ButtonShop::Migration
|
99
|
+
include PostgresKeyValue::Utils
|
100
|
+
|
101
|
+
def migrate_up
|
102
|
+
create_table('cursors', 'buttonshop_kv_store')
|
103
|
+
end
|
104
|
+
|
105
|
+
def migrate_down
|
106
|
+
drop_table('cursors', 'buttonshop_kv_store')
|
107
|
+
end
|
108
|
+
|
109
|
+
private
|
110
|
+
|
111
|
+
def connection
|
112
|
+
ButtonShop.config.primary_db_connection
|
113
|
+
end
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
117
|
+
And in a hypthetical deployment or provisioning tool
|
118
|
+
|
119
|
+
```
|
120
|
+
class CursorsKvPreparator
|
121
|
+
include PostgresKeyValue::Utils
|
122
|
+
DB_NAME = 'buttonshop_kv_store'
|
123
|
+
TABLE_NAME = 'cursors'
|
124
|
+
|
125
|
+
def initialize(connection)
|
126
|
+
@table_name = table_name
|
127
|
+
@connection = connection
|
128
|
+
end
|
129
|
+
|
130
|
+
def prepare
|
131
|
+
MyInfra::Databases::CreateDatabaseCommand.new(DB_NAME)
|
132
|
+
create_table(TABLE_NAME, DB_NAME)
|
133
|
+
end
|
134
|
+
|
135
|
+
private
|
136
|
+
|
137
|
+
attr_reader :connection
|
138
|
+
end
|
139
|
+
|
140
|
+
on :staging_server do
|
141
|
+
CursorsKvPreparator.new(@pg_connection).prepare
|
142
|
+
end
|
143
|
+
```
|
144
|
+
|
145
|
+
## Technical details
|
146
|
+
|
147
|
+
Keys can only be strings or symbols. So be sure to convert your object to a
|
148
|
+
string explicitely before using.
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
greetings[nil] #=> PostgresKeyValue::InvalidKey
|
152
|
+
greetings[42] #=> PostgresKeyValue::InvalidKey
|
153
|
+
greetings[''] #=> nil
|
154
|
+
greetings['42'] #=> nil
|
155
|
+
|
156
|
+
```
|
157
|
+
|
158
|
+
TODO: write about
|
159
|
+
|
160
|
+
* transactions
|
161
|
+
* indexes
|
162
|
+
* connection pools
|
163
|
+
* read/write copies
|
164
|
+
|
165
|
+
Database is configured to store key/value in two columns: key is primary key,
|
166
|
+
value of type json. Primary is of type string, so PG limitation on keys and string
|
167
|
+
storage apply.
|
168
|
+
|
169
|
+
PostgresKeyValue deliberately tries not to be fully compatible with Hash. But it
|
170
|
+
does offer a similar interface. Mainly because such an opaque abstraction is leaky:
|
171
|
+
|
172
|
+
A table with KV storages can, by design, grow very large, whereas a hash is
|
173
|
+
memory bound. so features like iterators `store.each {|k,v| ... }` or
|
174
|
+
`store.to_a` require the underlying limits to leak through. We'd then need
|
175
|
+
logic, config, etc to handle when the database becomes too big for memory to hold.
|
176
|
+
|
177
|
+
We allow keys only to be strings, and not "anything" as hash does. The database
|
178
|
+
stores keys as strings, so if we'd allow "anything" as key, the marshalling or
|
179
|
+
serializing would not only become complex, it puts a performance hit on all
|
180
|
+
usage: so the ones using it with strings as keys would become slower too.
|
181
|
+
|
182
|
+
The values are serialized using JSON. This is lossy. This is by-design, but for
|
183
|
+
security reasons. Marshalling code `object.marshall` retains the entire state,
|
184
|
+
including methods, or callbacks and allows the provider of data to even
|
185
|
+
monkeypatch your ruby codebase. We chose for JSON, as that is simplest, and
|
186
|
+
therefore secured from these attacks (unless JSON.parse is vulnarable, which is
|
187
|
+
not unthinkable).
|
188
|
+
|
189
|
+
Many methods on Hash don't make a lot of sense either. E.g. most methods that
|
190
|
+
operate on the entire hash, like `transform_keys!` or `compact` have little use
|
191
|
+
in a pure KV lookup system. When in need of such operations, you probably need
|
192
|
+
an actual database-table (which, not by coincidence, the `connection` already offers!)
|
193
|
+
|
194
|
+
Another reason for not wanting to have feature-parity with Hash, is that it
|
195
|
+
would grow this gem far beyond "simple", without there being a clear need for
|
196
|
+
all the added features. Hash is really large! Rather, if there are features you need, raise an issue
|
197
|
+
(or write a patch) so we can determine if it fits the scope and is worth the
|
198
|
+
extra code.
|
199
|
+
|
200
|
+
## Development
|
201
|
+
|
202
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
203
|
+
|
204
|
+
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).
|
205
|
+
|
206
|
+
## Contributing
|
207
|
+
|
208
|
+
Bug reports and pull requests are welcome on GitHub at
|
209
|
+
https://github.com/berkes/postgres_key_value. This project is intended to be a
|
210
|
+
safe, welcoming space for collaboration, and contributors are expected to
|
211
|
+
adhere to the [code of conduct](https://github.com/berkes/postgres_key_value/blob/master/CODE_OF_CONDUCT.md).
|
212
|
+
|
213
|
+
## License
|
214
|
+
|
215
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
216
|
+
|
217
|
+
## Code of Conduct
|
218
|
+
|
219
|
+
Everyone interacting in the PostgresKeyValue project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/berkes/postgres_key_value/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << 'test'
|
8
|
+
t.libs << 'lib'
|
9
|
+
t.test_files = FileList['test/**/*_test.rb']
|
10
|
+
end
|
11
|
+
|
12
|
+
Rake::TestTask.new(:benchmark) do |t|
|
13
|
+
t.libs << 'benchmark'
|
14
|
+
t.libs << 'lib'
|
15
|
+
t.test_files = FileList['benchmark/**/*_benchmark.rb']
|
16
|
+
end
|
17
|
+
require 'rubocop/rake_task'
|
18
|
+
|
19
|
+
RuboCop::RakeTask.new
|
20
|
+
|
21
|
+
task default: %i[test rubocop]
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'postgres_key_value'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PostgresKeyValue
|
4
|
+
##
|
5
|
+
# Interact with the Key Value Store
|
6
|
+
class Store
|
7
|
+
def initialize(connection, table, default = nil, &block)
|
8
|
+
@connection = connection
|
9
|
+
@table = table
|
10
|
+
|
11
|
+
@default = default
|
12
|
+
@block = block_given? ? block : nil
|
13
|
+
raise ArgumentError, 'cannot provide both default value and default block' if @default && @block
|
14
|
+
end
|
15
|
+
|
16
|
+
def []=(key, value)
|
17
|
+
assert_correct_key(key)
|
18
|
+
connection.exec_params(upsert_q, [key, value.to_json])
|
19
|
+
rescue PG::ProgramLimitExceeded
|
20
|
+
raise KeyLimitExceeded
|
21
|
+
end
|
22
|
+
|
23
|
+
def [](key)
|
24
|
+
assert_correct_key(key)
|
25
|
+
res = connection.exec_params(read_q, [key])
|
26
|
+
return instance_default(key) if res.num_tuples.zero?
|
27
|
+
|
28
|
+
val = res.getvalue(0, 0)
|
29
|
+
JSON.parse(val)
|
30
|
+
end
|
31
|
+
|
32
|
+
def key?(key)
|
33
|
+
assert_correct_key(key)
|
34
|
+
connection.exec_params(exists_q, [key]).num_tuples.positive?
|
35
|
+
end
|
36
|
+
|
37
|
+
def fetch(key, default = nil)
|
38
|
+
assert_correct_key(key)
|
39
|
+
res = connection.exec_params(read_q, [key])
|
40
|
+
|
41
|
+
if res.num_tuples.zero?
|
42
|
+
return default if default
|
43
|
+
fail KeyError, "key not found: \"#{key}\""
|
44
|
+
end
|
45
|
+
|
46
|
+
val = res.getvalue(0, 0)
|
47
|
+
JSON.parse(val)
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def instance_default(key)
|
53
|
+
if @block
|
54
|
+
@block.call(key)
|
55
|
+
else
|
56
|
+
@default
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def assert_correct_key(key)
|
61
|
+
return true if key.is_a?(String) || key.is_a?(Symbol)
|
62
|
+
|
63
|
+
raise InvalidKey
|
64
|
+
end
|
65
|
+
|
66
|
+
def upsert_q
|
67
|
+
"INSERT INTO #{table} (key, value) VALUES($1::text, $2::json) ON CONFLICT (key) DO UPDATE SET value = $2::json"
|
68
|
+
end
|
69
|
+
|
70
|
+
def read_q
|
71
|
+
"SELECT value FROM #{table} WHERE key = $1::text"
|
72
|
+
end
|
73
|
+
|
74
|
+
def exists_q
|
75
|
+
"SELECT 1 FROM #{table} WHERE key = $1::text LIMIT 1"
|
76
|
+
end
|
77
|
+
|
78
|
+
attr_reader :connection, :table
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PostgresKeyValue
|
4
|
+
##
|
5
|
+
# Utilities to generate the tables, indexes and databases for use with this gem
|
6
|
+
module Utils
|
7
|
+
def create_table(table_name)
|
8
|
+
maintainance_connection.exec("CREATE TABLE #{table_name} (key VARCHAR PRIMARY KEY, value json)")
|
9
|
+
end
|
10
|
+
|
11
|
+
def drop_table(table_name)
|
12
|
+
maintainance_connection.exec("DROP TABLE #{table_name}")
|
13
|
+
end
|
14
|
+
|
15
|
+
protected
|
16
|
+
|
17
|
+
def maintainance_connection
|
18
|
+
raise(
|
19
|
+
NotImplementedError,
|
20
|
+
'including class must implement maintainance_connection with CREATE/DROP table permissions'
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/postgres_key_value/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'postgres_key_value'
|
7
|
+
spec.version = PostgresKeyValue::VERSION
|
8
|
+
spec.authors = ["Bèr Kessels\n"]
|
9
|
+
spec.email = ['ber@berk.es']
|
10
|
+
|
11
|
+
spec.summary = 'Key-Value storage for Posgresql'
|
12
|
+
spec.description = "Performant and simple key-value storage in Posgresql. \
|
13
|
+
With a Hash-like interface. Only dependency is pg gem"
|
14
|
+
spec.homepage = 'https://github.com/berkes/postgres_key_value'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
|
17
|
+
|
18
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
|
19
|
+
|
20
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
21
|
+
spec.metadata['source_code_uri'] = 'https://github.com/berkes/postgres_key_value'
|
22
|
+
|
23
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features|benchmark)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = 'exe'
|
27
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ['lib']
|
29
|
+
end
|
data.tar.gz.sig
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: postgres_key_value
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- 'Bèr Kessels
|
8
|
+
|
9
|
+
'
|
10
|
+
autorequire:
|
11
|
+
bindir: exe
|
12
|
+
cert_chain:
|
13
|
+
- |
|
14
|
+
-----BEGIN CERTIFICATE-----
|
15
|
+
MIIDHDCCAgSgAwIBAgIBATANBgkqhkiG9w0BAQsFADAcMRowGAYDVQQDDBFiZXIv
|
16
|
+
REM9YmVyay9EQz1lczAeFw0yMjAzMTUxMTI5MzRaFw0yMzAzMTUxMTI5MzRaMBwx
|
17
|
+
GjAYBgNVBAMMEWJlci9EQz1iZXJrL0RDPWVzMIIBIjANBgkqhkiG9w0BAQEFAAOC
|
18
|
+
AQ8AMIIBCgKCAQEAsXQSBxd/3i1bUOMduBWCQ71XIaLTTGhYdCWe2m19iT+43SKh
|
19
|
+
2Eb35bTZkRnGI5gTJGo85nmdbjIJSAByUAVCEji46zf9i2wCpoSVZpFsDdsw2mAC
|
20
|
+
/GTGx7TVReLbDzjLw1G2z9vTO123q65vOxv8dEMjObtXhz21dlW9TdzxC9lODRoB
|
21
|
+
mPIguPdfQf69B1zYS6Ai/hK/oPEOj7Od18RCnlF1+Xe3ew8+R5nVZRG1H5IIU562
|
22
|
+
7OgPgmtmBJrTEgt1bpZvALXGl8f4HPkkEs+BxnTP5Uju1fPLtHPXLXiP7nm2Em5q
|
23
|
+
9xKjJwlRn8HGyesKaPMBNxTmHTBf5G5VHpuv8wIDAQABo2kwZzAJBgNVHRMEAjAA
|
24
|
+
MAsGA1UdDwQEAwIEsDAdBgNVHQ4EFgQU7UHvUt2G+emOGVsmsBRLOserCz0wFgYD
|
25
|
+
VR0RBA8wDYELYmVyQGJlcmsuZXMwFgYDVR0SBA8wDYELYmVyQGJlcmsuZXMwDQYJ
|
26
|
+
KoZIhvcNAQELBQADggEBAErrSFPXfRC8VF8wPIxKAOhah/pqryBLqzYW4+E3LpN0
|
27
|
+
AzcDoHT9Iq5vXMZNzuEE/IhtnUvPf/MjplH0o17d+qbAGYbR8nSWKd4PSvm9an17
|
28
|
+
wQbn4MnJX0IyVVsKmqHV6M1ZRHqZ5yFz05OW9lboOT2Tn2wWmy3ygU/iue1GfVpg
|
29
|
+
hyrjlnG4hkaQxTN+nzpO+OpBLshD+hexjeAQb5WEme1lESO8xXN2182QVmdFwRv1
|
30
|
+
zmmrAjmKS0AWvpoxyX6PK0q5Z0PbMELNaTXQtsgXBhUITwSXyyrWQzMHZoco1LhH
|
31
|
+
0Xd8MPh6al9zwQaMQchpSYsHoOX8zAJW2udyDdyylJo=
|
32
|
+
-----END CERTIFICATE-----
|
33
|
+
date: 2022-03-15 00:00:00.000000000 Z
|
34
|
+
dependencies: []
|
35
|
+
description: Performant and simple key-value storage in Posgresql. With
|
36
|
+
a Hash-like interface. Only dependency is pg gem
|
37
|
+
email:
|
38
|
+
- ber@berk.es
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- ".env.template"
|
44
|
+
- ".github/workflows/main.yml"
|
45
|
+
- ".gitignore"
|
46
|
+
- ".rubocop.yml"
|
47
|
+
- CODE_OF_CONDUCT.md
|
48
|
+
- Gemfile
|
49
|
+
- Gemfile.lock
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- bin/console
|
54
|
+
- bin/setup
|
55
|
+
- lib/postgres_key_value.rb
|
56
|
+
- lib/postgres_key_value/errors.rb
|
57
|
+
- lib/postgres_key_value/store.rb
|
58
|
+
- lib/postgres_key_value/utils.rb
|
59
|
+
- lib/postgres_key_value/version.rb
|
60
|
+
- postgres_key_value.gemspec
|
61
|
+
homepage: https://github.com/berkes/postgres_key_value
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
metadata:
|
65
|
+
allowed_push_host: https://rubygems.org/
|
66
|
+
homepage_uri: https://github.com/berkes/postgres_key_value
|
67
|
+
source_code_uri: https://github.com/berkes/postgres_key_value
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 2.4.0
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubygems_version: 3.2.3
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: Key-Value storage for Posgresql
|
87
|
+
test_files: []
|
metadata.gz.sig
ADDED
Binary file
|