regres 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +84 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/edm.gemspec +28 -0
- data/lib/regres.rb +28 -0
- data/lib/regres/connection.rb +41 -0
- data/lib/regres/table.rb +44 -0
- data/lib/regres/version.rb +3 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e312522da0cab27dd8fd7262306fa9abe5217705
|
4
|
+
data.tar.gz: be572b4191f9473cfee1f6b9a2e5c84b1dc547a4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d20b5df1455daffd9d8523cf4a005e51697557902e50a4d9be7b7218c350343108b25a1cc6e807f122415ebb8a530a86a5ff8437bbf5a092ad6c3e8ab93187bb
|
7
|
+
data.tar.gz: e9b22c8b22d66a8cef54c44c14dba14c129af758684e61e4945790a6186402a4f6ec1325eafaa521fc575b14f9ff008b87587e594d73bdf32e8b1331f417c634
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Peter Cooper
|
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,84 @@
|
|
1
|
+
# Regres – Simple Redis-like Data Storage on Postgres
|
2
|
+
|
3
|
+
A prototype Ruby library for presenting a simple data storage interface (initially just key/value), backed by Postgres and inspired by Redis.
|
4
|
+
|
5
|
+
## Simple Example
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
db = Regres.new
|
9
|
+
db.set("mykey", "hello, world")
|
10
|
+
db.get("mykey") # => "hello, world"
|
11
|
+
```
|
12
|
+
|
13
|
+
Note that all Regres needs is an accessible Postgres database. By default it uses the `regres_default` table and will create this if it doesn't exist.
|
14
|
+
|
15
|
+
The motivation is that I love Redis and its API but there are places where I want the same ease of use backed by a standard, persistent RDBMS, such as when building simple Heroku apps (where a decent sized Redis database is expensive).
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'regres'
|
23
|
+
```
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
$ bundle
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
$ gem install regres
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
To connect:
|
36
|
+
|
37
|
+
db = Regres.new
|
38
|
+
|
39
|
+
Or, more elaborately:
|
40
|
+
|
41
|
+
db = Regres.new("postgres://user:pass@host:5432/dbname")
|
42
|
+
# ... do stuff ...
|
43
|
+
db.close
|
44
|
+
|
45
|
+
*Note: If the `DATABASE_URL` environment variable exists, this will be used if no connection URL is specified. Alternatively, it will default to the current user on `localhost`, as the Pg gem does.*
|
46
|
+
|
47
|
+
Then issue commands:
|
48
|
+
|
49
|
+
db.set(key, value)
|
50
|
+
db.get(key)
|
51
|
+
db.del(key)
|
52
|
+
|
53
|
+
Those are all of the commands for now.
|
54
|
+
|
55
|
+
## TODOs
|
56
|
+
|
57
|
+
Some things to work on next:
|
58
|
+
|
59
|
+
* ~~Basic key/value storage with strings~~
|
60
|
+
* `keys` command
|
61
|
+
* `rename` command
|
62
|
+
* `exists` command
|
63
|
+
* Support `inc` and `decr` style manipulation of values
|
64
|
+
* Support Redis-style expiry times for keys
|
65
|
+
* Use json columns to support more complex data structures (e.g. hashes)
|
66
|
+
* Support lists via Postgres arrays(?)
|
67
|
+
* Support sets via Postgres arrays(?)
|
68
|
+
* Better support multiple tables
|
69
|
+
* Could this be a drop-in/substitute for the Redis client library with the right API?
|
70
|
+
|
71
|
+
## Development
|
72
|
+
|
73
|
+
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.
|
74
|
+
|
75
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
76
|
+
|
77
|
+
## Contributing
|
78
|
+
|
79
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/peterc/regres.
|
80
|
+
|
81
|
+
## License
|
82
|
+
|
83
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
84
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "regres"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/edm.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'regres/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "regres"
|
8
|
+
spec.version = Regres::VERSION
|
9
|
+
spec.authors = ["Peter Cooper"]
|
10
|
+
spec.email = ["git@peterc.org"]
|
11
|
+
|
12
|
+
spec.summary = %q{Easy schemaless key/value storage on top of Postgres.}
|
13
|
+
spec.homepage = "https://github.com/peterc/regres"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
26
|
+
|
27
|
+
spec.add_dependency "pg", "~> 1.0.0"
|
28
|
+
end
|
data/lib/regres.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require "pg"
|
2
|
+
|
3
|
+
require "regres/version"
|
4
|
+
require "regres/connection"
|
5
|
+
require "regres/table"
|
6
|
+
|
7
|
+
require "forwardable"
|
8
|
+
|
9
|
+
class Regres
|
10
|
+
extend Forwardable
|
11
|
+
|
12
|
+
attr_reader :connection, :table
|
13
|
+
|
14
|
+
def initialize(options = {})
|
15
|
+
defaults = {
|
16
|
+
table: 'regres_default'
|
17
|
+
}
|
18
|
+
|
19
|
+
@options = defaults.merge(options)
|
20
|
+
|
21
|
+
@connection = Connection.new(@options[:url])
|
22
|
+
|
23
|
+
@table = Table.new(@connection, @options[:table])
|
24
|
+
end
|
25
|
+
|
26
|
+
def_delegators :@table, :get, :set, :del
|
27
|
+
def_delegators :@connection, :close
|
28
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'forwardable'
|
3
|
+
|
4
|
+
class Regres
|
5
|
+
class Connection
|
6
|
+
extend Forwardable
|
7
|
+
|
8
|
+
attr_reader :connection
|
9
|
+
|
10
|
+
def self.parse_url(url)
|
11
|
+
uri = URI(url)
|
12
|
+
|
13
|
+
{
|
14
|
+
user: uri.user,
|
15
|
+
password: uri.password,
|
16
|
+
host: uri.host,
|
17
|
+
port: uri.port || 5432,
|
18
|
+
dbname: uri.path.sub(/^\//, '')
|
19
|
+
}
|
20
|
+
rescue
|
21
|
+
{}
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(url = nil)
|
25
|
+
defaults = {
|
26
|
+
user: ENV['USER'],
|
27
|
+
dbname: ENV['USER'],
|
28
|
+
host: "localhost",
|
29
|
+
sslmode: "prefer"
|
30
|
+
}
|
31
|
+
|
32
|
+
url ||= ENV['DATABASE_URL'] if ENV['DATABASE_URL'].to_s.start_with?('postgres')
|
33
|
+
|
34
|
+
options = defaults.merge(self.class.parse_url(url))
|
35
|
+
|
36
|
+
@connection = PG.connect(options)
|
37
|
+
end
|
38
|
+
|
39
|
+
def_delegators :@connection, :query, :close
|
40
|
+
end
|
41
|
+
end
|
data/lib/regres/table.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
class Regres
|
2
|
+
class Table
|
3
|
+
def initialize(connection, name)
|
4
|
+
@connection = connection
|
5
|
+
@name = name[/^[\w\_\-]+/]
|
6
|
+
|
7
|
+
create unless exists?
|
8
|
+
end
|
9
|
+
|
10
|
+
def create
|
11
|
+
@connection.query(%{
|
12
|
+
CREATE TABLE #{@name} (k varchar(255) primary key, v text, unique (k))
|
13
|
+
})
|
14
|
+
end
|
15
|
+
|
16
|
+
def exists?
|
17
|
+
res = @connection.query(%{SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = $1)}, [@name])
|
18
|
+
|
19
|
+
res.first == {"exists" => "t"}
|
20
|
+
end
|
21
|
+
|
22
|
+
def set(k, v)
|
23
|
+
@connection.query(%{
|
24
|
+
INSERT INTO #{@name} (k, v) VALUES ($1, $2)
|
25
|
+
ON CONFLICT (k) DO UPDATE SET v = $2 WHERE #{@name}.k = $1
|
26
|
+
}, [k, v])
|
27
|
+
|
28
|
+
v
|
29
|
+
end
|
30
|
+
|
31
|
+
def get(k)
|
32
|
+
res = @connection.query(%{
|
33
|
+
SELECT v FROM #{@name} WHERE k = $1
|
34
|
+
}, [k]).first
|
35
|
+
|
36
|
+
res && res['v']
|
37
|
+
end
|
38
|
+
|
39
|
+
def del(k)
|
40
|
+
@connection.query(%{DELETE FROM #{@name} WHERE k = $1}, [k])
|
41
|
+
true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: regres
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peter Cooper
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pg
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.0.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.0.0
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- git@peterc.org
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- bin/console
|
82
|
+
- bin/setup
|
83
|
+
- edm.gemspec
|
84
|
+
- lib/regres.rb
|
85
|
+
- lib/regres/connection.rb
|
86
|
+
- lib/regres/table.rb
|
87
|
+
- lib/regres/version.rb
|
88
|
+
homepage: https://github.com/peterc/regres
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.6.10
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Easy schemaless key/value storage on top of Postgres.
|
112
|
+
test_files: []
|