cassandra-mavericks 0.21.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. checksums.yaml +15 -0
  2. data/CHANGELOG +150 -0
  3. data/Gemfile +7 -0
  4. data/LICENSE +202 -0
  5. data/Manifest +94 -0
  6. data/README.md +373 -0
  7. data/Rakefile +191 -0
  8. data/bin/cassandra_helper +16 -0
  9. data/cassandra.gemspec +29 -0
  10. data/conf/0.6/cassandra.in.sh +47 -0
  11. data/conf/0.6/log4j.properties +38 -0
  12. data/conf/0.6/schema.json +57 -0
  13. data/conf/0.6/storage-conf.xml +352 -0
  14. data/conf/0.7/cassandra.in.sh +46 -0
  15. data/conf/0.7/cassandra.yaml +336 -0
  16. data/conf/0.7/log4j-server.properties +41 -0
  17. data/conf/0.7/schema.json +57 -0
  18. data/conf/0.7/schema.txt +45 -0
  19. data/conf/0.8/cassandra.in.sh +41 -0
  20. data/conf/0.8/cassandra.yaml +61 -0
  21. data/conf/0.8/log4j-server.properties +40 -0
  22. data/conf/0.8/schema.json +72 -0
  23. data/conf/0.8/schema.txt +57 -0
  24. data/conf/1.0/cassandra.in.sh +41 -0
  25. data/conf/1.0/cassandra.yaml +415 -0
  26. data/conf/1.0/log4j-server.properties +40 -0
  27. data/conf/1.0/schema.json +72 -0
  28. data/conf/1.0/schema.txt +57 -0
  29. data/conf/1.1/cassandra.in.sh +41 -0
  30. data/conf/1.1/cassandra.yaml +567 -0
  31. data/conf/1.1/log4j-server.properties +44 -0
  32. data/conf/1.1/schema.json +72 -0
  33. data/conf/1.1/schema.txt +57 -0
  34. data/conf/1.2/cassandra.in.sh +41 -0
  35. data/conf/1.2/cassandra.yaml +643 -0
  36. data/conf/1.2/log4j-server.properties +44 -0
  37. data/conf/1.2/schema.json +72 -0
  38. data/conf/1.2/schema.txt +57 -0
  39. data/ext/cassandra_native.c +69 -0
  40. data/ext/extconf.rb +9 -0
  41. data/lib/cassandra.rb +47 -0
  42. data/lib/cassandra/0.6.rb +7 -0
  43. data/lib/cassandra/0.6/cassandra.rb +113 -0
  44. data/lib/cassandra/0.6/columns.rb +78 -0
  45. data/lib/cassandra/0.6/protocol.rb +91 -0
  46. data/lib/cassandra/0.7.rb +7 -0
  47. data/lib/cassandra/0.7/cassandra.rb +2 -0
  48. data/lib/cassandra/0.7/columns.rb +4 -0
  49. data/lib/cassandra/0.7/protocol.rb +5 -0
  50. data/lib/cassandra/0.8.rb +7 -0
  51. data/lib/cassandra/0.8/cassandra.rb +25 -0
  52. data/lib/cassandra/0.8/columns.rb +28 -0
  53. data/lib/cassandra/0.8/protocol.rb +10 -0
  54. data/lib/cassandra/1.0.rb +7 -0
  55. data/lib/cassandra/1.0/cassandra.rb +1 -0
  56. data/lib/cassandra/1.0/columns.rb +1 -0
  57. data/lib/cassandra/1.0/protocol.rb +1 -0
  58. data/lib/cassandra/1.1.rb +7 -0
  59. data/lib/cassandra/1.1/cassandra.rb +1 -0
  60. data/lib/cassandra/1.1/columns.rb +1 -0
  61. data/lib/cassandra/1.1/protocol.rb +1 -0
  62. data/lib/cassandra/1.2.rb +7 -0
  63. data/lib/cassandra/1.2/cassandra.rb +1 -0
  64. data/lib/cassandra/1.2/columns.rb +1 -0
  65. data/lib/cassandra/1.2/protocol.rb +1 -0
  66. data/lib/cassandra/array.rb +8 -0
  67. data/lib/cassandra/batch.rb +41 -0
  68. data/lib/cassandra/cassandra.rb +1091 -0
  69. data/lib/cassandra/column_family.rb +3 -0
  70. data/lib/cassandra/columns.rb +172 -0
  71. data/lib/cassandra/comparable.rb +28 -0
  72. data/lib/cassandra/composite.rb +137 -0
  73. data/lib/cassandra/constants.rb +11 -0
  74. data/lib/cassandra/debug.rb +9 -0
  75. data/lib/cassandra/dynamic_composite.rb +118 -0
  76. data/lib/cassandra/helpers.rb +41 -0
  77. data/lib/cassandra/keyspace.rb +3 -0
  78. data/lib/cassandra/long.rb +58 -0
  79. data/lib/cassandra/mock.rb +536 -0
  80. data/lib/cassandra/ordered_hash.rb +195 -0
  81. data/lib/cassandra/protocol.rb +137 -0
  82. data/lib/cassandra/time.rb +11 -0
  83. data/test/cassandra_client_test.rb +20 -0
  84. data/test/cassandra_mock_test.rb +128 -0
  85. data/test/cassandra_test.rb +1367 -0
  86. data/test/comparable_types_test.rb +45 -0
  87. data/test/composite_type_test.rb +86 -0
  88. data/test/eventmachine_test.rb +42 -0
  89. data/test/ordered_hash_test.rb +386 -0
  90. data/test/test_helper.rb +19 -0
  91. data/vendor/0.6/gen-rb/cassandra.rb +1481 -0
  92. data/vendor/0.6/gen-rb/cassandra_constants.rb +12 -0
  93. data/vendor/0.6/gen-rb/cassandra_types.rb +482 -0
  94. data/vendor/0.7/gen-rb/cassandra.rb +1936 -0
  95. data/vendor/0.7/gen-rb/cassandra_constants.rb +12 -0
  96. data/vendor/0.7/gen-rb/cassandra_types.rb +681 -0
  97. data/vendor/0.8/gen-rb/cassandra.rb +2215 -0
  98. data/vendor/0.8/gen-rb/cassandra_constants.rb +12 -0
  99. data/vendor/0.8/gen-rb/cassandra_types.rb +824 -0
  100. data/vendor/1.0/gen-rb/cassandra.rb +2215 -0
  101. data/vendor/1.0/gen-rb/cassandra_constants.rb +12 -0
  102. data/vendor/1.0/gen-rb/cassandra_types.rb +857 -0
  103. data/vendor/1.1/gen-rb/cassandra.rb +2571 -0
  104. data/vendor/1.1/gen-rb/cassandra_constants.rb +12 -0
  105. data/vendor/1.1/gen-rb/cassandra_types.rb +928 -0
  106. data/vendor/1.2/gen-rb/cassandra.rb +3013 -0
  107. data/vendor/1.2/gen-rb/cassandra_constants.rb +13 -0
  108. data/vendor/1.2/gen-rb/cassandra_types.rb +965 -0
  109. metadata +288 -0
@@ -0,0 +1,373 @@
1
+ # cassandra
2
+ A Ruby client for the Cassandra distributed database.
3
+
4
+ Supports 1.8.7, 1.9.2, and rubinius on Cassandra 0.6.13, 0.7.9, 0.8.6, 1.0.0-rc2, 1.1.5.
5
+
6
+ ## Getting Started
7
+
8
+ Here is a quick sample of the general use (more details in Read/Write
9
+ API below):
10
+
11
+ require 'cassandra'
12
+ client = Cassandra.new('Twitter', '127.0.0.1:9160')
13
+ client.insert(:Users, "5", {'screen_name' => "buttonscat"})
14
+
15
+ ## License
16
+
17
+ Copyright 2009-2011 Twitter, Inc. See included LICENSE file. Portions copyright 2004-2009 David Heinemeier Hansson, and used with permission.
18
+
19
+ ## Cassandra Version
20
+
21
+ The Cassandra project is under very active development, and as such
22
+ there are a few different versions that you may need to use this gem
23
+ with. We have set up an easy sure fire mechanism for selecting the
24
+ specific version that you are connecting to while requiring the gem.
25
+
26
+ #### Require Method
27
+ The default version is the currently stable release of cassandra. (0.8
28
+ at this time.)
29
+
30
+ To use the default version simply use a normal require:
31
+
32
+ require 'cassandra'
33
+
34
+ To use a specific version (1.0 in this example) you would use a
35
+ slightly differently formatted require:
36
+
37
+ require 'cassandra/1.0'
38
+
39
+ #### Environment Variable Method
40
+ These mechanisms work well when you are using the cassandra gem in your
41
+ own projects or irb, but if you would rather not hard code your app to a
42
+ specific version you can always specify an environment variable with the
43
+ version you are using:
44
+
45
+ export CASSANDRA_VERSION=0.8
46
+
47
+ Then you would use the default require as listed above:
48
+
49
+ require 'cassandra'
50
+
51
+ ## Read/Write API Method Reference
52
+
53
+ ### insert
54
+
55
+ * column\_family - The column\_family that you are inserting into.
56
+ * key - The row key to insert.
57
+ * hash - The columns or super columns to insert.
58
+ * options - Valid options are:
59
+ * :timestamp - Uses the current time if none specified.
60
+ * :consistency - Uses the default write consistency if none specified.
61
+ * :ttl - If specified this is the number of seconds after the insert that this value will be available.
62
+
63
+ This is the main method used to insert rows into cassandra. If the
64
+ column\_family that you are inserting into is a SuperColumnFamily then
65
+ the hash passed in should be a nested hash, otherwise it should be a
66
+ flat hash.
67
+
68
+ This method can also be called while in batch mode. If in batch mode
69
+ then we queue up the mutations (an insert in this case) and pass them to
70
+ cassandra in a single batch at the end of the block.
71
+
72
+ Example:
73
+
74
+ @client.insert(:Statuses, key, {'body' => 'v', 'user' => 'v'})
75
+
76
+ columns = {@uuids[1] => 'v1', @uuids[2] => 'v2'}
77
+ @client.insert(:StatusRelationships, key, {'user_timelines' => columns})
78
+
79
+
80
+ ### remove
81
+
82
+ * column\_family - The column\_family that you are working with.
83
+ * key - The row key to remove (or remove columns from).
84
+ * columns - Either a single super_column or a list of columns to remove.
85
+ * sub_columns - The list of sub\_columns to remove.
86
+ * options - Valid options are:
87
+ * :timestamp - Uses the current time if none specified.
88
+ * :consistency - Uses the default write consistency if none specified.
89
+
90
+ This method is used to delete (actually marking them as deleted with a
91
+ tombstone) rows, columns, or super columns depending on the parameters
92
+ passed. If only a key is passed the entire row will be marked as deleted.
93
+ If a column name is passed in that column will be deleted.
94
+
95
+ Example:
96
+
97
+ @client.insert(:Statuses, key, {'body' => 'v', 'subject' => 'v'})
98
+
99
+ @client.remove(:Statuses, key, 'body') # removes the 'body' column
100
+ @client.remove(:Statuses, key) # removes the row
101
+
102
+ ### count\_columns
103
+
104
+ Count the columns for the provided parameters.
105
+
106
+ * column\_family - The column\_family that you are working with.
107
+ * key - The row key.
108
+ * columns - Either a single super_column or a list of columns.
109
+ * sub_columns - The list of sub\_columns to select.
110
+ * options - Valid options are:
111
+ * :start - The column name to start from.
112
+ * :stop - The column name to stop at.
113
+ * :count - The maximum count of columns to return. (By default cassandra will count up to 100 columns)
114
+ * :consistency - Uses the default read consistency if none specified.
115
+
116
+ Example:
117
+
118
+ @client.insert(:Statuses, key, {'body' => 'v1', 'user' => 'v2'})
119
+ @client.count_columns(:Statuses, key) # returns 2
120
+
121
+ ### get
122
+
123
+ Return a hash (actually, a Cassandra::OrderedHash) or a single value
124
+ representing the element at the column_family:key:[column]:[sub_column]
125
+ path you request.
126
+
127
+ * column\_family - The column\_family that you are working with.
128
+ * key - The row key to select.
129
+ * columns - Either a single super\_column or a list of columns.
130
+ * sub\_columns - The list of sub\_columns to select.
131
+ * options - Valid options are:
132
+ * :count - The number of columns requested to be returned.
133
+ * :start - The starting value for selecting a range of columns.
134
+ * :finish - The final value for selecting a range of columns.
135
+ * :reversed - If set to true the results will be returned in
136
+ reverse order.
137
+ * :consistency - Uses the default read consistency if none specified.
138
+
139
+ Example:
140
+
141
+ @client.insert(:Users, key, {'body' => 'v', 'user' => 'v'})
142
+ @client.get(:Users, key)) # returns {'body' => 'v', 'user' => 'v'}
143
+
144
+ ### multi\_get
145
+
146
+ Multi-key version of Cassandra#get.
147
+
148
+ This method allows you to select multiple rows with a single query.
149
+ If a key that is passed in doesn't exist an empty hash will be
150
+ returned.
151
+
152
+ Supports the same parameters as Cassandra#get.
153
+
154
+ * column_family - The column_family that you are working with.
155
+ * key - An array of keys to select.
156
+ * columns - Either a single super_column or a list of columns.
157
+ * sub_columns - The list of sub\_columns to select.
158
+ * options - Valid options are:
159
+ * :count - The number of columns requested to be returned.
160
+ * :start - The starting value for selecting a range of columns.
161
+ * :finish - The final value for selecting a range of columns.
162
+ * :reversed - If set to true the results will be returned in reverse order.
163
+ * :consistency - Uses the default read consistency if none specified.
164
+
165
+ Example:
166
+
167
+ @client.insert(:Users, '1', {'body' => 'v1', 'user' => 'v1'})
168
+ @client.insert(:Users, '2', {'body' => 'v2', 'user' => 'v2'})
169
+
170
+ expected = OrderedHash[
171
+ '1', {'body' => 'v1', 'user' => 'v1'},
172
+ '2', {'body' => 'v2', 'user' => 'v2'},
173
+ 'bogus', {}
174
+ ]
175
+ result = @client.multi_get(:Users, ['1', '2', 'bogus'])
176
+
177
+ ### exists?
178
+
179
+ Return true if the column\_family:key:[column]:[sub\_column] path you
180
+ request exists.
181
+
182
+ If passed in only a row key it will query for any columns (limiting
183
+ to 1) for that row key. If a column is passed in it will query for
184
+ that specific column/super column.
185
+
186
+ This method will return true or false.
187
+
188
+ * column\_family - The column\_family that you are working with.
189
+ * key - The row key to check.
190
+ * columns - Either a single super\_column or a list of columns.
191
+ * sub\_columns - The list of sub\_columns to check.
192
+ * options - Valid options are:
193
+ * :consistency - Uses the default read consistency if none specified.
194
+
195
+ Example:
196
+
197
+ @client.insert(:Statuses, 'key', {'body' => 'v'})
198
+ @client.exists?(:Statuses, 'key') # returns true
199
+ @client.exists?(:Statuses, 'bogus') # returns false
200
+ @client.exists?(:Statuses, 'key', 'body') # returns true
201
+ @client.exists?(:Statuses, 'key', 'bogus') # returns false
202
+
203
+ ### get\_range
204
+ Return an Cassandra::OrderedHash containing the columns specified for the given
205
+ range of keys in the column\_family you request.
206
+
207
+ This method is just a convenience wrapper around Cassandra#get_range_single
208
+ and Cassandra#get\_range\_batch. If :key\_size, :batch\_size, or a block
209
+ is passed in Cassandra#get\_range\_batch will be called. Otherwise
210
+ Cassandra#get\_range\_single will be used.
211
+
212
+ The start\_key and finish\_key parameters are only useful for iterating of all records
213
+ as is done in the Cassandra#each and Cassandra#each\_key methods if you are using the
214
+ RandomPartitioner.
215
+
216
+ If the table is partitioned with OrderPreservingPartitioner you may
217
+ use the start\_key and finish\_key params to select all records with
218
+ the same prefix value.
219
+
220
+ If a block is passed in we will yield the row key and columns for
221
+ each record returned.
222
+
223
+ Please note that Cassandra returns a row for each row that has existed in the
224
+ system since gc\_grace\_seconds. This is because deleted row keys are marked as
225
+ deleted, but left in the system until the cluster has had resonable time to replicate the deletion.
226
+ This function attempts to suppress deleted rows (actually any row returned without
227
+ columns is suppressed).
228
+
229
+ * column\_family - The column\_family that you are working with.
230
+ * options - Valid options are:
231
+ * :start\_key - The starting value for selecting a range of keys (only useful with OPP).
232
+ * :finish\_key - The final value for selecting a range of keys (only useful with OPP).
233
+ * :key\_count - The total number of keys to return from the query. (see note regarding deleted records)
234
+ * :batch\_size - The maximum number of keys to return per query. If specified will loop until :key\_count is obtained or all records have been returned.
235
+ * :columns - A list of columns to return.
236
+ * :count - The number of columns requested to be returned.
237
+ * :start - The starting value for selecting a range of columns.
238
+ * :finish - The final value for selecting a range of columns.
239
+ * :reversed - If set to true the results will be returned in reverse order.
240
+ * :consistency - Uses the default read consistency if none specified.
241
+
242
+ Example:
243
+
244
+ 10.times do |i|
245
+ @client.insert(:Statuses, i.to_s, {'body' => '1'})
246
+ end
247
+
248
+ @client.get_range_keys(:Statuses, :key_count => 4)
249
+
250
+ # returns:
251
+ #{
252
+ # '0' => {'body' => '1'},
253
+ # '1' => {'body' => '1'},
254
+ # '2' => {'body' => '1'},
255
+ # '3' => {'body' => '1'}
256
+ #}
257
+
258
+ ### count\_range
259
+
260
+ Return an Array containing all of the keys within a given range.
261
+
262
+ This method just calls Cassandra#get\_range and returns the
263
+ row keys for the records returned.
264
+
265
+ See Cassandra#get\_range for options.
266
+
267
+ ### get\_range\_keys
268
+
269
+ Return an Array containing all of the keys within a given range.
270
+
271
+ This method just calls Cassandra#get\_range and returns the
272
+ row keys for the records returned.
273
+
274
+ See Cassandra#get\_range for options.
275
+
276
+ ### each\_key
277
+ Iterate through each key within the given range parameters. This function can be
278
+ used to iterate over each key in the given column family.
279
+
280
+ This method just calls Cassandra#get\_range and yields each row key.
281
+
282
+ See Cassandra#get\_range for options.
283
+
284
+ Example:
285
+ 10.times do |i|
286
+ @client.insert(:Statuses, k + i.to_s, {"body-#{i.to_s}" => 'v'})
287
+ end
288
+
289
+ @client.each_key(:Statuses) do |key|
290
+ print key
291
+ end
292
+
293
+ # returns 0123456789
294
+
295
+ ### each
296
+ Iterate through each row within the given column\_family.
297
+
298
+ This method just calls Cassandra#get\_range and yields the key and
299
+ columns.
300
+
301
+ See Cassandra#get\_range for options.
302
+
303
+ ### get\_index\_slices
304
+ This method is used to query a secondary index with a set of
305
+ provided search parameters
306
+
307
+ Please note that you can either specify a
308
+ CassandraThrift::IndexClause or an array of hashes with the
309
+ format as below.
310
+
311
+ * column\_family - The Column Family this operation will be run on.
312
+ * index\_clause - This can either be a CassandraThrift::IndexClause or an array of hashes with the following keys:
313
+ * :column\_name - Column to be compared
314
+ * :value - Value to compare against
315
+ * :comparison - Type of comparison to do.
316
+ * options
317
+ * :key\_count - Set maximum number of rows to return. (Only works if CassandraThrift::IndexClause is not passed in.)
318
+ * :key\_start - Set starting row key for search. (Only works if CassandraThrift::IndexClause is not passed in.)
319
+ * :consistency
320
+
321
+ Example:
322
+
323
+ @client.create_index('Twitter', 'Statuses', 'x', 'LongType')
324
+
325
+ @client.insert(:Statuses, 'row1', { 'x' => [0,10].pack("NN") })
326
+
327
+ (2..10).to_a.each do |i|
328
+ @twitter.insert(:Statuses, 'row' + i.to_s, { 'x' => [0,20].pack("NN"), 'non_indexed' => [i].pack('N*') })
329
+ end
330
+
331
+ @client.insert(:Statuses, 'row11', { 'x' => [0,30].pack("NN") })
332
+
333
+ expressions = [{:column_name => 'x', :value => [0,20].pack("NN"), :comparison => "=="}]
334
+
335
+ # verify multiples will be returned
336
+ @client.get_indexed_slices(:Statuses, expressions).length # returns 9
337
+
338
+ # verify that GT and LT queries perform properly
339
+ expressions = [
340
+ { :column_name => 'x',
341
+ :value => [0,20].pack("NN"),
342
+ :comparison => "=="},
343
+ { :column_name => 'non_indexed',
344
+ :value => [5].pack("N*"),
345
+ :comparison => ">"}
346
+ ]
347
+
348
+ @client.get_indexed_slices(:Statuses, expressions).length # returns 5
349
+
350
+ ### batch
351
+ Takes a block where all the mutations (inserts and deletions) inside it are
352
+ queued, and at the end of the block are passed to cassandra in a single batch.
353
+
354
+ If you don't want to send all the mutations inside the block in a big single
355
+ batch, you can use the :queue\_size option to send smaller batches. If the
356
+ queue is not empty at the end of the block, the remaining mutations are sent.
357
+
358
+ * options
359
+ * :consistency - Override the consistency level from individual mutations.
360
+ * :queue\_size - Maximum number of mutations to send at once.
361
+
362
+ Example:
363
+
364
+ @client.batch do
365
+ @client.insert(:Statuses, 'k1', {'body' => 'v1'})
366
+ @client.insert(:Statuses, 'k2', {'body' => 'v2'})
367
+ @client.remove(:Statuses, 'k3')
368
+ end
369
+
370
+
371
+ ## Reporting Problems
372
+
373
+ The Github issue tracker is [here](http://github.com/twitter/cassandra/issues). If you have problems with this library or Cassandra itself, please use the [cassandra-user mailing list](http://mail-archives.apache.org/mod_mbox/incubator-cassandra-user/).
@@ -0,0 +1,191 @@
1
+ require 'fileutils'
2
+ require 'rake/testtask'
3
+ require 'rake/extensiontask'
4
+
5
+ CassandraBinaries = {
6
+ '0.6' => 'http://archive.apache.org/dist/cassandra/0.6.13/apache-cassandra-0.6.13-bin.tar.gz',
7
+ '0.7' => 'http://archive.apache.org/dist/cassandra/0.7.9/apache-cassandra-0.7.9-bin.tar.gz',
8
+ '0.8' => 'http://archive.apache.org/dist/cassandra/0.8.7/apache-cassandra-0.8.7-bin.tar.gz',
9
+ '1.0' => 'http://archive.apache.org/dist/cassandra/1.0.6/apache-cassandra-1.0.6-bin.tar.gz',
10
+ '1.1' => 'http://archive.apache.org/dist/cassandra/1.1.5/apache-cassandra-1.1.5-bin.tar.gz',
11
+ '1.2' => 'http://archive.apache.org/dist/cassandra/1.2.1/apache-cassandra-1.2.1-bin.tar.gz'
12
+ }
13
+
14
+ CASSANDRA_HOME = ENV['CASSANDRA_HOME'] || "#{ENV['HOME']}/cassandra"
15
+ CASSANDRA_VERSION = ENV['CASSANDRA_VERSION'] || '0.8'
16
+ CASSANDRA_PIDFILE = ENV['CASSANDRA_PIDFILE'] || "#{CASSANDRA_HOME}/cassandra.pid"
17
+
18
+ def setup_cassandra_version(version = CASSANDRA_VERSION)
19
+ FileUtils.mkdir_p CASSANDRA_HOME
20
+
21
+ destination_directory = File.join(CASSANDRA_HOME, 'cassandra-' + CASSANDRA_VERSION)
22
+
23
+ unless File.exists?(File.join(destination_directory, 'bin','cassandra'))
24
+ download_source = CassandraBinaries[CASSANDRA_VERSION]
25
+ download_destination = File.join("/tmp", File.basename(download_source))
26
+ untar_directory = File.join(CASSANDRA_HOME, File.basename(download_source,'-bin.tar.gz'))
27
+
28
+ puts "downloading cassandra"
29
+ sh "curl -L -o #{download_destination} #{download_source}"
30
+
31
+ sh "tar xzf #{download_destination} -C #{CASSANDRA_HOME}"
32
+ sh "mv #{untar_directory} #{destination_directory}"
33
+ end
34
+ end
35
+
36
+ def setup_environment
37
+ env = ""
38
+ if !ENV["CASSANDRA_INCLUDE"]
39
+ env << "CASSANDRA_INCLUDE=#{File.expand_path(Dir.pwd)}/conf/#{CASSANDRA_VERSION}/cassandra.in.sh "
40
+ env << "CASSANDRA_HOME=#{CASSANDRA_HOME}/cassandra-#{CASSANDRA_VERSION} "
41
+ env << "CASSANDRA_CONF=#{File.expand_path(Dir.pwd)}/conf/#{CASSANDRA_VERSION}"
42
+ else
43
+ env << "CASSANDRA_INCLUDE=#{ENV['CASSANDRA_INCLUDE']} "
44
+ env << "CASSANDRA_HOME=#{ENV['CASSANDRA_HOME']} "
45
+ env << "CASSANDRA_CONF=#{ENV['CASSANDRA_CONF']}"
46
+ end
47
+
48
+ env
49
+ end
50
+
51
+ def running?(pid_file = nil)
52
+ pid_file ||= CASSANDRA_PIDFILE
53
+
54
+ if File.exists?(pid_file)
55
+ pid = File.new(pid_file).read.to_i
56
+ begin
57
+ Process.kill(0, pid)
58
+ return true
59
+ rescue
60
+ File.delete(pid_file)
61
+ end
62
+ end
63
+
64
+ false
65
+ end
66
+
67
+ def listening?(host, port)
68
+ TCPSocket.new(host, port).close
69
+ true
70
+ rescue Errno::ECONNREFUSED => e
71
+ false
72
+ end
73
+
74
+ namespace :cassandra do
75
+ desc "Start Cassandra"
76
+ task :start, [:daemonize] => :java do |t, args|
77
+ args.with_defaults(:daemonize => true)
78
+
79
+ setup_cassandra_version
80
+ env = setup_environment
81
+
82
+ Dir.chdir(File.join(CASSANDRA_HOME, "cassandra-#{CASSANDRA_VERSION}")) do
83
+ sh("env #{env} bin/cassandra #{'-f' unless args.daemonize} -p #{CASSANDRA_PIDFILE}")
84
+ end
85
+
86
+ if args.daemonize
87
+ end_time = Time.now + 30
88
+ host = '127.0.0.1'
89
+ port = 9160
90
+
91
+ until Time.now >= end_time || listening?(host, port)
92
+ puts "waiting for 127.0.0.1:9160"
93
+ sleep 0.1
94
+ end
95
+
96
+ unless listening?(host, port)
97
+ raise "timed out waiting for cassandra to start"
98
+ end
99
+ end
100
+ end
101
+
102
+ desc "Stop Cassandra"
103
+ task :stop => :java do
104
+ setup_cassandra_version
105
+ env = setup_environment
106
+ sh("kill $(cat #{CASSANDRA_PIDFILE})")
107
+ end
108
+ end
109
+
110
+ desc "Start Cassandra"
111
+ task :cassandra => :java do
112
+ begin
113
+ Rake::Task["cassandra:start"].invoke(false)
114
+ rescue RuntimeError => e
115
+ raise e unless e.message =~ /Command failed with status \(130\)/ # handle keyboard interupt errors
116
+ end
117
+ end
118
+
119
+ desc "Run the Cassandra CLI"
120
+ task :cli do
121
+ Dir.chdir(File.join(CASSANDRA_HOME, "cassandra-#{CASSANDRA_VERSION}")) do
122
+ sh("bin/cassandra-cli -host localhost -port 9160")
123
+ end
124
+ end
125
+
126
+ desc "Check Java version"
127
+ task :java do
128
+ is_java16 = `java -version 2>&1`.split("\n").first =~ /java version "1.6/
129
+
130
+ if ['0.6', '0.7'].include?(CASSANDRA_VERSION) && !java16
131
+ puts "You need to configure your environment for Java 1.6."
132
+ puts "If you're on OS X, just export the following environment variables:"
133
+ puts ' JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home"'
134
+ puts ' PATH="/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/bin:$PATH"'
135
+ exit(1)
136
+ end
137
+ end
138
+
139
+ namespace :data do
140
+ desc "Reset test data"
141
+ task :reset do
142
+ puts "Resetting test data"
143
+ sh("rm -rf #{File.join(CASSANDRA_HOME, "cassandra-#{CASSANDRA_VERSION}", 'data')}")
144
+ end
145
+
146
+ desc "Load test data structures."
147
+ task :load do
148
+ unless CASSANDRA_VERSION == '0.6'
149
+
150
+ schema_path = "#{File.expand_path(Dir.pwd)}/conf/#{CASSANDRA_VERSION}/schema.txt"
151
+ puts "Loading test data structures."
152
+ Dir.chdir(File.join(CASSANDRA_HOME, "cassandra-#{CASSANDRA_VERSION}")) do
153
+ begin
154
+ sh("bin/cassandra-cli --host localhost --batch < #{schema_path}")
155
+ rescue
156
+ puts "Schema already loaded."
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
162
+
163
+ task :test => 'data:load'
164
+
165
+ # desc "Regenerate thrift bindings for Cassandra" # Dev only
166
+ task :thrift do
167
+ puts "Generating Thrift bindings"
168
+ FileUtils.mkdir_p "vendor/#{CASSANDRA_VERSION}"
169
+
170
+ system(
171
+ "cd vendor/#{CASSANDRA_VERSION} &&
172
+ rm -rf gen-rb &&
173
+ thrift -gen rb #{File.join(CASSANDRA_HOME, "cassandra-#{CASSANDRA_VERSION}")}/interface/cassandra.thrift")
174
+ end
175
+
176
+ task :fix_perms do
177
+ chmod_R 0755, './'
178
+ end
179
+
180
+ task :pkg => [:fix_perms]
181
+
182
+ Rake::ExtensionTask.new('cassandra_native') do |ext|
183
+ ext.ext_dir = 'ext'
184
+ end
185
+
186
+ Rake::TestTask.new do |t|
187
+ t.test_files = FileList['test/*.rb']
188
+ end
189
+
190
+ task :default => :test
191
+ task :test => :compile