extendi-cassandra_object 1.0.9 → 1.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a831b81ddc138be335b66caa6f64598f9c1cb851
4
- data.tar.gz: c6df634be44a3a7486ce52ffd0948fc1ed86c0a0
3
+ metadata.gz: ecd527d822a4895c5206c941bf16083025307c42
4
+ data.tar.gz: 918d17478765af790449a2545818856358854e35
5
5
  SHA512:
6
- metadata.gz: fecfdfa99fccb6b7856f06109b0ca122ad1a4e0ca7d3d402e78e654fc9083cb17a6d1bb5d648e14e002a39139575e29eaa1aecb436a27f092d509ff67a0ee0e1
7
- data.tar.gz: 69843e5f6e0d5c8235466c3a05bb308b8f914de88d58d44c7dacf24a6a60dedcce065fd931ee6b3926df364e367c1b26c801abec100615d4e3918ad85ec142f5
6
+ metadata.gz: d14760b8e19cfaf29a8b18822b37ba8e19140c3e679b35bd05425650f0c750180dd7428e544a98068d5c719241c5f0b61e2df5a89ab90ea29712f4062b92fe8a
7
+ data.tar.gz: 700146529ef52a4046b0d7f39bdfedc2b09c3889f878a724bad17af14e00f113feb15977709eed4ecafb33f6a8414643c8a609b690b6861f9fbc8e0fcb051374
data/.gitignore CHANGED
@@ -3,3 +3,4 @@ Gemfile.lock
3
3
  .idea
4
4
  .byebug_history
5
5
  .ruby-version
6
+ TAGS
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'extendi-cassandra_object'
5
- s.version = '1.0.9'
5
+ s.version = '1.0.10'
6
6
  s.description = 'Cassandra ActiveModel'
7
7
  s.summary = 'Cassandra ActiveModel'
8
8
  s.authors = ['Duccio Giovannelli', 'gotime']
@@ -41,7 +41,7 @@ module CassandraObject
41
41
  end
42
42
 
43
43
  def where_string_async(id)
44
- wheres = @scope.where_values.dup
44
+ wheres = @scope.where_values.dup.select.each_with_index { |_, i| i.even? }
45
45
  wheres << "#{@scope._key} = '#{id}'" if !id.nil?
46
46
  "WHERE #{wheres * ' AND '}" if wheres.any?
47
47
  end
@@ -139,7 +139,8 @@ module CassandraObject
139
139
  def select(scope)
140
140
  queries = QueryBuilder.new(self, scope).to_query_async
141
141
  # todo paginate
142
- cql_rows = execute_async(queries).map{|item| item.rows.map{|x| x}}.flatten!
142
+ arguments = scope.where_values.select.each_with_index{ |_, i| i.odd? }.reject{ |c| c.blank? }
143
+ cql_rows = execute_async(queries, arguments).map{|item| item.rows.map{|x| x}}.flatten!
143
144
  cql_rows.each do |cql_row|
144
145
  attributes = cql_row.to_hash
145
146
  key = attributes.delete(scope._key)
@@ -176,10 +177,23 @@ module CassandraObject
176
177
  execute_batchable(queries)
177
178
  end
178
179
 
179
- def delete(table, key, ids)
180
+ def delete(scope, ids, attributes = {})
180
181
  ids = [ids] if !ids.is_a?(Array)
181
- statement = "DELETE FROM #{table} WHERE #{key} IN (#{ids.map{|id| "'#{id}'"}.join(',')})"
182
- execute(statement, nil)
182
+ statement = "DELETE FROM #{scope.column_family} WHERE #{scope._key} IN (#{ids.map{|id| '?'}.join(',')})"
183
+ arguments = ids
184
+ unless attributes.blank?
185
+ statement += " AND #{attributes.keys.map{ |k| "#{k} = ?" }.join(' AND ')}"
186
+ arguments += attributes.values
187
+ end
188
+ execute(statement, arguments)
189
+ end
190
+
191
+ def delete_single(obj)
192
+ keys = obj.class._keys
193
+ wheres = keys.map{ |k| "#{k} = ?" }.join(' AND ')
194
+ arguments = keys.map{ |k| obj.attributes[k] }
195
+ statement = "DELETE FROM #{obj.class.column_family} WHERE #{wheres}"
196
+ execute(statement, arguments)
183
197
  end
184
198
 
185
199
  def execute_batch(statements)
@@ -223,7 +223,6 @@ module CassandraObject
223
223
  execute_batchable(queries)
224
224
  end
225
225
 
226
-
227
226
  def delete(table, ids)
228
227
  ids = [ids] if !ids.is_a?(Array)
229
228
  arguments = nil
@@ -27,6 +27,10 @@ module CassandraObject
27
27
 
28
28
  include Serialization
29
29
 
30
+ def self.timestamps
31
+ true
32
+ end
33
+
30
34
  end
31
35
  end
32
36
 
@@ -31,7 +31,11 @@ module CassandraObject
31
31
 
32
32
  def _key
33
33
  # todo only first key
34
- keys.tr('()','').split(',').first
34
+ _keys.first
35
+ end
36
+
37
+ def _keys
38
+ keys.tr('()','').gsub(/\s+/, "").split(',')
35
39
  end
36
40
 
37
41
  def keys=(value)
@@ -48,21 +48,25 @@ module CassandraObject
48
48
 
49
49
  def delete(ids, attributes = [])
50
50
  ids = [ids] if !ids.is_a?(Array)
51
- if !attributes.empty?
51
+
52
+ if self.schema_type == :standard
53
+ attrs = attributes.is_a?(Array) ? {} : attributes
54
+ adapter.delete self, ids, attrs
55
+ elsif attributes.blank?
56
+ adapter.delete column_family, ids
57
+ else
52
58
  attr = {}
53
59
  attributes.each{|a| attr[a] = nil}
54
60
  ids.each do |id|
55
61
  adapter.update column_family, id, encode_attributes(attr)
56
62
  end
57
- else
58
- if self.schema_type == :standard
59
- adapter.delete column_family, self._key, ids
60
- else
61
- adapter.delete column_family, ids
62
- end
63
63
  end
64
64
  end
65
65
 
66
+ def delete_schema(obj)
67
+ adapter.delete_single(obj)
68
+ end
69
+
66
70
  def insert_record(id, attributes)
67
71
  attributes = attributes.dup
68
72
  attributes[self._key] = id if self.schema_type == :standard
@@ -152,7 +156,12 @@ module CassandraObject
152
156
  end
153
157
 
154
158
  def destroy
155
- self.class.remove(id)
159
+ if self.class.schema_type == :standard
160
+ self.class.delete_schema self
161
+ else
162
+ self.class.remove(id)
163
+ end
164
+
156
165
  @destroyed = true
157
166
  end
158
167
 
@@ -47,7 +47,8 @@ module CassandraObject
47
47
 
48
48
  if self.schema_type == :standard
49
49
  klass.adapter.select(self) do |key, attributes|
50
- records[key] = attributes
50
+ records[key] ||= []
51
+ records[key] << attributes
51
52
  end
52
53
  else
53
54
  if @is_all && @id_values.empty?
@@ -73,7 +74,11 @@ module CassandraObject
73
74
  if self.raw_response || self.schema_type == :dynamic_attributes
74
75
  results << {key => attributes.values.compact.empty? ? attributes.keys : attributes}
75
76
  else
76
- results << klass.instantiate(key, attributes)
77
+ if attributes.is_a?(Array)
78
+ attributes.each{ |attrs| results << klass.instantiate(key, attrs) }
79
+ else
80
+ results << klass.instantiate(key, attributes)
81
+ end
77
82
  end
78
83
  end
79
84
  results = results.reduce({}, :merge!) if self.schema_type == :dynamic_attributes
@@ -7,12 +7,16 @@ module CassandraObject
7
7
  attribute :updated_at, type: :time
8
8
 
9
9
  before_create do
10
- self.created_at ||= Time.current
11
- self.updated_at ||= Time.current
10
+ if self.class.timestamps
11
+ self.created_at ||= Time.current
12
+ self.updated_at ||= Time.current
13
+ end
12
14
  end
13
15
 
14
16
  before_update if: :changed? do
15
- self.updated_at = Time.current
17
+ if self.class.timestamps
18
+ self.updated_at = Time.current
19
+ end
16
20
  end
17
21
  end
18
22
  end
@@ -50,7 +50,6 @@ module CassandraObject
50
50
  end
51
51
  else
52
52
 
53
-
54
53
  case object
55
54
  when ::String then
56
55
  Cassandra::Types.varchar
@@ -22,7 +22,7 @@ CassandraObject::Base.config = {
22
22
 
23
23
  begin
24
24
  CassandraObject::Schema.drop_keyspace 'cassandra_object_test', true
25
- CassandraObject::Schema.drop_keyspace 'blah', true
25
+ CassandraObject::Schema.drop_keyspace 'Blah', true
26
26
  rescue Exception => e
27
27
  puts e.message
28
28
  end
@@ -31,6 +31,7 @@ sleep 1
31
31
  CassandraObject::Schema.create_keyspace 'cassandra_object_test'
32
32
  CassandraObject::Schemaless.create_column_family 'Issues'
33
33
  CassandraObject::Schema.create_column_family 'IssueSchemas', {attributes: 'id text, title text, description text, field float, intero int, created_at timestamp, updated_at timestamp, PRIMARY KEY (id)', options: {}}
34
+ CassandraObject::Schema.create_column_family 'IssueSchemaCks', {attributes: 'id text, type text, date timestamp, value float, PRIMARY KEY (id, type, date)', options: {}}
34
35
  CassandraObject::Schemaless.create_column_family 'IssueDynamics'
35
36
  CassandraObject::Schemaless.create_column_family 'IssuesCustomConfig'
36
37
  CassandraObject::Schema.create_column_family 'IssueSchemaFathers', {attributes: 'id text, title text, field float, created_at timestamp, updated_at timestamp, PRIMARY KEY (id)', options: {}}
@@ -51,6 +52,7 @@ CassandraObject::Base.class_eval do
51
52
  # created_records.reject(&:destroyed?).each(&:destroy)
52
53
  Issue.delete_all
53
54
  IssueSchema.delete_all
55
+ IssueSchemaCk.delete_all
54
56
  IssueDynamic.delete_all
55
57
  created_records.clear
56
58
  end
@@ -0,0 +1,18 @@
1
+ class IssueSchemaCk < CassandraObject::BaseSchema
2
+ string :id
3
+ string :type
4
+ time :date
5
+ float :value
6
+
7
+ self.allow_filtering = true
8
+
9
+ self.keys = '(id, type, date)'
10
+
11
+ def self.timestamps
12
+ false
13
+ end
14
+
15
+ def self.for_key key
16
+ where_ids(key)
17
+ end
18
+ end
data/test/test_helper.rb CHANGED
@@ -13,6 +13,7 @@ require 'support/issue_custom_config'
13
13
  require 'support/issue_schema'
14
14
  require 'support/issue_schema_child'
15
15
  require 'support/issue_schema_father'
16
+ require 'support/issue_schema_ck'
16
17
 
17
18
  module CassandraObject
18
19
  class TestCase < ActiveSupport::TestCase
@@ -0,0 +1,69 @@
1
+ #!/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'test_helper'
5
+
6
+ class CassandraObject::PersistenceSchemaCkTest < CassandraObject::TestCase
7
+
8
+ test 'composite key' do
9
+ time1 = Time.now
10
+ time2 = time1 + 1.second
11
+
12
+ IssueSchemaCk.create(id: '1', type: 'first', date: time1, value: 1.to_f)
13
+ IssueSchemaCk.create(id: '1', type: 'second', date: time1, value: 1.to_f)
14
+ IssueSchemaCk.create(id: '1', type: 'first', date: time2, value: 2.to_f)
15
+ IssueSchemaCk.create(id: '1', type: 'second', date: time2, value: 2.to_f)
16
+
17
+ res = IssueSchemaCk.where('type = ?', 'first').find_by_id([1])
18
+ assert_equal 2, res.size
19
+ assert_equal 1, res.first.value
20
+
21
+ item = res[0]
22
+ assert_equal '1', item.id
23
+ assert_equal time1.to_i, item.date.to_i
24
+
25
+ item = res[1]
26
+ assert_equal '1', item.id
27
+ assert_equal time2.to_i, item.date.to_i
28
+
29
+ end
30
+
31
+ test 'delete' do
32
+ IssueSchemaCk.create(id: '1', type: 'first', date: Time.now, value: 1.to_f)
33
+ IssueSchemaCk.create(id: '1', type: 'second', date: Time.now, value: 1.to_f)
34
+
35
+ IssueSchemaCk.delete('1')
36
+ assert_equal 0, IssueSchemaCk.find_by_id([1]).size
37
+ end
38
+
39
+ test 'delete with attributes' do
40
+ time = Time.now
41
+ IssueSchemaCk.create(id: '1', type: 'first', date: time, value: 1.to_f)
42
+ IssueSchemaCk.create(id: '1', type: 'first', date: Time.now, value: 1.to_f)
43
+ IssueSchemaCk.create(id: '2', type: 'first', date: time, value: 1.to_f)
44
+ IssueSchemaCk.create(id: '2', type: 'first', date: Time.now, value: 1.to_f)
45
+
46
+ IssueSchemaCk.delete('1', type: 'first')
47
+ assert_equal 2, IssueSchemaCk.find_by_id([1,2]).size
48
+ end
49
+
50
+ test 'delete multiple' do
51
+ IssueSchemaCk.create(id: '1', type: 'first', date: Time.now, value: 1.to_f)
52
+ IssueSchemaCk.create(id: '1', type: 'second', date: Time.now, value: 1.to_f)
53
+ IssueSchemaCk.create(id: '2', type: 'first', date: Time.now, value: 1.to_f)
54
+ IssueSchemaCk.create(id: '2', type: 'first', date: Time.now, value: 1.to_f)
55
+
56
+ IssueSchemaCk.delete(['1','2'])
57
+ assert_equal 0, IssueSchemaCk.find_by_id([1]).size
58
+ assert_equal 0, IssueSchemaCk.find_by_id([2]).size
59
+ end
60
+
61
+ test 'destroy' do
62
+ IssueSchemaCk.create(id: '1', type: 'first', date: Time.now, value: 1.to_f)
63
+ IssueSchemaCk.create(id: '1', type: 'second', date: Time.now, value: 1.to_f)
64
+
65
+ IssueSchemaCk.find_by_id(['1']).first.destroy
66
+ assert_equal 1, IssueSchemaCk.find_by_id([1]).size
67
+ end
68
+
69
+ end
@@ -95,27 +95,27 @@ class CassandraObject::PersistenceSchemaTest < CassandraObject::TestCase
95
95
  assert_equal persisted_issue, reloaded_issue
96
96
  end
97
97
 
98
- test 'remove' do
99
- issue = IssueSchema.create(title: 'I rule', description: 'lololol')
100
- id = issue.id
101
- assert_equal id, IssueSchema.find(id).id
102
- IssueSchema.remove(id)
103
- assert_raise CassandraObject::RecordNotFound do
104
- IssueSchema.find(id)
105
- end
106
- end
107
-
108
- test 'remove multiple' do
109
- ids = []
110
- (1..10).each do
111
- issue = IssueSchema.create(title: 'I rule', description: 'lololol')
112
- ids << issue.id
113
- end
114
-
115
- IssueSchema.remove(ids)
116
-
117
- assert_equal [], IssueSchema.find(ids)
118
- end
98
+ # test 'remove' do
99
+ # issue = IssueSchema.create(title: 'I rule', description: 'lololol')
100
+ # id = issue.id
101
+ # assert_equal id, IssueSchema.find(id).id
102
+ # IssueSchema.remove(id)
103
+ # assert_raise CassandraObject::RecordNotFound do
104
+ # IssueSchema.find(id)
105
+ # end
106
+ # end
107
+
108
+ # test 'remove multiple' do
109
+ # ids = []
110
+ # (1..10).each do
111
+ # issue = IssueSchema.create(title: 'I rule', description: 'lololol')
112
+ # ids << issue.id
113
+ # end
114
+
115
+ # IssueSchema.remove(ids)
116
+
117
+ # assert_equal [], IssueSchema.find(ids)
118
+ # end
119
119
 
120
120
  test 'ttl' do
121
121
  description_test = 'this is the one with ttl'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: extendi-cassandra_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.9
4
+ version: 1.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Duccio Giovannelli
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-05-14 00:00:00.000000000 Z
12
+ date: 2018-08-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -139,6 +139,7 @@ files:
139
139
  - test/support/issue_dynamic.rb
140
140
  - test/support/issue_schema.rb
141
141
  - test/support/issue_schema_child.rb
142
+ - test/support/issue_schema_ck.rb
142
143
  - test/support/issue_schema_father.rb
143
144
  - test/support/reconnection.rb
144
145
  - test/test_helper.rb
@@ -159,6 +160,7 @@ files:
159
160
  - test/unit/identity_test.rb
160
161
  - test/unit/inspect_test.rb
161
162
  - test/unit/log_subscriber_test.rb
163
+ - test/unit/persistence_schema_ck_test.rb
162
164
  - test/unit/persistence_schema_test.rb
163
165
  - test/unit/persistence_test.rb
164
166
  - test/unit/railties/controller_runtime_test.rb