Class: Bmg::Redis::Relation
- Inherits:
-
Object
- Object
- Bmg::Redis::Relation
- Includes:
- Bmg::Relation
- Defined in:
- lib/bmg/redis/relation.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ serializer: :marshal }
Instance Attribute Summary collapse
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
-
#_restrict(type, predicate) ⇒ Object
optimization.
- #delete(predicate = Predicate.tautology) ⇒ Object
- #each ⇒ Object
-
#initialize(type, options) ⇒ Relation
constructor
A new instance of Relation.
- #insert(tuple_or_tuples) ⇒ Object
- #update(updating, predicate = Predicate.tautology) ⇒ Object
Constructor Details
#initialize(type, options) ⇒ Relation
Returns a new instance of Relation.
10 11 12 13 14 15 16 |
# File 'lib/bmg/redis/relation.rb', line 10 def initialize(type, ) @type = type @options = DEFAULT_OPTIONS.merge() serializer! candidate_key! end |
Instance Attribute Details
#type ⇒ Object
Returns the value of attribute type.
17 18 19 |
# File 'lib/bmg/redis/relation.rb', line 17 def type @type end |
Instance Method Details
#_restrict(type, predicate) ⇒ Object
optimization
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/bmg/redis/relation.rb', line 34 def _restrict(type, predicate) return super unless key = full_and_only_key?(predicate) redis_key = extract_key(key) if tuple_str = redis.get(redis_key) tuple = serializer.deserialize(tuple_str) Singleton.new(type, self, tuple) else Bmg::Relation.empty end end |
#delete(predicate = Predicate.tautology) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/bmg/redis/relation.rb', line 77 def delete(predicate = Predicate.tautology) keys = restrict(predicate).each.map{|t| extract_key(t) } redis.multi do |transaction| keys.each_slice(1000) do |slice| transaction.del(*slice) end end self end |
#each ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/bmg/redis/relation.rb', line 22 def each return to_enum unless block_given? redis.scan_each(match: "#{key_prefix}:*") do |key| tuple_str = redis.get(key) tuple = serializer.deserialize(tuple_str) yield(tuple) end end |
#insert(tuple_or_tuples) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/bmg/redis/relation.rb', line 48 def insert(tuple_or_tuples) case tuple_or_tuples when Hash insert_one(tuple_or_tuples, redis) else redis.multi do |transaction| tuple_or_tuples.each do |tuple| insert_one(tuple, transaction) end end end self end |
#update(updating, predicate = Predicate.tautology) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/bmg/redis/relation.rb', line 70 def update(updating, predicate = Predicate.tautology) updates = restrict(predicate).map do |tuple| tuple.merge(updating) end insert(updates) end |