Permalink
Browse files

Interim work on supporting dm-core 1.1, queries on has n :through. Re…

…moving catchall match_records
  • Loading branch information...
1 parent 0b5fc96 commit 103c0ed2985bbb74805b80a166d4cb6c45ce87a2 @whoahbot whoahbot committed Mar 30, 2011
Showing with 55 additions and 47 deletions.
  1. +6 −5 Gemfile
  2. +27 −24 Gemfile.lock
  3. +19 −13 lib/dm-redis-adapter/adapter.rb
  4. +2 −2 spec/dm_redis_spec.rb
  5. +1 −3 spec/dm_redis_validations_spec.rb
View
11 Gemfile
@@ -1,12 +1,13 @@
source :gemcutter
-gem "dm-core", ">= 1.0.2"
-gem "dm-validations", ">= 1.0.2"
-gem "dm-migrations", ">= 1.0.2"
-gem "dm-types", ">= 1.0.2"
-gem "redis", ">= 2.0.3"
+gem "dm-core", ">= 1.1.0"
+gem "dm-validations", ">= 1.1.0"
+gem "dm-migrations", ">= 1.1.0"
+gem "dm-types", ">= 1.1.0"
+gem "redis", ">= 2.2"
gem "jeweler", ">= 1.4.0"
gem "rspec", "~> 1.3.0"
gem "rake", ">= 0.8.7"
+gem "hiredis", "~> 0.3.0"
# gem "data_objects", ">= 0.10.1"
# gem "do_postgres", ">= 0.10.1"
View
51 Gemfile.lock
@@ -1,46 +1,49 @@
GEM
remote: http://rubygems.org/
specs:
- addressable (2.2.2)
- dm-core (1.0.2)
- addressable (~> 2.2)
- extlib (~> 0.9.15)
- dm-migrations (1.0.2)
- dm-core (~> 1.0.2)
- dm-types (1.0.2)
- dm-core (~> 1.0.2)
- fastercsv (~> 1.5.3)
- json_pure (~> 1.4)
- stringex (~> 1.1.0)
- uuidtools (~> 2.1.1)
- dm-validations (1.0.2)
- dm-core (~> 1.0.2)
- extlib (0.9.15)
- fastercsv (1.5.3)
+ addressable (2.2.4)
+ bcrypt-ruby (2.1.4)
+ dm-core (1.1.0)
+ addressable (~> 2.2.4)
+ dm-migrations (1.1.0)
+ dm-core (~> 1.1.0)
+ dm-types (1.1.0)
+ bcrypt-ruby (~> 2.1.4)
+ dm-core (~> 1.1.0)
+ fastercsv (~> 1.5.4)
+ json (~> 1.4.6)
+ stringex (~> 1.2.0)
+ uuidtools (~> 2.1.2)
+ dm-validations (1.1.0)
+ dm-core (~> 1.1.0)
+ fastercsv (1.5.4)
gemcutter (0.6.1)
git (1.2.5)
+ hiredis (0.3.1)
jeweler (1.4.0)
gemcutter (>= 0.1.0)
git (>= 1.2.5)
rubyforge (>= 2.0.0)
+ json (1.4.6)
json_pure (1.4.6)
rake (0.8.7)
- redis (2.1.1)
+ redis (2.2.0)
rspec (1.3.1)
rubyforge (2.0.4)
json_pure (>= 1.1.7)
- stringex (1.1.0)
- uuidtools (2.1.1)
+ stringex (1.2.1)
+ uuidtools (2.1.2)
PLATFORMS
ruby
DEPENDENCIES
- dm-core (>= 1.0.2)
- dm-migrations (>= 1.0.2)
- dm-types (>= 1.0.2)
- dm-validations (>= 1.0.2)
+ dm-core (>= 1.1.0)
+ dm-migrations (>= 1.1.0)
+ dm-types (>= 1.1.0)
+ dm-validations (>= 1.1.0)
+ hiredis (~> 0.3.0)
jeweler (>= 1.4.0)
rake (>= 0.8.7)
- redis (>= 2.0.3)
+ redis (>= 2.2)
rspec (~> 1.3.0)
View
32 lib/dm-redis-adapter/adapter.rb
@@ -1,5 +1,5 @@
require 'redis'
-require "base64"
+require 'base64'
module DataMapper
module Adapters
@@ -35,7 +35,7 @@ def create(resources)
#
# @api semipublic
def read(query)
- records = records_for(query).each do |record|
+ records_for(query).each do |record|
record_data = @redis.hgetall("#{query.model.to_s.downcase}:#{record[redis_key_for(query.model)]}")
query.fields.each do |property|
@@ -51,9 +51,6 @@ def read(query)
record[name] = [Integer, Date].include?(property.primitive) ? property.typecast( value ) : value
end
end
- records = query.match_records(records)
- records = query.sort_records(records)
- records
end
##
@@ -119,7 +116,7 @@ def update_attributes(resources)
properties_to_set = []
properties_to_del = []
- fields = model.properties(self.name).select {|property| attributes.key?(property) }
+ fields = model.properties(self.name).select {|property| attributes.key?(property)}
fields.each do |property|
value = attributes[property]
if value.nil?
@@ -154,11 +151,27 @@ def records_for(query)
keys << {"#{redis_key_for(query.model)}" => o.value}
end
end
+
+ if o.subject.is_a?(DataMapper::Associations::ManyToOne::Relationship)
+ if @redis.sismember("#{o.subject.child_model.to_s.downcase}:#{o.subject.child_key.first.name}:#{encode(o.value[o.subject.parent_key.first.name])}", o.value[o.subject.parent_key.first.name])
+ keys << {o.subject.parent_key.first.name.to_s => o.value[o.subject.parent_key.first.name]}
+ end
+ end
+
find_matches(query, o).each do |k|
keys << {"#{redis_key_for(query.model)}" => k.to_i, "#{o.subject.name}" => o.value}
end
end
+ query.conditions.operands.select {|o| o.is_a?(DataMapper::Query::Conditions::InclusionComparison)}.each do |o|
+ @redis.smembers(key_set_for(query.model)).each do |key|
+ hash_key = "#{query.model.to_s.downcase}:#{key}"
+ if (o.value).include?(o.subject.typecast(@redis.hget(hash_key, o.subject.name)))
+ keys << {"#{redis_key_for(query.model)}" => key.to_i}
+ end
+ end
+ end
+
if keys.empty? && (query.order || query.limit)
params = {}
params[:limit] = [query.offset, query.limit] if query.limit
@@ -173,13 +186,6 @@ def records_for(query)
end
end
- # Keys are empty, fall back and load all the values for this model
- if keys.empty?
- @redis.smembers(key_set_for(query.model)).each do |val|
- keys << {"#{redis_key_for(query.model)}" => val.to_i}
- end
- end
-
keys
end
View
4 spec/dm_redis_spec.rb
@@ -17,9 +17,9 @@
end
it_should_behave_like 'An Adapter'
-
+
after(:all) do
redis = Redis.new(:db => 15)
redis.flushdb
end
-end
+end
View
4 spec/dm_redis_validations_spec.rb
@@ -1,14 +1,12 @@
require File.expand_path("../spec_helper", __FILE__)
require 'dm-validations'
require 'dm-types'
-require 'logger'
describe DataMapper::Adapters::RedisAdapter do
before(:all) do
@adapter = DataMapper.setup(:default, {
:adapter => "redis",
:db => 15,
- # :logger => Logger.new(STDOUT)
})
end
@@ -117,7 +115,7 @@ class Blackguard
petey = Blackguard.create(:nickname => "Petey 'one-eye' McGraw")
james = Blackguard.create(:nickname => "James 'cannon-fingers' Doolittle")
Blackguard.get(petey.id).should_not be_destroyed
- #Blackguard.first(:nickname => "James 'cannon-fingers' Doolittle").should_not be_destroyed
+ Blackguard.first(:nickname => "James 'cannon-fingers' Doolittle").should_not be_destroyed
end
after(:each) do

0 comments on commit 103c0ed

Please sign in to comment.