Sha256: 8ee28e0f95ed55e50432cd3c98030146ec9236fa5d3c34154d68cf84eee8eda1

Contents?: true

Size: 1.56 KB

Versions: 20

Compression:

Stored size: 1.56 KB

Contents

module Partitioned
  #
  # Partitioned abstract class for all partitioned models based as a single integer field value.
  #
  class ByIntegerField < PartitionedBase
    self.abstract_class = true

    # the size of each table
    # @return [Integer] how many different values are in each partition
    def self.partition_table_size
      return 1
    end

    # the name of the partition key field
    # @return [String] the name of the field
    def self.partition_integer_field
      raise MethodNotImplemented.new(self, :partition_integer_field)
    end

    # the normalized key value for a given key value
    # @return [Integer] the normalized value
    def self.partition_normalize_key_value(integer_field_value)
      return integer_field_value / partition_table_size * partition_table_size
    end

    def self.partition_generate_range(start_value, end_value, step = :default)
      step = partition_table_size if step == :default
      return Range.new(start_value, end_value).step(step)
    end

    partitioned do |partition|
      partition.on lambda {|model| return model.partition_integer_field }

      partition.order "substring(tablename, 2)::integer desc"

      partition.check_constraint lambda { |model, id|
        value = model.partition_normalize_key_value(id)
        if model.partition_table_size == 1
          return "( #{model.partition_integer_field} = #{value} )"
        else
          return "( #{model.partition_integer_field} >= #{value} and #{model.partition_integer_field} < #{value + model.partition_table_size} )"
        end
      }
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
partitioned-2.1.0 lib/partitioned/by_integer_field.rb
partitioned-2.0.0 lib/partitioned/by_integer_field.rb
partitioned-1.3.5 lib/partitioned/by_integer_field.rb
partitioned-1.3.4 lib/partitioned/by_integer_field.rb
partitioned-1.3.3 lib/partitioned/by_integer_field.rb
partitioned-1.3.2 lib/partitioned/by_integer_field.rb
partitioned-1.3.1 lib/partitioned/by_integer_field.rb
partitioned-1.3.0 lib/partitioned/by_integer_field.rb
partitioned-1.2.1 lib/partitioned/by_integer_field.rb
partitioned-1.2.0 lib/partitioned/by_integer_field.rb
partitioned-1.1.11 lib/partitioned/by_integer_field.rb
partitioned-1.1.10 lib/partitioned/by_integer_field.rb
partitioned-1.1.9 lib/partitioned/by_integer_field.rb
partitioned-1.1.8 lib/partitioned/by_integer_field.rb
partitioned-1.1.7 lib/partitioned/by_integer_field.rb
partitioned-1.1.6 lib/partitioned/by_integer_field.rb
partitioned-1.1.5 lib/partitioned/by_integer_field.rb
partitioned-1.1.3 lib/partitioned/by_integer_field.rb
partitioned-1.1.1 lib/partitioned/by_integer_field.rb
partitioned-1.1.0 lib/partitioned/by_integer_field.rb