Class: MultiSolr::TimelineCoreHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_solr/timeline_core_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(solr_url, core_group_name, simple_core_handler_class) ⇒ TimelineCoreHandler

Returns a new instance of TimelineCoreHandler.



10
11
12
13
14
# File 'lib/multi_solr/timeline_core_handler.rb', line 10

def initialize solr_url, core_group_name, simple_core_handler_class
  @simple_core_handler_class = simple_core_handler_class
  @core_group_name = core_group_name
  @solr_url = solr_url || raise("No solr-url given!")
end

Instance Attribute Details

#core_group_nameObject (readonly)

Returns the value of attribute core_group_name.



6
7
8
# File 'lib/multi_solr/timeline_core_handler.rb', line 6

def core_group_name
  @core_group_name
end

#simple_core_handler_classObject (readonly)

Returns the value of attribute simple_core_handler_class.



5
6
7
# File 'lib/multi_solr/timeline_core_handler.rb', line 5

def simple_core_handler_class
  @simple_core_handler_class
end

#solr_urlObject (readonly)

Returns the value of attribute solr_url.



7
8
9
# File 'lib/multi_solr/timeline_core_handler.rb', line 7

def solr_url
  @solr_url
end

Instance Method Details

#base_connectionObject



51
52
53
# File 'lib/multi_solr/timeline_core_handler.rb', line 51

def base_connection
  @_connection ||= RSolr.connect :url => @solr_url
end

#core_connection(core_name = nil) ⇒ Object



56
57
58
59
# File 'lib/multi_solr/timeline_core_handler.rb', line 56

def core_connection core_name=nil
  core_name ||= last_core_name
  RSolr.connect :url => "#{@solr_url}/#{core_name}"
end

#core_status(core_name = nil) ⇒ Object

Liefert Core-Status evtl. aller Cores oder wenn Core-Name angegeben des einzelnen Cores returns Hash mit den Statusdaten

Beispiel für alle Cores:
   {'core-01'=>{'name'=>'core-01',
                'instanceDir'=>'solr/cobra-stock/',
                'dataDir'=>'solr/cobra-stock/../../data/cobra-stock/',
                'startTime'=>'2012-04-11T09:49:07.93Z',
                'uptime'=>7796905,
                'index'=>{'numDocs'=>1369046,'maxDoc'=>1369046,'version'=>1334134600492,'optimized'=>true,'current'=>true,'hasDeletions'=>false,'directory'=>'org.apache.lucene.store.NIOFSDirectory:org.apache.lucene.store.NIOFSDirectory@/home/bledig/Projects/lava/multi-solr/data/cobra-stock-20120411-1123/index lockFactory=org.apache.lucene.store.NativeFSLockFactory@1896a4c','lastModified'=>'2012-04-11T09:43:32Z'}},
               },...
   }


121
122
123
124
125
126
127
128
129
# File 'lib/multi_solr/timeline_core_handler.rb', line 121

def core_status core_name=nil
  params = {:action => 'STATUS' }
  params[:core] = core_name if core_name
  res = base_connection.get 'admin/cores', :params => params
  status = res && res['status']
  raise 'No status for cores available' if status.nil?
  return status[core_name] if core_name
  status
end

#create_core(suffix = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/multi_solr/timeline_core_handler.rb', line 62

def create_core suffix=nil
  suffix = Time.now.strftime('%Y%m%d-%H%M') if suffix.nil?
  new_core_name = "#{@core_group_name}-#{suffix}"
  status = self.core_status last_core_name
  old_name = status['name']
  instance_dir_old = status['instanceDir']
  instance_dir = instance_dir_old.sub(/^solr\//, '') # führendes solr im instanceDir ist zuviel, also entfernen
  data_dir = status['dataDir'].sub(/#{old_name}\/$/, new_core_name)
  data_dir.sub!(/^#{instance_dir_old}/, '')# führendes instanceDir im dataDir ist zuviel, also entfernen
  res = self.base_connection.get 'admin/cores', :params => {:action => 'CREATE',
                                                :name => new_core_name,
                                                :instanceDir => instance_dir,
                                                :dataDir => data_dir}
  reset_cores
  MultiSolr.logger.info "MultiSolr::TimelineCoreHandler.create_core: Core '#{new_core_name}' created. dataDir=#{data_dir}, instanceDir=#{instance_dir}"
  new_core_name
end

#create_single_core_handler(core_name = nil, options = nil) ⇒ Object



17
18
19
20
# File 'lib/multi_solr/timeline_core_handler.rb', line 17

def create_single_core_handler core_name=nil, options=nil
  core_name ||= last_core_name
  @simple_core_handler_class.new @solr_url, core_name, options
end

#dataimport(import_handler_name, command = 'full-import', core_name = nil) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/multi_solr/timeline_core_handler.rb', line 81

def dataimport import_handler_name, command='full-import', core_name=nil
  core_name ||= last_core_name
  con = core_connection core_name
  res = con.get import_handler_name, :params => {:command => command}
  MultiSolr.logger.info "MultiSolr::TimelineCoreHandler.dataimport: core=#{core_name}, command=#{command}, handler=#{import_handler_name}"
  res
end

#last_core_nameObject

Liefert Name des letzte Cores der laut alphabetischer Liste (see list_cores) existiert



47
48
49
# File 'lib/multi_solr/timeline_core_handler.rb', line 47

def last_core_name
  @last_core_name ||= list_cores.last
end

#list_cores(include_empties = false) ⇒ Object

Liefert Array mit den Namen der verfügbaren Cores, die mit dem Core-Namen (see core_name) beginnen Diese Liste wird gecached



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/multi_solr/timeline_core_handler.rb', line 26

def list_cores include_empties=false
  return @cores if @cores
  status = self.core_status
  @cores = []
  matcher = Regexp.new "^#{@core_group_name}"
  status.each do |core_name, data|
    next unless matcher.match(core_name)
    next if data['index']['numDocs'] < 3 && !include_empties
    @cores << core_name
  end
  @cores.sort!
  MultiSolr.logger.info "MultiSolr::TimelineCoreHandler.list_cores: #{@cores.join(',')}"
  @cores
end

#remove_core(core_name, with_data = false) ⇒ Object

Entfernen angegebenen Core aus der Core-Liste inkl. unload-Befehl an den Solr Achtung! Diese Methode löscht kann nur dann die physischen Daten des Cores löschen, wenn dieser mit einer absoluten Pfad-Angabe im solr konfiguriert ist! Parameter:

core_name:    Name des zu löschenden Cores
with_data:    optionaler Angabe ob Daten mit gelöscht werden sollen (Standardmäßig aus)


95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/multi_solr/timeline_core_handler.rb', line 95

def remove_core core_name, with_data=false
  status = self.core_status core_name
  raise "no core '#{core_name}'!" if status.nil? || status['name'].nil?
  base_connection.get 'admin/cores', :params => {:action => 'unload', :core => core_name}
  MultiSolr.logger.info "MultiSolr::TimelineCoreHandler.remove_core: Core '#{core_name}' unloaded"
  if with_data
    data_dir = status['dataDir']
    raise "No absolute DataDir-Path! Only Core was unloaded!" unless data_dir.start_with?('/')
    FileUtils.rm_rf data_dir
  end
  reset_cores
  nil
end

#reset_coresObject

Rücksetzen Cache der core-liste (see list_cores)



42
43
44
# File 'lib/multi_solr/timeline_core_handler.rb', line 42

def reset_cores
  @cores = @last_core_name = nil
end