Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Need to vendor ohm nest redis until redis 3.0 comes out.
- Loading branch information
Cyril David
committed
Mar 29, 2012
1 parent
6e49be0
commit c759299
Showing
122 changed files
with
13,259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2010 Michel Martens & Damian Janowski | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
Nest | ||
==== | ||
|
||
Object Oriented Keys for Redis. | ||
|
||
Description | ||
----------- | ||
|
||
If you are familiar with databases like [Redis](http://code.google.com/p/redis) | ||
and libraries like [Ohm](http://ohm.keyvalue.org) you already know how | ||
important it is to craft the keys that will hold the data. | ||
|
||
>> redis = Redis.new | ||
>> redis.sadd("event:3:attendees", "Albert") | ||
>> redis.smembers("event:3:attendees") | ||
=> ["Albert"] | ||
|
||
It is a design pattern in key-value databases to use the key to simulate | ||
structure, and you can read more about this in the [case study for a | ||
Twitter clone](http://code.google.com/p/redis/wiki/TwitterAlikeExample). | ||
|
||
Nest helps you generate those keys by providing chainable namespaces that are | ||
already connected to Redis: | ||
|
||
>> event = Nest.new("event") | ||
>> event[3][:attendees].sadd("Albert") | ||
>> event[3][:attendees].smembers | ||
=> ["Albert"] | ||
|
||
Usage | ||
----- | ||
|
||
To create a new namespace: | ||
|
||
>> ns = Nest.new("foo") | ||
=> "foo" | ||
|
||
>> ns["bar"] | ||
=> "foo:bar" | ||
|
||
>> ns["bar"]["baz"]["qux"] | ||
=> "foo:bar:baz:qux" | ||
|
||
And you can use any object as a key, not only strings: | ||
|
||
>> ns[:bar][42] | ||
=> "foo:bar:42" | ||
|
||
In a more realistic tone, lets assume you are working with Redis and | ||
dealing with events: | ||
|
||
>> events = Nest.new("events") | ||
=> "events" | ||
|
||
>> id = events[:id].incr | ||
=> 1 | ||
|
||
>> events[id][:attendees].sadd("Albert") | ||
=> "OK" | ||
|
||
>> meetup = events[id] | ||
=> "events:1" | ||
|
||
>> meetup[:attendees].smembers | ||
=> ["Albert"] | ||
|
||
Supplying your existing Redis instance | ||
-------------------------------------- | ||
|
||
You can supply a `Redis` instance as a second parameter. If you don't, a default | ||
instance is created for you: | ||
|
||
>> redis = Redis.new | ||
=> #<Redis::Client...> | ||
|
||
>> users = Nest.new("users", redis) | ||
=> "users" | ||
|
||
>> id = users[:id].incr | ||
=> 1 | ||
|
||
>> users[id].hset(:name, "Albert") | ||
=> "OK" | ||
|
||
`Nest` objects respond to `redis` and return a `Redis` instance. It is | ||
automatically reused when you create a new namespace, and you can reuse it when | ||
creating a new instance of Nest: | ||
|
||
>> events = Nest.new("events", meetup.redis) | ||
=> "events" | ||
|
||
>> events.sadd(meetup) | ||
=> true | ||
|
||
>> events.sismember(meetup) | ||
=> true | ||
|
||
>> events.smembers | ||
=> ["events:1"] | ||
|
||
>> events.del | ||
>> true | ||
|
||
Nest allows you to execute all the Redis commands that expect a key as the | ||
first parameter. Think of it as a | ||
[curried](http://en.wikipedia.org/wiki/Currying) Redis client. | ||
|
||
Differences with redis-namespace | ||
-------------------------------- | ||
|
||
[redis-namespace](http://github.com/defunkt/redis-namespace) wraps Redis | ||
and translates the keys back and forth transparently. | ||
|
||
Use redis-namespace when you want all your application keys to live in a | ||
different scope. | ||
|
||
Use Nest when you want to use the keys to represent structure. | ||
|
||
Tip: instead of using redis-namespace, it is recommended that you run a | ||
different instance of `redis-server`. Translating keys back and forth is not | ||
only delicate, but unnecessary and counterproductive. | ||
|
||
Differences with Ohm | ||
-------------------- | ||
|
||
[Ohm](http://ohm.keyvalue.org) lets you map Ruby objects to Redis with | ||
little effort. It not only alleviates you from the pain of generating | ||
keys for each object, but also helps you when dealing with references | ||
between objects. | ||
|
||
Use Ohm when you want to use Redis as your database. | ||
|
||
Use Nest when mapping objects with Ohm is not possible or overkill. | ||
|
||
Tip: Ohm uses Nest internally to deal with keys. Having a good knowledge | ||
of Nest will let you extend Ohm to suit your needs. | ||
|
||
Installation | ||
------------ | ||
|
||
$ gem install nest | ||
|
||
License | ||
------- | ||
|
||
Copyright (c) 2010 Michel Martens & Damian Janowski | ||
|
||
Permission is hereby granted, free of charge, to any person | ||
obtaining a copy of this software and associated documentation | ||
files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, | ||
copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following | ||
conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
task :test do | ||
require "cutest" | ||
Cutest.run(Dir["test/nest*"]) | ||
end | ||
|
||
task :default => :test | ||
|
||
task :commands do | ||
require "open-uri" | ||
require "par" | ||
require "json" | ||
|
||
file = File.expand_path("lib/nest.rb", File.dirname(__FILE__)) | ||
path = "https://github.com/antirez/redis-doc/raw/master/commands.json" | ||
|
||
commands = JSON.parse(open(path).read).select do |name, command| | ||
# Skip all DEBUG commands | ||
next if command["group"] == "server" | ||
|
||
# If the command has no arguments, it doesn't operate on a key | ||
next if command["arguments"].nil? | ||
|
||
arg = command["arguments"].first | ||
|
||
arg["type"] == "key" || | ||
Array(arg["name"]) == ["channel"] | ||
end | ||
|
||
commands = commands.keys.map { |key| key.downcase.to_sym } | ||
|
||
commands.delete(:mget) | ||
|
||
source = File.read(file).sub(/ METHODS = .+?\n\n/m) do | ||
Par.new(" METHODS = #{commands.inspect}\n\n", p: 2) | ||
end | ||
|
||
File.open(file, "w") { |f| f.write source } | ||
|
||
system "git diff --color-words #{file}" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require "redis" | ||
|
||
class Nest < String | ||
VERSION = "1.1.0" | ||
|
||
METHODS = [:append, :blpop, :brpop, :brpoplpush, :decr, :decrby, | ||
:del, :exists, :expire, :expireat, :get, :getbit, :getrange, :getset, | ||
:hdel, :hexists, :hget, :hgetall, :hincrby, :hkeys, :hlen, :hmget, | ||
:hmset, :hset, :hsetnx, :hvals, :incr, :incrby, :lindex, :linsert, | ||
:llen, :lpop, :lpush, :lpushx, :lrange, :lrem, :lset, :ltrim, :move, | ||
:persist, :publish, :rename, :renamenx, :rpop, :rpoplpush, :rpush, | ||
:rpushx, :sadd, :scard, :sdiff, :sdiffstore, :set, :setbit, :setex, | ||
:setnx, :setrange, :sinter, :sinterstore, :sismember, :smembers, | ||
:smove, :sort, :spop, :srandmember, :srem, :strlen, :subscribe, | ||
:sunion, :sunionstore, :ttl, :type, :unsubscribe, :watch, :zadd, | ||
:zcard, :zcount, :zincrby, :zinterstore, :zrange, :zrangebyscore, | ||
:zrank, :zrem, :zremrangebyrank, :zremrangebyscore, :zrevrange, | ||
:zrevrangebyscore, :zrevrank, :zscore, :zunionstore] | ||
|
||
attr :redis | ||
|
||
def initialize(key, redis = Redis.current) | ||
super(key.to_s) | ||
@redis = redis | ||
end | ||
|
||
def [](key) | ||
self.class.new("#{self}:#{key}", redis) | ||
end | ||
|
||
METHODS.each do |meth| | ||
define_method(meth) do |*args, &block| | ||
redis.send(meth, self, *args, &block) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Gem::Specification.new do |s| | ||
s.name = "nest" | ||
s.version = "1.1.0" | ||
s.summary = "Object-oriented keys for Redis." | ||
s.description = "It is a design pattern in key-value databases to use the key to simulate structure, and Nest can take care of that." | ||
s.authors = ["Michel Martens"] | ||
s.email = ["michel@soveran.com"] | ||
s.homepage = "http://github.com/soveran/nest" | ||
s.files = ["LICENSE", "README.markdown", "Rakefile", "lib/nest.rb", "nest.gemspec", "test/nest_test.rb", "test/test_helper.rb"] | ||
s.add_dependency "redis", "~> 2.1" | ||
s.add_development_dependency "cutest", "~> 0.1" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
require File.expand_path("test_helper", File.dirname(__FILE__)) | ||
|
||
# Creating namespaces. | ||
scope do | ||
test "return the namespace" do | ||
n1 = Nest.new("foo") | ||
assert "foo" == n1 | ||
end | ||
|
||
test "prepend the namespace" do | ||
n1 = Nest.new("foo") | ||
assert "foo:bar" == n1["bar"] | ||
end | ||
|
||
test "work in more than one level" do | ||
n1 = Nest.new("foo") | ||
n2 = Nest.new(n1["bar"]) | ||
assert "foo:bar:baz" == n2["baz"] | ||
end | ||
|
||
test "be chainable" do | ||
n1 = Nest.new("foo") | ||
assert "foo:bar:baz" == n1["bar"]["baz"] | ||
end | ||
|
||
test "accept symbols" do | ||
n1 = Nest.new(:foo) | ||
assert "foo:bar" == n1[:bar] | ||
end | ||
|
||
test "accept numbers" do | ||
n1 = Nest.new("foo") | ||
assert "foo:3" == n1[3] | ||
end | ||
end | ||
|
||
# Operating with redis. | ||
scope do | ||
prepare do | ||
@redis = Redis.new | ||
@redis.flushdb | ||
end | ||
|
||
test "work if no redis instance was passed" do | ||
n1 = Nest.new("foo") | ||
n1.set("s1") | ||
|
||
assert "s1" == n1.get | ||
end | ||
|
||
test "work if a redis instance is supplied" do | ||
n1 = Nest.new("foo", @redis) | ||
n1.set("s1") | ||
|
||
assert "s1" == n1.get | ||
end | ||
|
||
test "pass the redis instance to new keys" do | ||
n1 = Nest.new("foo", @redis) | ||
|
||
assert @redis.id == n1["bar"].redis.id | ||
end | ||
|
||
test "PubSub" do | ||
foo = Nest.new("foo", @redis) | ||
listening = false | ||
message_received = false | ||
|
||
Thread.new do | ||
while !listening; end | ||
Nest.new("foo", Redis.new(:db => 15)).publish("") | ||
end | ||
|
||
foo.subscribe do |on| | ||
on.message do | ||
message_received = true | ||
|
||
foo.unsubscribe | ||
end | ||
|
||
listening = true | ||
end | ||
|
||
assert message_received | ||
end | ||
end | ||
|
||
scope do | ||
prepare do | ||
@redis1 = Redis.connect(:db => 15) | ||
@redis2 = Redis.connect(:db => 14) | ||
|
||
@redis1.flushdb | ||
@redis2.flushdb | ||
end | ||
|
||
test "honors Redis.current" do | ||
Redis.current = @redis1 | ||
|
||
foo = Nest.new("foo") | ||
|
||
foo.set("bar") | ||
|
||
assert_equal "bar", foo.get | ||
|
||
Redis.current = @redis2 | ||
|
||
assert_equal nil, Nest.new("foo").get | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require "rubygems" | ||
require "cutest" | ||
require File.join(File.dirname(__FILE__), "..", "lib", "nest") |
Oops, something went wrong.