Class: Cassandra::Mock

### Home Classes Methods

### In Files

- lib/cassandra/mock.rb

### Parent

Object

### Methods

- ::new
- #add
- #add_column_family
- #batch
- #clear_column_family!
- #clear_keyspace!
- #column_families
- #column_family_property
- #count_columns
- #count_range
- #create_idx_clause
- #create_idx_expr
- #create_index
- #create_index_clause
- #create_index_expression
- #default_read_consistency=
- #default_write_consistency=
- #disconnect!
- #drop_column_family
- #drop_index
- #each
- #each_key
- #exists?
- #flush_batch
- #get
- #get_columns
- #get_indexed_slices
- #get_range
- #get_range_keys
- #get_standard
- #get_super
- #insert
- #insert_standard
- #insert_super
- #multi_count_columns
- #multi_get
- #multi_get_columns
- #remove
- #schema
- #update_column_family

### Included Modules

- Cassandra::Helpers

### Files

- CHANGELOG
- LICENSE
- Makefile

### Class/Module Index

Quicksearch

- Cassandra
- Cassandra::Batch
- Cassandra::ColumnFamily
- Cassandra::Comparable
- Cassandra::Composite
- Cassandra::Consistency
- Cassandra::Constants
- Cassandra::DynamicComposite
- Cassandra::Helpers
- Cassandra::Keyspace
- Cassandra::Long
- Cassandra::Mock
- CassandraThrift
- CassandraThrift::Cassandra
- CassandraThrift::Cassandra::Client
- SimpleUUID
- SimpleUUID::UUID
- Array
- Time

No matching classes.

# Cassandra::Mock

### Attributes

keyspace[R]

### Public Class Methods

new(keyspace, schema) click to toggle source

    # File lib/cassandra/mock.rb, line 18
    def initialize(keyspace, schema)
      @is_super = {}
      @keyspace = keyspace
      @column_name_class = {}
      @sub_column_name_class = {}
      @column_name_maker = {}
      @sub_column_name_maker = {}
      @indexes = {}
      @schema = schema[keyspace]
      clear_keyspace!
    end

### Public Instance Methods

add(column_family, key, value, *columns_and_options) click to toggle source

    # File lib/cassandra/mock.rb, line 342
    def add(column_family, key, value, *columns_and_options)
      if @batch
        @batch << [:add, column_family, key, value, *columns_and_options]
      else
        column_family, column, sub_column, options = extract_and_validate_params_for_real(column_family, key, columns_and_options, 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
    end

add_column_family(cf) click to toggle source

    # File lib/cassandra/mock.rb, line 386
    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(options={}) click to toggle source

    # File lib/cassandra/mock.rb, line 77
    def batch(options={})
      @batch = Cassandra::Batch.new(self, options)
      yield
      flush_batch(options)
    ensure
      @batch = nil
    end

clear_column_family!(column_family) click to toggle source

    # File lib/cassandra/mock.rb, line 37
    def clear_column_family!(column_family)
      @data[column_family.to_sym] = OrderedHash.new
    end

clear_keyspace!() click to toggle source

    # File lib/cassandra/mock.rb, line 33
    def clear_keyspace!
      @data = {}
    end

column_families() click to toggle source

    # File lib/cassandra/mock.rb, line 363
    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) click to toggle source

    # File lib/cassandra/mock.rb, line 382
    def column_family_property(column_family, key)
      schema[column_family.to_s][key]
    end

count_columns(column_family, key, *columns_and_options) click to toggle source

    # File lib/cassandra/mock.rb, line 196
    def count_columns(column_family, key, *columns_and_options)
      column_family, columns, sub_columns, options = extract_and_validate_params_for_real(column_family, key, columns_and_options, READ_DEFAULTS)

      get(column_family, key, columns, options).keys.length
    end

count_range(column_family, options = {}) click to toggle source

    # File lib/cassandra/mock.rb, line 247
    def count_range(column_family, options = {})
      Hash[get_range(column_family, options).select{|k,v| v.length > 0}].keys.compact.length
    end

create_idx_clause(idx_expressions, start = "", count = 100) click to toggle source
Alias for: create_index_clause
create_idx_expr(c_name, value, op) click to toggle source
Alias for: create_index_expression
create_index(ks_name, cf_name, c_name, v_class) click to toggle source

    # File lib/cassandra/mock.rb, line 278
    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) click to toggle source

    # File lib/cassandra/mock.rb, line 307
    def create_index_clause(idx_expressions, start = "", count = 100)
      {:start => start, :index_expressions => idx_expressions, :count => count, :type => :index_clause}
    end

Also aliased as: create_idx_clause
create_index_expression(c_name, value, op) click to toggle source

    # File lib/cassandra/mock.rb, line 302
    def create_index_expression(c_name, value, op)
      {:column_name => c_name, :value => value, :comparison => op}
    end

Also aliased as: create_idx_expr
default_read_consistency=(value) click to toggle source

    # File lib/cassandra/mock.rb, line 45
    def default_read_consistency=(value)
      READ_DEFAULTS[:consistency] = value
    end

default_write_consistency=(value) click to toggle source

    # File lib/cassandra/mock.rb, line 41
    def default_write_consistency=(value)
      WRITE_DEFAULTS[:consistency] = value
    end

disconnect!() click to toggle source

    # File lib/cassandra/mock.rb, line 30
    def disconnect!
    end

drop_column_family(column_family_name) click to toggle source

    # File lib/cassandra/mock.rb, line 402
    def drop_column_family(column_family_name)
      @schema.delete(column_family_name)
    end

drop_index(ks_name, cf_name, c_name) click to toggle source

    # File lib/cassandra/mock.rb, line 291
    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 = {}) click to toggle source

    # File lib/cassandra/mock.rb, line 257
    def each(column_family, options = {})
      batch_size    = options.delete(:batch_size) || 100
      count         = options.delete(:key_count)
      yielded_count = 0

      options[:start_key] ||= ''
      last_key  = nil

      while options[:start_key] != last_key && (count.nil? || count > yielded_count)
        options[:start_key] = last_key
        res = get_range(column_family, options.merge!(:start_key => last_key, :key_count => batch_size))
        res.each do |key, columns|
          next if options[: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 = {}) click to toggle source

    # File lib/cassandra/mock.rb, line 251
    def each_key(column_family, options = {})
      each(column_family, options.merge!(:columns => [])) do |key, value|
        yield key
      end
    end

exists?(column_family, key, *columns_and_options) click to toggle source

    # File lib/cassandra/mock.rb, line 138
    def exists?(column_family, key, *columns_and_options)
      column_family, column, sub_column, options = extract_and_validate_params_for_real(column_family, [key], columns_and_options, READ_DEFAULTS)
      results = get(column_family, key, column, sub_column)

      ![{}, nil].include?(results)
    end

flush_batch(options) click to toggle source

    # File lib/cassandra/mock.rb, line 85
    def flush_batch(options)
      b = @batch
      @batch = nil
      b.each do |mutation|
        send(*mutation)
      end
      @batch = b
    end

get(column_family, key, *columns_and_options) click to toggle source

    # File lib/cassandra/mock.rb, line 94
    def get(column_family, key, *columns_and_options)
      column_family, column, sub_column, options =
        extract_and_validate_params_for_real(column_family, [key], columns_and_options, READ_DEFAULTS)
      if !is_super(column_family)
        get_standard(column_family, key, column, options)
      else
        get_super(column_family, key, column, sub_column, options)
      end
    end

get_columns(column_family, key, *columns_and_options) click to toggle source

    # File lib/cassandra/mock.rb, line 181
    def get_columns(column_family, key, *columns_and_options)
      column_family, columns, sub_columns, options = extract_and_validate_params_for_real(column_family, key, columns_and_options, 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) click to toggle source

    # File lib/cassandra/mock.rb, line 312
    def get_indexed_slices(column_family, idx_clause, *columns_and_options)
      column_family, columns, _, options =
        extract_and_validate_params_for_real(column_family, [], columns_and_options,
        READ_DEFAULTS.merge(:key_count => 100, :start_key => nil, :key_start => nil))

      start_key = options[:start_key] || options[:key_start] || ""

      unless [Hash, OrderedHash].include?(idx_clause.class) && idx_clause[:type] == :index_clause
        idx_clause = create_index_clause(idx_clause, start_key, options[:key_count])
      end

      ret = OrderedHash.new
      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) click to toggle source

    # File lib/cassandra/mock.rb, line 216
    def get_range(column_family, options = {}, &blk)
      column_family, _, _, options = extract_and_validate_params_for_real(column_family, "", [options],
                                                                          READ_DEFAULTS.merge(:start_key  => nil,
                                                                                              :finish_key => nil,
                                                                                              :key_count  => 100,
                                                                                              :columns    => nil,
                                                                                              :reversed   => false
                                                                                             )
                                                                         )
      res = _get_range(column_family,
                 options[:start_key],
                 options[:finish_key],
                 options[:key_count],
                 options[:columns],
                 options[:start],
                 options[:finish],
                 options[:count],
                 options[:consistency],
                 options[:reversed], &blk)

      if blk.nil?
        res
      else
        nil
      end
    end

get_range_keys(column_family, options = {}) click to toggle source

    # File lib/cassandra/mock.rb, line 243
    def get_range_keys(column_family, options = {})
      get_range(column_family,options.merge!(:columns => [])).keys
    end

get_standard(column_family, key, column, options) click to toggle source

    # File lib/cassandra/mock.rb, line 104
    def get_standard(column_family, key, column, options)
      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, options[:start], options[:finish])
        row = apply_count(row, options[:count], options[:reversed])
      end
    end

get_super(column_family, key, column, sub_column, options) click to toggle source

    # File lib/cassandra/mock.rb, line 116
    def get_super(column_family, key, column, sub_column, options)
      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, options[:start], options[:finish], false)
          row = apply_count(row, options[:count], options[:reversed])
        end
      else
        row
      end
    end

insert(column_family, key, hash_or_array, options = {}) click to toggle source

    # File lib/cassandra/mock.rb, line 49
    def insert(column_family, key, hash_or_array, options = {})
      if @batch
        @batch << [:insert, column_family, key, hash_or_array, options]
      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) click to toggle source

    # File lib/cassandra/mock.rb, line 62
    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) click to toggle source

    # File lib/cassandra/mock.rb, line 67
    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) click to toggle source

    # File lib/cassandra/mock.rb, line 209
    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) click to toggle source

    # File lib/cassandra/mock.rb, line 145
    def multi_get(column_family, keys, *columns_and_options)
      column_family, column, sub_column, options = extract_and_validate_params_for_real(column_family, keys, columns_and_options, 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) click to toggle source

    # File lib/cassandra/mock.rb, line 202
    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) click to toggle source

    # File lib/cassandra/mock.rb, line 153
    def remove(column_family, key, *columns_and_options)
      column_family, columns, sub_column, options = extract_and_validate_params_for_real(column_family, key, columns_and_options, WRITE_DEFAULTS)

      if @batch
        @batch << [:remove, column_family, key, columns, sub_column]
      else
        if columns
          if sub_column
            if columns.is_a? Array
              raise ArgumentError, 'remove does not support sub_columns with array of columns'
            end

            if cf(column_family)[key][columns]
              cf(column_family)[key][columns].delete(sub_column.to_s)
            end
          else
            if cf(column_family)[key]
              Array(columns).each do |column|
                cf(column_family)[key].delete(column.to_s)
              end
            end
          end
        else
          cf(column_family).delete(key)
        end
      end
    end

schema(load=true) click to toggle source

    # File lib/cassandra/mock.rb, line 378
    def schema(load=true)
      @schema
    end

update_column_family(cf) click to toggle source

    # File lib/cassandra/mock.rb, line 394
    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

[Validate]

Generated with the Darkfish Rdoc Generator 2.