dm-redis-adapter 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/Rakefile +1 -1
- data/lib/dm-redis-adapter/adapter.rb +28 -6
- data/spec/textual_keys_spec.rb +37 -11
- metadata +41 -32
data/Gemfile
CHANGED
data/Rakefile
CHANGED
@@ -28,7 +28,7 @@ begin
|
|
28
28
|
gemspec.authors = AUTHORS
|
29
29
|
gemspec.add_dependency "dm-core", ">= 1.2.0"
|
30
30
|
gemspec.add_dependency "dm-types", ">= 1.2.0"
|
31
|
-
gemspec.add_dependency "hiredis", "~> 0.
|
31
|
+
gemspec.add_dependency "hiredis", "~> 0.4.0"
|
32
32
|
gemspec.add_dependency "redis", "~> 2.2"
|
33
33
|
gemspec.files = %w(MIT-LICENSE README.textile Rakefile) + Dir.glob("{lib,spec}/**/*")
|
34
34
|
gemspec.has_rdoc = false
|
@@ -43,7 +43,7 @@ module DataMapper
|
|
43
43
|
record_data = @redis.hgetall("#{storage_name}:#{record[redis_key_for(query.model)]}")
|
44
44
|
|
45
45
|
query.fields.each do |property|
|
46
|
-
next if query.model.key.include?(property)
|
46
|
+
next if query.model.key.include?(property) and query.model.key.size == 1
|
47
47
|
|
48
48
|
name = property.name.to_s
|
49
49
|
value = record_data[name]
|
@@ -140,6 +140,27 @@ module DataMapper
|
|
140
140
|
end
|
141
141
|
end
|
142
142
|
|
143
|
+
def is_composed_key_based_query?(query)
|
144
|
+
fields_length = query.model.key.size
|
145
|
+
is_key = query.condition_properties.select { |field| field.key? }
|
146
|
+
(fields_length >1 and fields_length == is_key.length)
|
147
|
+
end
|
148
|
+
|
149
|
+
def key_query(query)
|
150
|
+
matched_records = []
|
151
|
+
value =""
|
152
|
+
storage_name = query.model.storage_name
|
153
|
+
query.conditions.operands.each do |operand|
|
154
|
+
value += operand.value if operand.subject.key?
|
155
|
+
end
|
156
|
+
|
157
|
+
if @redis.sismember(key_set_for(query.model), value)
|
158
|
+
#matched_records << @redis.hgetall("#{storage_name}:#{value}")
|
159
|
+
matched_records << {redis_key_for(query.model) => value}
|
160
|
+
end
|
161
|
+
matched_records
|
162
|
+
end
|
163
|
+
|
143
164
|
##
|
144
165
|
# Retrieves records for a particular model.
|
145
166
|
#
|
@@ -158,6 +179,8 @@ module DataMapper
|
|
158
179
|
key = key.to_i if key =~ /^\d+$/
|
159
180
|
keys << {redis_key_for(query.model) => key}
|
160
181
|
end
|
182
|
+
elsif is_composed_key_based_query?(query)
|
183
|
+
keys = key_query(query)
|
161
184
|
else
|
162
185
|
query.conditions.operands.each do |operand|
|
163
186
|
if operand.is_a?(DataMapper::Query::Conditions::OrOperation)
|
@@ -237,20 +260,19 @@ module DataMapper
|
|
237
260
|
search_all_resources(query, operand, subject, matched_records)
|
238
261
|
end
|
239
262
|
when DataMapper::Query::Conditions::EqualToComparison
|
240
|
-
if query.model.key.include?(subject)
|
241
|
-
if @redis.sismember(key_set_for(query.model), value)
|
263
|
+
if query.model.key.include?(subject) and @redis.sismember(key_set_for(query.model), value)
|
242
264
|
matched_records << {redis_key_for(query.model) => value}
|
243
|
-
|
265
|
+
|
244
266
|
elsif subject.respond_to?(:index) && subject.index
|
245
267
|
find_indexed_matches(subject, value).each do |k|
|
246
268
|
matched_records << {redis_key_for(query.model) => k, "#{subject.name}" => value}
|
247
269
|
end
|
248
|
-
else
|
270
|
+
else
|
249
271
|
search_all_resources(query, operand, subject, matched_records)
|
250
272
|
end
|
251
273
|
else # worst case here, loop through all members, typecast and match
|
252
274
|
search_all_resources(query, operand, subject, matched_records)
|
253
|
-
|
275
|
+
end
|
254
276
|
matched_records
|
255
277
|
end
|
256
278
|
|
data/spec/textual_keys_spec.rb
CHANGED
@@ -3,28 +3,54 @@ require 'spec_helper'
|
|
3
3
|
describe DataMapper::Adapters::RedisAdapter do
|
4
4
|
before(:all) do
|
5
5
|
@adapter = DataMapper.setup(:default, {
|
6
|
-
|
7
|
-
|
6
|
+
:adapter => "redis",
|
7
|
+
:db => 15
|
8
8
|
})
|
9
|
+
|
10
|
+
class Foo_1
|
11
|
+
include DataMapper::Resource
|
12
|
+
property :hostname, Text, :key => true
|
13
|
+
property :ip_address, Text
|
14
|
+
end
|
15
|
+
class Foo
|
16
|
+
include DataMapper::Resource
|
17
|
+
property :key1, String, :key => true
|
18
|
+
property :key2, String, :key => true
|
19
|
+
end
|
20
|
+
DataMapper.finalize
|
21
|
+
|
9
22
|
end
|
10
23
|
|
11
|
-
after(:
|
24
|
+
after(:each) do
|
12
25
|
Redis.new(:db => 15).flushdb
|
13
26
|
end
|
14
27
|
|
15
28
|
describe "textual keys" do
|
16
29
|
it "should return the key" do
|
17
|
-
class Foo
|
18
|
-
include DataMapper::Resource
|
19
|
-
property :hostname, Text, :key => true
|
20
|
-
property :ip_address, Text
|
21
|
-
end
|
22
|
-
DataMapper.finalize
|
23
30
|
|
24
|
-
Foo.create(:hostname => "hostname1", :ip_address => '127.0.0.1')
|
25
31
|
|
26
|
-
|
32
|
+
Foo_1.create(:hostname => "hostname1", :ip_address => '127.0.0.1')
|
33
|
+
|
34
|
+
Foo_1.first.hostname.should == "hostname1"
|
35
|
+
|
36
|
+
Foo_1.first(:hostname => "hostname1").ip_address.should == "127.0.0.1"
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should find elements with two natural keys by querying with only one key" do
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
Foo.create(:key1 => "value1", :key2 => 'value2')
|
44
|
+
Foo.first(:key1 => "value1").key2.should == 'value2'
|
27
45
|
end
|
46
|
+
|
47
|
+
it "should support direct key-based query with get using two natural keys" do
|
48
|
+
|
49
|
+
|
50
|
+
Foo.create(:key1 => "value1", :key2 => 'value2')
|
51
|
+
Foo.get("value1","value2").key2.should == 'value2'
|
52
|
+
end
|
53
|
+
|
28
54
|
end
|
29
55
|
|
30
56
|
describe "textual keys" do
|
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.
|
4
|
+
version: 0.8.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-06-12 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: dm-core
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: 1.2.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.2.0
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: dm-types
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ! '>='
|
@@ -32,21 +37,31 @@ dependencies:
|
|
32
37
|
version: 1.2.0
|
33
38
|
type: :runtime
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.2.0
|
36
46
|
- !ruby/object:Gem::Dependency
|
37
47
|
name: hiredis
|
38
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
39
49
|
none: false
|
40
50
|
requirements:
|
41
51
|
- - ~>
|
42
52
|
- !ruby/object:Gem::Version
|
43
|
-
version: 0.
|
53
|
+
version: 0.4.0
|
44
54
|
type: :runtime
|
45
55
|
prerelease: false
|
46
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.4.0
|
47
62
|
- !ruby/object:Gem::Dependency
|
48
63
|
name: redis
|
49
|
-
requirement:
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
50
65
|
none: false
|
51
66
|
requirements:
|
52
67
|
- - ~>
|
@@ -54,7 +69,12 @@ dependencies:
|
|
54
69
|
version: '2.2'
|
55
70
|
type: :runtime
|
56
71
|
prerelease: false
|
57
|
-
version_requirements:
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '2.2'
|
58
78
|
description: DataMapper adapter for the Redis key-value database
|
59
79
|
email: whoahbot@gmail.com
|
60
80
|
executables:
|
@@ -86,24 +106,15 @@ files:
|
|
86
106
|
- spec/spec_helper.rb
|
87
107
|
- spec/textual_keys_spec.rb
|
88
108
|
- Gemfile
|
89
|
-
-
|
90
|
-
|
91
|
-
-
|
92
|
-
|
93
|
-
-
|
94
|
-
|
95
|
-
-
|
96
|
-
|
97
|
-
-
|
98
|
-
YmluL3ByZXR0aWZ5X2pzb24ucmI=
|
99
|
-
- !binary |-
|
100
|
-
YmluL3Jha2U=
|
101
|
-
- !binary |-
|
102
|
-
YmluL3JkZWJ1Zw==
|
103
|
-
- !binary |-
|
104
|
-
YmluL3J1Ynlmb3JnZQ==
|
105
|
-
- !binary |-
|
106
|
-
YmluL3NwZWM=
|
109
|
+
- bin/autospec
|
110
|
+
- bin/edit_json.rb
|
111
|
+
- bin/jeweler
|
112
|
+
- bin/minitar
|
113
|
+
- bin/prettify_json.rb
|
114
|
+
- bin/rake
|
115
|
+
- bin/rdebug
|
116
|
+
- bin/rubyforge
|
117
|
+
- bin/spec
|
107
118
|
homepage: http://github.com/whoahbot/dm-redis-adapter
|
108
119
|
licenses: []
|
109
120
|
post_install_message:
|
@@ -117,9 +128,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
117
128
|
- - ! '>='
|
118
129
|
- !ruby/object:Gem::Version
|
119
130
|
version: '0'
|
120
|
-
segments:
|
121
|
-
- 0
|
122
|
-
hash: 141054625361991744
|
123
131
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
132
|
none: false
|
125
133
|
requirements:
|
@@ -128,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
136
|
version: '0'
|
129
137
|
requirements: []
|
130
138
|
rubyforge_project:
|
131
|
-
rubygems_version: 1.8.
|
139
|
+
rubygems_version: 1.8.23
|
132
140
|
signing_key:
|
133
141
|
specification_version: 3
|
134
142
|
summary: DataMapper adapter for the Redis key-value database
|
@@ -141,3 +149,4 @@ test_files:
|
|
141
149
|
- spec/self_referential_spec.rb
|
142
150
|
- spec/spec_helper.rb
|
143
151
|
- spec/textual_keys_spec.rb
|
152
|
+
has_rdoc:
|