whoahbot-dm-redis-adapter 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +15 -12
- data/Rakefile +18 -37
- data/lib/{dm_redis_adapter.rb → dm_redis.rb} +18 -19
- data/spec/{dm_redis_adapter_spec.rb → dm_redis_spec.rb} +4 -5
- data/spec/spec_helper.rb +1 -1
- metadata +17 -25
data/README.textile
CHANGED
@@ -2,29 +2,26 @@ h1. dm-redis-adapter
|
|
2
2
|
|
3
3
|
This is a <a href="http://datamapper.org">DataMapper</a> adapter for the <a href="http://github.com/antirez/redis/">Redis</a> key-value database.
|
4
4
|
|
5
|
-
Redis is a very fast key-value store with some interesting data structures added. You can have a key that is a SET, LIST, or a STRING that is binary safe. Data structures like SET and LIST allow for even more interesting things. Redis is a fabulous and fast engine for data structures, and you can read more about it here: <a href="http://code.google.com/p/redis/">redis</a>. Redis is also a persistent data store, and can be used in large-scale environments with master-slave replication.
|
5
|
+
Redis is a very fast key-value store with some interesting data structures added. You can have a key that is a SET, LIST, or a STRING that is binary safe. Data structures like SET and LIST allow for even more interesting things. Redis is a fabulous and fast engine for data structures, and you can read more about it here: <a href="http://code.google.com/p/redis/">redis</a>. Redis is also a persistent data store, and can be used in large-scale environments with master-slave replication and consistent hashing on the client side.
|
6
6
|
|
7
7
|
<a href="http://datamapper.org">DataMapper</a> is a brilliant ORM that is based on the <a href="http://www.martinfowler.com/eaaCatalog/identityMap.html">IdentityMap</a> pattern. Usage of DataMapper resembles that of ActiveRecord, the popular ORM bundled with Ruby on Rails, but with some very important differences. A quote from the DM wiki: "One row in the database should equal one object reference. Pretty simple idea. Pretty profound impact." Having an identity map allows for very efficient queries to the database, as well as interesting forms of lazy loading of attributes or associations.
|
8
8
|
|
9
|
-
Marrying DataMapper to Redis allows for schema-less models, you can add fields at any time without having to create a migration. DataMapper also allows us to store non
|
9
|
+
Marrying DataMapper to Redis allows for schema-less models, you can add fields at any time without having to create a migration. DataMapper also allows us to store non-native Redis types in the db, like Date fields.
|
10
10
|
|
11
11
|
h1. Install
|
12
12
|
|
13
13
|
Prerequisites:
|
14
14
|
* Redis:
|
15
|
-
** <a href="http://code.google.com/p/redis/">Redis,
|
15
|
+
** <a href="http://code.google.com/p/redis/">Redis, v1.0</a>
|
16
16
|
* Gems:
|
17
|
-
** <a href="http://code.google.com/p/redis/">redis (0.0.3.4)</a>
|
18
17
|
** <a href="http://github.com/datamapper/extlib">extlib</a>, dependency for dm-core
|
19
|
-
** <a href="http://github.com/datamapper/dm-core/
|
20
|
-
|
21
|
-
I installed the redis gem from the redis .tgz file like so:
|
18
|
+
** <a href="http://github.com/datamapper/dm-core/">dm-core</a> v0.10
|
22
19
|
|
20
|
+
Install the dm-redis adapter:
|
23
21
|
<pre>
|
24
22
|
<code>
|
25
|
-
>
|
26
|
-
>
|
27
|
-
> sudo rake install
|
23
|
+
> gem sources -a http://gems.github.com
|
24
|
+
> sudo gem install whoahbot-dm-redis-adapter
|
28
25
|
</code>
|
29
26
|
</pre>
|
30
27
|
|
@@ -34,8 +31,9 @@ Setup your adapter, define your models and properties:
|
|
34
31
|
|
35
32
|
<pre>
|
36
33
|
<code>
|
34
|
+
require 'rubygems'
|
37
35
|
require 'dm-core'
|
38
|
-
require '
|
36
|
+
require 'dm_redis'
|
39
37
|
|
40
38
|
DataMapper.setup(:default, {:adapter => "redis"})
|
41
39
|
|
@@ -50,4 +48,9 @@ Setup your adapter, define your models and properties:
|
|
50
48
|
</code>
|
51
49
|
</pre>
|
52
50
|
|
53
|
-
Now you can use redis in a ORM style, and take advantage of all of the amazing things that DataMapper offers.
|
51
|
+
Now you can use redis in a ORM style, and take advantage of all of the amazing things that DataMapper offers.
|
52
|
+
|
53
|
+
h1. Badass contributors
|
54
|
+
|
55
|
+
* <a href="http://github.com/aeden">Anthony Eden (aeden)</a> Gem cleanup, update to jeweler
|
56
|
+
* <a href="http://github.com/sr">Simon Roset (sr)</a> Fixes for edge dm-core
|
data/Rakefile
CHANGED
@@ -1,56 +1,37 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
require 'rake/gempackagetask'
|
3
|
-
require 'rubygems/specification'
|
4
|
-
require 'date'
|
5
2
|
require 'spec/rake/spectask'
|
6
3
|
|
7
4
|
GEM = 'dm-redis-adapter'
|
8
5
|
GEM_NAME = 'dm-redis-adapter'
|
9
|
-
GEM_VERSION = '0.0.4'
|
10
6
|
AUTHORS = ['Dan Herrera']
|
11
7
|
EMAIL = "whoahbot@gmail.com"
|
12
8
|
HOMEPAGE = "http://github.com/whoahbot/dm-redis-adapter"
|
13
9
|
SUMMARY = "DataMapper adapter for the Redis key-value database"
|
14
10
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
11
|
+
begin
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gemspec|
|
14
|
+
gemspec.name = GEM
|
15
|
+
gemspec.summary = SUMMARY
|
16
|
+
gemspec.email = EMAIL
|
17
|
+
gemspec.homepage = HOMEPAGE
|
18
|
+
gemspec.description = SUMMARY
|
19
|
+
gemspec.authors = AUTHORS
|
20
|
+
gemspec.add_dependency "dm-core", "0.10.0"
|
21
|
+
gemspec.add_dependency "ezmobius-redis"
|
22
|
+
gemspec.files = %w(MIT-LICENSE README.textile Rakefile) + Dir.glob("{lib,spec}/**/*")
|
23
|
+
gemspec.has_rdoc = true
|
24
|
+
gemspec.extra_rdoc_files = ["MIT-LICENSE"]
|
25
|
+
end
|
26
|
+
rescue LoadError
|
27
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
32
28
|
end
|
33
29
|
|
30
|
+
|
34
31
|
task :default => :spec
|
35
32
|
|
36
33
|
desc "Run specs"
|
37
34
|
Spec::Rake::SpecTask.new do |t|
|
38
35
|
t.spec_files = FileList['spec/**/*_spec.rb']
|
39
36
|
t.spec_opts = %w(-fs --color)
|
40
|
-
end
|
41
|
-
|
42
|
-
Rake::GemPackageTask.new(spec) do |pkg|
|
43
|
-
pkg.gem_spec = spec
|
44
|
-
end
|
45
|
-
|
46
|
-
desc "install the gem locally"
|
47
|
-
task :install => [:package] do
|
48
|
-
sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
|
49
|
-
end
|
50
|
-
|
51
|
-
desc "create a gemspec file"
|
52
|
-
task :make_spec do
|
53
|
-
File.open("#{GEM}.gemspec", "w") do |file|
|
54
|
-
file.puts spec.to_ruby
|
55
|
-
end
|
56
37
|
end
|
@@ -3,7 +3,7 @@ require 'redis'
|
|
3
3
|
module DataMapper
|
4
4
|
module Adapters
|
5
5
|
Extlib::Inflection.word 'redis'
|
6
|
-
|
6
|
+
|
7
7
|
class RedisAdapter < AbstractAdapter
|
8
8
|
##
|
9
9
|
# Used by DataMapper to put records into the redis data-store: "INSERT" in SQL-speak.
|
@@ -17,14 +17,13 @@ module DataMapper
|
|
17
17
|
# @api semipublic
|
18
18
|
def create(resources)
|
19
19
|
resources.each do |resource|
|
20
|
-
|
21
|
-
initialize_identity_field(resource, @redis.incr("#{resource.model.to_s.downcase}:#{redis_key_for(resource.model)}:serial"))
|
20
|
+
initialize_serial(resource, @redis.incr("#{resource.model.to_s.downcase}:#{redis_key_for(resource.model)}:serial"))
|
22
21
|
@redis.set_add("#{resource.model.to_s.downcase}:#{redis_key_for(resource.model)}:all", resource.key)
|
23
22
|
end
|
24
|
-
|
23
|
+
|
25
24
|
update_attributes(resources)
|
26
25
|
end
|
27
|
-
|
26
|
+
|
28
27
|
##
|
29
28
|
# Looks up one record or a collection of records from the data-store:
|
30
29
|
# "SELECT" in SQL.
|
@@ -40,7 +39,7 @@ module DataMapper
|
|
40
39
|
def read(query)
|
41
40
|
records = records_for(query).each do |record|
|
42
41
|
query.fields.each do |property|
|
43
|
-
next if query.model.key.include?(property
|
42
|
+
next if query.model.key.include?(property)
|
44
43
|
record[property.name.to_s] = property.typecast(@redis["#{query.model.to_s.downcase}:#{record[redis_key_for(query.model)]}:#{property.name}"])
|
45
44
|
end
|
46
45
|
end
|
@@ -50,7 +49,7 @@ module DataMapper
|
|
50
49
|
records = query.sort_records(records)
|
51
50
|
records
|
52
51
|
end
|
53
|
-
|
52
|
+
|
54
53
|
##
|
55
54
|
# Used by DataMapper to update the attributes on existing records in the redis
|
56
55
|
# data-store: "UPDATE" in SQL-speak. It takes a hash of the attributes
|
@@ -65,12 +64,12 @@ module DataMapper
|
|
65
64
|
# @api semipublic
|
66
65
|
def update(attributes, collection)
|
67
66
|
attributes = attributes_as_fields(attributes)
|
68
|
-
|
67
|
+
|
69
68
|
records_to_update = records_for(collection.query)
|
70
69
|
records_to_update.each { |r| r.update(attributes) }
|
71
70
|
update_attributes(collection)
|
72
71
|
end
|
73
|
-
|
72
|
+
|
74
73
|
##
|
75
74
|
# Destroys all the records matching the given query. "DELETE" in SQL.
|
76
75
|
#
|
@@ -90,9 +89,9 @@ module DataMapper
|
|
90
89
|
@redis.set_delete("#{collection.query.model.to_s.downcase}:#{redis_key_for(collection.query.model)}:all", record[redis_key_for(collection.query.model)])
|
91
90
|
end
|
92
91
|
end
|
93
|
-
|
92
|
+
|
94
93
|
private
|
95
|
-
|
94
|
+
|
96
95
|
##
|
97
96
|
# Creates a string representation for the keys in a given model
|
98
97
|
#
|
@@ -107,7 +106,7 @@ module DataMapper
|
|
107
106
|
def redis_key_for(model)
|
108
107
|
model.key.collect {|k| k.name}.join(":")
|
109
108
|
end
|
110
|
-
|
109
|
+
|
111
110
|
##
|
112
111
|
# Saves each key value pair to the redis data store
|
113
112
|
#
|
@@ -123,7 +122,7 @@ module DataMapper
|
|
123
122
|
end
|
124
123
|
end
|
125
124
|
end
|
126
|
-
|
125
|
+
|
127
126
|
##
|
128
127
|
# Retrieves records for a particular model.
|
129
128
|
#
|
@@ -136,25 +135,25 @@ module DataMapper
|
|
136
135
|
# @api private
|
137
136
|
def records_for(query)
|
138
137
|
keys = []
|
139
|
-
query.conditions.operands.select {|o| o.is_a?(DataMapper::Query::Conditions::EqualToComparison) && query.model.key.include?(o.
|
138
|
+
query.conditions.operands.select {|o| o.is_a?(DataMapper::Query::Conditions::EqualToComparison) && query.model.key.include?(o.subject)}.each do |o|
|
140
139
|
if @redis.set_member?("#{query.model.to_s.downcase}:#{redis_key_for(query.model)}:all", o.value)
|
141
140
|
keys << {"#{redis_key_for(query.model)}" => o.value}
|
142
141
|
end
|
143
142
|
end
|
144
|
-
|
143
|
+
|
145
144
|
# if query.limit
|
146
145
|
# @redis.sort("#{query.model.to_s.downcase}:#{redis_key_for(query.model)}:all", :limit => [query.offset, query.limit]).each do |val|
|
147
146
|
# keys << {"#{redis_key_for(query.model)}" => val.to_i}
|
148
147
|
# end
|
149
148
|
# end
|
150
|
-
|
149
|
+
|
151
150
|
# Keys are empty, fall back and load all the values for this model
|
152
151
|
if keys.empty?
|
153
152
|
@redis.set_members("#{query.model.to_s.downcase}:#{redis_key_for(query.model)}:all").each do |val|
|
154
153
|
keys << {"#{redis_key_for(query.model)}" => val.to_i}
|
155
154
|
end
|
156
155
|
end
|
157
|
-
|
156
|
+
|
158
157
|
keys
|
159
158
|
end
|
160
159
|
|
@@ -174,7 +173,7 @@ module DataMapper
|
|
174
173
|
@redis = Redis.new(@options)
|
175
174
|
end
|
176
175
|
end # class RedisAdapter
|
177
|
-
|
176
|
+
|
178
177
|
const_added(:RedisAdapter)
|
179
178
|
end # module Adapters
|
180
|
-
end # module DataMapper
|
179
|
+
end # module DataMapper
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib/dm_redis_adapter'))
|
3
2
|
require 'redis'
|
4
3
|
|
5
4
|
require 'dm-core/spec/adapter_shared_spec'
|
@@ -11,11 +10,11 @@ describe DataMapper::Adapters::RedisAdapter do
|
|
11
10
|
:db => 15
|
12
11
|
})
|
13
12
|
end
|
14
|
-
|
13
|
+
|
15
14
|
after(:all) do
|
16
15
|
redis = Redis.new(:db => 15)
|
17
|
-
redis.
|
16
|
+
redis.flush_db
|
18
17
|
end
|
19
|
-
|
18
|
+
|
20
19
|
it_should_behave_like 'An Adapter'
|
21
|
-
end
|
20
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,27 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: whoahbot-dm-redis-adapter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan Herrera
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-09-15 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
|
-
- !ruby/object:Gem::Dependency
|
16
|
-
name: rspec
|
17
|
-
type: :runtime
|
18
|
-
version_requirement:
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: "0"
|
24
|
-
version:
|
25
15
|
- !ruby/object:Gem::Dependency
|
26
16
|
name: dm-core
|
27
17
|
type: :runtime
|
@@ -33,14 +23,14 @@ dependencies:
|
|
33
23
|
version: 0.10.0
|
34
24
|
version:
|
35
25
|
- !ruby/object:Gem::Dependency
|
36
|
-
name: redis
|
26
|
+
name: ezmobius-redis
|
37
27
|
type: :runtime
|
38
28
|
version_requirement:
|
39
29
|
version_requirements: !ruby/object:Gem::Requirement
|
40
30
|
requirements:
|
41
|
-
- - "
|
31
|
+
- - ">="
|
42
32
|
- !ruby/object:Gem::Version
|
43
|
-
version: 0
|
33
|
+
version: "0"
|
44
34
|
version:
|
45
35
|
description: DataMapper adapter for the Redis key-value database
|
46
36
|
email: whoahbot@gmail.com
|
@@ -54,14 +44,15 @@ files:
|
|
54
44
|
- MIT-LICENSE
|
55
45
|
- README.textile
|
56
46
|
- Rakefile
|
57
|
-
- lib/
|
58
|
-
- spec/
|
47
|
+
- lib/dm_redis.rb
|
48
|
+
- spec/dm_redis_spec.rb
|
59
49
|
- spec/spec_helper.rb
|
60
|
-
has_rdoc:
|
50
|
+
has_rdoc: true
|
61
51
|
homepage: http://github.com/whoahbot/dm-redis-adapter
|
52
|
+
licenses:
|
62
53
|
post_install_message:
|
63
|
-
rdoc_options:
|
64
|
-
|
54
|
+
rdoc_options:
|
55
|
+
- --charset=UTF-8
|
65
56
|
require_paths:
|
66
57
|
- lib
|
67
58
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -79,9 +70,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
70
|
requirements: []
|
80
71
|
|
81
72
|
rubyforge_project:
|
82
|
-
rubygems_version: 1.
|
73
|
+
rubygems_version: 1.3.5
|
83
74
|
signing_key:
|
84
|
-
specification_version:
|
75
|
+
specification_version: 2
|
85
76
|
summary: DataMapper adapter for the Redis key-value database
|
86
|
-
test_files:
|
87
|
-
|
77
|
+
test_files:
|
78
|
+
- spec/dm_redis_spec.rb
|
79
|
+
- spec/spec_helper.rb
|