Class: BigRecordDriver::CassandraServer

Inherits:
BigRecordServer show all
Defined in:
lib/big_record_driver/cassandra_driver/server.rb

Instance Method Summary collapse

Methods inherited from BigRecordServer

#create_table, #delete, #drop_table, #get_consecutive_rows, #method_missing, #respond_to?, #truncate_table

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BigRecordDriver::BigRecordServer

Instance Method Details

#configure(config = {}) ⇒ Object



17
18
19
20
21
22
# File 'lib/big_record_driver/cassandra_driver/server.rb', line 17

def configure(config = {})
  config[:adr]        ||= 'localhost'
  config[:port]       ||= 9160
  @config = config
  init_connection
end

#get(table_name, row, column, options = {}) ⇒ Object



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
# File 'lib/big_record_driver/cassandra_driver/server.rb', line 35

def get(table_name, row, column, options={})
  safe_exec do
    return nil unless row
    # Retreive only the last version by default
    options[:num_versions] ||= 1

    # validate the arguments
    raise ArgumentError, "num_versions must be >= 1" unless options[:num_versions] >= 1
    begin
      if options[:timestamp]
        raw_data = @cassandraClient.get_columns_since(table_name.to_s, row, column, options[:timestamp])
      else
        raw_data = @cassandraClient.get_column(table_name.to_s, row, column)
      end
    rescue NotFoundException => e2
      puts e2.message
      puts e2.class
    end
    # Return either a single value or an array, depending on the number of version that have been requested
    if options[:timestamp]
      return [] unless raw_data
      max_index = raw_data.length > options[:num_versions] || raw_data.length
      0..max_index.each do |i|
        arr[i] = Java::String.new(raw_data[i].value).to_s
      end
      arr
    else
      return nil unless raw_data
      Java::String.new(raw_data.value).to_s
    end
  end
end

#get_columns(table_name, row, columns, options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/big_record_driver/cassandra_driver/server.rb', line 68

def get_columns(table_name, row, columns, options={})
  safe_exec do
    return nil unless row
    raise ArgumentError, "timestamp on get_columns is not currently supported with cassandra" if options[:timestamp]
    arr = []
    columns.each_with_index do |col, i|
      begin
        if col[-1,1] == ':'
          arr + @cassandraClient.get_slice(table_name.to_s, row, col, -1, -1).to_a
        else
          arr + @cassandraClient.get_column(table_name.to_s, row, col)
        end
      rescue NotFoundException => e2
        puts e2.message
        puts e2.class
      end
    end
    unless !result or result.isEmpty
      values = {}
      arr.each do |column_t|
        values[column_t.getColumnName.to_s] = Java::String.new(column_t.value).to_s
      end
      values["attribute:id"] = row
      values
    end
    
  end
end

#pingObject

It’s currently impossible to have compliant delete with cassandra, you would have to do it famiyl by family

def delete(table_name, row)
  safe_exec do
    table.remove(table_name, row, ??, ??, true)
  end
end


105
106
107
108
109
# File 'lib/big_record_driver/cassandra_driver/server.rb', line 105

def ping
  safe_exec do
    @socket.isOpen
  end
end

#table_exists?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/big_record_driver/cassandra_driver/server.rb', line 117

def table_exists?(table_name)
  !@cassandraClient.describeTable(table_name.to_s).include?("not found.")
end

#table_namesObject



111
112
113
114
115
# File 'lib/big_record_driver/cassandra_driver/server.rb', line 111

def table_names
  safe_exec do
    @cassandraClient.getStringListProperty("tables") #.collect{|td| Java::String.new(td.getName).to_s}
  end
end

#update(table_name, row, values, timestamp = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/big_record_driver/cassandra_driver/server.rb', line 24

def update(table_name, row, values, timestamp=nil)
  safe_exec do
    return nil unless row
    timestamp = 0 unless timestamp
    values.each do |column, value|  
    @cassandraClient.insert(table_name.to_s, row, column, value.to_bytes, timestamp, true)
    end
    row
  end
end