Class: CassandraClient::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra_client/table.rb

Constant Summary collapse

MAX_INT =
2**31 - 1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent) ⇒ Table

Returns a new instance of Table.



7
8
9
10
11
12
13
14
15
# File 'lib/cassandra_client/table.rb', line 7

def initialize(name, parent)
  @parent = parent
  @client = parent.client
  @block_for = parent.block_for

  @name = name
  @schema = @client.describeTable(@name)
  extend(parent.serialization)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/cassandra_client/table.rb', line 3

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



3
4
5
# File 'lib/cassandra_client/table.rb', line 3

def parent
  @parent
end

#schemaObject (readonly)

Returns the value of attribute schema.



3
4
5
# File 'lib/cassandra_client/table.rb', line 3

def schema
  @schema
end

Instance Method Details

#count(key_range, column_family = nil, limit = MAX_INT) ⇒ Object

Count all rows in the column_family you request. Requires the table to be partitioned with OrderPreservingHash.



145
146
147
# File 'lib/cassandra_client/table.rb', line 145

def count(key_range, column_family = nil, limit = MAX_INT)
  get_key_range(key_range, column_family, limit).size
end

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

Count the elements at the column_family:key:super_column path you request.



75
76
77
78
79
# File 'lib/cassandra_client/table.rb', line 75

def count_columns(key, column_family, super_column = nil)
  column_family = column_family.to_s
  column_family += ":#{super_column}" if super_column
  @client.get_column_count(@name, key, column_family)
end

#get(key, column_family, super_column = nil, column = nil, offset = -1,, limit = 100) ⇒ Object

Return a hash (actually, a CassandraClient::OrderedHash) or a single value representing the element at the column_family:key:super_column:column path you request.



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
129
# File 'lib/cassandra_client/table.rb', line 100

def get(key, column_family, super_column = nil, column = nil, offset = -1, limit = 100)
  column_family = column_family.to_s
  column_family += ":#{super_column}" if super_column
  column_family += ":#{column}" if column          
  
  # You have got to be kidding
  if is_super(column_family)
    if column
      load(@client.get_column(@name, key, column_family).value)
    elsif super_column
      columns_to_hash(@client.get_superColumn(@name, key, column_family).columns)
    else
      columns_to_hash(@client.get_slice_super(@name, key, "#{column_family}:", offset, limit))
    end
  else
    if super_column
      load(@client.get_column(@name, key, column_family).value)
    elsif is_sorted_by_time(column_family)
      result = columns_to_hash(@client.get_columns_since(@name, key, column_family, 0))

      # FIXME Hack until get_slice on a time-sorted column family works again
      result = OrderedHash[*flatten_once(result.to_a[offset, limit])] if offset > -1
      result
    else
      columns_to_hash(@client.get_slice(@name, key, "#{column_family}:", offset, limit))
    end 
  end
rescue NotFoundException
  is_super(column_family) && !column ? {} : nil
end

#get_columns(key, column_family, super_columns, columns = nil) ⇒ Object

Return a list of single values for the elements at the column_family:key:super_column:column path you request.



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cassandra_client/table.rb', line 83

def get_columns(key, column_family, super_columns, columns = nil)
  column_family = column_family.to_s
  get_slice_by_names = (is_super(column_family) && !columns) ? :get_slice_super_by_names : :get_slice_by_names
  if super_columns and columns
    column_family += ":#{super_columns}" 
    columns = Array(columns)
  else
    columns = Array(super_columns)
  end
      
  hash = columns_to_hash(@client.send(get_slice_by_names, @name, key, column_family, columns))
  columns.map { |column| hash[column] }
end

#get_key_range(key_range, column_family = nil, limit = 100) ⇒ Object

Return a list of keys in the column_family you request. Requires the table to be partitioned with OrderPreservingHash.



137
138
139
140
141
# File 'lib/cassandra_client/table.rb', line 137

def get_key_range(key_range, column_family = nil, limit = 100)      
  column_family, key_range = key_range, ''..'' unless column_family
  column_families = Array(column_family).map {|c| c.to_s}
  @client.get_key_range(@name, column_families, key_range.begin, key_range.end, limit)
end

#insert(key, column_family, hash, timestamp = now) ⇒ Object

Insert a row for a key. Pass a flat hash for a regular column family, and a nested hash for a super column family.



27
28
29
30
31
# File 'lib/cassandra_client/table.rb', line 27

def insert(key, column_family, hash, timestamp = now)
  column_family = column_family.to_s    
  insert = is_super(column_family) ? :insert_super : :insert_standard
  send(insert, key, column_family, hash, timestamp)
end

#inspect(full = true) ⇒ Object



17
18
19
20
21
# File 'lib/cassandra_client/table.rb', line 17

def inspect(full = true)
  string = "#<CassandraClient::Table:#{object_id}, @name=#{name.inspect}"
  string += ", @schema={#{schema.map {|name, hash| ":#{name} => #{hash['type'].inspect}"}.join(', ')}}, @parent=#{parent.inspect(false)}" if full
  string + ">"
end

#remove(key, column_family, super_column = nil, column = nil, timestamp = now) ⇒ Object

Remove the element at the column_family:key:super_column:column path you request.



57
58
59
60
61
62
# File 'lib/cassandra_client/table.rb', line 57

def remove(key, column_family, super_column = nil, column = nil, timestamp = now)
  column_family = column_family.to_s
  column_family += ":#{super_column}" if super_column
  column_family += ":#{column}" if column
  @client.remove(@name, key, column_family, timestamp, @block_for )
end

#remove_all(column_family) ⇒ Object

Remove all rows in the column family you request.



65
66
67
68
69
# File 'lib/cassandra_client/table.rb', line 65

def remove_all(column_family)
  get_key_range(column_family).each do |key| 
    remove(key, column_family)
  end
end