Skip to content

Commit bb839ae

Browse files
Merge pull request #192 from datastax/RUBY-187
Ruby 187
2 parents cbf9da0 + 40d10b9 commit bb839ae

21 files changed

+614
-87
lines changed

integration/indexes/indexes_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def test_can_create_index
5454

5555
assert @cluster.keyspace('simplex').table('test').has_index?('b_index')
5656
index = @cluster.keyspace('simplex').table('test').index('b_index')
57+
assert @cluster.keyspace('simplex').has_index?('b_index')
58+
assert_same(index, @cluster.keyspace('simplex').index('b_index'))
5759
assert_equal 'b_index', index.name
5860
assert_equal 'test', index.table.name
5961
assert_equal :composites, index.kind

integration/indexes/materialized_view_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ def test_can_retrieve_materialized_view_metadata
7070
assert @cluster.keyspace('simplex').has_materialized_view?('monthlyhigh')
7171
mv_meta = @cluster.keyspace('simplex').materialized_view('monthlyhigh')
7272

73+
table = @cluster.keyspace('simplex').table('scores')
74+
assert table.has_materialized_view?('monthlyhigh')
75+
assert_same(mv_meta, table.materialized_view('monthlyhigh'))
76+
7377
assert_equal 'monthlyhigh', mv_meta.name
7478
refute_nil mv_meta.id
7579
assert_equal 'scores', mv_meta.base_table.name

integration/session_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,8 @@ def test_can_set_protocol_version_explicitly
843843
# @test_category connection
844844
#
845845
def test_cluster_session_inspect
846+
setup_schema
847+
846848
cluster = Cassandra.cluster(hosts: ['127.0.0.1'], consistency: :quorum, page_size: 10)
847849
session = cluster.connect('simplex')
848850

lib/cassandra.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ def self.validate_and_massage_options(options)
820820
require 'cassandra/materialized_view'
821821
require 'cassandra/keyspace'
822822
require 'cassandra/index'
823+
require 'cassandra/trigger'
823824

824825
require 'cassandra/execution/info'
825826
require 'cassandra/execution/options'

lib/cassandra/cluster/schema/fetchers.rb

Lines changed: 107 additions & 18 deletions
Large diffs are not rendered by default.

lib/cassandra/index.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def custom_class_name
6262
@options['class_name']
6363
end
6464

65-
# @return [String] a cql representation of this table
65+
# @return [String] a cql representation of this index
6666
def to_cql
6767
keyspace_name = Util.escape_name(@table.keyspace.name)
6868
table_name = Util.escape_name(@table.name)

lib/cassandra/keyspace.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,18 @@ def initialize(name,
6969
@views = views
7070

7171
# Set the keyspace attribute on the tables and views.
72+
# Also set up the index collection on the keyspace and the view-collection on each table.
73+
@indexes_hash = {}
7274
@tables.each_value do |t|
7375
t.set_keyspace(self)
76+
t.each_index do |index|
77+
@indexes_hash[index.name] = index
78+
end
7479
end
7580
@views.each_value do |v|
7681
v.set_keyspace(self)
82+
table = v.base_table
83+
table.add_view(v) if table
7784
end
7885
end
7986

@@ -110,6 +117,34 @@ def each_table(&block)
110117
end
111118
alias tables each_table
112119

120+
# @return [Boolean] whether this keyspace has an index with the given name
121+
# @param name [String] index name
122+
def has_index?(name)
123+
@indexes_hash.key?(name)
124+
end
125+
126+
# @return [Cassandra::Index, nil] an index or nil
127+
# @param name [String] index name
128+
def index(name)
129+
@indexes_hash[name]
130+
end
131+
132+
# Yield or enumerate each index defined in this keyspace
133+
# @overload each_index
134+
# @yieldparam index [Cassandra::Index] current index
135+
# @return [Cassandra::Keyspace] self
136+
# @overload each_index
137+
# @return [Array<Cassandra::Index>] a list of indexes
138+
def each_index(&block)
139+
if block_given?
140+
@indexes_hash.each_value(&block)
141+
self
142+
else
143+
@indexes_hash.values
144+
end
145+
end
146+
alias indexes each_index
147+
113148
# @return [Boolean] whether this keyspace has a materialized view with the given name
114149
# @param name [String] materialized view name
115150
def has_materialized_view?(name)

lib/cassandra/table.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def initialize(keyspace,
3838
@clustering_order = clustering_order.freeze
3939
@indexes = []
4040
@indexes_hash = {}
41+
@materialized_views = []
42+
@materialized_views_hash = {}
43+
@triggers = []
44+
@triggers_hash = {}
4145
end
4246

4347
# @param name [String] index name
@@ -68,6 +72,62 @@ def each_index(&block)
6872
end
6973
alias indexes each_index
7074

75+
# @param name [String] trigger name
76+
# @return [Boolean] whether this table has a given trigger
77+
def has_trigger?(name)
78+
@triggers_hash.key?(name)
79+
end
80+
81+
# @param name [String] trigger name
82+
# @return [Cassandra::Trigger, nil] a trigger or nil
83+
def trigger(name)
84+
@triggers_hash[name]
85+
end
86+
87+
# Yield or enumerate each trigger bound to this table
88+
# @overload each_trigger
89+
# @yieldparam trigger [Cassandra::Index] current trigger
90+
# @return [Cassandra::Table] self
91+
# @overload each_trigger
92+
# @return [Array<Cassandra::Trigger>] a list of triggers
93+
def each_trigger(&block)
94+
if block_given?
95+
@triggers.each(&block)
96+
self
97+
else
98+
@triggers.freeze
99+
end
100+
end
101+
alias triggers each_trigger
102+
103+
# @param name [String] materialized view name
104+
# @return [Boolean] whether this table has a given materialized view
105+
def has_materialized_view?(name)
106+
@materialized_views_hash.key?(name)
107+
end
108+
109+
# @param name [String] materialized view name
110+
# @return [Cassandra::MaterializedView, nil] a materialized view or nil
111+
def materialized_view(name)
112+
@materialized_views_hash[name]
113+
end
114+
115+
# Yield or enumerate each materialized view bound to this table
116+
# @overload each_materialized_view
117+
# @yieldparam materialized_view [Cassandra::MaterializedView] current materialized view
118+
# @return [Cassandra::Table] self
119+
# @overload each_materialized_view
120+
# @return [Array<Cassandra::MaterializedView>] a list of materialized views
121+
def each_materialized_view(&block)
122+
if block_given?
123+
@materialized_views.each(&block)
124+
self
125+
else
126+
@materialized_views.freeze
127+
end
128+
end
129+
alias materialized_views each_materialized_view
130+
71131
# @return [String] a cql representation of this table
72132
def to_cql
73133
cql = "CREATE TABLE #{Util.escape_name(@keyspace.name)}.#{Util.escape_name(@name)} (\n"
@@ -134,6 +194,18 @@ def add_index(index)
134194
@indexes_hash[index.name] = index
135195
end
136196

197+
# @private
198+
def add_view(view)
199+
@materialized_views << view
200+
@materialized_views_hash[view.name] = view
201+
end
202+
203+
# @private
204+
def add_trigger(trigger)
205+
@triggers << trigger
206+
@triggers_hash[trigger.name] = trigger
207+
end
208+
137209
# @private
138210
def eql?(other)
139211
other.is_a?(Table) &&

lib/cassandra/trigger.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# encoding: utf-8
2+
3+
#--
4+
# Copyright 2013-2016 DataStax, Inc.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#++
18+
19+
module Cassandra
20+
# Represents a trigger on a cassandra table
21+
class Trigger
22+
# @return [Cassandra::Table] table that the trigger applies to.
23+
attr_reader :table
24+
# @return [String] name of the trigger.
25+
attr_reader :name
26+
# @return [Hash] options of the trigger.
27+
attr_reader :options
28+
29+
# @private
30+
def initialize(table,
31+
name,
32+
options)
33+
@table = table
34+
@name = name.freeze
35+
@options = options.freeze
36+
end
37+
38+
# @return [String] name of the trigger class
39+
def custom_class_name
40+
@options['class']
41+
end
42+
43+
# @return [String] a cql representation of this trigger
44+
def to_cql
45+
keyspace_name = Util.escape_name(@table.keyspace.name)
46+
table_name = Util.escape_name(@table.name)
47+
trigger_name = Util.escape_name(@name)
48+
49+
"CREATE TRIGGER #{trigger_name} ON #{keyspace_name}.#{table_name} USING '#{@options['class']}';"
50+
end
51+
52+
# @private
53+
def eql?(other)
54+
other.is_a?(Trigger) &&
55+
@table == other.table &&
56+
@name == other.name &&
57+
@options == other.options
58+
end
59+
alias == eql?
60+
61+
# @private
62+
def inspect
63+
"#<#{self.class.name}:0x#{object_id.to_s(16)} " \
64+
"@name=#{@name.inspect} @table=#{@table.inspect} @options=#{@options.inspect}>"
65+
end
66+
end
67+
end

spec/cassandra/cluster/schema/fetchers/1.2.19-schema.cql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,4 +392,4 @@ WITH bloom_filter_fp_chance = 0.01
392392
AND gc_grace_seconds = 2
393393
AND populate_io_cache_on_flush = 'false'
394394
AND read_repair_chance = 0.1
395-
AND replicate_on_write = 'true';
395+
AND replicate_on_write = 'true';

0 commit comments

Comments
 (0)