cassandra_db 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dd796bfe59156a303e83eaf1569080033420de8f67653cde1260e4f59ebe150b
4
+ data.tar.gz: 5bb541cce2f2d44ca07bbcb5c798d2720362b2b5a64d1b20ff6adc087a8d771d
5
+ SHA512:
6
+ metadata.gz: ab0ed5ce53a33df29a4ec670044f1c019e2bbaef3772d1987bc929f9b5d33f6a2a229531231f15e9e3f58c14529e5cffda489a6eb6b7f6cc1b49acf4c3449182
7
+ data.tar.gz: 2782ab52a60b48d8258bc736a3c4376451ff830176144363002b24f302a68ae17abc6f4edbc8470c26e78c75a036810ff64c3c8b573d794f04c0f3a16d1e0725
data/.coveralls.yml ADDED
@@ -0,0 +1,2 @@
1
+ service_name: travis-ci
2
+ repo_token:
@@ -0,0 +1,33 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [ '**' ]
6
+ pull_request:
7
+ branches: [ '**' ]
8
+
9
+ jobs:
10
+ test:
11
+
12
+ name: Tests
13
+ runs-on: ubuntu-latest
14
+ strategy:
15
+ matrix:
16
+ ruby-version: ['2.3', '2.4', '2.5', '2.6', '2.7', '3.0', '3.1', 'jruby-9.2.9.0']
17
+
18
+ services:
19
+ cassandra:
20
+ image: cassandra:2.1.18
21
+ ports:
22
+ - 9042:9042
23
+ options: --name cassandra_test --health-cmd "cqlsh --debug" --health-interval 5s --health-retries 10
24
+
25
+ steps:
26
+ - uses: actions/checkout@v3
27
+ - name: Set up Ruby
28
+ uses: ruby/setup-ruby@v1
29
+ with:
30
+ ruby-version: ${{ matrix.ruby-version }}
31
+ bundler-cache: true
32
+ - name: Run tests
33
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ cassandra_db
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.3.8
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cassandra_db.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Gabriel Naiman
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # CassandraDB
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/cassandra_db.svg)](https://rubygems.org/gems/cassandra_db)
4
+ [![CI](https://github.com/gabynaiman/cassandra_db/actions/workflows/ci.yml/badge.svg)](https://github.com/gabynaiman/cassandra_db/actions/workflows/ci.yml)
5
+ [![Coverage Status](https://coveralls.io/repos/gabynaiman/cassandra_db/badge.svg?branch=master)](https://coveralls.io/r/gabynaiman/cassandra_db?branch=master)
6
+ [![Code Climate](https://codeclimate.com/github/gabynaiman/cassandra_db.svg)](https://codeclimate.com/github/gabynaiman/cassandra_db)
7
+
8
+ Cassandra DB adapter inspired on Sequel
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'cassandra_db'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install cassandra_db
25
+
26
+ ## Usage
27
+
28
+ ### Connect
29
+ ```ruby
30
+ db = CassandraDB.connect hosts: ['localhost'], port: 9042, keyspace: 'system'
31
+ ```
32
+
33
+ ### Metadata
34
+ ```ruby
35
+ db.keyspace # => :system
36
+
37
+ db.keyspaces # => [:system, :system_traces]
38
+
39
+ db.tables # => Array of table names in current keyspace
40
+ ```
41
+
42
+ ### Change keyspace
43
+ ```ruby
44
+ db.use_keyspace 'my_keyspace'
45
+ db.keyspace # => :my_keyspace
46
+ ```
47
+
48
+ ### Create and drop keyspaces
49
+ ```ruby
50
+ replication_opts = {
51
+ class: 'SimpleStrategy',
52
+ replication_factor: 1
53
+ }
54
+ db.create_keyspace :my_keyspace, replication: replication_opts, durable_writes: true
55
+
56
+ db.drop_keyspace :my_keyspace
57
+ ```
58
+
59
+ ### Queries
60
+ ```ruby
61
+ dataset = db[table_name] # => CassandraDB::Dataset
62
+ dataset = db[table_name].where(field: 'value') # => CassandraDB::Dataset
63
+ ```
64
+
65
+ ### Dataset/Enumerable methods
66
+ ```ruby
67
+ dataset.count
68
+ dataset.each
69
+ dataset.map
70
+ dataset.all
71
+ ```
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gabynaiman/cassandra_db.
76
+
77
+
78
+ ## License
79
+
80
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
81
+
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:spec) do |t|
5
+ t.libs << 'spec'
6
+ t.libs << 'lib'
7
+ t.pattern = ENV['DIR'] ? File.join(ENV['DIR'], '**', '*_spec.rb') : 'spec/**/*_spec.rb'
8
+ t.verbose = false
9
+ t.warning = false
10
+ t.loader = nil if ENV['TEST']
11
+ ENV['TEST'], ENV['LINE'] = ENV['TEST'].split(':') if ENV['TEST'] && !ENV['LINE']
12
+ t.options = ''
13
+ t.options << "--name=/#{ENV['NAME']}/ " if ENV['NAME']
14
+ t.options << "-l #{ENV['LINE']} " if ENV['LINE'] && ENV['TEST']
15
+ end
16
+
17
+ task default: :spec
18
+
19
+ desc 'Pry console'
20
+ task :console do
21
+ require 'cassandra_db'
22
+ require 'pry'
23
+ ARGV.clear
24
+ Pry.start
25
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cassandra_db/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'cassandra_db'
8
+ spec.version = CassandraDB::VERSION
9
+ spec.authors = ['Gabriel Naiman']
10
+ spec.email = ['gabynaiman@gmail.com']
11
+
12
+ spec.summary = 'Cassandra DB adapter inspired on Sequel'
13
+ spec.description = 'Cassandra DB adapter inspired on Sequel'
14
+ spec.homepage = 'https://github.com/gabynaiman/cassandra_db'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_runtime_dependency 'cassandra-driver', '~> 3.0'
23
+ if RUBY_VERSION >= '3.0.0'
24
+ spec.add_runtime_dependency 'sorted_set', '~> 1.0'
25
+ end
26
+
27
+ spec.add_development_dependency 'rake', '~> 11.0'
28
+ spec.add_development_dependency 'minitest', '~> 5.0', '< 5.11'
29
+ spec.add_development_dependency 'minitest-colorin', '~> 0.1'
30
+ spec.add_development_dependency 'minitest-line', '~> 0.6'
31
+ spec.add_development_dependency 'simplecov', '~> 0.14'
32
+ spec.add_development_dependency 'coveralls', '~> 0.8'
33
+ spec.add_development_dependency 'pry-nav', '~> 0.2'
34
+ end
@@ -0,0 +1,64 @@
1
+ module CassandraDB
2
+ class Database
3
+
4
+ DEFAULTS = {
5
+ keyspace: DEFAULT_KEYSPACE
6
+ }.freeze
7
+
8
+ def initialize(options={})
9
+ @options = DEFAULTS.merge(options)
10
+ keyspace = @options.delete :keyspace
11
+ @cluster = Cassandra.cluster @options
12
+ @session = use_keyspace keyspace
13
+ end
14
+
15
+ def keyspace
16
+ session.keyspace.to_sym
17
+ end
18
+
19
+ def use_keyspace(name)
20
+ @session = cluster.connect name.to_s
21
+ end
22
+
23
+ def keyspaces
24
+ cluster.refresh_schema
25
+ cluster.keyspaces.map { |k| k.name.to_sym }.sort
26
+ end
27
+
28
+ def tables
29
+ cluster.refresh_schema
30
+ cluster.keyspaces.detect { |k| k.name == session.keyspace }.tables.map { |t| t.name.to_sym }.sort
31
+ end
32
+
33
+ def from(table)
34
+ Dataset.new self, from: table
35
+ end
36
+ alias_method :[], :from
37
+
38
+ def create_keyspace(name, replication:DEFAULT_REPLICATION, durable_writes:true)
39
+ cql = %Q{
40
+ CREATE KEYSPACE #{name}
41
+ WITH REPLICATION = #{JSON.dump(replication).gsub('"', '\'')}
42
+ AND DURABLE_WRITES = #{durable_writes};
43
+ }
44
+ execute cql
45
+ end
46
+
47
+ def drop_keyspace(name)
48
+ execute "DROP KEYSPACE #{name};"
49
+ end
50
+
51
+ def execute(*args)
52
+ session.execute(*args)
53
+ end
54
+
55
+ def inspect
56
+ "#<#{self.class.name}: options=#{options.inspect} keyspace=#{keyspace.inspect}>"
57
+ end
58
+
59
+ private
60
+
61
+ attr_reader :cluster, :session, :options
62
+
63
+ end
64
+ end
@@ -0,0 +1,52 @@
1
+ module CassandraDB
2
+ class Dataset
3
+
4
+ include Enumerable
5
+
6
+ def initialize(db, from:, where:[])
7
+ @db = db
8
+ @table = from
9
+ @conditions = where
10
+ end
11
+
12
+ def where(filters)
13
+ new_conditions = filters.map do |field, value|
14
+ {
15
+ field: field,
16
+ operator: value.is_a?(Array) ? 'IN' : '=',
17
+ value: value
18
+ }
19
+ end
20
+
21
+ Dataset.new db, from: table, where: conditions + new_conditions
22
+ end
23
+
24
+ def each(&block)
25
+ all.each(&block)
26
+ end
27
+
28
+ def all
29
+ db.execute(*cql).to_a
30
+ end
31
+
32
+ def cql
33
+ select = 'SELECT *'
34
+ from = "FROM #{table}"
35
+ where = conditions.any? ? "WHERE #{conditions.map { |c| "#{c[:field]} #{c[:operator]} :#{c[:field]}" }.join(' AND ')}" : ''
36
+ arguments = conditions.each_with_object({}) { |c,h| h[c[:field]] = c[:value] }
37
+
38
+ [
39
+ "#{select} #{from} #{where}".strip,
40
+ {arguments: arguments}
41
+ ]
42
+ end
43
+
44
+ def inspect
45
+ "#<#{self.class.name}: #{cql.join(', ')}>"
46
+ end
47
+
48
+ private
49
+
50
+ attr_reader :db, :table, :conditions
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module CassandraDB
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,21 @@
1
+ require 'cassandra'
2
+ require 'json'
3
+
4
+ module CassandraDB
5
+
6
+ DEFAULT_KEYSPACE = 'system'.freeze
7
+
8
+ DEFAULT_REPLICATION = {
9
+ class: 'SimpleStrategy',
10
+ replication_factor: 1
11
+ }.freeze
12
+
13
+ def self.connect(options={})
14
+ Database.new options
15
+ end
16
+
17
+ end
18
+
19
+ require_relative 'cassandra_db/version'
20
+ require_relative 'cassandra_db/database'
21
+ require_relative 'cassandra_db/dataset'
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cassandra_db
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Naiman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-11-24 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: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '11.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '11.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ - - "<"
49
+ - !ruby/object:Gem::Version
50
+ version: '5.11'
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '5.0'
58
+ - - "<"
59
+ - !ruby/object:Gem::Version
60
+ version: '5.11'
61
+ - !ruby/object:Gem::Dependency
62
+ name: minitest-colorin
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '0.1'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '0.1'
75
+ - !ruby/object:Gem::Dependency
76
+ name: minitest-line
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '0.6'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '0.6'
89
+ - !ruby/object:Gem::Dependency
90
+ name: simplecov
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '0.14'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '0.14'
103
+ - !ruby/object:Gem::Dependency
104
+ name: coveralls
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '0.8'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '0.8'
117
+ - !ruby/object:Gem::Dependency
118
+ name: pry-nav
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '0.2'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '0.2'
131
+ description: Cassandra DB adapter inspired on Sequel
132
+ email:
133
+ - gabynaiman@gmail.com
134
+ executables: []
135
+ extensions: []
136
+ extra_rdoc_files: []
137
+ files:
138
+ - ".coveralls.yml"
139
+ - ".github/workflows/ci.yml"
140
+ - ".gitignore"
141
+ - ".ruby-gemset"
142
+ - ".ruby-version"
143
+ - Gemfile
144
+ - LICENSE.txt
145
+ - README.md
146
+ - Rakefile
147
+ - cassandra_db.gemspec
148
+ - lib/cassandra_db.rb
149
+ - lib/cassandra_db/database.rb
150
+ - lib/cassandra_db/dataset.rb
151
+ - lib/cassandra_db/version.rb
152
+ homepage: https://github.com/gabynaiman/cassandra_db
153
+ licenses:
154
+ - MIT
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 2.7.8
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: Cassandra DB adapter inspired on Sequel
176
+ test_files: []