bmg-redis 0.20.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/Rakefile +11 -0
- data/lib/bmg/redis/relation.rb +111 -0
- data/lib/bmg/redis/serializer/json.rb +18 -0
- data/lib/bmg/redis/serializer/marshal.rb +17 -0
- data/lib/bmg/redis/serializer.rb +8 -0
- data/lib/bmg/redis.rb +4 -0
- data/tasks/gem.rake +39 -0
- data/tasks/test.rake +24 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2eb3aa2cf27c4636c1a5f4ba3c8e285ff3a7637b3aba741af20c33fafdd52c08
|
4
|
+
data.tar.gz: c41d2a45cbaabd4805fb85d071aa381c3e4931e31ec6c2a7cf2145c27d2c6882
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 01eea86c3261aad9cf80f60b9b72e7293c12e0a49fa976e260c18ff0a7f164c14ed1f9b1e867097972cd6120b29b43b54033183826322dfe3d6c548a672cdfa9
|
7
|
+
data.tar.gz: ebfb639103f4fc25fc606cd54d26d247b22dcc1315d4c46ca4ec63497244dc6fc7c93183e0252c62a012909d8d53486945f681dc2dec38e9f537b0b4c54f91d6
|
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
module Bmg
|
2
|
+
module Redis
|
3
|
+
class Relation
|
4
|
+
include Bmg::Relation
|
5
|
+
|
6
|
+
DEFAULT_OPTIONS = {
|
7
|
+
serializer: :marshal
|
8
|
+
}
|
9
|
+
|
10
|
+
def initialize(type, options)
|
11
|
+
@type = type
|
12
|
+
@options = DEFAULT_OPTIONS.merge(options)
|
13
|
+
|
14
|
+
serializer!
|
15
|
+
candidate_key!
|
16
|
+
end
|
17
|
+
attr_accessor :type
|
18
|
+
|
19
|
+
attr_reader :options
|
20
|
+
protected :options
|
21
|
+
|
22
|
+
def each
|
23
|
+
return to_enum unless block_given?
|
24
|
+
|
25
|
+
redis.scan_each(match: "#{key_prefix}:*") do |key|
|
26
|
+
tuple_str = redis.get(key)
|
27
|
+
tuple = serializer.deserialize(tuple_str)
|
28
|
+
yield(tuple)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
###
|
33
|
+
|
34
|
+
def insert(tuple_or_tuples)
|
35
|
+
case tuple_or_tuples
|
36
|
+
when Hash
|
37
|
+
key = extract_key(tuple_or_tuples)
|
38
|
+
serialized = serializer.serialize(tuple_or_tuples)
|
39
|
+
redis.set(key, serialized)
|
40
|
+
else
|
41
|
+
tuple_or_tuples.each do |tuple|
|
42
|
+
insert(tuple)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
self
|
46
|
+
end
|
47
|
+
|
48
|
+
def update(updating, predicate = Predicate.tautology)
|
49
|
+
each do |tuple|
|
50
|
+
next unless predicate.call(tuple)
|
51
|
+
|
52
|
+
insert(tuple.merge(updating))
|
53
|
+
end
|
54
|
+
self
|
55
|
+
end
|
56
|
+
|
57
|
+
def delete(predicate = Predicate.tautology)
|
58
|
+
keys = self
|
59
|
+
.each
|
60
|
+
.select{|tuple| predicate.call(tuple) }
|
61
|
+
.map{|tuple| extract_key(tuple) }
|
62
|
+
|
63
|
+
redis.del(*keys)
|
64
|
+
|
65
|
+
self
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def serializer
|
71
|
+
@serializer ||= begin
|
72
|
+
case s = options[:serializer] || :marshal
|
73
|
+
when :marshal
|
74
|
+
Serializer::Marshal.new
|
75
|
+
when :json
|
76
|
+
Serializer::Json.new
|
77
|
+
when Serializer
|
78
|
+
s
|
79
|
+
else
|
80
|
+
raise Bmg::Error, "Unknown serializer `#{s}`"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
alias :serializer! :serializer
|
85
|
+
|
86
|
+
def candidate_key
|
87
|
+
@candidate_key ||= begin
|
88
|
+
raise Bmg::Error, "A key must be provided" unless type.knows_keys?
|
89
|
+
type.keys.first
|
90
|
+
end
|
91
|
+
end
|
92
|
+
alias :candidate_key! :candidate_key
|
93
|
+
|
94
|
+
def redis
|
95
|
+
options[:redis]
|
96
|
+
end
|
97
|
+
|
98
|
+
def key_prefix
|
99
|
+
options[:key_prefix] || "bmg"
|
100
|
+
end
|
101
|
+
|
102
|
+
def extract_key(tuple)
|
103
|
+
key = candidate_key
|
104
|
+
key = TupleAlgebra.project(tuple, key)
|
105
|
+
key = serializer.serialize(key)
|
106
|
+
"#{key_prefix}:#{key}"
|
107
|
+
end
|
108
|
+
|
109
|
+
end # class Relation
|
110
|
+
end # module Redis
|
111
|
+
end # module Bmg
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Bmg
|
2
|
+
module Redis
|
3
|
+
class Serializer
|
4
|
+
class Json < Serializer
|
5
|
+
|
6
|
+
def serialize(tuple)
|
7
|
+
tuple.to_json
|
8
|
+
end
|
9
|
+
|
10
|
+
def deserialize(serialized)
|
11
|
+
parsed = JSON.parse(serialized)
|
12
|
+
TupleAlgebra.symbolize_keys(parsed)
|
13
|
+
end
|
14
|
+
|
15
|
+
end # class Json
|
16
|
+
end # class Serializer
|
17
|
+
end # module Redis
|
18
|
+
end # module Bmg
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Bmg
|
2
|
+
module Redis
|
3
|
+
class Serializer
|
4
|
+
class Marshal < Serializer
|
5
|
+
|
6
|
+
def serialize(tuple)
|
7
|
+
::Marshal.dump(tuple)
|
8
|
+
end
|
9
|
+
|
10
|
+
def deserialize(serialized)
|
11
|
+
::Marshal.load(serialized)
|
12
|
+
end
|
13
|
+
|
14
|
+
end # class Marshal
|
15
|
+
end # class Serializer
|
16
|
+
end # module Redis
|
17
|
+
end # module Bmg
|
data/lib/bmg/redis.rb
ADDED
data/tasks/gem.rake
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'rubygems/package_task'
|
2
|
+
|
3
|
+
# Dynamically load the gem spec
|
4
|
+
gemspec_file = File.expand_path('../../bmg-redis.gemspec', __FILE__)
|
5
|
+
gemspec = Kernel.eval(File.read(gemspec_file))
|
6
|
+
|
7
|
+
Gem::PackageTask.new(gemspec) do |t|
|
8
|
+
|
9
|
+
# Name of the package
|
10
|
+
t.name = gemspec.name
|
11
|
+
|
12
|
+
# Version of the package
|
13
|
+
t.version = gemspec.version
|
14
|
+
|
15
|
+
# Directory used to store the package files
|
16
|
+
t.package_dir = "pkg"
|
17
|
+
|
18
|
+
# True if a gzipped tar file (tgz) should be produced
|
19
|
+
t.need_tar = false
|
20
|
+
|
21
|
+
# True if a gzipped tar file (tar.gz) should be produced
|
22
|
+
t.need_tar_gz = false
|
23
|
+
|
24
|
+
# True if a bzip2'd tar file (tar.bz2) should be produced
|
25
|
+
t.need_tar_bz2 = false
|
26
|
+
|
27
|
+
# True if a zip file should be produced (default is false)
|
28
|
+
t.need_zip = false
|
29
|
+
|
30
|
+
# List of files to be included in the package.
|
31
|
+
t.package_files = gemspec.files
|
32
|
+
|
33
|
+
# Tar command for gzipped or bzip2ed archives.
|
34
|
+
t.tar_command = "tar"
|
35
|
+
|
36
|
+
# Zip command for zipped archives.
|
37
|
+
t.zip_command = "zip"
|
38
|
+
|
39
|
+
end
|
data/tasks/test.rake
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
namespace :test do
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
tests = []
|
5
|
+
|
6
|
+
desc "Runs unit tests"
|
7
|
+
RSpec::Core::RakeTask.new(:unit) do |t|
|
8
|
+
t.pattern = "spec/unit/**/test_*.rb"
|
9
|
+
t.rspec_opts = ["-Ilib", "-Ispec/unit", "--color", "--backtrace", "--format=progress"]
|
10
|
+
end
|
11
|
+
tests << :unit
|
12
|
+
|
13
|
+
desc "Runs integration tests"
|
14
|
+
RSpec::Core::RakeTask.new(:integration) do |t|
|
15
|
+
t.pattern = "spec/integration/**/test_*.rb"
|
16
|
+
t.rspec_opts = ["-Ilib", "-Ispec/integration", "--color", "--backtrace", "--format=progress"]
|
17
|
+
end
|
18
|
+
tests << :integration
|
19
|
+
|
20
|
+
task :all => tests
|
21
|
+
end
|
22
|
+
|
23
|
+
desc "Runs all tests"
|
24
|
+
task :test => :'test:all'
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bmg-redis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.20.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bernard Lambeau
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-05-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bmg
|
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
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: redis
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.6'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '13'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '13'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.6'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.6'
|
69
|
+
description: bmg-redis provides an adapter to expose redis databases as relations
|
70
|
+
email: blambeau@gmail.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- Gemfile
|
76
|
+
- Rakefile
|
77
|
+
- lib/bmg/redis.rb
|
78
|
+
- lib/bmg/redis/relation.rb
|
79
|
+
- lib/bmg/redis/serializer.rb
|
80
|
+
- lib/bmg/redis/serializer/json.rb
|
81
|
+
- lib/bmg/redis/serializer/marshal.rb
|
82
|
+
- tasks/gem.rake
|
83
|
+
- tasks/test.rake
|
84
|
+
homepage: http://github.com/enspirit/bmg-redis
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata: {}
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubygems_version: 3.1.4
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: Expose redis as relations.
|
107
|
+
test_files: []
|