Module: MartSearch::ControllerUtils

Included in:
Controller
Defined in:
lib/martsearch/controller_utils.rb

Overview

Utility module for the Controller class.

Author:

  • Darren Oakley

Instance Method Summary collapse

Instance Method Details

#build_datasources(config_dir) ⇒ Hash

Helper function to process the MartSearch::DataSource configuration and, build appropriate DataSource objects based on this configuration.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/martsearch/controller_utils.rb', line 25

def build_datasources( config_dir )
  datasources     = {}
  datasource_conf = JSON.load( File.new( "#{config_dir}/datasources.json", 'r' ) )
  datasource_conf.recursively_symbolize_keys!
  datasource_conf.each do |ds_name,ds_conf|
    ds_conf[:internal_name] = ds_name
    datasources[ ds_name ]  = MartSearch.const_get("#{ds_conf[:type]}DataSource").new( ds_conf )
  end
  
  return datasources
end

#build_index_builder_conf(config_dir) ⇒ Hash

Helper function to build up the MartSearch::IndexBuilder configuration object (for populating/rebuilding) the Solr index.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/martsearch/controller_utils.rb', line 42

def build_index_builder_conf( config_dir )
  index_builder_conf = JSON.load( File.new( "#{config_dir}/index_builder.json", 'r' ) )
  index_builder_conf['datasets_to_index'].each do |index_dataset|
    datasource_conf = JSON.load( File.new( "#{config_dir}/datasets/#{index_dataset}.json", 'r' ) )
    index_builder_conf['datasets'][index_dataset] = datasource_conf
  end
  
  index_builder_conf.recursively_symbolize_keys!
  
  return index_builder_conf
end

#build_index_conf(config_dir) ⇒ Hash

Helper function to read in and process the MartSearch::Index configuration.



14
15
16
17
18
# File 'lib/martsearch/controller_utils.rb', line 14

def build_index_conf( config_dir )
  index_conf = JSON.load( File.new( "#{config_dir}/index.json", 'r' ) )
  index_conf.recursively_symbolize_keys!
  return index_conf
end

#build_server_conf(config_dir) ⇒ Hash

Helper funcion to build the MartSearch::Server configuration object.



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/martsearch/controller_utils.rb', line 58

def build_server_conf( config_dir )
  server_conf                      = JSON.load( File.new( "#{config_dir}/server.json", 'r' ) )
  dataviews_conf                   = process_dataviews_conf( config_dir, server_conf['dataviews'] )
  server_conf['dataviews']         = dataviews_conf[:dataviews]
  server_conf['dataviews_by_name'] = dataviews_conf[:dataviews_by_name]
  server_conf['datasets']          = process_datasets_conf( config_dir, server_conf['datasets'] )
  server_conf['browsable_content'] = server_conf['browsable_content']
  
  server_conf.recursively_symbolize_keys!
  server_conf
end

#initialize_cache(config = { :type => 'memory' }) ⇒ ActiveSupport::Cache Object

Helper function to initialize the caching system. Uses ActiveSupport::Cache so that we can easily support multiple cache backends.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/martsearch/controller_utils.rb', line 76

def initialize_cache( config={ :type => 'memory' } )
  case config[:type]
  when /memcache/
    servers = ['localhost']
    opts    = { :namespace => 'martsearch', :no_reply => true }
    
    servers          = config[:servers]   if config[:servers]
    opts[:namespace] = config[:namespace] if config[:namespace]
    opts[:namespace] = "#{opts[:namespace]}-#{MartSearch::ENVIRONMENT}"
    
    return ActiveSupport::Cache::MemCacheStore.new( servers, opts )
  when /file/
    file_store = "#{MARTSEARCH_PATH}/tmp/cache"
    file_store = config[:file_store] if config[:file_store]
    
    return ActiveSupport::Cache::FileStore.new( file_store )
  when /mongo/
    server     = config[:server]     ? config[:server]     : 'localhost'
    port       = config[:port]       ? config[:port].to_i  : 27017
    db         = config[:db]         ? config[:db]         : 'martsearch'
    mongo      = Mongo::Connection.new( server, port, :pool_size => 5, :timeout => 5 ).db("#{db}-#{MartSearch::ENVIRONMENT}")
    
    return MartSearch::MongoCache.new( :db => mongo, :collection_name => 'martsearch_cache' )
  else
    return ActiveSupport::Cache::MemoryStore.new()
  end
end