regres 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify the gem's dependencies in regres.gemspec
4
+ gemspec
@@ -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.
@@ -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
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -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__)
@@ -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,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
@@ -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
@@ -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
@@ -0,0 +1,3 @@
1
+ class Regres
2
+ VERSION = "0.0.1"
3
+ 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: []