Module: Aliyun::Log::Record::Persistence::ClassMethods

Defined in:
lib/aliyun/log/record/persistence.rb

Instance Method Summary collapse

Instance Method Details

#auto_load_schemaObject



50
51
52
53
54
55
56
57
# 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) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Object



79
80
81
# File 'lib/aliyun/log/record/persistence.rb', line 79

def create!(data)
  create(data, true)
end

#create_logstore(options = {}) ⇒ Object



39
40
41
42
43
# 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?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
# 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_nameObject



18
19
20
21
# 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) ⇒ Object



23
24
25
26
27
28
29
# 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_nameObject



31
32
33
34
35
36
37
# 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_indexObject



45
46
47
48
# File 'lib/aliyun/log/record/persistence.rb', line 45

def sync_index
  return if field_indices.blank?
  has_index? ? update_index : create_index
end