Class: Cassandra::Mock

Inherits:
Object
  • Object
show all
Includes:
Columns, Helpers
Defined in:
lib/cassandra/mock.rb

Instance Method Summary collapse

Methods included from Helpers

#extract_and_validate_params, #s_map

Constructor Details

#initialize(keyspace, storage_xml) ⇒ Mock

Returns a new instance of Mock.



18
19
20
21
22
23
24
# File 'lib/cassandra/mock.rb', line 18

def initialize(keyspace, storage_xml)
  @keyspace = keyspace
  @column_name_class = {}
  @sub_column_name_class = {}
  @storage_xml = storage_xml
  clear_keyspace!
end

Instance Method Details

#batchObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cassandra/mock.rb', line 65

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



33
34
35
# File 'lib/cassandra/mock.rb', line 33

def clear_column_family!(column_family)
  @data[column_family.to_sym] = OrderedHash.new
end

#clear_keyspace!Object



29
30
31
# File 'lib/cassandra/mock.rb', line 29

def clear_keyspace!
  @data = {}
end

#count_columns(column_family, key, column = nil) ⇒ Object



175
176
177
# File 'lib/cassandra/mock.rb', line 175

def count_columns(column_family, key, column=nil)
  get(column_family, key, column).keys.length
end

#count_range(column_family, options = {}) ⇒ Object



198
199
200
201
202
203
204
205
206
207
# File 'lib/cassandra/mock.rb', line 198

def count_range(column_family, options={})
  count = 0
  l = []
  start_key = ''
  while (l = get_range(column_family, options.merge(:count => 1000, :start => start_key))).size > 0
    count += l.size
    start_key = l.last.succ
  end
  count
end

#disconnect!Object



26
27
# File 'lib/cassandra/mock.rb', line 26

def disconnect!
end

#exists?(column_family, key, column = nil) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/cassandra/mock.rb', line 130

def exists?(column_family, key, column=nil)
  !!get(column_family, key, column)
end

#get(column_family, key, *columns_and_options) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/cassandra/mock.rb', line 77

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 column_family_type(column_family) == 'Standard'
    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) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/cassandra/mock.rb', line 159

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_range(column_family, options = {}) ⇒ Object



193
194
195
196
# File 'lib/cassandra/mock.rb', line 193

def get_range(column_family, options = {})
  column_family, _, _, options = extract_and_validate_params_for_real(column_family, "", [options], READ_DEFAULTS)
  _get_range(column_family, options[:start], options[:finish], options[:count]).keys
end

#get_standard(column_family, key, column, options) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/cassandra/mock.rb', line 87

def get_standard(column_family, key, column, options)
  row = cf(column_family)[key] || OrderedHash.new
  if column
    row[column]
  else
    apply_count(row, options[:count], options[:reversed])
  end
end

#get_super(column_family, key, column, sub_column, options) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cassandra/mock.rb', line 96

def get_super(column_family, key, column, sub_column, options)
  if column
    if sub_column
      if cf(column_family)[key] &&
         cf(column_family)[key][column] &&
         cf(column_family)[key][column][sub_column]
        cf(column_family)[key][column][sub_column]
      else
        nil
      end
    else
      row = cf(column_family)[key] && cf(column_family)[key][column] ?
        cf(column_family)[key][column] :
        OrderedHash.new
      if options[:start] || options[:finish]
        start  = to_compare_with_type(options[:start],  column_family, false)
        finish = to_compare_with_type(options[:finish], column_family, false)
        ret = OrderedHash.new
        row.keys.each do |key|
          if (start.nil? || key >= start) && (finish.nil? || key <= finish)
            ret[key] = row[key]
          end
        end
        row = ret
      end
      apply_count(row, options[:count], options[:reversed])
    end
  elsif cf(column_family)[key]
    cf(column_family)[key]
  else
    OrderedHash.new
  end
end

#insert(column_family, key, hash_or_array, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cassandra/mock.rb', line 37

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 column_family_type(column_family) == 'Standard'
      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



50
51
52
53
# File 'lib/cassandra/mock.rb', line 50

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

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
# File 'lib/cassandra/mock.rb', line 55

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



186
187
188
189
190
191
# File 'lib/cassandra/mock.rb', line 186

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



134
135
136
137
138
139
140
# File 'lib/cassandra/mock.rb', line 134

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) ⇒ Object



179
180
181
182
183
184
# File 'lib/cassandra/mock.rb', line 179

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/cassandra/mock.rb', line 142

def remove(column_family, key, *columns_and_options)
  column_family, column, sub_column, options = extract_and_validate_params_for_real(column_family, key, columns_and_options, WRITE_DEFAULTS)
  if @batch
    @batch << [:remove, column_family, key, column]
  else
    if column
      if sub_column
        cf(column_family)[key][column].delete(sub_column)
      else
        cf(column_family)[key].delete(column)
      end
    else
      cf(column_family).delete(key)
    end
  end
end

#schema(load = true) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/cassandra/mock.rb', line 209

def schema(load=true)
  if !load && !@schema
    []
  else
    @schema ||= schema_for_keyspace(@keyspace)
  end
end