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
# File 'lib/cassanity/column_family.rb', line 26

def initialize(args = {})
  @name = args.fetch(:name)
  @keyspace = args.fetch(:keyspace)
  @executor = args.fetch(:executor) { @keyspace.executor }
  @schema = args[:schema]
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: {}). :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.



138
139
140
141
142
143
144
145
146
# File 'lib/cassanity/column_family.rb', line 138

def alter(args = {})
  @executor.call({
    command: :column_family_alter,
    arguments: args.merge({
      name: @name,
      keyspace_name: @keyspace.name,
    }),
  })
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

: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.


71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cassanity/column_family.rb', line 71

def create(args = {})
  forced_arguments = {
    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.



160
161
162
163
164
165
166
167
168
# File 'lib/cassanity/column_family.rb', line 160

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: {}). :name and :keyspace_name are always included.

Returns whatever is returned by executor.



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

def delete(args = {})
  @executor.call({
    command: :column_family_delete,
    arguments: args.merge({
      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: {}). :name and :keyspace_name are always included.

Examples

drop # you should rarely need more than this

Returns whatever is returned by executor.



115
116
117
118
119
120
121
122
123
# File 'lib/cassanity/column_family.rb', line 115

def drop(args = {})
  @executor.call({
    command: :column_family_drop,
    arguments: args.merge({
      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.



180
181
182
183
184
185
# File 'lib/cassanity/column_family.rb', line 180

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)


37
38
39
40
41
42
43
44
# File 'lib/cassanity/column_family.rb', line 37

def exists?
  @executor.call({
    command: :column_families,
    arguments: {
      keyspace_name: @keyspace.name,
    }
  }).any? { |row| row['columnfamily'].to_s == @name.to_s }
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: {}). :name and :keyspace_name are always included.

Returns whatever is returned by executor.



209
210
211
212
213
214
215
216
217
# File 'lib/cassanity/column_family.rb', line 209

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

#recreateObject

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

Returns nothing.



51
52
53
54
# File 'lib/cassanity/column_family.rb', line 51

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: {}). :name and :keyspace_name are always included.

Returns whatever is returned by executor.



193
194
195
196
197
198
199
200
201
# File 'lib/cassanity/column_family.rb', line 193

def select(args = {})
  @executor.call({
    command: :column_family_select,
    arguments: args.merge({
      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: {}). :name and :keyspace_name are always included.

Examples

truncate # you should rarely need more than this

Returns whatever is returned by executor.



95
96
97
98
99
100
101
102
103
# File 'lib/cassanity/column_family.rb', line 95

def truncate(args = {})
  @executor.call({
    command: :column_family_truncate,
    arguments: args.merge({
      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: {}). :name and :keyspace_name are always included.

Returns whatever is returned by executor.



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

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