dm-redis-adapter 0.5.2 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dm-redis-adapter/adapter.rb +21 -9
- data/spec/dm_redis_associations_spec.rb +2 -0
- data/spec/dm_redis_inheritenance_spec.rb +71 -0
- data/spec/dm_redis_spec.rb +3 -3
- metadata +15 -10
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'redis/connection/hiredis'
|
2
2
|
require 'redis'
|
3
3
|
require 'base64'
|
4
|
+
require 'dm-core'
|
4
5
|
|
5
6
|
module DataMapper
|
6
7
|
module Adapters
|
@@ -16,8 +17,9 @@ module DataMapper
|
|
16
17
|
#
|
17
18
|
# @api semipublic
|
18
19
|
def create(resources)
|
20
|
+
storage_name = resources.first.model.storage_name
|
19
21
|
resources.each do |resource|
|
20
|
-
initialize_serial(resource, @redis.incr("#{
|
22
|
+
initialize_serial(resource, @redis.incr("#{storage_name}:#{redis_key_for(resource.model)}:serial"))
|
21
23
|
@redis.sadd(key_set_for(resource.model), resource.key.join)
|
22
24
|
end
|
23
25
|
update_attributes(resources)
|
@@ -36,9 +38,10 @@ module DataMapper
|
|
36
38
|
#
|
37
39
|
# @api semipublic
|
38
40
|
def read(query)
|
41
|
+
storage_name = query.model.storage_name
|
39
42
|
records = records_for(query)
|
40
43
|
records.each do |record|
|
41
|
-
record_data = @redis.hgetall("#{
|
44
|
+
record_data = @redis.hgetall("#{storage_name}:#{record[redis_key_for(query.model)]}")
|
42
45
|
|
43
46
|
query.fields.each do |property|
|
44
47
|
next if query.model.key.include?(property)
|
@@ -89,11 +92,12 @@ module DataMapper
|
|
89
92
|
#
|
90
93
|
# @api semipublic
|
91
94
|
def delete(collection)
|
95
|
+
storage_name = collection.query.model.storage_name
|
92
96
|
collection.each do |record|
|
93
|
-
@redis.del("#{
|
97
|
+
@redis.del("#{storage_name}:#{record[redis_key_for(collection.query.model)]}")
|
94
98
|
@redis.srem(key_set_for(collection.query.model), record[redis_key_for(collection.query.model)])
|
95
99
|
record.model.properties.select {|p| p.index}.each do |p|
|
96
|
-
@redis.srem("#{
|
100
|
+
@redis.srem("#{storage_name}:#{p.name}:#{encode(record[p.name])}", record[redis_key_for(collection.query.model)])
|
97
101
|
end
|
98
102
|
end
|
99
103
|
end
|
@@ -108,12 +112,13 @@ module DataMapper
|
|
108
112
|
#
|
109
113
|
# @api private
|
110
114
|
def update_attributes(resources)
|
115
|
+
storage_name = resources.first.model.storage_name
|
111
116
|
resources.each do |resource|
|
112
117
|
model = resource.model
|
113
118
|
attributes = resource.dirty_attributes
|
114
119
|
|
115
120
|
resource.model.properties.select {|p| p.index}.each do |property|
|
116
|
-
@redis.sadd("#{
|
121
|
+
@redis.sadd("#{storage_name}:#{property.name}:#{encode(resource[property.name.to_s])}", resource.key.first.to_s)
|
117
122
|
end
|
118
123
|
|
119
124
|
properties_to_set = []
|
@@ -129,7 +134,7 @@ module DataMapper
|
|
129
134
|
end
|
130
135
|
end
|
131
136
|
|
132
|
-
hash_key = "#{
|
137
|
+
hash_key = "#{storage_name}:#{resource.key.join}"
|
133
138
|
properties_to_del.each {|prop| @redis.hdel(hash_key, prop) }
|
134
139
|
@redis.hmset(hash_key, *properties_to_set) unless properties_to_set.empty?
|
135
140
|
end
|
@@ -176,7 +181,11 @@ module DataMapper
|
|
176
181
|
#
|
177
182
|
# @api private
|
178
183
|
def perform_query(query, operand)
|
184
|
+
storage_name = query.model.storage_name
|
185
|
+
|
179
186
|
matched_records = []
|
187
|
+
#p 'query: ' + query.inspect
|
188
|
+
#p 'operand: ' + operand.inspect
|
180
189
|
|
181
190
|
if operand.is_a?(DataMapper::Query::Conditions::NotOperation)
|
182
191
|
subject = operand.first.subject
|
@@ -197,6 +206,9 @@ module DataMapper
|
|
197
206
|
subject = subject.child_key.first
|
198
207
|
end
|
199
208
|
|
209
|
+
#p 'subject: ' + subject.inspect
|
210
|
+
#p 'value: ' + value.inspect
|
211
|
+
|
200
212
|
if query.model.key.include?(subject)
|
201
213
|
if operand.is_a?(DataMapper::Query::Conditions::NotOperation)
|
202
214
|
@redis.smembers(key_set_for(query.model)).each do |key|
|
@@ -228,7 +240,7 @@ module DataMapper
|
|
228
240
|
end
|
229
241
|
else # worst case, loop through each record and match
|
230
242
|
@redis.smembers(key_set_for(query.model)).each do |key|
|
231
|
-
if operand.matches?(subject.typecast(@redis.hget("#{
|
243
|
+
if operand.matches?(subject.typecast(@redis.hget("#{storage_name}:#{key}", subject.name)))
|
232
244
|
matched_records << {redis_key_for(query.model) => key}
|
233
245
|
end
|
234
246
|
end
|
@@ -257,7 +269,7 @@ module DataMapper
|
|
257
269
|
# The string key for the :all set
|
258
270
|
# @api private
|
259
271
|
def key_set_for(model)
|
260
|
-
"#{model.
|
272
|
+
"#{model.storage_name}:#{redis_key_for(model)}:all"
|
261
273
|
end
|
262
274
|
|
263
275
|
##
|
@@ -267,7 +279,7 @@ module DataMapper
|
|
267
279
|
# Array of id's of all members for an indexed field
|
268
280
|
# @api private
|
269
281
|
def find_indexed_matches(subject, value)
|
270
|
-
@redis.smembers("#{subject.model.
|
282
|
+
@redis.smembers("#{subject.model.storage_name}:#{subject.name}:#{encode(value)}").map {|id| id.to_i}
|
271
283
|
end
|
272
284
|
|
273
285
|
##
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DataMapper::Adapters::RedisAdapter do
|
4
|
+
before(:all) do
|
5
|
+
@adapter = DataMapper.setup(:default, {
|
6
|
+
:adapter => "redis",
|
7
|
+
:db => 15
|
8
|
+
})
|
9
|
+
@repository = DataMapper.repository(@adapter.name)
|
10
|
+
@redis = Redis.new(:db => 15)
|
11
|
+
end
|
12
|
+
describe 'Inheritance' do
|
13
|
+
|
14
|
+
before :all do
|
15
|
+
module MINT
|
16
|
+
class Person
|
17
|
+
include DataMapper::Resource
|
18
|
+
|
19
|
+
property :name, String
|
20
|
+
property :job, String, :length => 1..255
|
21
|
+
property :classtype, Discriminator
|
22
|
+
property :id, Serial
|
23
|
+
end
|
24
|
+
|
25
|
+
class Male < Person; end
|
26
|
+
class Father < Male; end
|
27
|
+
class Son < Male; end
|
28
|
+
|
29
|
+
class Woman < Person; end
|
30
|
+
class Mother < Woman; end
|
31
|
+
class Daughter < Woman; end
|
32
|
+
end
|
33
|
+
DataMapper.finalize
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should select all women, mothers, and daughters based on Woman query' do
|
38
|
+
|
39
|
+
w = MINT::Woman.create(:name => "woman")
|
40
|
+
m = MINT::Mother.create(:name => "mother")
|
41
|
+
d = MINT::Daughter.create(:name => "daughter")
|
42
|
+
|
43
|
+
r = MINT::Woman.all
|
44
|
+
r.to_set.should == [w,m,d].to_set
|
45
|
+
r.size.should == [w,m,d].size
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should select all women, mothers, and daughters based on Person query' do
|
49
|
+
w = MINT::Woman.create(:name => "woman")
|
50
|
+
m = MINT::Mother.create(:name => "mother")
|
51
|
+
d = MINT::Daughter.create(:name => "daughter")
|
52
|
+
p = MINT::Person.all
|
53
|
+
p.to_set.should == [w,m,d].to_set
|
54
|
+
p.size.should == [w,m,d].size
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should select all mothers' do
|
58
|
+
w = MINT::Woman.create(:name => "woman")
|
59
|
+
m = MINT::Mother.create(:name => "mother")
|
60
|
+
d = MINT::Daughter.create(:name => "daughter")
|
61
|
+
|
62
|
+
mo = MINT::Mother.all
|
63
|
+
mo.to_set.should == [m].to_set
|
64
|
+
mo.size.should == [m].size
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
after(:each) do
|
69
|
+
@redis.flushdb
|
70
|
+
end
|
71
|
+
end
|
data/spec/dm_redis_spec.rb
CHANGED
@@ -26,17 +26,17 @@ describe DataMapper::Adapters::RedisAdapter do
|
|
26
26
|
|
27
27
|
it "should save the id of the resource in a set" do
|
28
28
|
h = Hooloovoo.create
|
29
|
-
@redis.smembers("
|
29
|
+
@redis.smembers("hooloovoos:id:all").should == [h.id.to_s]
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should save indexed fields in a set by Base64 encoding the value" do
|
33
33
|
h = Hooloovoo.create(:shade => '336699')
|
34
|
-
@redis.smembers("
|
34
|
+
@redis.smembers("hooloovoos:shade:MzM2Njk5").should == [h.id.to_s]
|
35
35
|
end
|
36
36
|
|
37
37
|
it "should create a hash of properties for the resource" do
|
38
38
|
h = Hooloovoo.create(:iq => '4069')
|
39
|
-
@redis.hmget("
|
39
|
+
@redis.hmget("hooloovoos:#{h.id}", 'iq').should == ['4069']
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dm-redis-adapter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,12 +9,12 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-08 00:00:00.000000000 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: dm-core
|
17
|
-
requirement: &
|
17
|
+
requirement: &70343134239820 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: 1.2.0
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *70343134239820
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: dm-types
|
28
|
-
requirement: &
|
28
|
+
requirement: &70343134210860 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: 1.2.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *70343134210860
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: hiredis
|
39
|
-
requirement: &
|
39
|
+
requirement: &70343134209560 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ~>
|
@@ -44,10 +44,10 @@ dependencies:
|
|
44
44
|
version: 0.3.0
|
45
45
|
type: :runtime
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *70343134209560
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: redis
|
50
|
-
requirement: &
|
50
|
+
requirement: &70343134205940 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ~>
|
@@ -55,7 +55,7 @@ dependencies:
|
|
55
55
|
version: '2.2'
|
56
56
|
type: :runtime
|
57
57
|
prerelease: false
|
58
|
-
version_requirements: *
|
58
|
+
version_requirements: *70343134205940
|
59
59
|
description: DataMapper adapter for the Redis key-value database
|
60
60
|
email: whoahbot@gmail.com
|
61
61
|
executables: []
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- lib/dm-redis-adapter/spec/setup.rb
|
72
72
|
- spec/dm_redis_associations_spec.rb
|
73
73
|
- spec/dm_redis_finding_spec.rb
|
74
|
+
- spec/dm_redis_inheritenance_spec.rb
|
74
75
|
- spec/dm_redis_spec.rb
|
75
76
|
- spec/dm_redis_validations_spec.rb
|
76
77
|
- spec/spec_helper.rb
|
@@ -89,6 +90,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
89
90
|
- - ! '>='
|
90
91
|
- !ruby/object:Gem::Version
|
91
92
|
version: '0'
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
hash: 4147115164932724252
|
92
96
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
97
|
none: false
|
94
98
|
requirements:
|
@@ -104,6 +108,7 @@ summary: DataMapper adapter for the Redis key-value database
|
|
104
108
|
test_files:
|
105
109
|
- spec/dm_redis_associations_spec.rb
|
106
110
|
- spec/dm_redis_finding_spec.rb
|
111
|
+
- spec/dm_redis_inheritenance_spec.rb
|
107
112
|
- spec/dm_redis_spec.rb
|
108
113
|
- spec/dm_redis_validations_spec.rb
|
109
114
|
- spec/spec_helper.rb
|