Class: CassandraObject::Cursor

Inherits:
Object
  • Object
show all
Includes:
Consistency
Defined in:
lib/cassandra_object/cursor.rb

Instance Method Summary collapse

Constructor Details

#initialize(target_class, column_family, key, super_column, options = {}) ⇒ Cursor

Returns a new instance of Cursor.



5
6
7
8
9
10
11
12
# File 'lib/cassandra_object/cursor.rb', line 5

def initialize(target_class, column_family, key, super_column, options={})
  @target_class  = target_class
  @column_family = column_family
  @key           = key.to_s
  @super_column  = super_column
  @options       = options
  @validators    = []
end

Instance Method Details

#connectionObject



78
79
80
# File 'lib/cassandra_object/cursor.rb', line 78

def connection
  @target_class.connection
end

#find(number_to_find) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cassandra_object/cursor.rb', line 14

def find(number_to_find)
  limit       = number_to_find
  objects     = CassandraObject::Collection.new
  out_of_keys = false

  if start_with = @options[:start_after]
    limit += 1
  else
    start_with = nil
  end
  
  while objects.size < number_to_find && !out_of_keys
    index_results = connection.get(@column_family, @key, @super_column,
      count: limit,
      start: start_with,
      reversed: @options[:reversed],
      consistency: target_class.thrift_read_consistency)

    out_of_keys  = index_results.size < limit

    if !start_with.blank?
      index_results.delete(start_with)
    end

    keys = index_results.keys
    values = index_results.values
    
    missing_keys = []
    
    results = values.empty? ? {} : @target_class.multi_get(values)
    results.each do |(key, result)|
      if result.nil?
        missing_keys << key
      end
    end

    unless missing_keys.empty?
      @target_class.multi_get(missing_keys, :quorum=>true).each do |(key, result)|
        index_key = index_results.index(key)
        if result.nil?
          remove(index_key)
          results.delete(key)
        else
          results[key] = result
        end
      end
    end

    results.values.each do |o|
      if @validators.all? {|v| v.call(o) }
        objects << o
      else
        remove(index_results.index(o.key))
      end
    end
    
    start_with = objects.last_column_name = keys.last
    limit = (number_to_find - results.size) + 1
    
  end
  
  return objects
end

#remove(index_key) ⇒ Object



82
83
84
# File 'lib/cassandra_object/cursor.rb', line 82

def remove(index_key)
  connection.remove(@column_family, @key, @super_column, index_key, consistency: target_class.thrift_write_consistency)
end

#validator(&validator) ⇒ Object



86
87
88
# File 'lib/cassandra_object/cursor.rb', line 86

def validator(&validator)
  @validators << validator
end