hallelujah-cassandra 0.12.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. data/CHANGELOG +111 -0
  2. data/Gemfile +8 -0
  3. data/LICENSE +202 -0
  4. data/Manifest +91 -0
  5. data/README.md +352 -0
  6. data/Rakefile +171 -0
  7. data/bin/cassandra_helper +16 -0
  8. data/conf/0.6/cassandra.in.sh +47 -0
  9. data/conf/0.6/log4j.properties +38 -0
  10. data/conf/0.6/schema.json +57 -0
  11. data/conf/0.6/storage-conf.xml +352 -0
  12. data/conf/0.7/cassandra.in.sh +46 -0
  13. data/conf/0.7/cassandra.yaml +336 -0
  14. data/conf/0.7/log4j-server.properties +41 -0
  15. data/conf/0.7/schema.json +57 -0
  16. data/conf/0.7/schema.txt +45 -0
  17. data/conf/0.8/cassandra.in.sh +41 -0
  18. data/conf/0.8/cassandra.yaml +61 -0
  19. data/conf/0.8/log4j-server.properties +40 -0
  20. data/conf/0.8/schema.json +69 -0
  21. data/conf/0.8/schema.txt +51 -0
  22. data/conf/1.0/cassandra.in.sh +41 -0
  23. data/conf/1.0/cassandra.yaml +415 -0
  24. data/conf/1.0/log4j-server.properties +40 -0
  25. data/conf/1.0/schema.json +69 -0
  26. data/conf/1.0/schema.txt +51 -0
  27. data/conf/1.1/cassandra.in.sh +41 -0
  28. data/conf/1.1/cassandra.yaml +560 -0
  29. data/conf/1.1/log4j-server.properties +44 -0
  30. data/conf/1.1/schema.json +69 -0
  31. data/conf/1.1/schema.txt +51 -0
  32. data/hallelujah-cassandra.gemspec +43 -0
  33. data/lib/cassandra.rb +39 -0
  34. data/lib/cassandra/0.6.rb +7 -0
  35. data/lib/cassandra/0.6/cassandra.rb +113 -0
  36. data/lib/cassandra/0.6/columns.rb +78 -0
  37. data/lib/cassandra/0.6/protocol.rb +90 -0
  38. data/lib/cassandra/0.7.rb +7 -0
  39. data/lib/cassandra/0.7/cassandra.rb +2 -0
  40. data/lib/cassandra/0.7/columns.rb +4 -0
  41. data/lib/cassandra/0.7/protocol.rb +5 -0
  42. data/lib/cassandra/0.8.rb +7 -0
  43. data/lib/cassandra/0.8/cassandra.rb +25 -0
  44. data/lib/cassandra/0.8/columns.rb +28 -0
  45. data/lib/cassandra/0.8/protocol.rb +10 -0
  46. data/lib/cassandra/1.0.rb +7 -0
  47. data/lib/cassandra/1.0/cassandra.rb +25 -0
  48. data/lib/cassandra/1.0/columns.rb +28 -0
  49. data/lib/cassandra/1.0/protocol.rb +12 -0
  50. data/lib/cassandra/1.1.rb +6 -0
  51. data/lib/cassandra/1.1/cassandra.rb +25 -0
  52. data/lib/cassandra/1.1/columns.rb +28 -0
  53. data/lib/cassandra/1.1/protocol.rb +12 -0
  54. data/lib/cassandra/array.rb +8 -0
  55. data/lib/cassandra/cassandra.rb +1070 -0
  56. data/lib/cassandra/column_family.rb +3 -0
  57. data/lib/cassandra/columns.rb +147 -0
  58. data/lib/cassandra/comparable.rb +28 -0
  59. data/lib/cassandra/composite.rb +118 -0
  60. data/lib/cassandra/constants.rb +11 -0
  61. data/lib/cassandra/debug.rb +9 -0
  62. data/lib/cassandra/helpers.rb +41 -0
  63. data/lib/cassandra/keyspace.rb +3 -0
  64. data/lib/cassandra/long.rb +58 -0
  65. data/lib/cassandra/mock.rb +511 -0
  66. data/lib/cassandra/ordered_hash.rb +192 -0
  67. data/lib/cassandra/protocol.rb +120 -0
  68. data/lib/cassandra/time.rb +11 -0
  69. data/test/cassandra_client_test.rb +20 -0
  70. data/test/cassandra_mock_test.rb +122 -0
  71. data/test/cassandra_test.rb +922 -0
  72. data/test/comparable_types_test.rb +45 -0
  73. data/test/composite_type_test.rb +29 -0
  74. data/test/eventmachine_test.rb +42 -0
  75. data/test/ordered_hash_test.rb +386 -0
  76. data/test/test_helper.rb +15 -0
  77. data/vendor/0.6/gen-rb/cassandra.rb +1481 -0
  78. data/vendor/0.6/gen-rb/cassandra_constants.rb +12 -0
  79. data/vendor/0.6/gen-rb/cassandra_types.rb +482 -0
  80. data/vendor/0.7/gen-rb/cassandra.rb +1936 -0
  81. data/vendor/0.7/gen-rb/cassandra_constants.rb +12 -0
  82. data/vendor/0.7/gen-rb/cassandra_types.rb +681 -0
  83. data/vendor/0.8/gen-rb/cassandra.rb +2215 -0
  84. data/vendor/0.8/gen-rb/cassandra_constants.rb +12 -0
  85. data/vendor/0.8/gen-rb/cassandra_types.rb +824 -0
  86. data/vendor/1.0/gen-rb/cassandra.rb +2215 -0
  87. data/vendor/1.0/gen-rb/cassandra_constants.rb +12 -0
  88. data/vendor/1.0/gen-rb/cassandra_types.rb +857 -0
  89. data/vendor/1.1/gen-rb/cassandra.rb +2571 -0
  90. data/vendor/1.1/gen-rb/cassandra_constants.rb +12 -0
  91. data/vendor/1.1/gen-rb/cassandra_types.rb +928 -0
  92. metadata +260 -0
@@ -0,0 +1,192 @@
1
+ # OrderedHash is namespaced to prevent conflicts with other implementations
2
+ class Cassandra
3
+ class OrderedHashInt < Hash #:nodoc:
4
+ def initialize(*args, &block)
5
+ super
6
+ @keys = []
7
+ end
8
+
9
+ def self.[](*args)
10
+ ordered_hash = new
11
+
12
+ if (args.length == 1 && args.first.is_a?(Array))
13
+ args.first.each do |key_value_pair|
14
+ next unless (key_value_pair.is_a?(Array))
15
+ ordered_hash[key_value_pair[0]] = key_value_pair[1]
16
+ end
17
+
18
+ return ordered_hash
19
+ end
20
+
21
+ unless (args.size % 2 == 0)
22
+ raise ArgumentError.new("odd number of arguments for Hash")
23
+ end
24
+
25
+ args.each_with_index do |val, ind|
26
+ next if (ind % 2 != 0)
27
+ ordered_hash[val] = args[ind + 1]
28
+ end
29
+
30
+ ordered_hash
31
+ end
32
+
33
+ def initialize_copy(other)
34
+ super
35
+ # make a deep copy of keys
36
+ @keys = other.keys
37
+ end
38
+
39
+ def []=(key, value)
40
+ @keys << key if !has_key?(key)
41
+ super
42
+ end
43
+
44
+ def delete(key)
45
+ if has_key? key
46
+ index = @keys.index(key)
47
+ @keys.delete_at index
48
+ end
49
+ super
50
+ end
51
+
52
+ def delete_if
53
+ super
54
+ sync_keys!
55
+ self
56
+ end
57
+
58
+ def reject!
59
+ super
60
+ sync_keys!
61
+ self
62
+ end
63
+
64
+ def reject(&block)
65
+ dup.reject!(&block)
66
+ end
67
+
68
+ def keys
69
+ @keys.dup
70
+ end
71
+
72
+ def values
73
+ @keys.collect { |key| self[key] }
74
+ end
75
+
76
+ def to_hash
77
+ self
78
+ end
79
+
80
+ def to_a
81
+ @keys.map { |key| [ key, self[key] ] }
82
+ end
83
+
84
+ def each_key
85
+ @keys.each { |key| yield key }
86
+ end
87
+
88
+ def each_value
89
+ @keys.each { |key| yield self[key]}
90
+ end
91
+
92
+ def each
93
+ @keys.each {|key| yield [key, self[key]]}
94
+ end
95
+
96
+ alias_method :each_pair, :each
97
+
98
+ def clear
99
+ super
100
+ @keys.clear
101
+ self
102
+ end
103
+
104
+ def shift
105
+ k = @keys.first
106
+ v = delete(k)
107
+ [k, v]
108
+ end
109
+
110
+ def merge!(other_hash)
111
+ other_hash.each {|k,v| self[k] = v }
112
+ self
113
+ end
114
+
115
+ def merge(other_hash)
116
+ dup.merge!(other_hash)
117
+ end
118
+
119
+ # When replacing with another hash, the initial order of our keys must come from the other hash -ordered or not.
120
+ def replace(other)
121
+ super
122
+ @keys = other.keys
123
+ self
124
+ end
125
+
126
+ def reverse
127
+ OrderedHashInt[self.to_a.reverse]
128
+ end
129
+
130
+ private
131
+
132
+ def sync_keys!
133
+ @keys.delete_if {|k| !has_key?(k)}
134
+ end
135
+ end
136
+
137
+ class OrderedHash < OrderedHashInt #:nodoc:
138
+ def initialize(*args, &block)
139
+ @timestamps = OrderedHashInt.new
140
+ super
141
+ end
142
+
143
+ def initialize_copy(other)
144
+ @timestamps = other.timestamps
145
+ super
146
+ end
147
+
148
+ def []=(key, value, timestamp = nil)
149
+ @timestamps[key] = timestamp
150
+ super(key, value)
151
+ end
152
+
153
+ def delete(key)
154
+ @timestamps.delete(key)
155
+ super
156
+ end
157
+
158
+ def delete_if(&block)
159
+ @timestamps.delete_if(&block)
160
+ super
161
+ end
162
+
163
+ def reject!(&block)
164
+ @timestamps.reject!(&block)
165
+ super
166
+ end
167
+
168
+ def timestamps
169
+ @timestamps.dup
170
+ end
171
+
172
+ def clear
173
+ @timestamps.clear
174
+ super
175
+ end
176
+
177
+ def shift
178
+ k, v = super
179
+ @timestamps.delete(k)
180
+ [k, v]
181
+ end
182
+
183
+ def replace(other)
184
+ @timestamps = other.timestamps
185
+ super
186
+ end
187
+
188
+ def inspect
189
+ "#<OrderedHash #{super}\n#{@timestamps.inspect}>"
190
+ end
191
+ end
192
+ end
@@ -0,0 +1,120 @@
1
+ class Cassandra
2
+ # Inner methods for actually doing the Thrift calls
3
+ module Protocol #:nodoc:
4
+ private
5
+
6
+ def _mutate(mutation_map, consistency_level)
7
+ client.batch_mutate(mutation_map, consistency_level)
8
+ end
9
+
10
+ def _remove(key, column_path, timestamp, consistency_level)
11
+ client.remove(key, column_path, timestamp, consistency_level)
12
+ end
13
+
14
+ def _count_columns(column_family, key, super_column, start, stop, count, consistency)
15
+ client.get_count(key,
16
+ CassandraThrift::ColumnParent.new(:column_family => column_family, :super_column => super_column),
17
+ CassandraThrift::SlicePredicate.new(:slice_range =>
18
+ CassandraThrift::SliceRange.new(
19
+ :start => start || '',
20
+ :finish => stop || '',
21
+ :count => count || 100
22
+ )),
23
+ consistency
24
+ )
25
+ end
26
+
27
+ # FIXME: Add support for start, stop, count
28
+ def _get_columns(column_family, key, columns, sub_columns, consistency)
29
+ result = if is_super(column_family)
30
+ if sub_columns
31
+ columns_to_hash(column_family, client.get_slice(key,
32
+ CassandraThrift::ColumnParent.new(:column_family => column_family, :super_column => columns),
33
+ CassandraThrift::SlicePredicate.new(:column_names => sub_columns),
34
+ consistency))
35
+ else
36
+ columns_to_hash(column_family, client.get_slice(key,
37
+ CassandraThrift::ColumnParent.new(:column_family => column_family),
38
+ CassandraThrift::SlicePredicate.new(:column_names => columns),
39
+ consistency))
40
+ end
41
+ else
42
+ columns_to_hash(column_family, client.get_slice(key,
43
+ CassandraThrift::ColumnParent.new(:column_family => column_family),
44
+ CassandraThrift::SlicePredicate.new(:column_names => columns),
45
+ consistency))
46
+ end
47
+
48
+ klass = column_name_class(column_family)
49
+ (sub_columns || columns).map { |name| result[klass.new(name)] }
50
+ end
51
+
52
+ def _multiget(column_family, keys, column, sub_column, count, start, finish, reversed, consistency)
53
+ # Single values; count and range parameters have no effect
54
+ if is_super(column_family) and sub_column
55
+ predicate = CassandraThrift::SlicePredicate.new(:column_names => [sub_column])
56
+ column_parent = CassandraThrift::ColumnParent.new(:column_family => column_family, :super_column => column)
57
+ column_hash = multi_sub_columns_to_hash!(column_family, client.multiget_slice(keys, column_parent, predicate, consistency))
58
+
59
+ klass = sub_column_name_class(column_family)
60
+ keys.inject({}){|hash, key| hash[key] = column_hash[key][klass.new(sub_column)]; hash}
61
+ elsif !is_super(column_family) and column
62
+ predicate = CassandraThrift::SlicePredicate.new(:column_names => [column])
63
+ column_parent = CassandraThrift::ColumnParent.new(:column_family => column_family)
64
+ column_hash = multi_columns_to_hash!(column_family, client.multiget_slice(keys, column_parent, predicate, consistency))
65
+
66
+ klass = column_name_class(column_family)
67
+ keys.inject({}){|hash, key| hash[key] = column_hash[key][klass.new(column)]; hash}
68
+
69
+ # Slices
70
+ else
71
+ predicate = CassandraThrift::SlicePredicate.new(:slice_range =>
72
+ CassandraThrift::SliceRange.new(
73
+ :reversed => reversed,
74
+ :count => count,
75
+ :start => start,
76
+ :finish => finish))
77
+
78
+ if is_super(column_family) and column
79
+ column_parent = CassandraThrift::ColumnParent.new(:column_family => column_family, :super_column => column)
80
+ multi_sub_columns_to_hash!(column_family, client.multiget_slice(keys, column_parent, predicate, consistency))
81
+ else
82
+ column_parent = CassandraThrift::ColumnParent.new(:column_family => column_family)
83
+ multi_columns_to_hash!(column_family, client.multiget_slice(keys, column_parent, predicate, consistency))
84
+ end
85
+ end
86
+ end
87
+
88
+ def _get_range(column_family, start_key, finish_key, key_count, columns, start, finish, count, consistency, reversed=false)
89
+ column_parent = CassandraThrift::ColumnParent.new(:column_family => column_family)
90
+ predicate = if columns
91
+ CassandraThrift::SlicePredicate.new(:column_names => columns)
92
+ else
93
+ CassandraThrift::SlicePredicate.new(:slice_range =>
94
+ CassandraThrift::SliceRange.new(
95
+ :start => start,
96
+ :finish => finish,
97
+ :count => count,
98
+ :reversed => reversed))
99
+ end
100
+ range = CassandraThrift::KeyRange.new(:start_key => start_key, :end_key => finish_key, :count => key_count)
101
+ client.get_range_slices(column_parent, predicate, range, consistency)
102
+ end
103
+
104
+ # TODO: Supercolumn support
105
+ def _get_indexed_slices(column_family, index_clause, column, count, start, finish, reversed, consistency)
106
+ column_parent = CassandraThrift::ColumnParent.new(:column_family => column_family)
107
+ if column
108
+ predicate = CassandraThrift::SlicePredicate.new(:column_names => [column])
109
+ else
110
+ predicate = CassandraThrift::SlicePredicate.new(:slice_range =>
111
+ CassandraThrift::SliceRange.new(
112
+ :reversed => reversed,
113
+ :count => count,
114
+ :start => start,
115
+ :finish => finish))
116
+ end
117
+ client.get_indexed_slices(column_parent, index_clause, predicate, consistency)
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,11 @@
1
+
2
+ class Time
3
+ def self.stamp
4
+ Time.now.stamp
5
+ end
6
+
7
+ def stamp
8
+ to_i * 1_000_000 + usec
9
+ end
10
+ end
11
+
@@ -0,0 +1,20 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ class CassandraClientTest < Test::Unit::TestCase
4
+ include Cassandra::Constants
5
+
6
+ def setup
7
+ @twitter = Cassandra.new('Twitter', "127.0.0.1:9160", :retries => 2, :exception_classes => [])
8
+ end
9
+
10
+ def test_client_method_is_called
11
+ assert_nil @twitter.instance_variable_get(:@client)
12
+ @twitter.insert(:Statuses, key, {'1' => 'v', '2' => 'v', '3' => 'v'})
13
+ assert_not_nil @twitter.instance_variable_get(:@client)
14
+ end
15
+
16
+ def key
17
+ caller.first[/`(.*?)'/, 1]
18
+ end
19
+
20
+ end
@@ -0,0 +1,122 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/cassandra_test')
3
+ require 'cassandra/mock'
4
+ require 'json'
5
+
6
+ class CassandraMockTest < CassandraTest
7
+ include Cassandra::Constants
8
+
9
+ def setup
10
+ @test_schema = JSON.parse(File.read(File.join(File.expand_path(File.dirname(__FILE__)), '..','conf', CASSANDRA_VERSION, 'schema.json')))
11
+ @twitter = Cassandra::Mock.new('Twitter', @test_schema)
12
+ @twitter.clear_keyspace!
13
+
14
+ @blogs = Cassandra::Mock.new('Multiblog', @test_schema)
15
+ @blogs.clear_keyspace!
16
+
17
+ @blogs_long = Cassandra::Mock.new('MultiblogLong', @test_schema)
18
+ @blogs_long.clear_keyspace!
19
+
20
+ @type_conversions = Cassandra::Mock.new('TypeConversions', @test_schema)
21
+ @type_conversions.clear_keyspace!
22
+
23
+ @uuids = (0..6).map {|i| SimpleUUID::UUID.new(Time.at(2**(24+i))) }
24
+ @longs = (0..6).map {|i| Long.new(Time.at(2**(24+i))) }
25
+ @composites = [
26
+ Cassandra::Composite.new([5].pack('N'), "zebra"),
27
+ Cassandra::Composite.new([5].pack('N'), "aardvark"),
28
+ Cassandra::Composite.new([1].pack('N'), "elephant"),
29
+ Cassandra::Composite.new([10].pack('N'), "kangaroo"),
30
+ ]
31
+ end
32
+
33
+ def test_setup
34
+ assert @twitter
35
+ assert @blogs
36
+ assert @blogs_long
37
+ end
38
+
39
+ def test_schema_for_keyspace
40
+ data = @test_schema['Twitter']
41
+ stuff = @twitter.send(:schema_for_keyspace, 'Twitter')
42
+ data.keys.each do |k|
43
+ assert_equal data[k], stuff[k], k
44
+ end
45
+ end
46
+
47
+ def test_sorting_row_keys
48
+ @twitter.insert(:Statuses, 'b', {:text => 'foo'})
49
+ @twitter.insert(:Statuses, 'a', {:text => 'foo'})
50
+ assert_equal ['a'], @twitter.get_range(:Statuses, :key_count => 1).keys
51
+ end
52
+
53
+ def test_get_range_reversed
54
+ data = 3.times.map { |i| ["body-#{i.to_s}", "v"] }
55
+ hash = Cassandra::OrderedHash[data]
56
+ reversed_hash = Cassandra::OrderedHash[data.reverse]
57
+
58
+ @twitter.insert(:Statuses, "all-keys", hash)
59
+
60
+ columns = @twitter.get_range(:Statuses, :reversed => true)["all-keys"]
61
+ columns.each do |column|
62
+ assert_equal reversed_hash.shift, column
63
+ end
64
+ end
65
+
66
+ def test_get_range_reversed_slice
67
+ data = 4.times.map { |i| ["body-#{i.to_s}", "v"] }
68
+ hash = Cassandra::OrderedHash[data]
69
+ sliced_hash = Cassandra::OrderedHash[data.reverse[1..-1]]
70
+
71
+ @twitter.insert(:Statuses, "all-keys", hash)
72
+
73
+ columns = @twitter.get_range(
74
+ :Statuses,
75
+ :start => sliced_hash.keys.first,
76
+ :reversed => true
77
+ )["all-keys"]
78
+
79
+ columns.each do |column|
80
+ assert_equal sliced_hash.shift, column
81
+ end
82
+ end
83
+
84
+ def test_get_range_count
85
+ data = 3.times.map { |i| ["body-#{i.to_s}", "v"] }
86
+ hash = Cassandra::OrderedHash[data]
87
+
88
+ @twitter.insert(:Statuses, "all-keys", hash)
89
+
90
+ columns = @twitter.get_range(:Statuses, :count => 2)["all-keys"]
91
+ assert_equal 2, columns.count
92
+ end
93
+
94
+ def test_inserting_array_for_indices
95
+ @twitter.insert(:TimelinishThings, 'a', ['1','2'])
96
+ row = @twitter.get(:TimelinishThings, 'a')
97
+ assert_equal({'1' => nil, '2' => nil}, row)
98
+
99
+ assert_raises(ArgumentError) {
100
+ @twitter.insert(:UserRelationships, 'a', ['u1','u2'])
101
+ }
102
+ end
103
+
104
+ def test_column_timestamps
105
+ base_time = Time.now
106
+ @twitter.insert(:Statuses, "time-key", { "body" => "value" })
107
+
108
+ results = @twitter.get(:Statuses, "time-key")
109
+ assert(results.timestamps["body"] / 1000000 >= base_time.to_i)
110
+ end
111
+
112
+ def test_supercolumn_timestamps
113
+ base_time = Time.now
114
+ @twitter.insert(:StatusRelationships, "time-key", { "super" => { @uuids[1] => "value" }})
115
+
116
+ results = @twitter.get(:StatusRelationships, "time-key")
117
+ assert_nil(results.timestamps["super"])
118
+
119
+ columns = results["super"]
120
+ assert(columns.timestamps[@uuids[1]] / 1000000 >= base_time.to_i)
121
+ end
122
+ end