Class: Eurydice::Pelops::ColumnFamily

Inherits:
Object
  • Object
show all
Includes:
ByteHelpers, ConsistencyLevelHelpers, ExceptionHelpers
Defined in:
lib/eurydice/pelops/column_family.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConsistencyLevelHelpers

#default_cl?, #get_cl

Methods included from ByteHelpers

#byte_array_to_s, #empty_pelops_bytes, #nio_bytes_to_s, #pelops_bytes_to_s, #to_byte_array, #to_nio_bytes, #to_pelops_bytes

Methods included from ExceptionHelpers

#thrift_exception_handler, #transform_thrift_exception

Constructor Details

#initialize(keyspace, name) ⇒ ColumnFamily

Returns a new instance of ColumnFamily.



12
13
14
# File 'lib/eurydice/pelops/column_family.rb', line 12

def initialize(keyspace, name)
  @keyspace, @name = keyspace, name
end

Instance Attribute Details

#keyspaceObject (readonly)

Returns the value of attribute keyspace.



10
11
12
# File 'lib/eurydice/pelops/column_family.rb', line 10

def keyspace
  @keyspace
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/eurydice/pelops/column_family.rb', line 10

def name
  @name
end

Instance Method Details

#batch(options = {}) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/eurydice/pelops/column_family.rb', line 162

def batch(options={})
  batch = Batch.new(@name, @keyspace)
  if block_given?
    yield batch
    batch.execute!(options)
  end
  nil
end

#create!(options = {}) ⇒ Object



27
28
29
30
31
# File 'lib/eurydice/pelops/column_family.rb', line 27

def create!(options={})
  thrift_exception_handler do
    @keyspace.column_family_manger.add_column_family(Cassandra::CfDef.from_h(options.merge(:keyspace => @keyspace.name, :name => @name)))
  end
end

#definition(reload = false) ⇒ Object



16
17
18
19
20
21
# File 'lib/eurydice/pelops/column_family.rb', line 16

def definition(reload=false)
  thrift_exception_handler do
    @definition = nil if reload
    @definition ||= @keyspace.definition(true)[:column_families][@name]
  end
end

#delete(row_key, options = {}) ⇒ Object



45
46
47
48
49
50
# File 'lib/eurydice/pelops/column_family.rb', line 45

def delete(row_key, options={})
  thrift_exception_handler do
    deletor = @keyspace.create_row_deletor
    deletor.delete_row(@name, row_key, get_cl(options))
  end
end

#delete_column(row_key, column_key, options = {}) ⇒ Object



52
53
54
55
56
# File 'lib/eurydice/pelops/column_family.rb', line 52

def delete_column(row_key, column_key, options={})
  @keyspace.batch(options) do |b|
    b.delete_column(@name, row_key, column_key)
  end
end

#delete_columns(row_key, column_keys, options = {}) ⇒ Object



58
59
60
61
62
# File 'lib/eurydice/pelops/column_family.rb', line 58

def delete_columns(row_key, column_keys, options={})
  @keyspace.batch(options) do |b|
    b.delete_columns(@name, row_key, column_keys)
  end
end

#drop!Object



33
34
35
36
37
# File 'lib/eurydice/pelops/column_family.rb', line 33

def drop!
  thrift_exception_handler do
    @keyspace.column_family_manger.drop_column_family(@name)
  end
end

#each_column(row_key, options = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/eurydice/pelops/column_family.rb', line 116

def each_column(row_key, options={})
  thrift_exception_handler do
    reversed = options.fetch(:reversed, false)
    batch_size = options.fetch(:batch_size, 100)
    start_beyond = options.fetch(:start_beyond, nil)
    start_beyond = to_pelops_bytes(start_beyond) if start_beyond
    selector = @keyspace.create_selector
    iterator = selector.iterate_columns_from_row(@name, to_pelops_bytes(row_key), start_beyond, reversed, batch_size, get_cl(options))
    if block_given?
      iterator.each do |column|
        yield column_to_kv(column, options)
      end
    else
      Enumerator.new do |y|
        iterator.each do |column|
          y << column_to_kv(column, options)
        end
      end
    end
  end
end

#exists?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/eurydice/pelops/column_family.rb', line 23

def exists?
  !!definition(true)
end

#get(row_or_rows, options = {}) ⇒ Object Also known as: get_row, get_rows



92
93
94
95
96
97
# File 'lib/eurydice/pelops/column_family.rb', line 92

def get(row_or_rows, options={})
  case row_or_rows
  when Array then get_multi(row_or_rows, options)
  else get_single(row_or_rows, options)
  end
end

#get_column(row_key, column_key, options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/eurydice/pelops/column_family.rb', line 101

def get_column(row_key, column_key, options={})
  thrift_exception_handler do
    selector = @keyspace.create_selector
    if counter_columns?
      column = selector.get_counter_column_from_row(@name, to_pelops_bytes(row_key), to_pelops_bytes(column_key), get_cl(options))
      column.get_value
    else
      column =selector.get_column_from_row(@name, to_pelops_bytes(row_key), to_pelops_bytes(column_key), get_cl(options))
      byte_array_to_s(column.get_value)
    end
  end
rescue NotFoundError => e
  nil
end

#get_column_count(row_key, options = {}) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/eurydice/pelops/column_family.rb', line 138

def get_column_count(row_key, options={})
  thrift_exception_handler do
    selector = @keyspace.create_selector
    column_predicate = create_column_predicate(options)
    selector.get_column_count(@name, to_pelops_bytes(row_key), column_predicate, get_cl(options))
  end
end

#get_indexed(column_key, operator, value, options = {}) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/eurydice/pelops/column_family.rb', line 146

def get_indexed(column_key, operator, value, options={})
  thrift_exception_handler do
    selector = @keyspace.create_selector
    op = Cassandra::INDEX_OPERATORS[operator]
    max_rows = options.fetch(:max_row_count, 20)
    types = options[:validations] || {}
    key_type = options[:comparator]
    raise ArgumentError, %(Unsupported index operator: "#{operator}") unless op
    index_expression = selector.class.new_index_expression(to_pelops_bytes(column_key, key_type), op, to_pelops_bytes(value, types[column_key]))
    index_clause = selector.class.new_index_clause(empty_pelops_bytes, max_rows, index_expression)
    column_predicate = create_column_predicate(options)
    rows = selector.get_indexed_columns(@name, index_clause, column_predicate, get_cl(options))
    rows_to_h(rows, options)
  end
end

#increment(row_key, column_key, amount = 1, options = {}) ⇒ Object Also known as: inc, incr, increment_column



71
72
73
74
75
76
77
# File 'lib/eurydice/pelops/column_family.rb', line 71

def increment(row_key, column_key, amount=1, options={})
  thrift_exception_handler do
    mutator = @keyspace.create_mutator
    mutator.write_counter_column(@name, to_pelops_bytes(row_key), to_pelops_bytes(column_key), amount)
    mutator.execute(get_cl(options))
  end
end

#key?(row_key, options = {}) ⇒ Boolean Also known as: row_exists?

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
# File 'lib/eurydice/pelops/column_family.rb', line 82

def key?(row_key, options={})
  thrift_exception_handler do
    selector = @keyspace.create_selector
    predicate = Cassandra::SlicePredicate.new
    count = selector.get_column_count(@name, row_key, get_cl(options))
    count > 0
  end
end

#truncate!Object



39
40
41
42
43
# File 'lib/eurydice/pelops/column_family.rb', line 39

def truncate!
  thrift_exception_handler do
    @keyspace.column_family_manger.truncate_column_family(@name)
  end
end

#update(row_key, properties, options = {}) ⇒ Object Also known as: insert



64
65
66
67
68
# File 'lib/eurydice/pelops/column_family.rb', line 64

def update(row_key, properties, options={})
  @keyspace.batch(options) do |b|
    b.update(@name, row_key, properties, options)
  end
end