class Cassandra::Mock

Attributes

keyspace[R]

Public Class Methods

new(keyspace, schema) click to toggle source
   # File lib/cassandra/mock.rb
18 def initialize(keyspace, schema)
19   @is_super = {}
20   @keyspace = keyspace
21   @column_name_class = {}
22   @sub_column_name_class = {}
23   @column_name_maker = {}
24   @sub_column_name_maker = {}
25   @indexes = {}
26   @schema = schema[keyspace]
27   clear_keyspace!
28 end

Public Instance Methods

add(column_family, key, value, *columns_and_options) click to toggle source
    # File lib/cassandra/mock.rb
342 def add(column_family, key, value, *columns_and_options)
343   if @batch
344     @batch << [:add, column_family, key, value, *columns_and_options]
345   else
346     column_family, column, sub_column, options = extract_and_validate_params_for_real(column_family, key, columns_and_options, WRITE_DEFAULTS)
347 
348     if is_super(column_family)
349       cf(column_family)[key]                      ||= OrderedHash.new
350       cf(column_family)[key][column]              ||= OrderedHash.new
351       cf(column_family)[key][column][sub_column]  ||= 0
352       cf(column_family)[key][column][sub_column]  += value
353     else
354       cf(column_family)[key]                      ||= OrderedHash.new
355       cf(column_family)[key][column]              ||= 0
356       cf(column_family)[key][column]              += value
357     end
358 
359     nil
360   end
361 end
add_column_family(cf) click to toggle source
    # File lib/cassandra/mock.rb
386 def add_column_family(cf)
387   @schema[cf.name.to_s] ||= OrderedHash.new
388 
389   cf.instance_variables.each do |var|
390     @schema[cf.name.to_s][var.slice(1..-1)] = cf.instance_variable_get(var)
391   end
392 end
batch(options={}) { || ... } click to toggle source
   # File lib/cassandra/mock.rb
77 def batch(options={})
78   @batch = Cassandra::Batch.new(self, options)
79   yield
80   flush_batch(options)
81 ensure
82   @batch = nil
83 end
clear_column_family!(column_family) click to toggle source
   # File lib/cassandra/mock.rb
37 def clear_column_family!(column_family)
38   @data[column_family.to_sym] = OrderedHash.new
39 end
clear_keyspace!() click to toggle source
   # File lib/cassandra/mock.rb
33 def clear_keyspace!
34   @data = {}
35 end
column_families() click to toggle source
    # File lib/cassandra/mock.rb
363 def column_families
364   cf_defs = {}
365   schema.each do |key, value|
366     cf_def = Cassandra::ColumnFamily.new
367 
368     value.each do |property, property_value|
369       cf_def.send(:"#{property}=", property_value)
370     end
371 
372     cf_defs[key] = cf_def
373   end
374 
375   cf_defs
376 end
column_family_property(column_family, key) click to toggle source
    # File lib/cassandra/mock.rb
382 def column_family_property(column_family, key)
383   schema[column_family.to_s][key]
384 end
count_columns(column_family, key, *columns_and_options) click to toggle source
    # File lib/cassandra/mock.rb
196 def count_columns(column_family, key, *columns_and_options)
197   column_family, columns, sub_columns, options = extract_and_validate_params_for_real(column_family, key, columns_and_options, READ_DEFAULTS)
198 
199   get(column_family, key, columns, options).keys.length
200 end
count_range(column_family, options = {}) click to toggle source
    # File lib/cassandra/mock.rb
247 def count_range(column_family, options = {})
248   Hash[get_range(column_family, options).select{|k,v| v.length > 0}].keys.compact.length
249 end
create_idx_clause(idx_expressions, start = "", count = 100)
Alias for: create_index_clause
create_idx_expr(c_name, value, op)
create_index(ks_name, cf_name, c_name, v_class) click to toggle source
    # File lib/cassandra/mock.rb
278 def create_index(ks_name, cf_name, c_name, v_class)
279   if @indexes[ks_name] &&
280     @indexes[ks_name][cf_name] &&
281     @indexes[ks_name][cf_name][c_name]
282     nil
283 
284   else
285     @indexes[ks_name] ||= {}
286     @indexes[ks_name][cf_name] ||= {}
287     @indexes[ks_name][cf_name][c_name] = true
288   end
289 end
create_index_clause(idx_expressions, start = "", count = 100) click to toggle source
    # File lib/cassandra/mock.rb
307 def create_index_clause(idx_expressions, start = "", count = 100)
308   {:start => start, :index_expressions => idx_expressions, :count => count, :type => :index_clause}
309 end
Also aliased as: create_idx_clause
create_index_expression(c_name, value, op) click to toggle source
    # File lib/cassandra/mock.rb
302 def create_index_expression(c_name, value, op)
303   {:column_name => c_name, :value => value, :comparison => op}
304 end
Also aliased as: create_idx_expr
default_read_consistency=(value) click to toggle source
   # File lib/cassandra/mock.rb
45 def default_read_consistency=(value)
46   READ_DEFAULTS[:consistency] = value
47 end
default_write_consistency=(value) click to toggle source
   # File lib/cassandra/mock.rb
41 def default_write_consistency=(value)
42   WRITE_DEFAULTS[:consistency] = value
43 end
disconnect!() click to toggle source
   # File lib/cassandra/mock.rb
30 def disconnect!
31 end
drop_column_family(column_family_name) click to toggle source
    # File lib/cassandra/mock.rb
402 def drop_column_family(column_family_name)
403   @schema.delete(column_family_name)
404 end
drop_index(ks_name, cf_name, c_name) click to toggle source
    # File lib/cassandra/mock.rb
291 def drop_index(ks_name, cf_name, c_name)
292   if @indexes[ks_name] &&
293     @indexes[ks_name][cf_name] &&
294     @indexes[ks_name][cf_name][c_name]
295 
296     @indexes[ks_name][cf_name].delete(c_name)
297   else
298     nil
299   end
300 end
each(column_family, options = {}) { |key, columns| ... } click to toggle source
    # File lib/cassandra/mock.rb
257 def each(column_family, options = {})
258   batch_size    = options.delete(:batch_size) || 100
259   count         = options.delete(:key_count)
260   yielded_count = 0
261 
262   options[:start_key] ||= ''
263   last_key  = nil
264 
265   while options[:start_key] != last_key && (count.nil? || count > yielded_count)
266     options[:start_key] = last_key
267     res = get_range(column_family, options.merge!(:start_key => last_key, :key_count => batch_size))
268     res.each do |key, columns|
269       next if options[:start_key] == key
270       next if yielded_count == count
271       yield key, columns
272       yielded_count += 1
273       last_key = key
274     end
275   end
276 end
each_key(column_family, options = {}) { |key| ... } click to toggle source
    # File lib/cassandra/mock.rb
251 def each_key(column_family, options = {})
252   each(column_family, options.merge!(:columns => [])) do |key, value|
253     yield key
254   end
255 end
exists?(column_family, key, *columns_and_options) click to toggle source
    # File lib/cassandra/mock.rb
138 def exists?(column_family, key, *columns_and_options)
139   column_family, column, sub_column, options = extract_and_validate_params_for_real(column_family, [key], columns_and_options, READ_DEFAULTS)
140   results = get(column_family, key, column, sub_column)
141 
142   ![{}, nil].include?(results)
143 end
flush_batch(options) click to toggle source
   # File lib/cassandra/mock.rb
85 def flush_batch(options)
86   b = @batch
87   @batch = nil
88   b.each do |mutation|
89     send(*mutation)
90   end
91   @batch = b
92 end
get(column_family, key, *columns_and_options) click to toggle source
    # File lib/cassandra/mock.rb
 94 def get(column_family, key, *columns_and_options)
 95   column_family, column, sub_column, options =
 96     extract_and_validate_params_for_real(column_family, [key], columns_and_options, READ_DEFAULTS)
 97   if !is_super(column_family)
 98     get_standard(column_family, key, column, options)
 99   else
100     get_super(column_family, key, column, sub_column, options)
101   end
102 end
get_columns(column_family, key, *columns_and_options) click to toggle source
    # File lib/cassandra/mock.rb
181 def get_columns(column_family, key, *columns_and_options)
182   column_family, columns, sub_columns, options = extract_and_validate_params_for_real(column_family, key, columns_and_options, READ_DEFAULTS)
183   d = get(column_family, key)
184 
185   if sub_columns
186     sub_columns.collect do |sub_column|
187       d[columns][sub_column]
188     end
189   else
190     columns.collect do |column|
191       d[column]
192     end
193   end
194 end
get_indexed_slices(column_family, idx_clause, *columns_and_options) click to toggle source
    # File lib/cassandra/mock.rb
312 def get_indexed_slices(column_family, idx_clause, *columns_and_options)
313   column_family, columns, _, options =
314     extract_and_validate_params_for_real(column_family, [], columns_and_options,
315     READ_DEFAULTS.merge(:key_count => 100, :start_key => nil, :key_start => nil))
316 
317   start_key = options[:start_key] || options[:key_start] || ""
318 
319   unless [Hash, OrderedHash].include?(idx_clause.class) && idx_clause[:type] == :index_clause
320     idx_clause = create_index_clause(idx_clause, start_key, options[:key_count])
321   end
322 
323   ret = OrderedHash.new
324   cf(column_family).each do |key, row|
325     next if idx_clause[:start] != '' && key < idx_clause[:start]
326     next if ret.length == idx_clause[:count]
327 
328     matches = []
329     idx_clause[:index_expressions].each do |expr|
330       next if row[expr[:column_name]].nil?
331       next unless row[expr[:column_name]].send(expr[:comparison].to_sym, expr[:value])
332 
333       matches << expr
334     end
335 
336     ret[key] = row if matches.length == idx_clause[:index_expressions].length
337   end
338 
339   ret
340 end
get_range(column_family, options = {}, &blk) click to toggle source
    # File lib/cassandra/mock.rb
216 def get_range(column_family, options = {}, &blk)
217   column_family, _, _, options = extract_and_validate_params_for_real(column_family, "", [options],
218                                                                       READ_DEFAULTS.merge(:start_key  => nil,
219                                                                                           :finish_key => nil,
220                                                                                           :key_count  => 100,
221                                                                                           :columns    => nil,
222                                                                                           :reversed   => false
223                                                                                          )
224                                                                      )
225   res = _get_range(column_family,
226              options[:start_key],
227              options[:finish_key],
228              options[:key_count],
229              options[:columns],
230              options[:start],
231              options[:finish],
232              options[:count],
233              options[:consistency],
234              options[:reversed], &blk)
235 
236   if blk.nil?
237     res
238   else
239     nil
240   end
241 end
get_range_keys(column_family, options = {}) click to toggle source
    # File lib/cassandra/mock.rb
243 def get_range_keys(column_family, options = {})
244   get_range(column_family,options.merge!(:columns => [])).keys
245 end
get_standard(column_family, key, column, options) click to toggle source
    # File lib/cassandra/mock.rb
104 def get_standard(column_family, key, column, options)
105   columns = cf(column_family)[key] || OrderedHash.new
106   row = columns_to_hash(column_family, columns)
107 
108   if column
109     row[column]
110   else
111     row = apply_range(row, column_family, options[:start], options[:finish])
112     row = apply_count(row, options[:count], options[:reversed])
113   end
114 end
get_super(column_family, key, column, sub_column, options) click to toggle source
    # File lib/cassandra/mock.rb
116 def get_super(column_family, key, column, sub_column, options)
117   columns = cf(column_family)[key] || OrderedHash.new
118   row = columns_to_hash(column_family, columns)
119 
120   if column
121     if sub_column
122       if row[column] &&
123         row[column][sub_column]
124         row[column][sub_column]
125       else
126         nil
127       end
128     else
129       row = row[column] || OrderedHash.new
130       row = apply_range(row, column_family, options[:start], options[:finish], false)
131       row = apply_count(row, options[:count], options[:reversed])
132     end
133   else
134     row
135   end
136 end
insert(column_family, key, hash_or_array, options = {}) click to toggle source
   # File lib/cassandra/mock.rb
49 def insert(column_family, key, hash_or_array, options = {})
50   if @batch
51     @batch << [:insert, column_family, key, hash_or_array, options]
52   else
53     raise ArgumentError if key.nil?
54     if !is_super(column_family)
55       insert_standard(column_family, key, hash_or_array)
56     else
57       insert_super(column_family, key, hash_or_array)
58     end
59   end
60 end
insert_standard(column_family, key, hash_or_array) click to toggle source
   # File lib/cassandra/mock.rb
62 def insert_standard(column_family, key, hash_or_array)
63   old = cf(column_family)[key] || OrderedHash.new
64   cf(column_family)[key] = merge_and_sort(old, hash_or_array)
65 end
insert_super(column_family, key, hash) click to toggle source
   # File lib/cassandra/mock.rb
67 def insert_super(column_family, key, hash)
68   raise ArgumentError unless hash.is_a?(Hash)
69   cf(column_family)[key] ||= OrderedHash.new
70 
71   hash.keys.each do |sub_key|
72     old = cf(column_family)[key][sub_key] || OrderedHash.new
73     cf(column_family)[key][sub_key] = merge_and_sort(old, hash[sub_key])
74   end
75 end
multi_count_columns(column_family, keys) click to toggle source
    # File lib/cassandra/mock.rb
209 def multi_count_columns(column_family, keys)
210   keys.inject(OrderedHash.new) do |hash, key|
211     hash[key] = count_columns(column_family, key)
212     hash
213   end
214 end
multi_get(column_family, keys, *columns_and_options) click to toggle source
    # File lib/cassandra/mock.rb
145 def multi_get(column_family, keys, *columns_and_options)
146   column_family, column, sub_column, options = extract_and_validate_params_for_real(column_family, keys, columns_and_options, READ_DEFAULTS)
147   keys.inject(OrderedHash.new) do |hash, key|
148     hash[key] = get(column_family, key)
149     hash
150   end
151 end
multi_get_columns(column_family, keys, columns) click to toggle source
    # File lib/cassandra/mock.rb
202 def multi_get_columns(column_family, keys, columns)
203   keys.inject(OrderedHash.new) do |hash, key|
204     hash[key] = get_columns(column_family, key, columns)
205     hash
206   end
207 end
remove(column_family, key, *columns_and_options) click to toggle source
    # File lib/cassandra/mock.rb
153 def remove(column_family, key, *columns_and_options)
154   column_family, columns, sub_column, options = extract_and_validate_params_for_real(column_family, key, columns_and_options, WRITE_DEFAULTS)
155 
156   if @batch
157     @batch << [:remove, column_family, key, columns, sub_column]
158   else
159     if columns
160       if sub_column
161         if columns.is_a? Array
162           raise ArgumentError, 'remove does not support sub_columns with array of columns'
163         end
164 
165         if cf(column_family)[key][columns]
166           cf(column_family)[key][columns].delete(sub_column.to_s)
167         end
168       else
169         if cf(column_family)[key]
170           Array(columns).each do |column|
171             cf(column_family)[key].delete(column.to_s)
172           end
173         end
174       end
175     else
176       cf(column_family).delete(key)
177     end
178   end
179 end
schema(load=true) click to toggle source
    # File lib/cassandra/mock.rb
378 def schema(load=true)
379   @schema
380 end
update_column_family(cf) click to toggle source
    # File lib/cassandra/mock.rb
394 def update_column_family(cf)
395   return false unless @schema.include?(cf.name.to_s)
396 
397   cf.instance_variables.each do |var|
398     @schema[cf.name.to_s][var.slice(1..-1)] = cf.instance_variable_get(var)
399   end
400 end

Private Instance Methods

_get_range(column_family, start_key, finish_key, key_count, columns, start, finish, count, consistency, reversed, &blk) click to toggle source
    # File lib/cassandra/mock.rb
412 def _get_range(column_family, start_key, finish_key, key_count, columns, start, finish, count, consistency, reversed, &blk)
413   ret = OrderedHash.new
414   start  = to_compare_with_type(start,  column_family)
415   finish = to_compare_with_type(finish, column_family)
416   cf(column_family).keys.sort.each do |key|
417     break if ret.keys.size >= key_count
418     if (start_key.nil? || key >= start_key) && (finish_key.nil? || key <= finish_key)
419       if columns
420         #ret[key] = columns.inject(OrderedHash.new){|hash, column_name| hash[column_name] = cf(column_family)[key][column_name]; hash;}
421         selected_hash = OrderedHash.new
422         cf(column_family)[key].each do |k, v|
423           selected_hash.[]=(k, v, cf(column_family)[key].timestamps[k]) if columns.include?(k)
424         end
425         ret[key] = columns_to_hash(column_family, selected_hash)
426         ret[key] = apply_count(ret[key], count, reversed)
427         blk.call(key,ret[key]) unless blk.nil?
428       else
429         #ret[key] = apply_range(cf(column_family)[key], column_family, start, finish, !is_super(column_family))
430         start, finish = finish, start if reversed
431         ret[key] = apply_range(columns_to_hash(column_family, cf(column_family)[key]), column_family, start, finish)
432         ret[key] = apply_count(ret[key], count, reversed)
433         blk.call(key,ret[key]) unless blk.nil?
434       end
435     end
436   end
437   ret
438 end
apply_count(row, count, reversed=false) click to toggle source
    # File lib/cassandra/mock.rb
509 def apply_count(row, count, reversed=false)
510   if count
511     keys = row.keys.sort
512     keys = keys.reverse if reversed
513     keys = keys[0...count]
514     keys.inject(OrderedHash.new) do |memo, key|
515       memo.[]=(key, row[key], row.timestamps[key])
516       memo
517     end
518   else
519     row
520   end
521 end
apply_range(row, column_family, strt, fin, standard=true) click to toggle source
    # File lib/cassandra/mock.rb
523 def apply_range(row, column_family, strt, fin, standard=true)
524   start  = to_compare_with_type(strt, column_family, standard)
525   finish = to_compare_with_type(fin,  column_family, standard)
526   ret = OrderedHash.new
527   row.keys.each do |key|
528     if (start.nil? || key >= start) && (finish.nil? || key <= finish)
529       ret.[]=(key, row[key], row.timestamps[key])
530     end
531   end
532   ret
533 end
cf(column_family) click to toggle source
    # File lib/cassandra/mock.rb
468 def cf(column_family)
469   @data[column_family.to_sym] ||= OrderedHash.new
470 end
columns_to_hash(column_family, columns) click to toggle source
    # File lib/cassandra/mock.rb
487 def columns_to_hash(column_family, columns)
488   column_class, sub_column_class = column_name_class(column_family), sub_column_name_class(column_family)
489   output = OrderedHash.new
490 
491   columns.each do |column_name, value|
492     timestamp = columns.timestamps[column_name]
493     column = column_class.new(column_name)
494 
495     if [Hash, OrderedHash].include?(value.class)
496       output[column] ||= OrderedHash.new
497       value.each do |sub_column, sub_column_value|
498         timestamp = value.timestamps[sub_column]
499         output[column].[]=(sub_column_class.new(sub_column), sub_column_value, timestamp)
500       end
501     else
502       output.[]=(column_class.new(column_name), value, timestamp)
503     end
504   end
505 
506   output
507 end
extract_and_validate_params_for_real(column_family, keys, args, options) click to toggle source
    # File lib/cassandra/mock.rb
440 def extract_and_validate_params_for_real(column_family, keys, args, options)
441   column_family, columns, sub_column, options = extract_and_validate_params(column_family, keys, args, options)
442   options[:start] = nil if options[:start] == ''
443   options[:finish] = nil if options[:finish] == ''
444   [column_family, to_compare_with_types(columns, column_family), to_compare_with_types(sub_column, column_family, false), options]
445 end
merge_and_sort(old_stuff, new_stuff) click to toggle source
    # File lib/cassandra/mock.rb
472 def merge_and_sort(old_stuff, new_stuff)
473   if new_stuff.is_a?(Array)
474     new_stuff = new_stuff.inject({}){|h,k| h[k] = nil; h }
475   end
476 
477   new_stuff = new_stuff.to_a.inject({}){|h,k| h[k[0].to_s] = k[1]; h }
478 
479   new_stuff.each { |k,v| old_stuff.[]=(k, v, (Time.now.to_f * 1000000).to_i) }
480   hash = OrderedHash.new
481   old_stuff.sort{ |a,b| a[0] <=> b[0] }.each do |k, v|
482     hash.[]=(k, v, old_stuff.timestamps[k])
483   end
484   hash
485 end
schema_for_keyspace(keyspace) click to toggle source
    # File lib/cassandra/mock.rb
408 def schema_for_keyspace(keyspace)
409   @schema
410 end
to_compare_with_type(column_name, column_family, standard=true) click to toggle source
    # File lib/cassandra/mock.rb
457 def to_compare_with_type(column_name, column_family, standard=true)
458   return column_name if column_name.nil?
459   klass = if standard
460     column_name_class(column_family)
461   else
462     sub_column_name_class(column_family)
463   end
464 
465   klass.new(column_name)
466 end
to_compare_with_types(column_names, column_family, standard=true) click to toggle source
    # File lib/cassandra/mock.rb
447 def to_compare_with_types(column_names, column_family, standard=true)
448   if column_names.is_a?(Array)
449     column_names.collect do |name|
450       to_compare_with_type(name, column_family, standard)
451     end
452   else
453     to_compare_with_type(column_names, column_family, standard)
454   end
455 end