cassandra_store 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rubocop.yml +123 -0
- data/.travis.yml +14 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +282 -0
- data/Rakefile +8 -0
- data/cassandra_store.gemspec +31 -0
- data/docker-compose.yml +9 -0
- data/lib/cassandra_store.rb +18 -0
- data/lib/cassandra_store/base.rb +426 -0
- data/lib/cassandra_store/migration.rb +41 -0
- data/lib/cassandra_store/railtie.rb +7 -0
- data/lib/cassandra_store/relation.rb +187 -0
- data/lib/cassandra_store/schema_migration.rb +11 -0
- data/lib/cassandra_store/tasks/cassandra.rake +45 -0
- data/lib/cassandra_store/version.rb +3 -0
- data/spec/cassandra_store/base_spec.rb +691 -0
- data/spec/cassandra_store/migration_spec.rb +79 -0
- data/spec/cassandra_store/relation_spec.rb +222 -0
- data/spec/cassandra_store/schema_migration_spec.rb +19 -0
- data/spec/fixtures/1589957812_migration1.rb +5 -0
- data/spec/fixtures/1589957813_migration2.rb +5 -0
- data/spec/spec_helper.rb +87 -0
- metadata +214 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 55cc964c9acca186677f0a9de4ea89d62526c050e50e52c09735c5948c5d7dd8
|
4
|
+
data.tar.gz: 63ae9aac6d672d2d45779a78fe7b911014ec752e4c878dc6f6e5c062f3bbe8a5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a24dc8c1517c235b4c0a406199a3048e92724554f4072d34a106ac42e21d0778e02ff372fd17af5be0a0aae6a113b3340bad0bb06f3014d3ddbcb3afbf5eea74
|
7
|
+
data.tar.gz: 04c4394a2c87fab0cbfd947931f9602f600d04d4642a4ee9264ea087da861f75c17c2749805efb065c6fba5d04600d0ede89f12a314c09db71059cb1eb176cf5
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
AllCops:
|
2
|
+
NewCops: enable
|
3
|
+
|
4
|
+
Naming/FileName:
|
5
|
+
Exclude:
|
6
|
+
- lib/cassandra-record.rb
|
7
|
+
|
8
|
+
Metrics/ParameterLists:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
Style/DoubleNegation:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Style/FrozenStringLiteralComment:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Style/RedundantPercentQ:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Style/PercentLiteralDelimiters:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
Style/SpecialGlobalVars:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Security/Eval:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Style/WordArray:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Style/ClassAndModuleChildren:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
Style/TrivialAccessors:
|
36
|
+
Enabled: false
|
37
|
+
|
38
|
+
Style/Alias:
|
39
|
+
Enabled: false
|
40
|
+
|
41
|
+
Style/StringLiteralsInInterpolation:
|
42
|
+
EnforcedStyle: double_quotes
|
43
|
+
|
44
|
+
Metrics/ClassLength:
|
45
|
+
Enabled: false
|
46
|
+
|
47
|
+
Naming/MethodParameterName:
|
48
|
+
Enabled: false
|
49
|
+
|
50
|
+
Style/SymbolArray:
|
51
|
+
Enabled: false
|
52
|
+
|
53
|
+
Layout/RescueEnsureAlignment:
|
54
|
+
Enabled: false
|
55
|
+
|
56
|
+
Layout/LineLength:
|
57
|
+
Enabled: false
|
58
|
+
|
59
|
+
Metrics/MethodLength:
|
60
|
+
Enabled: false
|
61
|
+
|
62
|
+
Metrics/ModuleLength:
|
63
|
+
Enabled: false
|
64
|
+
|
65
|
+
Style/ZeroLengthPredicate:
|
66
|
+
Enabled: false
|
67
|
+
|
68
|
+
Metrics/PerceivedComplexity:
|
69
|
+
Enabled: false
|
70
|
+
|
71
|
+
Metrics/AbcSize:
|
72
|
+
Enabled: false
|
73
|
+
|
74
|
+
Metrics/CyclomaticComplexity:
|
75
|
+
Enabled: false
|
76
|
+
|
77
|
+
Metrics/BlockLength:
|
78
|
+
Enabled: false
|
79
|
+
|
80
|
+
Metrics/BlockNesting:
|
81
|
+
Enabled: false
|
82
|
+
|
83
|
+
Style/NumericPredicate:
|
84
|
+
Enabled: false
|
85
|
+
|
86
|
+
Naming/AccessorMethodName:
|
87
|
+
Enabled: false
|
88
|
+
|
89
|
+
Naming/MemoizedInstanceVariableName:
|
90
|
+
Enabled: false
|
91
|
+
|
92
|
+
Style/StringLiterals:
|
93
|
+
EnforcedStyle: double_quotes
|
94
|
+
|
95
|
+
Style/Documentation:
|
96
|
+
Enabled: false
|
97
|
+
|
98
|
+
Naming/ConstantName:
|
99
|
+
Enabled: false
|
100
|
+
|
101
|
+
Style/MutableConstant:
|
102
|
+
Enabled: false
|
103
|
+
|
104
|
+
Layout/MultilineMethodCallIndentation:
|
105
|
+
EnforcedStyle: indented
|
106
|
+
|
107
|
+
Layout/ParameterAlignment:
|
108
|
+
EnforcedStyle: with_fixed_indentation
|
109
|
+
|
110
|
+
Lint/UnusedMethodArgument:
|
111
|
+
Enabled: false
|
112
|
+
|
113
|
+
Layout/FirstArrayElementIndentation:
|
114
|
+
EnforcedStyle: consistent
|
115
|
+
|
116
|
+
Style/IfUnlessModifier:
|
117
|
+
Enabled: false
|
118
|
+
|
119
|
+
Style/RedundantBegin:
|
120
|
+
Enabled: false
|
121
|
+
|
122
|
+
Style/OptionalArguments:
|
123
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Benjamin Vetter
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,282 @@
|
|
1
|
+
[![Build Status](https://secure.travis-ci.org/mrkamel/cassandra-store.png?branch=master)](http://travis-ci.org/mrkamel/cassandra-store)
|
2
|
+
|
3
|
+
# CassandraStore
|
4
|
+
|
5
|
+
CassandraStore is a fun to use ORM for Cassandra with a chainable,
|
6
|
+
ActiveStore like DSL for querying, inserting, updating and deleting records
|
7
|
+
plus built-in migration support. It is built on-top of the cassandra-driver
|
8
|
+
gem, using its built-in automated paging what is drastically reducing the
|
9
|
+
complexity of the code base.
|
10
|
+
|
11
|
+
## Install
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'cassandra-store'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install cassandra-store
|
24
|
+
|
25
|
+
# Usage
|
26
|
+
|
27
|
+
## Connecting
|
28
|
+
|
29
|
+
First and foremost, you need to connect to your cassandra cluster like so:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
CassandraStore::Base.configure(
|
33
|
+
hosts: ["127.0.0.1"],
|
34
|
+
keyspace: "my_keyspace",
|
35
|
+
cluster_settings: { consistency: :quorum }
|
36
|
+
)
|
37
|
+
```
|
38
|
+
|
39
|
+
When using rails, you want to do that in an initializer. If you do not yet have
|
40
|
+
a keyspace, you additionally want to pass `replication` settings:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
CassandraStore::Base.configure(
|
44
|
+
hosts: ["127.0.0.1"],
|
45
|
+
keyspace: "my_keyspace",
|
46
|
+
cluster_settings: { consistency: :quorum },
|
47
|
+
replication: { class: 'SimpleStrategy', replication_factor: 1 }
|
48
|
+
)
|
49
|
+
```
|
50
|
+
|
51
|
+
Afterwards, you can create/drop the specified keyspace:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
rake cassandra:keyspace:create
|
55
|
+
rake cassandra:keyspace:drop
|
56
|
+
```
|
57
|
+
|
58
|
+
## Migrations
|
59
|
+
|
60
|
+
If you are on rails and you don't have any tables yet, you can add migrations
|
61
|
+
now. There is no generator yet, so you have to create them manually:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
# cassandra/migrate/1589896040_create_posts.rb
|
65
|
+
|
66
|
+
class CreatePosts < CassandraStore::Migration
|
67
|
+
def up
|
68
|
+
execute <<-CQL
|
69
|
+
CREATE TABLE posts (
|
70
|
+
user TEXT,
|
71
|
+
domain TEXT,
|
72
|
+
id TIMEUUID,
|
73
|
+
message TEXT,
|
74
|
+
PRIMARY KEY ((user, domain), id)
|
75
|
+
)
|
76
|
+
CQL
|
77
|
+
end
|
78
|
+
|
79
|
+
def down
|
80
|
+
execute "DROP TABLE posts"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
Afterwards, simply run `rake cassandra:migrate`.
|
86
|
+
|
87
|
+
## Models
|
88
|
+
|
89
|
+
Creating models couldn't be easier:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
class Post < CassandraStore::Base
|
93
|
+
column :user, :text, partition_key: true
|
94
|
+
column :domain, :text, partition_key: true
|
95
|
+
column :id, :timeuuid, clustering_key: true
|
96
|
+
column :message, :text
|
97
|
+
|
98
|
+
validates_presence_of :user, :domain, :message
|
99
|
+
|
100
|
+
before_create do
|
101
|
+
self.id ||= generate_timeuuid
|
102
|
+
end
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
106
|
+
Let's check this out in detail:
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
column :user, :text, partition_key: true
|
110
|
+
column :domain, :text, partition_key: true
|
111
|
+
```
|
112
|
+
|
113
|
+
tells CassandraStore that your partition key is comprised of the `user` column
|
114
|
+
as well as the `domain` column. For more information regarding partition keys
|
115
|
+
and the data model of cassandra, please check out the cassandra docs. Afterwards,
|
116
|
+
the clustering/sorting key is specified via:
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
column :id, :timeuuid, clustering_key: true
|
120
|
+
```
|
121
|
+
|
122
|
+
The `id` is assigned here:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
self.id ||= generate_timeuuid
|
126
|
+
```
|
127
|
+
|
128
|
+
Please note, CassandraStore never auto-assigns any values for you, but you
|
129
|
+
have to assign them. You can pass a timestamp to `generate_timeuuid` as well:
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
generate_timeuuid(Time.now)
|
133
|
+
```
|
134
|
+
|
135
|
+
This is desirable when you have timestamp columns as well and you want them
|
136
|
+
to match with your timeuuid key.
|
137
|
+
|
138
|
+
Similarly, when using `UUID` instead of `TIMEUUID` you have to use
|
139
|
+
`generate_uuid` instead.
|
140
|
+
|
141
|
+
In addition, you can of course use all kinds of validations, hooks, etc.
|
142
|
+
|
143
|
+
## Querying
|
144
|
+
|
145
|
+
The interface for dealing with records and querying them is very similar
|
146
|
+
to the interface of `ActiveRecord`:
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
Post.create!(user: "mrkamel", ...)
|
150
|
+
Post.create(...)
|
151
|
+
Post.new(...).save
|
152
|
+
Post.new(...).save!
|
153
|
+
Post.first.delete
|
154
|
+
Post.first.destroy
|
155
|
+
```
|
156
|
+
|
157
|
+
CassandraStore supports comprehensive query methods in a chainable way:
|
158
|
+
|
159
|
+
* `all`
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
Post.all
|
163
|
+
```
|
164
|
+
|
165
|
+
* `where`
|
166
|
+
|
167
|
+
```ruby
|
168
|
+
Post.where(user: "mrkamel", domain: "example.com")
|
169
|
+
```
|
170
|
+
|
171
|
+
* `where_cql`
|
172
|
+
|
173
|
+
```ruby
|
174
|
+
Post.where_cql("user = :user", user: "mrkamel")
|
175
|
+
```
|
176
|
+
|
177
|
+
* `limit`
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
Post.where(...).limit(10)
|
181
|
+
```
|
182
|
+
|
183
|
+
* `order`
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
Post.where(...).order(id: "asc")
|
187
|
+
```
|
188
|
+
|
189
|
+
* `distinct`
|
190
|
+
|
191
|
+
```ruby
|
192
|
+
Post.select(:user, :domain).distinct
|
193
|
+
```
|
194
|
+
|
195
|
+
* `select`
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
Post.select(:user, :domain)
|
199
|
+
```
|
200
|
+
|
201
|
+
Please note, when using `select` in the end an array of hashes will be returned
|
202
|
+
instead of an array of `Post` objects.
|
203
|
+
|
204
|
+
* `count`
|
205
|
+
|
206
|
+
```ruby
|
207
|
+
Post.where(...).count
|
208
|
+
```
|
209
|
+
|
210
|
+
* `first`
|
211
|
+
|
212
|
+
```ruby
|
213
|
+
Post.where(...).first
|
214
|
+
```
|
215
|
+
|
216
|
+
* `find_each`
|
217
|
+
|
218
|
+
```ruby
|
219
|
+
Post.where(...).find_each(batch_size: 100) do |post|
|
220
|
+
# ...
|
221
|
+
end
|
222
|
+
```
|
223
|
+
|
224
|
+
* `find_in_batches`
|
225
|
+
|
226
|
+
```ruby
|
227
|
+
Post.where(...).find_in_batches(batch_size: 100) do |batch|
|
228
|
+
# ...
|
229
|
+
end
|
230
|
+
```
|
231
|
+
|
232
|
+
* `update_all`
|
233
|
+
|
234
|
+
```ruby
|
235
|
+
Post.where(...).update_all("message = 'test'")
|
236
|
+
Post.where(...).update_all(message: "test")
|
237
|
+
```
|
238
|
+
|
239
|
+
* `delete_all`
|
240
|
+
|
241
|
+
```ruby
|
242
|
+
Post.where(...).delete_all
|
243
|
+
```
|
244
|
+
|
245
|
+
Please note, that `delete_in_batches` will run `find_in_batches` iteratively
|
246
|
+
and then delete each batch. When dealing with large amounts of records to
|
247
|
+
delete you usually want to use `delete_in_batches` instead of `delete_all`, as
|
248
|
+
`delete_all` can time out.
|
249
|
+
|
250
|
+
* `delete_in_batches`
|
251
|
+
|
252
|
+
```ruby
|
253
|
+
Post.where(...).delete_in_batches
|
254
|
+
```
|
255
|
+
|
256
|
+
Again, please note, that `delete_in_batches` will run `find_in_batches` iteratively
|
257
|
+
and then delete each batch. When dealing with large amounts of records to
|
258
|
+
delete you usually want to use `delete_in_batches` instead of `delete_all`, as
|
259
|
+
`delete_all` can time out.
|
260
|
+
|
261
|
+
* `truncate_table`
|
262
|
+
|
263
|
+
```ruby
|
264
|
+
Post.truncate_table
|
265
|
+
```
|
266
|
+
|
267
|
+
Deletes all records from the table. This is much faster than `delete_all` or
|
268
|
+
`delete_in_batches`. However, it is not chainable, such that your only option
|
269
|
+
is to remove all records from the table.
|
270
|
+
|
271
|
+
## Semantic Versioning
|
272
|
+
|
273
|
+
CassandraStore is using Semantic Versioning: [SemVer](http://semver.org/)
|
274
|
+
|
275
|
+
## Contributing
|
276
|
+
|
277
|
+
1. Fork it
|
278
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
279
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
280
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
281
|
+
5. Create new Pull Request
|
282
|
+
|