redi2casa 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -6,7 +6,8 @@ Redis datastructures using Cassandra
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
- gem 'redi2casa'
9
+ gem "cassandra-cql", :git => "git://github.com/vireshas/cassandra-cql.git"
10
+ gem "redi2casa", :git => "git://github.com/vireshas/redi2casa.git"
10
11
 
11
12
  And then execute:
12
13
 
@@ -17,29 +18,24 @@ Or install it yourself as:
17
18
  $ gem install redi2casa
18
19
 
19
20
  ## Usage
20
- #### create these tables
21
+
22
+ #### configuring cassandra
23
+
24
+ create namespace: CREATE KEYSPACE redi2casa WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
21
25
  cqlsh: create table sets(namespace text primary key, key map<text, text>);
22
26
  cassandra-cli: create column family counters with column_type = 'Standard' and comparator = 'UTF8Type' and default_validation_class = 'CounterColumnType' and key_validation_class = 'UTF8Type';
23
-
27
+
24
28
  ### Now you can
25
- require "redi2casa"
26
- r = Redi2casa.new "127.0.0.1:9160", {:keyspace => 'keyspace1'}
27
-
28
- #### hincrby & hget
29
29
 
30
- Increment: r.hincrby "c", "e", 10
31
- Decrement: r.hincrby "c", "e", -10
32
- r.hget "c", "e", "counters"
33
-
34
- #### hset & hget
30
+ require "redi2casa"
31
+ r = Redi2casa.new "127.0.0.1:9160", {:keyspace => 'keyspace1'}
35
32
 
33
+ You should keep one thing in mind while using hget with hash!, you should explicitly mention that you are using hget with hash
34
+
36
35
  r.hset "a", "b", "hello_world"
37
- r.hget "a", "b", "sets"
38
-
39
- #### hgetall
40
-
41
- r.hgetall "c", "counters"
42
- r.hgetall "a", "sets"
36
+ r.hget "a", "b", "hash" #the last parameter is needed for hash
37
+
38
+ You can find more examples here: https://gist.github.com/vireshas/99bc322cf0ac42fbf7ee
43
39
 
44
40
  ## Contributing
45
41
 
@@ -1,6 +1,6 @@
1
1
  class Redi2casa
2
2
  def hget namespace, key, type = "counters"
3
- if type.to_s == "sets"
3
+ if type.to_s == "hash"
4
4
  resp = @db_conn.execute("select * from sets where namespace = ?", namespace).fetch
5
5
  resp.nil? ? nil : resp.to_hash['key'][key]
6
6
  elsif type.to_s == "counters"
@@ -5,7 +5,7 @@ class Redi2casa
5
5
  hsh = {}
6
6
  response.fetch {|resp| entry = resp.to_hash; hsh[entry["column1"]] = entry["value"] }
7
7
  hsh
8
- elsif type.to_s == "sets"
8
+ elsif type.to_s == "hash"
9
9
  resp = @db_conn.execute("select * from sets where namespace = ?", key).fetch
10
10
  resp.nil? ? nil : resp.to_hash['key']
11
11
  end
@@ -1,5 +1,5 @@
1
1
  class Redi2casa
2
- def hincrby namespace, key, value
2
+ def hincrby namespace, key, value = 1
3
3
  query = "update counters set value = value + #{value} where key='#{namespace}' and column1 = '#{key}'"
4
4
  @db_conn.execute(query)
5
5
  end
@@ -0,0 +1,10 @@
1
+ class Redi2casa
2
+ def lpop namespace
3
+ resp = @db_conn.execute("select values from lists where namespace='#{namespace}'")
4
+ values = []
5
+ resp.fetch {|entry| values = entry.to_hash["values"]}
6
+ resp = values.shift
7
+ lrepush(namespace, values)
8
+ resp
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ class Redi2casa
2
+ def lpush namespace, data
3
+ @db_conn.execute("UPDATE lists SET values = ['#{data}'] + values WHERE namespace = '#{namespace}'")
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ class Redi2casa
2
+ def lrange namespace, first, last
3
+ resp = @db_conn.execute("select values from lists where namespace='#{namespace}'")
4
+ resp.fetch {|entry| return entry.to_hash["values"][first..last]}
5
+ end
6
+ end
@@ -0,0 +1,26 @@
1
+ class Redi2casa
2
+ def ltrim namespace, first, last
3
+ resp = @db_conn.execute("select values from lists where namespace='#{namespace}'")
4
+ values = {}
5
+ resp.fetch {|entry| values = entry.to_hash["values"]}
6
+ values_count = values.count
7
+ #if first is greater than list length, redis returns empty list
8
+ #if negative value of last is equal or greater than list length a similar behaviour is shown
9
+ if (first > values_count) || (-last >= values_count)
10
+ lflush(namespace)
11
+ elsif last > values_count
12
+ last = values_count
13
+ else
14
+ new_list = values[first..last]
15
+ lrepush(namespace, new_list)
16
+ end
17
+ end
18
+
19
+ def lflush namespace
20
+ @db_conn.execute("UPDATE lists SET values = [] WHERE namespace = '#{namespace}'")
21
+ end
22
+
23
+ def lrepush namespace, list
24
+ @db_conn.execute("UPDATE lists SET values = [?] WHERE namespace = '#{namespace}'", list)
25
+ end
26
+ end
@@ -0,0 +1,10 @@
1
+ class Redi2casa
2
+ def rpop namespace
3
+ resp = @db_conn.execute("select values from lists where namespace='#{namespace}'")
4
+ values = []
5
+ resp.fetch {|entry| values = entry.to_hash["values"]}
6
+ resp = values.pop
7
+ lrepush(namespace, values)
8
+ resp
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ class Redi2casa
2
+ def rpush namespace, data
3
+ @db_conn.execute("UPDATE lists SET values = values + ['#{data}'] WHERE namespace = '#{namespace}'")
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  class Redi2casa
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redi2casa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-08 00:00:00.000000000 Z
12
+ date: 2013-11-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -60,6 +60,12 @@ files:
60
60
  - lib/redi2casa/hgetall.rb
61
61
  - lib/redi2casa/hincrby.rb
62
62
  - lib/redi2casa/hset.rb
63
+ - lib/redi2casa/lpop.rb
64
+ - lib/redi2casa/lpush.rb
65
+ - lib/redi2casa/lrange.rb
66
+ - lib/redi2casa/ltrim.rb
67
+ - lib/redi2casa/rpop.rb
68
+ - lib/redi2casa/rpush.rb
63
69
  - lib/redi2casa/version.rb
64
70
  - redi2casa.gemspec
65
71
  homepage: https://github.com/vireshas/redi2casa
@@ -77,7 +83,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
77
83
  version: '0'
78
84
  segments:
79
85
  - 0
80
- hash: -342368479
86
+ hash: -990581828638707000
81
87
  required_rubygems_version: !ruby/object:Gem::Requirement
82
88
  none: false
83
89
  requirements:
@@ -86,10 +92,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
92
  version: '0'
87
93
  segments:
88
94
  - 0
89
- hash: -342368479
95
+ hash: -990581828638707000
90
96
  requirements: []
91
97
  rubyforge_project:
92
- rubygems_version: 1.8.24
98
+ rubygems_version: 1.8.25
93
99
  signing_key:
94
100
  specification_version: 3
95
101
  summary: Moving redis calls to equivalent cassandra calls can be a pain unless you