Class: Cassanity::ColumnFamily

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ ColumnFamily

Public: Initializes a ColumnFamily.

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

:name - The String name of the column family.
:keyspace - The Cassanity::Keyspace the column family is in.
:executor - What will execute the queries (optional).
            Must respond to `call`.
:schema - The schema used to create the column family (optional).


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cassanity/column_family.rb', line 26

def initialize(args = {})
  @name = args.fetch(:name).to_sym
  @keyspace = args.fetch(:keyspace)
  @executor = args.fetch(:executor) { @keyspace.executor }

  schema = args[:schema]
  @schema = if schema.is_a?(Hash)
    Cassanity::Schema.new(schema)
  else
    schema
  end
end

Instance Attribute Details

#executorObject (readonly)

Private



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

def executor
  @executor
end

#keyspaceObject (readonly)

Public



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

def keyspace
  @keyspace
end

#nameObject (readonly)

Public



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

def name
  @name
end

#schemaObject (readonly)

Internal



15
16
17
# File 'lib/cassanity/column_family.rb', line 15

def schema
  @schema
end

Instance Method Details

#alter(args = {}) ⇒ Object

Public: Alters the column family.

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

(default: {}). :column_family_name and :keyspace_name are
always included.

Examples

alter(alter: {created_at: :timestamp})
alter(add: {description: :text})
alter(drop: :description)
alter(with: {read_repair_chance: 0.2})

Returns whatever is returned by executor.



150
151
152
153
154
155
156
157
158
# File 'lib/cassanity/column_family.rb', line 150

def alter(args = {})
  @executor.call({
    command: :column_family_alter,
    arguments: args.merge({
      column_family_name: @name,
      keyspace_name: @keyspace.name,
    }),
  })
end

#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, column_family_name: 'apps', data: {id: '1', name: 'github'}],
    [:insert, column_family_name: 'apps', data: {id: '2', name: 'gist'}],
    [:update, column_family_name: 'apps', set: {name: 'github.com'}, where: {id: '1'}],
    [:delete, column_family_name: 'apps', where: {id: '2'}],
  ]
})

Returns whatever is returned by executor.



284
285
286
287
288
289
290
291
292
293
294
# File 'lib/cassanity/column_family.rb', line 284

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

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

#columnsObject

Public: Get all columns for column family.

Returns Array of Cassanity::Column instances.



299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/cassanity/column_family.rb', line 299

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

#create(args = {}) ⇒ Object

Public: Creates the column family in the keyspace based on the schema.

args - The Hash of arguments to pass to the executor. Always passes

:column_family_name and :keyspace_name.
:schema - The Schema to use to create the column family
          (defaults to schema provided during initialization).

Examples

create # uses schema from initialization
create(schema: Cassanity::Schema.new(...))

Returns nothing. Raises Cassanity::Error if schema not set during initialization and also

not passed in via arguments.


80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cassanity/column_family.rb', line 80

def create(args = {})
  forced_arguments = {
    column_family_name: @name,
    keyspace_name: @keyspace.name,
  }
  arguments = args.merge(forced_arguments)
  arguments[:schema] = schema unless arguments[:schema]

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

#create_index(args = {}) ⇒ Object

Public: Creates an index

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

(default: {}). :column_family_name and :keyspace_name are
always included.

Examples

create_index(column_name: 'ability_id')
create_index(name: 'ability_index', column_name: 'ability_id')

Returns whatever is returned by executor.



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

def create_index(args = {})
  @executor.call({
    command: :index_create,
    arguments: args.merge({
      column_family_name: @name,
      keyspace_name: @keyspace.name,
    }),
  })
end

#delete(args = {}) ⇒ Object

Public: Makes it possible to delete data from the column family.

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

(default: {}). :column_family_name and :keyspace_name are
always included.

Returns whatever is returned by executor.



257
258
259
260
261
262
263
264
265
# File 'lib/cassanity/column_family.rb', line 257

def delete(args = {})
  @executor.call({
    command: :column_family_delete,
    arguments: args.merge({
      column_family_name: @name,
      keyspace_name: @keyspace.name,
    }),
  })
end

#drop(args = {}) ⇒ Object

Public: Drops the column family.

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

(default: {}). :column_family_name and :keyspace_name are
always included.

Examples

drop # you should rarely need more than this

Returns whatever is returned by executor.



126
127
128
129
130
131
132
133
134
# File 'lib/cassanity/column_family.rb', line 126

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

#drop_index(args = {}) ⇒ Object

Public: Drops an index

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

(default: {}).

Examples

drop_index(name: 'my_index_name')

Returns whatever is returned by executor.



192
193
194
195
196
197
# File 'lib/cassanity/column_family.rb', line 192

def drop_index(args = {})
  @executor.call({
    command: :index_drop,
    arguments: args,
  })
end

#exists?Boolean Also known as: exist?

Public: Returns true or false depending on if column family exists in the keyspace.

Returns true if column family exists, false if it does not.

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cassanity/column_family.rb', line 43

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

#insert(args = {}) ⇒ Object

Public: Makes it possible to insert data into the column family.

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

(default: {}). :column_family_name and :keyspace_name are
always included.

Returns whatever is returned by executor.



223
224
225
226
227
228
229
230
231
# File 'lib/cassanity/column_family.rb', line 223

def insert(args = {})
  @executor.call({
    command: :column_family_insert,
    arguments: args.merge({
      column_family_name: @name,
      keyspace_name: @keyspace.name,
    }),
  })
end

#inspectObject

Public



318
319
320
321
322
323
324
325
326
# File 'lib/cassanity/column_family.rb', line 318

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

#recreateObject

Public: Drops column family if it exists and then calls create.

Returns nothing.



60
61
62
63
# File 'lib/cassanity/column_family.rb', line 60

def recreate
  drop if exists?
  create
end

#select(args = {}) ⇒ Object

Public: Makes it possible to query data from the column family.

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

(default: {}). :column_family_name and :keyspace_name are
always included.

Returns whatever is returned by executor.



206
207
208
209
210
211
212
213
214
# File 'lib/cassanity/column_family.rb', line 206

def select(args = {})
  @executor.call({
    command: :column_family_select,
    arguments: args.merge({
      column_family_name: @name,
      keyspace_name: @keyspace.name,
    })
  })
end

#truncate(args = {}) ⇒ Object

Public: Truncates the column family.

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

(default: {}). :column_family_name and :keyspace_name are
always included.

Examples

truncate # you should rarely need more than this

Returns whatever is returned by executor.



105
106
107
108
109
110
111
112
113
# File 'lib/cassanity/column_family.rb', line 105

def truncate(args = {})
  @executor.call({
    command: :column_family_truncate,
    arguments: args.merge({
      column_family_name: @name,
      keyspace_name: @keyspace.name,
    }),
  })
end

#update(args = {}) ⇒ Object

Public: Makes it possible to update data in the column family.

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

(default: {}). :column_family_name and :keyspace_name are
always included.

Returns whatever is returned by executor.



240
241
242
243
244
245
246
247
248
# File 'lib/cassanity/column_family.rb', line 240

def update(args = {})
  @executor.call({
    command: :column_family_update,
    arguments: args.merge({
      column_family_name: @name,
      keyspace_name: @keyspace.name,
    }),
  })
end