cassandra-helpers 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8cb34bc57cff105ccf5010bef074312b3048de3f491917610656baa30aba7396
4
+ data.tar.gz: 530f52c6c2d06562133750ab76769ecbd1ab315c36bf3ba6b8be9cb7d32aaea4
5
+ SHA512:
6
+ metadata.gz: 7c574501bfb105590c8433d7bbc7d35e1a67e0f89672b11bead4830773f98f20d2083b19242f02342bddfc3c64a13ac507da34c1452a8ca6fefcf98cc751c529
7
+ data.tar.gz: 31ad252b4561fd564d9015947d7935fd28997727b3ce4d1ca812ed61a20f9ce58eddf137b8ddbe5e9e41ecf11e8da4486b4226b314cc475a2e86254b58dbaae7
@@ -0,0 +1,58 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
57
+
58
+ *~
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 dougyouch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ # cassandra-helpers
2
+ Utility methods for working with Cassandra
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'cassandra-helpers'
5
+ s.version = '0.1.0'
6
+ s.licenses = ['MIT']
7
+ s.summary = 'Cassandra Helper Methods'
8
+ s.description = 'Utility methods for working with Cassandra'
9
+ s.authors = ['Doug Youch']
10
+ s.email = 'dougyouch@gmail.com'
11
+ s.homepage = 'https://github.com/dougyouch/cassandra-helpers'
12
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
13
+
14
+ s.add_runtime_dependency 'cassandra-driver'
15
+ end
@@ -0,0 +1,86 @@
1
+ require 'cassandra'
2
+
3
+ module CassandraHelpers
4
+ module_function
5
+
6
+ def logger
7
+ @logger
8
+ end
9
+
10
+ def logger=(logger)
11
+ @logger = logger
12
+ end
13
+
14
+ def create_cluster(hosts, port = 9042)
15
+ Cassandra.cluster(hosts: hosts, port: port)
16
+ end
17
+
18
+ def get_keyspace(cluster, name)
19
+ cluster.keyspaces.detect { |keyspace| keyspace.name == name }
20
+ end
21
+
22
+ def create_session(cluster, keyspace_name)
23
+ cluster.connect(keyspace_name)
24
+ end
25
+
26
+ def get_table(keyspace, table_name)
27
+ keyspace.tables.detect { |table| table.name == table_name }
28
+ end
29
+
30
+ def execute_query(session, cql, options = {})
31
+ started_at = Time.now
32
+ result = session.execute(cql, options)
33
+ took = '%.2f' % (Time.now - started_at)
34
+ logger.debug("KS [#{took}] #{cql}") if logger && logger.debug?
35
+ result
36
+ end
37
+
38
+ def delete_record(session, table, record)
39
+ cql = "DELETE FROM #{Cassandra::Util.escape_name(table.name)} WHERE "
40
+ cql += table.primary_key.map { |key| "#{Cassandra::Util.escape_name(key.name)} = #{Cassandra::Util.encode_object(record[key.name])}" }.join(' AND ')
41
+ execute_query(session, cql)
42
+ end
43
+
44
+ def delete_records(session, table, records)
45
+ records.each do |record|
46
+ delete_record(session, table, record)
47
+ end
48
+ end
49
+
50
+ def each_record(session, cql, page_size = 100)
51
+ result = execute_query(session, cql, page_size: page_size)
52
+ loop do
53
+ result.each { |row| yield row }
54
+ break if result.last_page?
55
+
56
+ result = result.next_page
57
+ end
58
+ end
59
+
60
+ def retry(retries = 10, sleep_times_between_retries = nil)
61
+ max_retries = retries
62
+ sleep_times_between_retries = [sleep_times_between_retries] if sleep_times_between_retries.is_a?(Numeric)
63
+
64
+ begin
65
+ yield
66
+ rescue Cassandra::Errors::TimeoutError => e
67
+ logger.error(e) if logger
68
+
69
+ sleep_time =
70
+ if sleep_times_between_retries
71
+ sleep_times_between_retries[max_retries - retries] || sleep_times_between_retries.last
72
+ end
73
+
74
+ retries -= 1
75
+ if retries <= 0
76
+ logger.error('no more retries') if logger
77
+ raise(e)
78
+ end
79
+
80
+ logger.warn("retrying, remaining attempts #{retries}, sleeping for #{sleep_time.to_f} seconds") if logger
81
+ sleep(sleep_time) if sleep_time
82
+
83
+ retry
84
+ end
85
+ end
86
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cassandra-helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Doug Youch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cassandra-driver
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Utility methods for working with Cassandra
28
+ email: dougyouch@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".gitignore"
34
+ - LICENSE
35
+ - README.md
36
+ - cassandra-helpers.gemspec
37
+ - lib/cassandra-helpers.rb
38
+ homepage: https://github.com/dougyouch/cassandra-helpers
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.0.6
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Cassandra Helper Methods
61
+ test_files: []