Class: Cassandra::Mock
- Inherits:
-
Object
- Object
- Cassandra::Mock
- Defined in:
- lib/cassandra/mock.rb
Instance Attribute Summary collapse
-
#keyspace ⇒ Object
readonly
Returns the value of attribute keyspace.
Instance Method Summary collapse
- #add(column_family, key, value, *columns_and_options) ⇒ Object
- #add_column_family(cf) ⇒ Object
- #batch ⇒ Object
- #clear_column_family!(column_family) ⇒ Object
- #clear_keyspace! ⇒ Object
- #column_families ⇒ Object
- #column_family_property(column_family, key) ⇒ Object
- #count_columns(column_family, key, *columns_and_options) ⇒ Object
- #count_range(column_family, options = {}) ⇒ Object
- #create_index(ks_name, cf_name, c_name, v_class) ⇒ Object
- #create_index_clause(idx_expressions, start = "", count = 100) ⇒ Object (also: #create_idx_clause)
- #create_index_expression(c_name, value, op) ⇒ Object (also: #create_idx_expr)
- #default_read_consistency=(value) ⇒ Object
- #default_write_consistency=(value) ⇒ Object
- #disconnect! ⇒ Object
- #drop_column_family(column_family_name) ⇒ Object
- #drop_index(ks_name, cf_name, c_name) ⇒ Object
- #each(column_family, options = {}) ⇒ Object
- #each_key(column_family, options = {}) ⇒ Object
- #exists?(column_family, key, *columns_and_options) ⇒ Boolean
- #get(column_family, key, *columns_and_options) ⇒ Object
- #get_columns(column_family, key, *columns_and_options) ⇒ Object
- #get_indexed_slices(column_family, idx_clause, *columns_and_options) ⇒ Object
- #get_range(column_family, options = {}, &blk) ⇒ Object
- #get_range_keys(column_family, options = {}) ⇒ Object
- #get_standard(column_family, key, column, options) ⇒ Object
- #get_super(column_family, key, column, sub_column, options) ⇒ Object
-
#initialize(keyspace, schema) ⇒ Mock
constructor
A new instance of Mock.
- #insert(column_family, key, hash_or_array, options = {}) ⇒ Object
- #insert_standard(column_family, key, hash_or_array) ⇒ Object
- #insert_super(column_family, key, hash) ⇒ Object
- #multi_count_columns(column_family, keys) ⇒ Object
- #multi_get(column_family, keys, *columns_and_options) ⇒ Object
- #multi_get_columns(column_family, keys, columns) ⇒ Object
- #remove(column_family, key, *columns_and_options) ⇒ Object
- #schema(load = true) ⇒ Object
- #update_column_family(cf) ⇒ Object
Methods included from Helpers
#extract_and_validate_params, #s_map
Constructor Details
#initialize(keyspace, schema) ⇒ Mock
Returns a new instance of Mock.
18 19 20 21 22 23 24 25 26 |
# File 'lib/cassandra/mock.rb', line 18 def initialize(keyspace, schema) @is_super = {} @keyspace = keyspace @column_name_class = {} @sub_column_name_class = {} @indexes = {} @schema = schema[keyspace] clear_keyspace! end |
Instance Attribute Details
#keyspace ⇒ Object (readonly)
Returns the value of attribute keyspace.
16 17 18 |
# File 'lib/cassandra/mock.rb', line 16 def keyspace @keyspace end |
Instance Method Details
#add(column_family, key, value, *columns_and_options) ⇒ Object
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/cassandra/mock.rb', line 321 def add(column_family, key, value, *) column_family, column, sub_column, = extract_and_validate_params_for_real(column_family, key, , WRITE_DEFAULTS) if is_super(column_family) cf(column_family)[key] ||= OrderedHash.new cf(column_family)[key][column] ||= OrderedHash.new cf(column_family)[key][column][sub_column] ||= 0 cf(column_family)[key][column][sub_column] += value else cf(column_family)[key] ||= OrderedHash.new cf(column_family)[key][column] ||= 0 cf(column_family)[key][column] += value end nil end |
#add_column_family(cf) ⇒ Object
361 362 363 364 365 366 367 |
# File 'lib/cassandra/mock.rb', line 361 def add_column_family(cf) @schema[cf.name.to_s] ||= OrderedHash.new cf.instance_variables.each do |var| @schema[cf.name.to_s][var.slice(1..-1)] = cf.instance_variable_get(var) end end |
#batch ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cassandra/mock.rb', line 75 def batch @batch = [] yield b = @batch @batch = nil b.each do |mutation| send(*mutation) end ensure @batch = nil end |
#clear_column_family!(column_family) ⇒ Object
35 36 37 |
# File 'lib/cassandra/mock.rb', line 35 def clear_column_family!(column_family) @data[column_family.to_sym] = OrderedHash.new end |
#clear_keyspace! ⇒ Object
31 32 33 |
# File 'lib/cassandra/mock.rb', line 31 def clear_keyspace! @data = {} end |
#column_families ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/cassandra/mock.rb', line 338 def column_families cf_defs = {} schema.each do |key, value| cf_def = Cassandra::ColumnFamily.new value.each do |property, property_value| cf_def.send(:"#{property}=", property_value) end cf_defs[key] = cf_def end cf_defs end |
#column_family_property(column_family, key) ⇒ Object
357 358 359 |
# File 'lib/cassandra/mock.rb', line 357 def column_family_property(column_family, key) schema[column_family.to_s][key] end |
#count_columns(column_family, key, *columns_and_options) ⇒ Object
178 179 180 181 182 |
# File 'lib/cassandra/mock.rb', line 178 def count_columns(column_family, key, *) column_family, columns, sub_columns, = extract_and_validate_params_for_real(column_family, key, , READ_DEFAULTS) get(column_family, key, columns, ).keys.length end |
#count_range(column_family, options = {}) ⇒ Object
229 230 231 |
# File 'lib/cassandra/mock.rb', line 229 def count_range(column_family, = {}) Hash[get_range(column_family, ).select{|k,v| v.length > 0}].keys.compact.length end |
#create_index(ks_name, cf_name, c_name, v_class) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/cassandra/mock.rb', line 260 def create_index(ks_name, cf_name, c_name, v_class) if @indexes[ks_name] && @indexes[ks_name][cf_name] && @indexes[ks_name][cf_name][c_name] nil else @indexes[ks_name] ||= {} @indexes[ks_name][cf_name] ||= {} @indexes[ks_name][cf_name][c_name] = true end end |
#create_index_clause(idx_expressions, start = "", count = 100) ⇒ Object Also known as: create_idx_clause
289 290 291 |
# File 'lib/cassandra/mock.rb', line 289 def create_index_clause(idx_expressions, start = "", count = 100) {:start => start, :index_expressions => idx_expressions, :count => count, :type => :index_clause} end |
#create_index_expression(c_name, value, op) ⇒ Object Also known as: create_idx_expr
284 285 286 |
# File 'lib/cassandra/mock.rb', line 284 def create_index_expression(c_name, value, op) {:column_name => c_name, :value => value, :comparison => op} end |
#default_read_consistency=(value) ⇒ Object
43 44 45 |
# File 'lib/cassandra/mock.rb', line 43 def default_read_consistency=(value) READ_DEFAULTS[:consistency] = value end |
#default_write_consistency=(value) ⇒ Object
39 40 41 |
# File 'lib/cassandra/mock.rb', line 39 def default_write_consistency=(value) WRITE_DEFAULTS[:consistency] = value end |
#disconnect! ⇒ Object
28 29 |
# File 'lib/cassandra/mock.rb', line 28 def disconnect! end |
#drop_column_family(column_family_name) ⇒ Object
377 378 379 |
# File 'lib/cassandra/mock.rb', line 377 def drop_column_family(column_family_name) @schema.delete(column_family_name) end |
#drop_index(ks_name, cf_name, c_name) ⇒ Object
273 274 275 276 277 278 279 280 281 282 |
# File 'lib/cassandra/mock.rb', line 273 def drop_index(ks_name, cf_name, c_name) if @indexes[ks_name] && @indexes[ks_name][cf_name] && @indexes[ks_name][cf_name][c_name] @indexes[ks_name][cf_name].delete(c_name) else nil end end |
#each(column_family, options = {}) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/cassandra/mock.rb', line 239 def each(column_family, = {}) batch_size = .delete(:batch_size) || 100 count = .delete(:key_count) yielded_count = 0 [:start_key] ||= '' last_key = nil while [:start_key] != last_key && (count.nil? || count > yielded_count) [:start_key] = last_key res = get_range(column_family, .merge!(:start_key => last_key, :key_count => batch_size)) res.each do |key, columns| next if [:start_key] == key next if yielded_count == count yield key, columns yielded_count += 1 last_key = key end end end |
#each_key(column_family, options = {}) ⇒ Object
233 234 235 236 237 |
# File 'lib/cassandra/mock.rb', line 233 def each_key(column_family, = {}) each(column_family, .merge!(:columns => [])) do |key, value| yield key end end |
#exists?(column_family, key, *columns_and_options) ⇒ Boolean
131 132 133 134 135 136 |
# File 'lib/cassandra/mock.rb', line 131 def exists?(column_family, key, *) column_family, column, sub_column, = extract_and_validate_params_for_real(column_family, [key], , READ_DEFAULTS) results = get(column_family, key, column, sub_column) ![{}, nil].include?(results) end |
#get(column_family, key, *columns_and_options) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/cassandra/mock.rb', line 87 def get(column_family, key, *) column_family, column, sub_column, = extract_and_validate_params_for_real(column_family, [key], , READ_DEFAULTS) if !is_super(column_family) get_standard(column_family, key, column, ) else get_super(column_family, key, column, sub_column, ) end end |
#get_columns(column_family, key, *columns_and_options) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/cassandra/mock.rb', line 163 def get_columns(column_family, key, *) column_family, columns, sub_columns, = extract_and_validate_params_for_real(column_family, key, , READ_DEFAULTS) d = get(column_family, key) if sub_columns sub_columns.collect do |sub_column| d[columns][sub_column] end else columns.collect do |column| d[column] end end end |
#get_indexed_slices(column_family, idx_clause, *columns_and_options) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/cassandra/mock.rb', line 294 def get_indexed_slices(column_family, idx_clause, *) column_family, columns, _, = extract_and_validate_params_for_real(column_family, [], , READ_DEFAULTS.merge(:key_count => 100, :key_start => "")) unless [Hash, OrderedHash].include?(idx_clause.class) && idx_clause[:type] == :index_clause idx_clause = create_index_clause(idx_clause, [:key_start], [:key_count]) end ret = {} cf(column_family).each do |key, row| next if idx_clause[:start] != '' && key < idx_clause[:start] next if ret.length == idx_clause[:count] matches = [] idx_clause[:index_expressions].each do |expr| next if row[expr[:column_name]].nil? next unless row[expr[:column_name]].send(expr[:comparison].to_sym, expr[:value]) matches << expr end ret[key] = row if matches.length == idx_clause[:index_expressions].length end ret end |
#get_range(column_family, options = {}, &blk) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/cassandra/mock.rb', line 198 def get_range(column_family, = {}, &blk) column_family, _, _, = extract_and_validate_params_for_real(column_family, "", [], READ_DEFAULTS.merge(:start_key => nil, :finish_key => nil, :key_count => 100, :columns => nil, :reversed => false ) ) res = _get_range(column_family, [:start_key], [:finish_key], [:key_count], [:columns], [:start], [:finish], [:count], [:consistency], [:reversed], &blk) if blk.nil? res else nil end end |
#get_range_keys(column_family, options = {}) ⇒ Object
225 226 227 |
# File 'lib/cassandra/mock.rb', line 225 def get_range_keys(column_family, = {}) get_range(column_family,.merge!(:columns => [])).keys end |
#get_standard(column_family, key, column, options) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/cassandra/mock.rb', line 97 def get_standard(column_family, key, column, ) columns = cf(column_family)[key] || OrderedHash.new row = columns_to_hash(column_family, columns) if column row[column] else row = apply_range(row, column_family, [:start], [:finish]) row = apply_count(row, [:count], [:reversed]) end end |
#get_super(column_family, key, column, sub_column, options) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/cassandra/mock.rb', line 109 def get_super(column_family, key, column, sub_column, ) columns = cf(column_family)[key] || OrderedHash.new row = columns_to_hash(column_family, columns) if column if sub_column if row[column] && row[column][sub_column] row[column][sub_column] else nil end else row = row[column] || OrderedHash.new row = apply_range(row, column_family, [:start], [:finish], false) row = apply_count(row, [:count], [:reversed]) end else row end end |
#insert(column_family, key, hash_or_array, options = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/cassandra/mock.rb', line 47 def insert(column_family, key, hash_or_array, = {}) if @batch @batch << [:insert, column_family, key, hash_or_array, ] else raise ArgumentError if key.nil? if !is_super(column_family) insert_standard(column_family, key, hash_or_array) else insert_super(column_family, key, hash_or_array) end end end |
#insert_standard(column_family, key, hash_or_array) ⇒ Object
60 61 62 63 |
# File 'lib/cassandra/mock.rb', line 60 def insert_standard(column_family, key, hash_or_array) old = cf(column_family)[key] || OrderedHash.new cf(column_family)[key] = merge_and_sort(old, hash_or_array) end |
#insert_super(column_family, key, hash) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/cassandra/mock.rb', line 65 def insert_super(column_family, key, hash) raise ArgumentError unless hash.is_a?(Hash) cf(column_family)[key] ||= OrderedHash.new hash.keys.each do |sub_key| old = cf(column_family)[key][sub_key] || OrderedHash.new cf(column_family)[key][sub_key] = merge_and_sort(old, hash[sub_key]) end end |
#multi_count_columns(column_family, keys) ⇒ Object
191 192 193 194 195 196 |
# File 'lib/cassandra/mock.rb', line 191 def multi_count_columns(column_family, keys) keys.inject(OrderedHash.new) do |hash, key| hash[key] = count_columns(column_family, key) hash end end |
#multi_get(column_family, keys, *columns_and_options) ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/cassandra/mock.rb', line 138 def multi_get(column_family, keys, *) column_family, column, sub_column, = extract_and_validate_params_for_real(column_family, keys, , READ_DEFAULTS) keys.inject(OrderedHash.new) do |hash, key| hash[key] = get(column_family, key) hash end end |
#multi_get_columns(column_family, keys, columns) ⇒ Object
184 185 186 187 188 189 |
# File 'lib/cassandra/mock.rb', line 184 def multi_get_columns(column_family, keys, columns) keys.inject(OrderedHash.new) do |hash, key| hash[key] = get_columns(column_family, key, columns) hash end end |
#remove(column_family, key, *columns_and_options) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/cassandra/mock.rb', line 146 def remove(column_family, key, *) column_family, column, sub_column, = extract_and_validate_params_for_real(column_family, key, , WRITE_DEFAULTS) if @batch @batch << [:remove, column_family, key, column, sub_column] else if column if sub_column cf(column_family)[key][column].delete(sub_column.to_s) if cf(column_family)[key][column] else cf(column_family)[key].delete(column.to_s) if cf(column_family)[key] end else cf(column_family).delete(key) end end end |
#schema(load = true) ⇒ Object
353 354 355 |
# File 'lib/cassandra/mock.rb', line 353 def schema(load=true) @schema end |
#update_column_family(cf) ⇒ Object
369 370 371 372 373 374 375 |
# File 'lib/cassandra/mock.rb', line 369 def update_column_family(cf) return false unless @schema.include?(cf.name.to_s) cf.instance_variables.each do |var| @schema[cf.name.to_s][var.slice(1..-1)] = cf.instance_variable_get(var) end end |