Module: CassandraModel::Persistence::ClassMethods

Defined in:
lib/cassandra-model/persistence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#read_consistency_level(level) ⇒ Object



54
55
56
# File 'lib/cassandra-model/persistence.rb', line 54

def read_consistency_level(level)
  @read_consistency_level = level
end

#write_consistency_level(level) ⇒ Object



50
51
52
# File 'lib/cassandra-model/persistence.rb', line 50

def write_consistency_level(level)
  @write_consistency_level = level
end

Instance Method Details

#[](key) ⇒ Object

Raises:



71
72
73
74
75
# File 'lib/cassandra-model/persistence.rb', line 71

def [](key)
  record = get(key)
  raise RecordNotFound, "cannot find out key=`#{key}` in `#{column_family}`" unless record
  record
end

#all(keyrange = ''..'', options = {}) ⇒ Object



85
86
87
88
89
90
# File 'lib/cassandra-model/persistence.rb', line 85

def all(keyrange = ''..'', options = {})
  results = connection.get_range(column_family, :start => keyrange.first,
                                 :finish => keyrange.last, :count => (options[:limit] || 100))
  keys = results.map(&:key)
  keys.map {|key| get(key) }
end

#create(attributes) ⇒ Object



96
97
98
99
100
# File 'lib/cassandra-model/persistence.rb', line 96

def create(attributes)
  new(attributes).tap do |object|
    object.save
  end
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
# File 'lib/cassandra-model/persistence.rb', line 77

def exists?(key)
  return false if key.nil? || key == ''
  #connection.exists?(column_family, key)
  !connection.get(column_family, key).empty?
rescue
  false
end

#first(keyrange = ''..'', options = {}) ⇒ Object



92
93
94
# File 'lib/cassandra-model/persistence.rb', line 92

def first(keyrange = ''..'', options = {})
  all(keyrange, options.merge(:limit => 1)).first
end

#get(key, options = {}) ⇒ Object Also known as: find



58
59
60
61
62
63
64
65
66
67
# File 'lib/cassandra-model/persistence.rb', line 58

def get(key, options = {})
  attrs = connection.get(column_family, key, options)
  return nil if attrs.empty?
  new(attrs, false).tap do |object|
    object.key = key
    object.new_record = false
  end
rescue
  nil
end

#remove(key) ⇒ Object



108
109
110
111
# File 'lib/cassandra-model/persistence.rb', line 108

def remove(key)
  connection.remove(column_family, key,
                    :consistency => @write_consistency_level || Cassandra::Consistency::QUORUM)
end

#remove_column(key, column) ⇒ Object



113
114
115
116
# File 'lib/cassandra-model/persistence.rb', line 113

def remove_column(key, column)
  connection.remove(column_family, key, column,
                    :consistency => @write_consistency_level || Cassandra::Consistency::QUORUM)
end

#truncate!Object



118
119
120
# File 'lib/cassandra-model/persistence.rb', line 118

def truncate!
  connection.truncate!(column_family.to_s)
end

#write(key, attributes) ⇒ Object



102
103
104
105
106
# File 'lib/cassandra-model/persistence.rb', line 102

def write(key, attributes)
  connection.insert(column_family, key, attributes,
                    :consistency => @write_consistency_level || Cassandra::Consistency::QUORUM)
  key
end