ruby-redis 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/LICENSE +9 -0
  2. data/README +90 -0
  3. data/bin/ruby-redis +12 -0
  4. data/bin/ruby-redis.compiled.rbc +243 -0
  5. data/lib/redis/bin.rb +74 -0
  6. data/lib/redis/bin.rbc +1325 -0
  7. data/lib/redis/buftok.rbc +2658 -0
  8. data/lib/redis/config.rb +46 -0
  9. data/lib/redis/config.rbc +1009 -0
  10. data/lib/redis/connection.rb +69 -0
  11. data/lib/redis/connection.rbc +1354 -0
  12. data/lib/redis/database.rb +109 -0
  13. data/lib/redis/database.rbc +2275 -0
  14. data/lib/redis/hashes.rb +72 -0
  15. data/lib/redis/hashes.rbc +1843 -0
  16. data/lib/redis/hiredis.rbc +658 -0
  17. data/lib/redis/keys.rb +165 -0
  18. data/lib/redis/keys.rbc +3386 -0
  19. data/lib/redis/lists.rb +227 -0
  20. data/lib/redis/lists.rbc +5241 -0
  21. data/lib/redis/logger.rb +81 -0
  22. data/lib/redis/logger.rbc +2106 -0
  23. data/lib/redis/protocol.rb +170 -0
  24. data/lib/redis/protocol.rbc +3735 -0
  25. data/lib/redis/pubsub.rb +153 -0
  26. data/lib/redis/pubsub.rbc +3447 -0
  27. data/lib/redis/reader.rb +164 -0
  28. data/lib/redis/reader.rbc +2769 -0
  29. data/lib/redis/send.rbc +1268 -0
  30. data/lib/redis/sender.rb +49 -0
  31. data/lib/redis/sender.rbc +1057 -0
  32. data/lib/redis/server.rb +62 -0
  33. data/lib/redis/server.rbc +1177 -0
  34. data/lib/redis/sets.rb +105 -0
  35. data/lib/redis/sets.rbc +2800 -0
  36. data/lib/redis/strict.rb +67 -0
  37. data/lib/redis/strict.rbc +1419 -0
  38. data/lib/redis/strings.rb +144 -0
  39. data/lib/redis/strings.rbc +3338 -0
  40. data/lib/redis/synchrony.rb +58 -0
  41. data/lib/redis/synchrony.rbc +1397 -0
  42. data/lib/redis/version.rb +7 -0
  43. data/lib/redis/version.rbc +180 -0
  44. data/lib/redis/zsets.rb +281 -0
  45. data/lib/redis/zsets.rbc +6596 -0
  46. data/lib/redis.rb +215 -0
  47. data/lib/redis.rbc +4391 -0
  48. metadata +117 -0
data/lib/redis/keys.rb ADDED
@@ -0,0 +1,165 @@
1
+ require File.expand_path '../redis', File.dirname(__FILE__)
2
+
3
+ class Redis
4
+ module Keys
5
+
6
+ def redis_RANDOMKEY
7
+ return nil if @database.empty?
8
+ @database.random_key
9
+ end
10
+
11
+ def redis_KEYS pattern
12
+ @database.reduce([]) do |memo, key_val|
13
+ key = key_val[0]
14
+ memo.push key if File.fnmatch(pattern, key)
15
+ memo
16
+ end
17
+ end
18
+
19
+ def redis_SORT key, *args
20
+ record = @database[key] || []
21
+ sort = 'ASC'
22
+ gets = []
23
+ alpha = false
24
+ by = by_hash = offset = count = store = nil
25
+ until args.empty?
26
+ arg = args.shift
27
+ case arg.upcase
28
+ when 'LIMIT'
29
+ offset = args.shift.to_i
30
+ count = args.shift.to_i
31
+ when 'ASC'
32
+ sort = 'ASC'
33
+ when 'DESC'
34
+ sort = 'DESC'
35
+ when 'ALPHA'
36
+ alpha = true
37
+ when 'STORE'
38
+ store = args.shift
39
+ when 'GET'
40
+ gets << args.shift
41
+ when 'BY'
42
+ by, by_hash = args.shift.split '->', 2
43
+ else
44
+ raise "#{arg} bad argument"
45
+ end
46
+ end
47
+ result = record.sort do |a, b|
48
+ if by
49
+ a = @database[by.sub /\*/, a]
50
+ a = a[by_hash] if by_hash
51
+ b = @database[by.sub /\*/, b]
52
+ b = b[by_hash] if by_hash
53
+ end
54
+ if alpha
55
+ a = a.to_s
56
+ b = b.to_s
57
+ else
58
+ a = a.to_f
59
+ b = b.to_f
60
+ end
61
+ if sort == 'DESC'
62
+ b <=> a
63
+ else
64
+ a <=> b
65
+ end
66
+ end
67
+ unless gets.empty?
68
+ original = result
69
+ result = []
70
+ original.each do |r|
71
+ gets.each do |g|
72
+ get, get_hash = g.split('->', 2)
73
+ r = @database[get.sub /\*/, r] unless get == '#'
74
+ r = r[get_hash] if get_hash
75
+ result << r
76
+ end
77
+ end
78
+ end
79
+ if count and offset
80
+ result = result[offset,count]
81
+ elsif count
82
+ result = result[0,count]
83
+ elsif offset
84
+ result = result[offset..-1]
85
+ end
86
+ if Array === result[0]
87
+ result = result.collect {|r| r.first}
88
+ end
89
+ @database[store] = result if store
90
+ result
91
+ end
92
+
93
+ def redis_DEL *keys
94
+ count = 0
95
+ keys.each do |key|
96
+ count += 1 if @database.has_key? key
97
+ @database.delete key
98
+ end
99
+ count
100
+ end
101
+
102
+ def redis_TYPE key
103
+ if String === @database[key]
104
+ 'string'
105
+ elsif Numeric === @database[key]
106
+ 'string'
107
+ elsif Array === @database[key]
108
+ 'list'
109
+ elsif Set === @database[key]
110
+ 'set'
111
+ elsif ZSet === @database[key]
112
+ 'zset'
113
+ elsif Hash === @database[key]
114
+ 'hash'
115
+ else
116
+ 'unknown'
117
+ end
118
+ end
119
+
120
+ def redis_EXISTS key
121
+ @database.has_key? key
122
+ end
123
+
124
+ def redis_EXPIRE key, seconds
125
+ @database.expire key, redis_pos_i(seconds)
126
+ end
127
+
128
+ def redis_EXPIREAT key, timestamp
129
+ @database.expire_at key, redis_pos_i(timestamp)
130
+ end
131
+
132
+ def redis_PERSIST key
133
+ @database.persist key
134
+ end
135
+
136
+ def redis_TTL key
137
+ @database.ttl key
138
+ end
139
+
140
+ def redis_RENAME key, newkey
141
+ raise 'key and newkey are identical' if key == newkey
142
+ raise 'key not found' unless @database.has_key? key
143
+ @database[newkey] = @database[key]
144
+ @database.delete key
145
+ end
146
+
147
+ def redis_RENAMENX key, newkey
148
+ return false if @database.has_key? newkey
149
+ redis_RENAME key, newkey
150
+ true
151
+ end
152
+
153
+ def redis_MOVE key, db
154
+ raise unless @database.has_key? key
155
+ raise if Redis.databases[redis_i db].has_key? key
156
+ Redis.databases[redis_i db][key] = @database[key]
157
+ @database.delete key
158
+ true
159
+ rescue
160
+ false
161
+ end
162
+
163
+
164
+ end
165
+ end