bmg-redis 0.20.4 → 0.21.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 56b8ae766b08e3a5fa2cedb417e894c478b7c023
4
- data.tar.gz: e068691483f8b84339b81463545e1cf7c0cfe638
3
+ metadata.gz: '07189d7515f70a5d013486141404708cc364906f'
4
+ data.tar.gz: ded99a9c7d8ee20fd20e1566e2cd1029c06c4e11
5
5
  SHA512:
6
- metadata.gz: d6c6f00d24b51edfff7bc64e7e38afcfd7e1a3ca6ba70a7e3c8b84d0da43717015a0b1dc5fb06afbff6b6cb506f831c59e914876af78bc9a3db09486c5e559ae
7
- data.tar.gz: c494eafb748f8057a3d92530c5950ad2ef41230a7cd3670c6daed63ee968b39e6f9fe1f5828000942cea4825d2614a1ca979189cb52d186a535530290efa0d84
6
+ metadata.gz: c341c8049360086c7e1ce5ec14c1c15e33b079440e6cb1604072a61eb10f644029db5b4f2846e1ba6707bfab54712a82bf0181766f461bce7060abce1a2d6782
7
+ data.tar.gz: 4227490dfb2d323ffffb4924a5c9a6c2328662f50bbc8ea285e25df7d888a89126e584a71373ff745337dbf534a5134925e31515f823f243ced5e7abd703faea
@@ -29,39 +29,58 @@ module Bmg
29
29
  end
30
30
  end
31
31
 
32
+ ### optimization
33
+
34
+ def _restrict(type, predicate)
35
+ return super unless key = full_and_only_key?(predicate)
36
+
37
+ redis_key = extract_key(key)
38
+ if tuple_str = redis.get(redis_key)
39
+ tuple = serializer.deserialize(tuple_str)
40
+ Singleton.new(type, self, tuple)
41
+ else
42
+ Bmg::Relation.empty
43
+ end
44
+ end
45
+
32
46
  ###
33
47
 
34
48
  def insert(tuple_or_tuples)
35
49
  case tuple_or_tuples
36
50
  when Hash
37
- key = extract_key(tuple_or_tuples)
38
- serialized = serializer.serialize(tuple_or_tuples)
39
- redis.set(key, serialized)
51
+ insert_one(tuple_or_tuples, redis)
40
52
  else
41
- tuple_or_tuples.each do |tuple|
42
- insert(tuple)
53
+ redis.multi do |transaction|
54
+ tuple_or_tuples.each do |tuple|
55
+ insert_one(tuple, transaction)
56
+ end
43
57
  end
44
58
  end
45
59
  self
46
60
  end
47
61
 
48
- def update(updating, predicate = Predicate.tautology)
49
- each do |tuple|
50
- next unless predicate.call(tuple)
62
+ def insert_one(tuple, redis)
63
+ key = extract_key(tuple)
64
+ serialized = serializer.serialize(tuple)
65
+ redis.set(key, serialized)
66
+ self
67
+ end
68
+ private :insert_one
51
69
 
52
- insert(tuple.merge(updating))
70
+ def update(updating, predicate = Predicate.tautology)
71
+ updates = restrict(predicate).map do |tuple|
72
+ tuple.merge(updating)
53
73
  end
54
- self
74
+ insert(updates)
55
75
  end
56
76
 
57
77
  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
-
78
+ keys = restrict(predicate).each.map{|t| extract_key(t) }
79
+ redis.multi do |transaction|
80
+ keys.each_slice(1000) do |slice|
81
+ transaction.del(*slice)
82
+ end
83
+ end
65
84
  self
66
85
  end
67
86
 
@@ -99,10 +118,25 @@ module Bmg
99
118
  options[:key_prefix] || "bmg"
100
119
  end
101
120
 
121
+ def full_and_only_key?(predicate)
122
+ h = begin
123
+ predicate.to_hash
124
+ rescue ArgumentError
125
+ return false
126
+ end
127
+
128
+ return false unless h.keys == candidate_key
129
+ return false unless candidate_key.all?{|k|
130
+ h[k].is_a?(String) || h[k].is_a?(Integer)
131
+ }
132
+
133
+ h
134
+ end
135
+
102
136
  def extract_key(tuple)
103
137
  key = candidate_key
104
138
  key = TupleAlgebra.project(tuple, key)
105
- key = serializer.serialize(key)
139
+ key = key.to_json
106
140
  "#{key_prefix}:#{key}"
107
141
  end
108
142
 
@@ -0,0 +1,35 @@
1
+ module Bmg
2
+ module Redis
3
+ class Singleton
4
+ include Bmg::Relation
5
+
6
+ def initialize(type, parent, tuple)
7
+ @type = type
8
+ @parent = parent
9
+ @tuple = tuple
10
+ end
11
+ attr_accessor :type
12
+
13
+ def each
14
+ return to_enum unless block_given?
15
+
16
+ yield(@tuple) if @tuple
17
+ end
18
+
19
+ def insert(tuple_or_tuples)
20
+ @parent.insert(tuple_or_tuples)
21
+ self
22
+ end
23
+
24
+ def update(updating, predicate = Predicate.tautology)
25
+ @parent.update(updating, predicate & type.predicate)
26
+ self
27
+ end
28
+
29
+ def delete(predicate = Predicate.tautology)
30
+ @parent.delete(predicate & type.predicate)
31
+ end
32
+
33
+ end # class Singleton
34
+ end # module Redis
35
+ end # module Bmg
data/lib/bmg/redis.rb CHANGED
@@ -2,3 +2,4 @@ require 'redis'
2
2
  require 'json'
3
3
  require_relative 'redis/serializer'
4
4
  require_relative 'redis/relation'
5
+ require_relative 'redis/singleton'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bmg-redis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.20.4
4
+ version: 0.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bernard Lambeau
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-15 00:00:00.000000000 Z
11
+ date: 2023-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bmg
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 0.20.4
19
+ version: 0.21.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 0.20.4
26
+ version: 0.21.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: redis
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -85,6 +85,7 @@ files:
85
85
  - lib/bmg/redis/serializer.rb
86
86
  - lib/bmg/redis/serializer/json.rb
87
87
  - lib/bmg/redis/serializer/marshal.rb
88
+ - lib/bmg/redis/singleton.rb
88
89
  - tasks/gem.rake
89
90
  - tasks/test.rake
90
91
  homepage: http://github.com/enspirit/bmg-redis