Class: Cassandra::Mapper

Inherits:
Object
  • Object
show all
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

Attributes included from Utility::StoreInstances

#instances

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utility::StoreInstances

extended, new

Constructor Details

#initialize(keyspace, table, &block) ⇒ Mapper

Returns a new instance of Mapper.



28
29
30
31
32
# File 'lib/cassandra/mapper.rb', line 28

def initialize(keyspace, table, &block)
  @keyspace = keyspace.to_s
  @table    = table.to_s
  @config   = Utility::Config.new(&block)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



26
27
28
# File 'lib/cassandra/mapper.rb', line 26

def config
  @config
end

#tableObject (readonly)

Returns the value of attribute table.



26
27
28
# File 'lib/cassandra/mapper.rb', line 26

def table
  @table
end

Class Method Details

.envObject



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

.migrateObject



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|
    options  = schema.fetch(env, {}).fetch(name, {})
    options  = Utility::Hash.stringify_keys options
    strategy = options.delete('strategy') || 'SimpleStrategy'
    options['replication_factor'] = options.fetch('replication_factor', 1).to_s

    cassandra.add_keyspace Cassandra::Keyspace.new \
      name:             "#{name}_#{env}",
      strategy_class:   strategy,
      strategy_options: options,
      cf_defs:          []
  end
end

.schemaObject



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

#allObject



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

#envObject



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

#keyspaceObject



34
35
36
# File 'lib/cassandra/mapper.rb', line 34

def keyspace
  Thread.current["keyspace_#{keyspace_name}"] ||= Cassandra.new keyspace_name
end

#keyspace_nameObject



38
39
40
# File 'lib/cassandra/mapper.rb', line 38

def keyspace_name
  "#{@keyspace}_#{env}"
end

#migrateObject



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

#remove(hash) ⇒ Object



10
11
12
13
14
# File 'lib/cassandra/mapper/extend/queries.rb', line 10

def remove(hash)
  data = Data::Remove.new config, hash
  keyspace.remove table, data.packed_keys, data.columns
  data.return!
end