Class: Partitioned::PartitionedBase
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Partitioned::PartitionedBase
- Extended by:
- BulkMethodsMixin, SingleForwardable
- Includes:
- ActiveRecordOverrides
- Defined in:
- lib/partitioned/partitioned_base.rb,
lib/partitioned/partitioned_base/sql_adapter.rb,
lib/partitioned/partitioned_base/configurator.rb,
lib/partitioned/partitioned_base/configurator/dsl.rb,
lib/partitioned/partitioned_base/configurator/data.rb,
lib/partitioned/partitioned_base/partition_manager.rb,
lib/partitioned/partitioned_base/configurator/reader.rb
Overview
PartitionedBase an ActiveRecord::Base class that can be partitioned.
Uses a domain specific language to configure, see Partitioned::PartitionedBase::Configurator for more information.
Extends Partitioned::BulkMethodsMixin to provide create_many and update_many.
Uses PartitionManager to manage creation of child tables.
Monkey patches some ActiveRecord routines to call back to this class when INSERT and UPDATE statements are built (to determine the table_name with respect to values being inserted or updated)
Direct Known Subclasses
Defined Under Namespace
Modules: Configurator Classes: PartitionManager, SqlAdapter
Class Method Summary (collapse)
-
+ ({Configurator::Reader}) configurator
Return a object used to read configurator information.
-
+ ({Configurator::Dsl}) configurator_dsl
Returns the configurator DSL object.
-
+ (Arel::Table) dynamic_arel_table(values, as = nil)
In activerecord 3.0 we need to supply an Arel::Table for the key value(s) used to determine the specific child table to access.
-
+ (Hash) from_partition(*partition_field)
Real scope (uses #from_partition_scope).
-
+ (Hash) from_partitioned_without_alias(*partition_field)
Real scope (uses #from_partitioned_without_alias_scope).
-
+ (Enumerable) partition_generate_range(start_value, end_value, step = 1)
Range generation provided for methods like created_infrastructure that need a set of partition key values to operate on.
-
+ (Object+) partition_key_values(values)
The specific values for a partition of this active record's type which are defined by #self#self.partition_keys.
-
+ (String+) partition_keys
Returns an array of attribute names (strings) used to fetch the key value(s) the determine this specific partition table.
-
+ ({PartitionManager}) partition_manager
Return an instance of this partition table's table manager.
-
+ (Object) partition_normalize_key_value(value)
Normalize the value to be used for partitioning.
-
+ ({Configurator::Dsl}) partitioned {|@configurator_dsl| ... }
Yields an object used to configure the ActiveRecord class for partitioning using the Configurator Domain Specific Language.
-
+ ({SqlAdapter}) sql_adapter
Return an instance of this partition table's sql_adapter (used by the partition manage to create SQL statements).
Instance Method Summary (collapse)
-
- (Arel::Table) dynamic_arel_table(as = nil)
Used by our active record hacks to supply an Arel::Table given this active record's current attributes.
-
- (Hash) from_partition_scope
:from_partition_scope is generally not used directly, use helper self.from_partition so that the derived class can be passed into :from_partition_scope.
-
- (Hash) from_partitioned_without_alias_scope
:from_partitioned_without_alias_scope is generally not used directly, use helper self.from_partitioned_without_alias so that the derived class can be passed into :from_partitioned_without_alias_scope.
-
- (String) partition_table_name
The name of the current partition table determined by this active records attributes that define the key value(s) for the constraint check.
Methods included from BulkMethodsMixin
Methods included from ActiveRecordOverrides
#arel_attributes_values, #delete
Class Method Details
+ ({Configurator::Reader}) configurator
Return a object used to read configurator information.
214 215 216 217 218 219 |
# File 'lib/partitioned/partitioned_base.rb', line 214 def self.configurator unless @configurator @configurator = self::Configurator::Reader.new(self) end return @configurator end |
+ ({Configurator::Dsl}) configurator_dsl
Returns the configurator DSL object.
242 243 244 |
# File 'lib/partitioned/partitioned_base.rb', line 242 def self.configurator_dsl return @configurator_dsl end |
+ (Arel::Table) dynamic_arel_table(values, as = nil)
In activerecord 3.0 we need to supply an Arel::Table for the key value(s) used to determine the specific child table to access.
115 116 117 118 119 120 121 122 123 |
# File 'lib/partitioned/partitioned_base.rb', line 115 def self.dynamic_arel_table(values, as = nil) @arel_tables ||= {} key_values = self.partition_key_values(values) new_arel_table = @arel_tables[key_values] arel_engine_hash = {:engine => self.arel_engine} arel_engine_hash[:as] = as unless as.blank? new_arel_table = Arel::Table.new(self.partition_name(*key_values), arel_engine_hash) return new_arel_table end |
+ (Hash) from_partition(*partition_field)
Real scope (uses #from_partition_scope). This scope is used to target the active record find() to a specific child table and alias it to the name of the parent table (so activerecord can generally work with it)
Use as:
Foo.from_partition(KEY).find(:first)
where KEY is the key value(s) used as the check constraint on Foo's table.
Because the scope is specific to a class (a class method) but unlike class methods is not inherited, one must use this form (#from_partition) instead of #from_partition_scope to get the most derived classes specific active record scope.
165 166 167 |
# File 'lib/partitioned/partitioned_base.rb', line 165 def self.from_partition(*partition_field) from_partition_scope(self, *partition_field) end |
+ (Hash) from_partitioned_without_alias(*partition_field)
Real scope (uses #from_partitioned_without_alias_scope). This scope is used to target the active record find() to a specific child table. Is probably best used in advanced activerecord queries when a number of tables are involved in the query.
Use as:
Foo.from_partitioned_without_alias(KEY).find(:all, :select => "*")
where KEY is the key value(s) used as the check constraint on Foo's table.
it's not obvious why :select => "*" is supplied. note activerecord wants to use the name of parent table for access to any attributes, so without the :select argument the sql result would be something like:
SELECT foos.* FROM foos_partitions.pXXX
which fails because table foos is not referenced. using the form #from_partition is almost always the correct thing when using activerecord.
Because the scope is specific to a class (a class method) but unlike class methods is not inherited, one must use this form (#from_partitioned_without_alias) instead of #from_partitioned_without_alias_scope to get the most derived classes specific active record scope.
206 207 208 |
# File 'lib/partitioned/partitioned_base.rb', line 206 def self.from_partitioned_without_alias(*partition_field) from_partitioned_without_alias_scope(self, *partition_field) end |
+ (Enumerable) partition_generate_range(start_value, end_value, step = 1)
Range generation provided for methods like created_infrastructure that need a set of partition key values to operate on.
85 86 87 |
# File 'lib/partitioned/partitioned_base.rb', line 85 def self.partition_generate_range(start_value, end_value, step = 1) return Range.new(start_value, end_value).step(step) end |
+ (Object+) partition_key_values(values)
The specific values for a partition of this active record's type which are defined by Partitioned::PartitionedBase#self#self.partition_keys
51 52 53 54 |
# File 'lib/partitioned/partitioned_base.rb', line 51 def self.partition_key_values(values) symbolized_values = values.symbolize_keys return self.partition_keys.map{|key| symbolized_values[key.to_sym]} end |
+ (String+) partition_keys
Returns an array of attribute names (strings) used to fetch the key value(s) the determine this specific partition table.
40 41 42 |
# File 'lib/partitioned/partitioned_base.rb', line 40 def self.partition_keys return configurator.on_fields end |
+ ({PartitionManager}) partition_manager
Return an instance of this partition table's table manager.
93 94 95 96 |
# File 'lib/partitioned/partitioned_base.rb', line 93 def self.partition_manager @partition_manager = self::PartitionManager.new(self) unless @partition_manager.present? return @partition_manager end |
+ (Object) partition_normalize_key_value(value)
Normalize the value to be used for partitioning. This allows, for instance, a class that partitions on a time field to group the times by month. An integer field might be grouped by every 10mil values, A string field might be grouped by its first character.
73 74 75 |
# File 'lib/partitioned/partitioned_base.rb', line 73 def self.partition_normalize_key_value(value) return value end |
+ ({Configurator::Dsl}) partitioned {|@configurator_dsl| ... }
Yields an object used to configure the ActiveRecord class for partitioning using the Configurator Domain Specific Language.
usage:
partitioned do |partition|
partition.on :company_id
partition.index :id, :unique => true
partition.foreign_key :company_id
end
233 234 235 236 |
# File 'lib/partitioned/partitioned_base.rb', line 233 def self.partitioned @configurator_dsl ||= self::Configurator::Dsl.new(self) yield @configurator_dsl end |
+ ({SqlAdapter}) sql_adapter
Return an instance of this partition table's sql_adapter (used by the partition manage to create SQL statements)
103 104 105 106 |
# File 'lib/partitioned/partitioned_base.rb', line 103 def self.sql_adapter @sql_adapter = self::SqlAdapter.new(self) unless @sql_adapter.present? return @sql_adapter end |
Instance Method Details
- (Arel::Table) dynamic_arel_table(as = nil)
Used by our active record hacks to supply an Arel::Table given this active record's current attributes.
131 132 133 134 135 |
# File 'lib/partitioned/partitioned_base.rb', line 131 def dynamic_arel_table(as = nil) symbolized_attributes = attributes.symbolize_keys key_values = Hash[*self.class.partition_keys.map{|name| [name,symbolized_attributes[name]]}.flatten] return self.class.dynamic_arel_table(key_values, as) end |
- (Hash) from_partition_scope
:from_partition_scope is generally not used directly, use helper self.from_partition so that the derived class can be passed into :from_partition_scope
142 143 144 145 146 |
# File 'lib/partitioned/partitioned_base.rb', line 142 scope :from_partition_scope, lambda { |target_class, *partition_field| { :from => "#{target_class.partition_name(*partition_field)} AS #{target_class.table_name}" } } |
- (Hash) from_partitioned_without_alias_scope
:from_partitioned_without_alias_scope is generally not used directly, use helper self.from_partitioned_without_alias so that the derived class can be passed into :from_partitioned_without_alias_scope
174 175 176 177 178 |
# File 'lib/partitioned/partitioned_base.rb', line 174 scope :from_partitioned_without_alias_scope, lambda { |target_class, *partition_field| { :from => target_class.partition_name(*partition_field) } } |
- (String) partition_table_name
The name of the current partition table determined by this active records attributes that define the key value(s) for the constraint check.
61 62 63 64 |
# File 'lib/partitioned/partitioned_base.rb', line 61 def partition_table_name symbolized_attributes = attributes.symbolize_keys return self.class.partition_name(*self.class.partition_keys.map{|attribute_name| symbolized_attributes[attribute_name]}) end |