cassie 1.0.0.beta.33 → 1.0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/cassie +8 -181
- data/lib/cassie/configuration/core.rb +26 -3
- data/lib/cassie/configuration/generator.rb +1 -0
- data/lib/cassie/configuration/loading.rb +5 -2
- data/lib/cassie/configuration.rb +1 -0
- data/lib/cassie/connection.rb +13 -7
- data/lib/cassie/connection_handler/README.md +13 -3
- data/lib/cassie/connection_handler/cluster.rb +11 -0
- data/lib/cassie/connection_handler/sessions.rb +9 -0
- data/lib/cassie/connection_handler.rb +8 -7
- data/lib/cassie/definition.rb +28 -0
- data/lib/cassie/extensions/object/color_methods.rb +21 -0
- data/lib/cassie/instrumentation.rb +4 -0
- data/lib/cassie/modification.rb +29 -0
- data/lib/cassie/query.rb +27 -0
- data/lib/cassie/schema/README.md +306 -0
- data/lib/cassie/schema/apply_command.rb +24 -0
- data/lib/cassie/schema/cassandra_migrations/importer.rb +91 -0
- data/lib/cassie/schema/cassandra_migrations/migration_file.rb +51 -0
- data/lib/cassie/schema/configuration.rb +35 -0
- data/lib/cassie/schema/migration/cassandra_support.rb +34 -0
- data/lib/cassie/schema/migration/dsl/announcing.rb +47 -0
- data/lib/cassie/schema/migration/dsl/column_operations.rb +42 -0
- data/lib/cassie/schema/migration/dsl/table_definition.rb +299 -0
- data/lib/cassie/schema/migration/dsl/table_operations.rb +64 -0
- data/lib/cassie/schema/migration/dsl.rb +17 -0
- data/lib/cassie/schema/migration.rb +12 -0
- data/lib/cassie/schema/migrator.rb +115 -0
- data/lib/cassie/schema/queries/create_keyspace_query.rb +26 -0
- data/lib/cassie/{migration → schema}/queries/create_versions_table_query.rb +6 -6
- data/lib/cassie/schema/queries/delete_version_query.rb +17 -0
- data/lib/cassie/schema/queries/drop_keyspace_query.rb +14 -0
- data/lib/cassie/schema/queries/insert_version_query.rb +22 -0
- data/lib/cassie/schema/queries/select_versions_query.rb +18 -0
- data/lib/cassie/{migration → schema}/queries.rb +4 -2
- data/lib/cassie/schema/rollback_command.rb +24 -0
- data/lib/cassie/schema/structure_dumper.rb +117 -0
- data/lib/cassie/{migration → schema}/structure_loader.rb +3 -3
- data/lib/cassie/schema/version.rb +143 -0
- data/lib/cassie/schema/version_file_loader.rb +34 -0
- data/lib/cassie/schema/version_loader.rb +31 -0
- data/lib/cassie/schema/version_object_loader.rb +19 -0
- data/lib/cassie/schema/version_writer.rb +108 -0
- data/lib/cassie/schema/versioning.rb +162 -0
- data/lib/cassie/schema.rb +24 -0
- data/lib/cassie/statements/README.md +61 -9
- data/lib/cassie/statements/core.rb +16 -5
- data/lib/cassie/statements/execution/results/core.rb +1 -1
- data/lib/cassie/statements/execution/results/modification_result.rb +1 -1
- data/lib/cassie/statements/execution/results/query_result.rb +1 -1
- data/lib/cassie/statements/execution.rb +40 -13
- data/lib/cassie/statements/statement/assignments.rb +33 -3
- data/lib/cassie/statements/statement/conditions.rb +3 -1
- data/lib/cassie/statements/statement/deleting.rb +27 -19
- data/lib/cassie/statements/statement/idempotency.rb +23 -4
- data/lib/cassie/statements/statement/inserting.rb +17 -10
- data/lib/cassie/statements/statement/limiting.rb +5 -2
- data/lib/cassie/statements/statement/mapping.rb +34 -6
- data/lib/cassie/statements/statement/preparation/cache.rb +1 -1
- data/lib/cassie/statements/statement/preparation.rb +37 -7
- data/lib/cassie/statements/statement/relations.rb +29 -8
- data/lib/cassie/statements/statement/selection.rb +51 -15
- data/lib/cassie/statements/statement/type_hinting.rb +12 -4
- data/lib/cassie/statements/statement/updating.rb +22 -8
- data/lib/cassie/statements/statement.rb +39 -14
- data/lib/cassie/statements.rb +12 -0
- data/lib/cassie/support/server_process.rb +117 -0
- data/lib/cassie/support/statement_parser.rb +3 -5
- data/lib/cassie/support/{command_runner.rb → system_command.rb} +22 -13
- data/lib/cassie/support.rb +3 -1
- data/lib/cassie/tasks/configuration/generate.rake +35 -0
- data/lib/cassie/tasks/io.rb +15 -0
- data/lib/cassie/tasks/migration/create.rake +49 -0
- data/lib/cassie/tasks/migration/import.rake +39 -0
- data/lib/cassie/tasks/migration/reset.rake +9 -0
- data/lib/cassie/tasks/restart.rake +5 -0
- data/lib/cassie/tasks/schema/drop.rake +28 -0
- data/lib/cassie/tasks/schema/dump.rake +21 -0
- data/lib/cassie/tasks/schema/history.rake +18 -0
- data/lib/cassie/tasks/schema/import.rake +40 -0
- data/lib/cassie/tasks/schema/init.rake +54 -0
- data/lib/cassie/tasks/schema/load.rake +19 -0
- data/lib/cassie/tasks/schema/migrate.rake +42 -0
- data/lib/cassie/tasks/schema/reset.rake +6 -0
- data/lib/cassie/tasks/schema/status.rake +19 -0
- data/lib/cassie/tasks/schema/version.rake +18 -0
- data/lib/cassie/tasks/schema/version_display.rb +50 -0
- data/lib/cassie/tasks/start.rake +17 -0
- data/lib/cassie/tasks/stop.rake +33 -0
- data/lib/cassie/tasks/tail.rake +14 -0
- data/lib/cassie/tasks/task_runner.rb +49 -0
- data/lib/cassie/tasks.rb +18 -0
- data/lib/cassie/testing/fake/execution_info.rb +1 -1
- data/lib/cassie/testing/fake/result.rb +3 -3
- data/lib/cassie/testing.rb +4 -0
- data/lib/cassie/version.rb +1 -1
- data/lib/cassie.rb +4 -1
- metadata +73 -17
- data/lib/cassie/migration/README.md +0 -141
- data/lib/cassie/migration/configuration.rb +0 -18
- data/lib/cassie/migration/initialization.rb +0 -70
- data/lib/cassie/migration/queries/create_schema_keyspace_query.rb +0 -17
- data/lib/cassie/migration/queries/insert_version_query.rb +0 -23
- data/lib/cassie/migration/queries/select_versions_query.rb +0 -14
- data/lib/cassie/migration/structure_dumper.rb +0 -94
- data/lib/cassie/migration/version.rb +0 -4
- data/lib/cassie/migration.rb +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 699a8f4a3e3028c10784854a30872b34d4d484e9
|
4
|
+
data.tar.gz: 8fe8d9ae25aa618c70bf97ce8d77cbb24cda64db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '08b12a72644d875eecf1fa1ced9ba3be680d853e7ee6d7072f56c22b052bccd6aa668e202b6aae17382604dd9b63691d45fb737753b7e8f155b70248cc799307'
|
7
|
+
data.tar.gz: 0d639234ec78ca7ba54b83507a4d27790d9ac0f46bf7a277d2fea6549d3493d31b9f7d473d2f64f5214a48b65da3064748c7055fdf10e432804a828cbdab3ff9
|
data/bin/cassie
CHANGED
@@ -1,185 +1,12 @@
|
|
1
|
-
|
2
|
-
require_relative '../lib/cassie'
|
3
|
-
|
4
|
-
def start
|
5
|
-
runner = Cassie::Support::CommandRunner.new("cassandra")
|
6
|
-
puts("Starting Cassandra...")
|
7
|
-
runner.run
|
8
|
-
runner.fail unless runner.completed?
|
9
|
-
|
10
|
-
if runner.output =~ /state jump to NORMAL/
|
11
|
-
puts "[#{green('✓')}] Cassandra Running"
|
12
|
-
else
|
13
|
-
runner.output.split("\n").grep(/ERROR/).each{|e| puts red(" " + e.gsub("; nested exception is:", "")) }
|
14
|
-
puts "[#{red('✘')}] Cassandra Failed to Start"
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def stop(kill_all=false)
|
19
|
-
runner = Cassie::Support::CommandRunner.new("ps", ["-awx"])
|
20
|
-
runner.run
|
21
|
-
fail runner.failure_message unless runner.success?
|
22
|
-
|
23
|
-
cassandra_awx = runner.output.split("\n").grep(/cassandra/)
|
24
|
-
pids = cassandra_awx.map{ |p| p.split(' ').first.to_i }
|
25
|
-
|
26
|
-
if pids.empty?
|
27
|
-
puts red("No Cassandra process was found. Is Cassandra running?")
|
28
|
-
exit(1)
|
29
|
-
elsif pids.length > 1 && !kill_all
|
30
|
-
puts red("Couldn't single out a Cassandra process.")
|
31
|
-
puts red(" - Is cqlsh running?")
|
32
|
-
puts red(" - Kill all cassandra processes with --all")
|
33
|
-
cassandra_awx.each do |p|
|
34
|
-
puts " - #{p.split(' ').first.ljust(5,' ')} | #{p.split(' ').last}"
|
35
|
-
end
|
36
|
-
exit(1)
|
37
|
-
end
|
38
|
-
|
39
|
-
puts("Stopping Cassandra...")
|
40
|
-
pids.each do|pid|
|
41
|
-
Process.kill("TERM", pid)
|
42
|
-
loop do
|
43
|
-
sleep(0.1)
|
44
|
-
begin
|
45
|
-
Process.getpgid( pid )
|
46
|
-
rescue Errno::ESRCH
|
47
|
-
break
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
puts "[#{green('✓')}] Cassandra Stopped"
|
53
|
-
end
|
54
|
-
|
55
|
-
def kick
|
56
|
-
stop
|
57
|
-
start
|
58
|
-
end
|
59
|
-
|
60
|
-
def generate_config
|
61
|
-
opts = {}
|
62
|
-
if ARGV[1]
|
63
|
-
opts[:destination_path] = if ARGV[1][0] == "/"
|
64
|
-
# cassie configuration:generate /usr/var/my_config_dir/cassandra_db.yml
|
65
|
-
ARGV[1]
|
66
|
-
else
|
67
|
-
# cassie configuration:generate my_config_dir/cassandra_db.yml
|
68
|
-
File.join(Dir.pwd, ARGV[1])
|
69
|
-
end
|
70
|
-
end
|
71
|
-
opts[:app_name] = ARGV[2] if ARGV[2]
|
72
|
-
|
73
|
-
generator = Cassie::Configuration::Generator.new(opts)
|
74
|
-
generator.save
|
75
|
-
puts "[✓] Cassandra configuration written to #{generator.destination_path}"
|
76
|
-
end
|
77
|
-
|
78
|
-
def dump_structure
|
79
|
-
dumper = Cassie::Migration::StructureDumper.new
|
80
|
-
dumper.dump
|
81
|
-
puts "[#{green("✓")}] Cassandra schema written to #{dumper.destination_path}"
|
82
|
-
|
83
|
-
rescue => e
|
84
|
-
puts e.message
|
85
|
-
exit(1)
|
86
|
-
end
|
87
|
-
|
88
|
-
def load_structure
|
89
|
-
loader = Cassie::Migration::StructureLoader.new
|
90
|
-
loader.load
|
91
|
-
puts "[#{green("✓")}] Cassandra schema loaded from #{loader.source_path}"
|
1
|
+
#!/usr/bin/env ruby
|
92
2
|
|
93
|
-
|
94
|
-
|
95
|
-
exit(1)
|
96
|
-
end
|
97
|
-
|
98
|
-
def tail_log
|
99
|
-
runner = Cassie::Support::CommandRunner.new("which", ["cassandra"])
|
100
|
-
runner.run!
|
101
|
-
|
102
|
-
bin_path = runner.output.gsub("\n", '')
|
103
|
-
log_path = bin_path.sub('bin/cassandra', 'logs/system.log')
|
104
|
-
puts white("Tailing Cassandra system log, Ctrl-C to stop...")
|
105
|
-
puts " #{log_path}:\n\n"
|
106
|
-
|
107
|
-
args = ['-f', log_path, '>', '/dev/tty']
|
108
|
-
runner = Cassie::Support::CommandRunner.new("tail", args)
|
109
|
-
runner.run!
|
110
|
-
end
|
111
|
-
|
112
|
-
# Use current schema to initialize versioned migrations
|
113
|
-
# * import current schema as initial migration
|
114
|
-
# * initialize cassie_schema keyspace and version table
|
115
|
-
# * insert initial version
|
116
|
-
# * dump structure
|
117
|
-
def initialize_migrations
|
118
|
-
Cassie::Migration.initialize_versioning
|
119
|
-
puts "[#{green("✓")}] Versioned migrations initialized. Current version: #{Cassie::Migration.version_number}"
|
120
|
-
rescue Cassie::Migration::AlreadyInitiailizedError
|
121
|
-
puts "[#{red('✘')}] Versioned migration metatdata already exists. Current version: #{Cassie::Migration.version_number}"
|
122
|
-
rescue => e
|
123
|
-
puts e.message
|
124
|
-
exit(1)
|
125
|
-
end
|
126
|
-
|
127
|
-
def schema_history
|
128
|
-
print_versions(Cassie::Migration.versions)
|
129
|
-
rescue Cassie::Migration::UninitializedError => e
|
130
|
-
puts red(e.message)
|
131
|
-
end
|
132
|
-
|
133
|
-
def schema_version
|
134
|
-
print_versions([Cassie::Migration.version])
|
135
|
-
rescue Cassie::Migration::UninitializedError => e
|
136
|
-
puts red(e.message)
|
137
|
-
end
|
138
|
-
|
139
|
-
def print_versions(versions)
|
140
|
-
require 'terminal-table'
|
141
|
-
members = [:version_number, :description, :migrator, :migrated_at]
|
142
|
-
titles = ['Version', 'Description', 'Migrated by', 'Migrated at']
|
143
|
-
table = Terminal::Table.new(headings: titles)
|
144
|
-
versions.each.with_index do |v, i|
|
145
|
-
v.version_number = "* #{v.version_number}" if i == 0
|
146
|
-
table.add_row(v.to_h.values_at(*members))
|
147
|
-
end
|
148
|
-
table.align_column(0, :right)
|
149
|
-
puts table
|
150
|
-
end
|
3
|
+
require_relative '../lib/cassie'
|
4
|
+
require_relative "../lib/cassie/tasks"
|
151
5
|
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
end
|
158
|
-
def green(message)
|
159
|
-
"\e[1;32m#{message}\e[0m"
|
6
|
+
runner = Cassie::Tasks::TaskRunner.new
|
7
|
+
if ARGV.length > 0
|
8
|
+
runner.run_command(ARGV)
|
9
|
+
else
|
10
|
+
runner.print_documentation
|
160
11
|
end
|
161
12
|
|
162
|
-
case ARGV[0]
|
163
|
-
when "start"
|
164
|
-
start
|
165
|
-
when "stop"
|
166
|
-
stop(ARGV.include?('--all'))
|
167
|
-
when /kick|restart/
|
168
|
-
kick
|
169
|
-
when /tail|log/
|
170
|
-
tail_log
|
171
|
-
when "configuration:generate"
|
172
|
-
generate_config
|
173
|
-
when "structure:dump"
|
174
|
-
dump_structure
|
175
|
-
when "structure:load"
|
176
|
-
load_structure
|
177
|
-
when /migrations?\:initialize/
|
178
|
-
initialize_migrations
|
179
|
-
when "schema:history"
|
180
|
-
schema_history
|
181
|
-
when "schema:version"
|
182
|
-
schema_version
|
183
|
-
else
|
184
|
-
puts red("`#{ARGV[0]}` is not a supported command.")
|
185
|
-
end
|
@@ -1,17 +1,24 @@
|
|
1
1
|
require_relative 'loading'
|
2
2
|
|
3
3
|
module Cassie::Configuration
|
4
|
-
#
|
5
|
-
#
|
4
|
+
# Extend a class with Core to add configuration management
|
5
|
+
# methods and attributes
|
6
6
|
module Core
|
7
7
|
include Loading
|
8
8
|
|
9
|
-
|
9
|
+
# The currently selected keyspace.
|
10
|
+
# If no keyspace has been explicitly set, then
|
11
|
+
# the default +:keyspace+ from {#configuration} is used.
|
12
|
+
attr_accessor :keyspace
|
10
13
|
|
14
|
+
# @!visibility private
|
11
15
|
def self.extended(extender)
|
12
16
|
extender.paths["cluster_configurations"] = "config/cassandra.yml"
|
13
17
|
end
|
14
18
|
|
19
|
+
# The currently active environment.
|
20
|
+
# Used to select which configuration will be used
|
21
|
+
# @!parse attr_accessor :env
|
15
22
|
def env
|
16
23
|
@env ||= ActiveSupport::StringInquirer.new(ENV["CASSANDRA_ENV"] || ENV["RACK_ENV"] || "development")
|
17
24
|
end
|
@@ -20,10 +27,17 @@ module Cassie::Configuration
|
|
20
27
|
@env = ActiveSupport::StringInquirer.new(val)
|
21
28
|
end
|
22
29
|
|
30
|
+
# Paths used for configuration loading.
|
31
|
+
#
|
32
|
+
# @return [Hash]
|
33
|
+
# * +:cluster_configurations+ - The .yml file defining the configuration for your cluster for various environments.
|
23
34
|
def paths
|
24
35
|
@paths ||= {}.with_indifferent_access
|
25
36
|
end
|
26
37
|
|
38
|
+
# The cluster configurations available for all environments
|
39
|
+
# @return [Hash{String => [Hash]}] Cluster configurations, keyed by environment.
|
40
|
+
# @!parse attr_accessor :configurations
|
27
41
|
def configurations
|
28
42
|
@configurations ||= cluster_configurations
|
29
43
|
end
|
@@ -35,6 +49,15 @@ module Cassie::Configuration
|
|
35
49
|
@configurations = val
|
36
50
|
end
|
37
51
|
|
52
|
+
# The currently active configuration used
|
53
|
+
# for initializing new cluster connections.
|
54
|
+
# If none has been set, the configuration for the
|
55
|
+
# current {#env} is used from {#configuration}
|
56
|
+
#
|
57
|
+
# Note: setting a {#configuration} value will override
|
58
|
+
# the {#configurations} attribute, rendering it useless
|
59
|
+
# @return [Hash{String,Symbol => Object}] Hash of +cassandra-driver+ supported cluster configuration attributes
|
60
|
+
# @!parse attr_accessor :configuration
|
38
61
|
def configuration
|
39
62
|
return @configuration if defined?(@configuration)
|
40
63
|
configurations[env]
|
@@ -1,6 +1,9 @@
|
|
1
1
|
module Cassie::Configuration
|
2
2
|
module Loading
|
3
|
-
|
3
|
+
# Fetch cluster configurations
|
4
|
+
# from configuraiton file located at
|
5
|
+
# +paths["cluster_configurations"]+
|
6
|
+
# @return [Hash{String => Object}] Configuration loaded from yml file
|
4
7
|
def cluster_configurations
|
5
8
|
path = paths["cluster_configurations"]
|
6
9
|
|
@@ -32,7 +35,7 @@ module Cassie::Configuration
|
|
32
35
|
msg = "Could not load cassandra cluster configurations. "
|
33
36
|
msg += "No cluster configurations exists at #{path}.\n"
|
34
37
|
msg += generation_instructions
|
35
|
-
msg
|
38
|
+
msg + ", or configure the correct path via Cassie::Configuration.paths['cluster_configurations'] = <path>."
|
36
39
|
end
|
37
40
|
|
38
41
|
def generation_instructions
|
data/lib/cassie/configuration.rb
CHANGED
data/lib/cassie/connection.rb
CHANGED
@@ -1,15 +1,21 @@
|
|
1
1
|
module Cassie
|
2
|
-
#
|
3
|
-
#
|
2
|
+
# Defines interface providing Cassandra connection management
|
3
|
+
# including object cacheing and reuse
|
4
4
|
# behavior in line with Cassandra best practices
|
5
|
+
#
|
6
|
+
# +include Cassie::Connection+ to add +keyspace+
|
7
|
+
# and +session+ attributes to the including module.
|
5
8
|
module Connection
|
6
9
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
10
|
+
# @!visibility private
|
11
|
+
def self.included(base)
|
12
|
+
base.instance_eval do
|
13
|
+
attr_writer :keyspace
|
14
|
+
end
|
15
|
+
base.extend ClassMethods
|
11
16
|
end
|
12
17
|
|
18
|
+
# @!parse extend ClassMethods
|
13
19
|
module ClassMethods
|
14
20
|
def keyspace(val=NilClass)
|
15
21
|
# support DSL style
|
@@ -30,7 +36,7 @@ module Cassie
|
|
30
36
|
end
|
31
37
|
|
32
38
|
def keyspace=(val)
|
33
|
-
#support Class.keyspace = :foo
|
39
|
+
#support non DSL style Class.keyspace = :foo
|
34
40
|
@keyspace = val
|
35
41
|
end
|
36
42
|
end
|
@@ -61,7 +61,7 @@ class HelpfulCounter
|
|
61
61
|
end
|
62
62
|
```
|
63
63
|
|
64
|
-
Ignoring the likely irresponsible example query used
|
64
|
+
Ignoring the likely irresponsible example query used. The object falls back to using the `Cassie::keyspace` value by default.
|
65
65
|
|
66
66
|
```ruby
|
67
67
|
Cassie.keyspace
|
@@ -76,7 +76,17 @@ object.user_count
|
|
76
76
|
=> 302525
|
77
77
|
```
|
78
78
|
|
79
|
-
|
79
|
+
Connection options follow the following lookup path:
|
80
|
+
|
81
|
+
1. the object instance value
|
82
|
+
2. the class instance value
|
83
|
+
3. the `Cassie` instance value
|
84
|
+
4. the `Cassie::configuruation[:option]` value
|
85
|
+
5. the `Cassandra::cluster` value (default cassandra driver value)
|
86
|
+
|
87
|
+
##### Examples:
|
88
|
+
|
89
|
+
###### Set at the class level:
|
80
90
|
|
81
91
|
```ruby
|
82
92
|
class Analytics::HelpfulCounter
|
@@ -102,7 +112,7 @@ object.user_count
|
|
102
112
|
|
103
113
|
```
|
104
114
|
|
105
|
-
|
115
|
+
###### Set at the object level:
|
106
116
|
|
107
117
|
```ruby
|
108
118
|
Cassie.keyspace
|
@@ -13,6 +13,11 @@ module Cassie::ConnectionHandler
|
|
13
13
|
include Instrumentation
|
14
14
|
end
|
15
15
|
|
16
|
+
# The cluster connection and metadata. This cluster connection is
|
17
|
+
# lazy-loaded, not calling out to Cassandra until it is first
|
18
|
+
# accessed. The single cluster object is cached for the entire process.
|
19
|
+
# @return [Cassandra::Cluster] The underlying driver cluster object
|
20
|
+
# @!parse attr_reader :cluster
|
16
21
|
def cluster
|
17
22
|
# Cassandra::cluster parses suppored
|
18
23
|
# options from the passed hash, no need
|
@@ -20,6 +25,12 @@ module Cassie::ConnectionHandler
|
|
20
25
|
@cluster ||= initialize_cluster
|
21
26
|
end
|
22
27
|
|
28
|
+
# @param [String, Symbol] name The keyspace name to search for
|
29
|
+
# @return [Boolean] indicating if the cluster has the keyspace defined
|
30
|
+
def keyspace_exists?(name)
|
31
|
+
Cassie.cluster.keyspaces.map(&:name).any?{|k| k.to_s == name.to_s}
|
32
|
+
end
|
33
|
+
|
23
34
|
protected
|
24
35
|
|
25
36
|
def initialize_cluster
|
@@ -7,10 +7,19 @@ module Cassie::ConnectionHandler
|
|
7
7
|
include Instrumentation
|
8
8
|
end
|
9
9
|
|
10
|
+
# Sessions cache containint sessions that
|
11
|
+
# have been opened to the {#cluster}
|
12
|
+
# @return [Hash{String => Cassandra::Session}] The underlying driver sessions, keyed by keyspaced name
|
13
|
+
# @!parse attr_reader :sessions
|
10
14
|
def sessions
|
11
15
|
@sessions ||= {}
|
12
16
|
end
|
13
17
|
|
18
|
+
# Fetches a session from the sessions cache.
|
19
|
+
# If no session has been opened to the requested keyspace
|
20
|
+
# a new session is created to the {#cluster} and cached.
|
21
|
+
# @param [String] keyspace The keyspace used for session scope. If +nil+, session will not be scoped (scoped to global space).
|
22
|
+
# @return [Cassandra::Session]
|
14
23
|
def session(keyspace=self.keyspace)
|
15
24
|
sessions[keyspace] || initialize_session(keyspace)
|
16
25
|
end
|
@@ -2,7 +2,14 @@ module Cassie
|
|
2
2
|
# Extend Cassie::ConnectionHandler to provide cluster and session
|
3
3
|
# cacheing in line with Cassandra best practices.
|
4
4
|
#
|
5
|
-
# The
|
5
|
+
# The extending class must provide two methods
|
6
|
+
# that will be used by the connection handler for connections:
|
7
|
+
# * +self.configuration+
|
8
|
+
# * A configuration hash defining cluster options per environment
|
9
|
+
# * Used when connecting through +cassandra_driver+
|
10
|
+
# * See [Configuration::Core] for a sample implementation with a .yml back-end
|
11
|
+
# * +self.keyspace+
|
12
|
+
# * The default keyspace to connect to when none is specified for a session.
|
6
13
|
module ConnectionHandler
|
7
14
|
require_relative 'connection_handler/cluster'
|
8
15
|
require_relative 'connection_handler/sessions'
|
@@ -10,11 +17,5 @@ module Cassie
|
|
10
17
|
|
11
18
|
include Cluster
|
12
19
|
include Sessions
|
13
|
-
|
14
|
-
def self.extended(extender)
|
15
|
-
#TODO: raise if extender doesn't
|
16
|
-
# respond to configuration
|
17
|
-
# and keyspace
|
18
|
-
end
|
19
20
|
end
|
20
21
|
end
|
data/lib/cassie/definition.rb
CHANGED
@@ -1,4 +1,32 @@
|
|
1
1
|
module Cassie
|
2
|
+
# A concrete implementation of a functional base class used to
|
3
|
+
# make CQL DDL or generic queries without any DSL.
|
4
|
+
# Inherit from this class to create application query classes.
|
5
|
+
#
|
6
|
+
# * The Cassandra connection is provided and managed by {Cassie::Connection}
|
7
|
+
# * Generic statement functionality is provided by {Statements::Core}
|
8
|
+
# * Defining the statement and options is up the subclass's responsibility (e.g. no DSL is provided)
|
9
|
+
#
|
10
|
+
# See the {file:lib/cassie/statements/README.md} for information on usage and examples.
|
11
|
+
#
|
12
|
+
# @example Inserting a record into a Table
|
13
|
+
# class CreateKeyspaceQuery < Cassie::Definition
|
14
|
+
# attr_accessor :name
|
15
|
+
#
|
16
|
+
# self.prepare = false
|
17
|
+
#
|
18
|
+
# def statement
|
19
|
+
# cql = %(
|
20
|
+
# CREATE KEYSPACE #{name}
|
21
|
+
# WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}
|
22
|
+
# AND durable_writes = true;
|
23
|
+
# )
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# CreateKeyspaceQuery.new(name: "my keyspace").excecute
|
28
|
+
# #=> true
|
29
|
+
#
|
2
30
|
class Definition
|
3
31
|
require_relative 'statements'
|
4
32
|
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Cassie::Extensions
|
2
|
+
module Object
|
3
|
+
module ColorMethods
|
4
|
+
def white(message)
|
5
|
+
"\e[1;37m#{message}\e[0m"
|
6
|
+
end
|
7
|
+
|
8
|
+
def red(message)
|
9
|
+
"\e[1;31m#{message}\e[0m"
|
10
|
+
end
|
11
|
+
|
12
|
+
def green(message)
|
13
|
+
"\e[1;32m#{message}\e[0m"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Object
|
20
|
+
include Cassie::Extensions::Object::ColorMethods
|
21
|
+
end
|
data/lib/cassie/modification.rb
CHANGED
@@ -1,4 +1,33 @@
|
|
1
1
|
module Cassie
|
2
|
+
# A concrete implementation of a functional base class used to
|
3
|
+
# make CQL +INSERT+, +UPDATE+, and +DELETE+ queries.
|
4
|
+
# Inherit from this class to create application query classes.
|
5
|
+
#
|
6
|
+
# * The Cassandra connection is provided and managed by {Cassie::Connection}
|
7
|
+
# * Generic statement functionality is provided by {Statements::Core}
|
8
|
+
# * +INSERT|UPDATE|DELETE+ specific statement DSL and functionality is provided by {Statements::Modification}
|
9
|
+
#
|
10
|
+
# See the {file:lib/cassie/statements/README.md} for information on usage and examples.
|
11
|
+
#
|
12
|
+
# @example Inserting a record into a Table
|
13
|
+
# class InsertUserQuery < Cassie::Modification
|
14
|
+
#
|
15
|
+
# insert_into :users_by_username
|
16
|
+
# consistency :all
|
17
|
+
#
|
18
|
+
# set :id
|
19
|
+
# set :username
|
20
|
+
#
|
21
|
+
# map_from :user
|
22
|
+
#
|
23
|
+
# def id
|
24
|
+
# Cassandra::TimeUuid::Generator.new.now
|
25
|
+
# end
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# InsertUserQuery.new(user: user).excecute
|
29
|
+
# #=> true
|
30
|
+
#
|
2
31
|
class Modification
|
3
32
|
require_relative 'statements'
|
4
33
|
|
data/lib/cassie/query.rb
CHANGED
@@ -1,4 +1,28 @@
|
|
1
1
|
module Cassie
|
2
|
+
# A concrete implementation of a functional base class used to
|
3
|
+
# make CQL +SELECT+ queries. Inherit from this class to create application query classes.
|
4
|
+
#
|
5
|
+
# * The Cassandra connection is provided and managed by {Cassie::Connection}
|
6
|
+
# * Generic statement functionality is provided by {Statements::Core}
|
7
|
+
# * +SELECT+ specific statement DSL and functionality is provided by {Statements::Query}
|
8
|
+
#
|
9
|
+
# See the {file:lib/cassie/statements/README.md} for information on usage and examples.
|
10
|
+
#
|
11
|
+
# @example Selecting a record from a Table
|
12
|
+
# class UsersByUsernameQuery < Cassie::Query
|
13
|
+
#
|
14
|
+
# select_from :users_by_username
|
15
|
+
#
|
16
|
+
# where :username, :eq
|
17
|
+
#
|
18
|
+
# def build_result(row)
|
19
|
+
# User.new(row)
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# user = UsersByUsernameQuery.new(username: 'eprothro').fetch_first
|
24
|
+
# #=> #<User:0x007fedec219cd8 @id=123, @username="eprothro">
|
25
|
+
#
|
2
26
|
class Query
|
3
27
|
require_relative 'statements'
|
4
28
|
|
@@ -6,5 +30,8 @@ module Cassie
|
|
6
30
|
include Statements::Core
|
7
31
|
include Statements::Query
|
8
32
|
|
33
|
+
# @!parse extend Cassie::Connection::ClassMethods
|
34
|
+
# @!parse extend Cassie::Statements::Core::ClassMethods
|
35
|
+
# @!parse extend Cassie::Statements::Query::ClassMethods
|
9
36
|
end
|
10
37
|
end
|