Class: Cassanity::Keyspace

Inherits:
Object
  • Object
show all
Defined in:
lib/cassanity/keyspace.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Keyspace

Public: Initializes a Keyspace.

args - The Hash of arguments (default: {}).

:name - The String name of the keyspace.
:executor - What will execute the queries. Must respond to `call`.
:replication - Hash of replication options (e.g., :class,
               :replication_factor)

22
23
24
25
26
# File 'lib/cassanity/keyspace.rb', line 22

def initialize(args = {})
  @name = args.fetch(:name).to_sym
  @executor = args.fetch(:executor)
  @replication = args.fetch(:replication, {})
end

Instance Attribute Details

#executorObject (readonly)

Internal


9
10
11
# File 'lib/cassanity/keyspace.rb', line 9

def executor
  @executor
end

#nameObject (readonly)

Public


6
7
8
# File 'lib/cassanity/keyspace.rb', line 6

def name
  @name
end

#replicationObject (readonly)

Internal


12
13
14
# File 'lib/cassanity/keyspace.rb', line 12

def replication
  @replication
end

Instance Method Details

#batch(args = {}) ⇒ Object

Public: Group multiple statements into a batch.

args - The Hash of arguments to pass to the argument generator

(default: {}).

Examples

batch({
  modifications: [
    [:insert, name: 'apps', data: {id: '1', name: 'github'}],
    [:insert, name: 'apps', data: {id: '2', name: 'gist'}],
    [:update, name: 'apps', set: {name: 'github.com'}, where: {id: '1'}],
    [:delete, name: 'apps', where: {id: '2'}],
  ]
})

Returns whatever is returned by executor.


171
172
173
174
175
176
177
178
179
180
# File 'lib/cassanity/keyspace.rb', line 171

def batch(args = {})
  default_arguments = {
    keyspace_name: @name,
  }

  @executor.call({
    command: :batch,
    arguments: default_arguments.merge(args),
  })
end

#column_familiesObject

Public: Get all column families for keyspace.

Returns Array of Cassanity::ColumnFamily instances.


122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cassanity/keyspace.rb', line 122

def column_families
  @executor.call({
    command: :column_families,
    arguments: {
      keyspace_name: @name,
    },
    transformer_arguments: {
      keyspace: self,
    }
  })
end

#column_family(name_or_args, args = {}) ⇒ Object Also known as: table, []

Public: Get a column family instance

name_or_args - The String name of the column family or a Hash which has

the name key and possibly other arguments.

args - The Hash of arguments to use for ColumnFamily initialization

(optional, default: {}). :keyspace is always included.

Returns a Cassanity::ColumnFamily instance.


142
143
144
145
146
147
148
149
150
# File 'lib/cassanity/keyspace.rb', line 142

def column_family(name_or_args, args = {})
  column_family_args = if name_or_args.is_a?(Hash)
    name_or_args.merge(args)
  else
    args.merge(name: name_or_args)
  end

  ColumnFamily.new(column_family_args.merge(keyspace: self))
end

#create(args = {}) ⇒ Object

Public: Creates the keyspace

args - The Hash of arguments to pass to the argument generator

(default: {}). :keyspace_name is always included.

Examples

create # uses options from initialization

# override options from initialization
create({
  replication: {
    class: 'NetworkTopologyStrategy',
    dc1: 1,
    dc2: 3,
  }
})

Returns whatever is returned by executor.


62
63
64
65
66
67
68
69
70
71
# File 'lib/cassanity/keyspace.rb', line 62

def create(args = {})
  create_arguments = {}.merge(args)
  create_arguments[:replication] = @replication.merge(create_arguments[:replication] || {})
  create_arguments[:keyspace_name] = @name

  @executor.call({
    command: :keyspace_create,
    arguments: create_arguments,
  })
end

#drop(args = {}) ⇒ Object

Public: Drops a keyspace

args - The Hash of arguments to pass to the argument generator

(default: {}). :keyspace_name is always included.

Examples

drop # you shouldn't really ever need more than this

Returns whatever is returned by executor.


110
111
112
113
114
115
116
117
# File 'lib/cassanity/keyspace.rb', line 110

def drop(args = {})
  @executor.call({
    command: :keyspace_drop,
    arguments: args.merge({
      keyspace_name: @name,
    }),
  })
end

#exists?Boolean Also known as: exist?

Public: Returns true or false depending on if keyspace exists in the current cluster.

Returns true if keyspace exists, false if it does not.

Returns:

  • (Boolean)

32
33
34
35
36
37
38
39
# File 'lib/cassanity/keyspace.rb', line 32

def exists?
  @executor.call({
    command: :keyspaces,
    transformer_arguments: {
      executor: @executor,
    },
  }).any? { |keyspace| keyspace.name == @name }
end

#inspectObject

Public


183
184
185
186
187
188
# File 'lib/cassanity/keyspace.rb', line 183

def inspect
  attributes = [
    "name=#{@name.inspect}",
  ]
  "#<#{self.class.name}:#{object_id} #{attributes.join(', ')}>"
end

#recreateObject

Public: Drops keyspace if it exists and then calls create.

Returns nothing.


76
77
78
79
# File 'lib/cassanity/keyspace.rb', line 76

def recreate
  drop if exists?
  create
end

#use(args = {}) ⇒ Object

Public: Uses a keyspace

args - The Hash of arguments to pass to the argument generator

(default: {}). :keyspace_name is always included.

Examples

use # you shouldn't really ever need more than this

Returns whatever is returned by executor.


91
92
93
94
95
96
97
98
# File 'lib/cassanity/keyspace.rb', line 91

def use(args = {})
  @executor.call({
    command: :keyspace_use,
    arguments: args.merge({
      keyspace_name: @name,
    }),
  })
end