module Aliyun::Log::Record::Persistence::ClassMethods

Public Instance Methods

auto_load_schema() click to toggle source
# File lib/aliyun/log/record/persistence.rb, line 50
def auto_load_schema
  @lock.synchronize do
    return if _schema_load
    create_logstore
    sync_index
    self._schema_load = true
  end
end
create(data, force = false) click to toggle source
# File lib/aliyun/log/record/persistence.rb, line 66
def create(data, force = false)
  auto_load_schema
  if data.is_a?(Array)
    # TODO batch insert
    data.each do |log|
      saved = new(log).save(force)
      return false unless saved
    end
  else
    new(data).save(force)
  end
end
create!(data) click to toggle source
# File lib/aliyun/log/record/persistence.rb, line 79
def create!(data)
  create(data, true)
end
create_logstore(options = {}) click to toggle source
# File lib/aliyun/log/record/persistence.rb, line 39
def create_logstore(options = {})
  Log.record_connection.get_logstore(project_name, logstore_name)
rescue ServerError => e
  Log.record_connection.create_logstore(project_name, logstore_name, options)
end
has_index?() click to toggle source
# File lib/aliyun/log/record/persistence.rb, line 59
def has_index?
  Log.record_connection.get_index(project_name, logstore_name)
  true
rescue ServerError
  false
end
logstore_name() click to toggle source
# File lib/aliyun/log/record/persistence.rb, line 18
def logstore_name
  @logstore_name ||= options[:name] ||
                     base_class.name.split('::').last.underscore.pluralize
end
logstore_name=(value) click to toggle source
# File lib/aliyun/log/record/persistence.rb, line 23
def logstore_name=(value)
  if defined?(@logstore_name)
    return if value == @logstore_name
  end

  @logstore_name = value
end
project_name() click to toggle source
# File lib/aliyun/log/record/persistence.rb, line 31
def project_name
  unless @project_name
    @project_name = options[:project] || Config.project
    raise ProjectNameError, "project can't be empty" if @project_name.blank?
  end
  @project_name
end
sync_index() click to toggle source
# File lib/aliyun/log/record/persistence.rb, line 45
def sync_index
  return if field_indices.blank?
  has_index? ? update_index : create_index
end

Private Instance Methods

create_index() click to toggle source
# File lib/aliyun/log/record/persistence.rb, line 104
def create_index
  Log.record_connection.create_index(
    project_name,
    logstore_name,
    field_indices
  )
end
evaluate_default_value(val) click to toggle source
# File lib/aliyun/log/record/persistence.rb, line 85
def evaluate_default_value(val)
  if val.respond_to?(:call)
    val.call
  elsif val.duplicable?
    val.dup
  else
    val
  end
end
field_index_types() click to toggle source
# File lib/aliyun/log/record/persistence.rb, line 130
def field_index_types
  field_indices.tap do |tap|
    tap.each do |_, v|
      v[:alias] ||= ''
      v[:caseSensitive] ||= false
      v[:chn] ||= false
      v[:doc_value] = options[:field_doc_value] != false if v[:doc_value].nil?
    end
  end
end
field_indices() click to toggle source
# File lib/aliyun/log/record/persistence.rb, line 95
def field_indices
  indices = if options[:field_index] == false
              attributes.select { |_, value| value[:index] == true }
            else
              attributes.reject { |_, value| value[:index] == false }
            end
  indices.reject { |key, _| RESERVED_FIELDS.include?(key) }
end
update_index() click to toggle source
# File lib/aliyun/log/record/persistence.rb, line 112
def update_index
  conf_res = Log.record_connection.get_index(project_name, logstore_name)
  raw_conf = JSON.parse(conf_res)
  index_conf = raw_conf.deep_dup
  field_index_types.each do |k, v|
    index_conf['keys'] ||= {}
    index_conf['keys'][k.to_s] ||= {}
    index_conf['keys'][k.to_s].merge!(v.as_json)
  end
  return if index_conf['keys'] == raw_conf['keys']

  Log.record_connection.update_index(
    project_name,
    logstore_name,
    index_conf['keys'].with_indifferent_access
  )
end