Class: Cassandra::Mapper
- Inherits:
-
Object
- Object
- Cassandra::Mapper
- Extended by:
- Utility::StoreInstances
- Defined in:
- lib/cassandra/mapper/extend/queries.rb,
lib/cassandra/mapper/extend/migrate.rb,
lib/cassandra/mapper/extend/schema.rb,
lib/cassandra/mapper.rb
Constant Summary collapse
- BATCH_SIZE =
500
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Attributes included from Utility::StoreInstances
Class Method Summary collapse
Instance Method Summary collapse
- #all ⇒ Object
- #each(&block) ⇒ Object
- #env ⇒ Object
- #get(query) ⇒ Object
-
#initialize(keyspace, table, &block) ⇒ Mapper
constructor
A new instance of Mapper.
- #insert(hash) ⇒ Object
- #keyspace ⇒ Object
- #keyspace_name ⇒ Object
- #migrate ⇒ Object
- #one(keys) ⇒ Object
- #remove(hash) ⇒ Object
Methods included from Utility::StoreInstances
Constructor Details
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
26 27 28 |
# File 'lib/cassandra/mapper.rb', line 26 def config @config end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
26 27 28 |
# File 'lib/cassandra/mapper.rb', line 26 def table @table end |
Class Method Details
.env ⇒ Object
10 11 12 |
# File 'lib/cassandra/mapper/extend/schema.rb', line 10 def self.env @@env end |
.env=(env) ⇒ Object
14 15 16 |
# File 'lib/cassandra/mapper/extend/schema.rb', line 14 def self.env=(env) @@env = env.to_sym end |
.migrate ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/cassandra/mapper/extend/migrate.rb', line 2 def self.migrate cassandra = Cassandra.new('system') schema[:keyspaces].each do |name| = schema.fetch(env, {}).fetch(name, {}) = Utility::Hash.stringify_keys strategy = .delete('strategy') || 'SimpleStrategy' ['replication_factor'] = .fetch('replication_factor', 1).to_s cassandra.add_keyspace Cassandra::Keyspace.new \ name: "#{name}_#{env}", strategy_class: strategy, strategy_options: , cf_defs: [] end end |
.schema ⇒ Object
2 3 4 |
# File 'lib/cassandra/mapper/extend/schema.rb', line 2 def self.schema @@schema end |
.schema=(schema) ⇒ Object
6 7 8 |
# File 'lib/cassandra/mapper/extend/schema.rb', line 6 def self.schema=(schema) @@schema = Utility::Hash.symbolize_keys schema end |
Instance Method Details
#all ⇒ Object
34 35 36 |
# File 'lib/cassandra/mapper/extend/queries.rb', line 34 def all to_enum.to_a end |
#each(&block) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/cassandra/mapper/extend/queries.rb', line 27 def each(&block) keyspace.each_key table do |packed_keys| keys = unpack_keys packed_keys get(keys).each &block end end |
#env ⇒ Object
18 19 20 |
# File 'lib/cassandra/mapper/extend/schema.rb', line 18 def env @@env end |
#get(query) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/cassandra/mapper/extend/queries.rb', line 16 def get(query) request = Data::Request.new config, query columns = columns_for request response = Data::Response.new config, request.keys, columns response.unpack end |
#insert(hash) ⇒ Object
4 5 6 7 8 |
# File 'lib/cassandra/mapper/extend/queries.rb', line 4 def insert(hash) data = Data::Insert.new config, hash keyspace.insert table, data.packed_keys, data.columns data.return! end |
#keyspace ⇒ Object
34 35 36 |
# File 'lib/cassandra/mapper.rb', line 34 def keyspace Thread.current["keyspace_#{keyspace_name}"] ||= Cassandra.new keyspace_name end |
#keyspace_name ⇒ Object
38 39 40 |
# File 'lib/cassandra/mapper.rb', line 38 def keyspace_name "#{@keyspace}_#{env}" end |
#migrate ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/cassandra/mapper/extend/migrate.rb', line 18 def migrate subkey_types = config.subkey.map do |it| Convert.type config.types[it] end # field subkey subkey_types.push Convert::TEXT_TYPE keyspace.add_column_family Cassandra::ColumnFamily.new \ keyspace: keyspace.keyspace, name: table, comparator_type: "CompositeType(#{subkey_types.join ','})" end |
#one(keys) ⇒ Object
23 24 25 |
# File 'lib/cassandra/mapper/extend/queries.rb', line 23 def one(keys) get(keys).first end |