sessionm-cassandra_object 2.2.55 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,7 @@ module CassandraObject
4
4
  extend ActiveSupport::Autoload
5
5
 
6
6
  autoload :Base
7
+ autoload :Configuration
7
8
  autoload :AsyncConnection
8
9
  autoload :Connection
9
10
  autoload :Attributes
@@ -42,11 +42,18 @@ module CassandraObject
42
42
  module ClassMethods
43
43
  DEFAULT_OPTIONS = {
44
44
  servers: "127.0.0.1:9160",
45
- thrift: {}
46
45
  }
47
- def establish_connection(spec)
48
- spec.reverse_merge!(DEFAULT_OPTIONS)
49
- spec[:thrift].symbolize_keys!
46
+ DEFAULT_THRIFT_OPTIONS = {
47
+ exception_class_overrides: [],
48
+ }
49
+
50
+ # This doesn't open a connection. It merely conifgures the connection object.
51
+ def establish_connection(config)
52
+ spec = config.reverse_merge(DEFAULT_OPTIONS)
53
+
54
+ spec[:thrift] = (spec[:thrift] || {}).reverse_merge(DEFAULT_THRIFT_OPTIONS)
55
+ spec[:thrift][:exception_class_overrides] = spec[:thrift][:exception_class_overrides].map(&:constantize)
56
+
50
57
  self.connection_spec = spec
51
58
  end
52
59
  end
@@ -29,7 +29,8 @@ module CassandraObject
29
29
  extend ActiveModel::Naming
30
30
  include ActiveModel::Conversion
31
31
  extend ActiveSupport::DescendantsTracker
32
-
32
+
33
+ include Configuration
33
34
  include Fiber.respond_to?(:current) ? AsyncConnection : Connection
34
35
  include Consistency
35
36
  include RowTTL
@@ -0,0 +1,29 @@
1
+ module CassandraObject
2
+ module Configuration
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+
7
+ def recursive_symbolize_keys!(hash)
8
+ hash.symbolize_keys!
9
+ hash.values.select{|v| v.is_a? Hash}.each{|h| recursive_symbolize_keys!(h)}
10
+ end
11
+
12
+ @@config = nil
13
+ def config=(config)
14
+ raise('attempt to set config multiple times') if @@config
15
+
16
+ recursive_symbolize_keys!(config)
17
+
18
+ (@@config = config).tap do
19
+ set_default_consistencies(@@config)
20
+ establish_connection(@@config)
21
+ end
22
+ end
23
+ def config
24
+ @@config
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -9,11 +9,19 @@ module CassandraObject
9
9
  module ClassMethods
10
10
  DEFAULT_OPTIONS = {
11
11
  servers: "127.0.0.1:9160",
12
- thrift: {}
13
12
  }
14
- def establish_connection(spec)
15
- spec.reverse_merge!(DEFAULT_OPTIONS)
16
- self.connection = Cassandra.new(spec[:keyspace], spec[:servers], spec[:thrift].symbolize_keys!)
13
+ DEFAULT_THRIFT_OPTIONS = {
14
+ exception_class_overrides: [],
15
+ }
16
+
17
+ # This doesn't open a connection. It merely conifgures the connection object.
18
+ def establish_connection(config)
19
+ spec = config.reverse_merge(DEFAULT_OPTIONS)
20
+
21
+ spec[:thrift] = (spec[:thrift] || {}).reverse_merge(DEFAULT_THRIFT_OPTIONS)
22
+ spec[:thrift][:exception_class_overrides] = spec[:thrift][:exception_class_overrides].map(&:constantize)
23
+
24
+ self.connection = Cassandra.new(spec[:keyspace], spec[:servers], spec[:thrift])
17
25
  end
18
26
  end
19
27
  end
@@ -3,29 +3,40 @@ module CassandraObject
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
- cattr_accessor :consistency_levels
7
- self.consistency_levels = [:one, :quorum, :all]
8
-
9
6
  class_attribute :write_consistency
10
7
  class_attribute :read_consistency
11
- self.write_consistency = :quorum
12
- self.read_consistency = :quorum
13
8
  end
14
9
 
15
10
  module ClassMethods
16
11
  THRIFT_LEVELS = {
17
12
  :one => Cassandra::Consistency::ONE,
18
13
  :quorum => Cassandra::Consistency::QUORUM,
14
+ :local_quorum => Cassandra::Consistency::LOCAL_QUORUM,
19
15
  :all => Cassandra::Consistency::ALL
20
16
  }
17
+
18
+ DEFAULT_OPTIONS = {
19
+ :read_default => :quorum,
20
+ :write_default => :quorum,
21
+ }
22
+
23
+ @@default_read_consistency
24
+ @@default_write_consistency
25
+ def set_default_consistencies(config)
26
+ config = (config[:consistency] || {}).reverse_merge(DEFAULT_OPTIONS)
27
+ @@default_read_consistency = config[:read_default].to_sym
28
+ @@default_write_consistency = config[:write_default].to_sym
29
+ end
21
30
 
22
31
  def thrift_read_consistency
23
- THRIFT_LEVELS[read_consistency] || (raise "Invalid consistency level #{read_consistency}")
32
+ consistency = read_consistency || @@default_read_consistency
33
+ THRIFT_LEVELS[consistency] || (raise "Invalid consistency level #{consistency}")
24
34
  end
25
35
 
26
36
  def thrift_write_consistency
27
- THRIFT_LEVELS[write_consistency] || (raise "Invalid consistency level #{write_consistency}")
37
+ consistency = write_consistency || @@default_write_consistency
38
+ THRIFT_LEVELS[consistency] || (raise "Invalid consistency level #{consistency}")
28
39
  end
29
40
  end
30
41
  end
31
- end
42
+ end
@@ -23,7 +23,7 @@ module CassandraObject
23
23
  end
24
24
 
25
25
  def attribute_will_change!(attribute)
26
- logger.warn "#{self.class}##{attribute} added/removed/changed and attribute_will_change! not implemented."
26
+ Rails.logger.warn "#{self.class}##{attribute} added/removed/changed and attribute_will_change! not implemented."
27
27
  end
28
28
  end
29
29
 
@@ -45,7 +45,7 @@ module CassandraObject
45
45
  # Scroll down to the CfDef definition.
46
46
  def self.create_column_family(name, &block)
47
47
  say_with_time("create_column_family #{name}") do
48
- column_family_tasks.create(name, &block)
48
+ column_family_tasks.create(name, CassandraObject::Base.config[:column_family_defaults], &block)
49
49
  end
50
50
  end
51
51
 
@@ -21,12 +21,18 @@ module CassandraObject
21
21
  connection.schema.cf_defs.find { |cf_def| cf_def.name == name.to_s }
22
22
  end
23
23
 
24
- def create(name, &block)
24
+ def create(name, options={}, &block)
25
+ options = {
26
+ :comparator_type => 'BytesType',
27
+ :column_type => 'Standard',
28
+ }.merge(options)
29
+
25
30
  cf = Cassandra::ColumnFamily.new
26
31
  cf.name = name.to_s
27
32
  cf.keyspace = @keyspace.to_s
28
- cf.comparator_type = 'BytesType'
29
- cf.column_type = 'Standard'
33
+ options.each do |option, value|
34
+ cf.send("#{option}=", value)
35
+ end
30
36
 
31
37
  block.call cf if block
32
38
 
@@ -18,9 +18,16 @@ module CassandraObject
18
18
  end
19
19
 
20
20
  def create(name, options = {})
21
+ options = options.symbolize_keys
22
+
23
+ replication_opts = {
24
+ :strategy => 'org.apache.cassandra.locator.SimpleStrategy',
25
+ :factor => 1,
26
+ }.merge(options[:replication].symbolize_keys || {})
27
+
21
28
  opts = { :name => name.to_s,
22
- :strategy_class => 'org.apache.cassandra.locator.SimpleStrategy',
23
- :replication_factor => 1,
29
+ :strategy_class => replication_opts[:strategy],
30
+ :replication_factor => replication_opts[:factor],
24
31
  :cf_defs => [] }.merge(options)
25
32
 
26
33
  ks = Cassandra::Keyspace.new.with_fields(opts)
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'sessionm-cassandra_object'
5
- s.version = '2.2.55'
5
+ s.version = '2.3.0'
6
6
  s.description = 'Cassandra ActiveModel'
7
7
  s.summary = 'Cassandra ActiveModel'
8
8
 
@@ -4,10 +4,6 @@ class CassandraObject::ConsistencyTest < CassandraObject::TestCase
4
4
  class TestModel < CassandraObject::Base
5
5
  end
6
6
 
7
- test 'consistency_levels' do
8
- assert_equal [:one, :quorum, :all].to_set, TestModel.consistency_levels.to_set
9
- end
10
-
11
7
  test 'thrift_write_consistency' do
12
8
  TestModel.write_consistency = :all
13
9
  assert_equal Cassandra::Consistency::ALL, TestModel.thrift_write_consistency
@@ -17,4 +13,4 @@ class CassandraObject::ConsistencyTest < CassandraObject::TestCase
17
13
  TestModel.read_consistency = :all
18
14
  assert_equal Cassandra::Consistency::ALL, TestModel.thrift_read_consistency
19
15
  end
20
- end
16
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sessionm-cassandra_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.55
4
+ version: 2.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-08-02 00:00:00.000000000 Z
14
+ date: 2012-11-01 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rails
@@ -85,6 +85,7 @@ files:
85
85
  - lib/cassandra_object/batches.rb
86
86
  - lib/cassandra_object/callbacks.rb
87
87
  - lib/cassandra_object/collection.rb
88
+ - lib/cassandra_object/configuration.rb
88
89
  - lib/cassandra_object/connection.rb
89
90
  - lib/cassandra_object/consistency.rb
90
91
  - lib/cassandra_object/cursor.rb