Class: StrokeDB::Config

Inherits:
Object show all
Defined in:
lib/strokedb/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default = false) ⇒ Config

Returns a new instance of Config.



79
80
81
82
83
# File 'lib/strokedb/config.rb', line 79

def initialize(default = false)
  @storages, @indexes, @stores = {}, {}, {}

  ::StrokeDB.default_config = self if default
end

Instance Attribute Details

#build_configObject

Returns the value of attribute build_config.



76
77
78
# File 'lib/strokedb/config.rb', line 76

def build_config
  @build_config
end

#indexesObject (readonly)

Returns the value of attribute indexes.



77
78
79
# File 'lib/strokedb/config.rb', line 77

def indexes
  @indexes
end

#storagesObject (readonly)

Returns the value of attribute storages.



77
78
79
# File 'lib/strokedb/config.rb', line 77

def storages
  @storages
end

#storesObject (readonly)

Returns the value of attribute stores.



77
78
79
# File 'lib/strokedb/config.rb', line 77

def stores
  @stores
end

Class Method Details

.build(opts = {}) ⇒ Object

Build the config from given options.

Supported options are:

:default - if set to true, config becomes the default one.
:storages - must be an array of storage types.
            Appropriate storages will be initialized and chained
            together. Defaults to [:memory_chunk, :file_chunk]
:index_storages - index storages. Defaults to [:inverted_list_file].
:index - index type. Defaults to :inverted_list. 
:base_path - if set, specifies the path for storages. Otherwise,
             current directory is used.
:store - store type to use. Defaults to :skiplist.
:store_options - options passed to the created store


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/strokedb/config.rb', line 32

def Config.build(opts={})
  opts = opts.stringify_keys
  
  config = new(opts['default'])
  storages = opts['storages'] || [:memory, :file]

  base_path = opts['base_path'] || './'

  add_storage = lambda do |name| 
    config.add_storage(name, name, :path => File.join(base_path, name.to_s))
  end

  ### setup document storages ###
  
  initialized_storages = storages.map(&add_storage)       
  config.chain(*storages) if storages.size >= 2

  initialized_storages.each_consecutive_pair do |cur, nxt|
    # next storage is authoritative for each storage
    cur.authoritative_source = nxt
  end
  
  ### setup index storages and indexes ###

  index_storages = opts['index_storages'] || [:inverted_list_file]
  index_storages.each(&add_storage)

  config.add_index(:default, opts['index'] || :inverted_list, index_storages.first)

  config.add_store(:default, opts['store'], # FIXME: nil here is a Bad Thing (tm) 
                   { :storage => storages.first, :path => base_path }.merge(opts['store_options'] || {}))

  ### save config ###

  config.build_config = opts.except('default')
  
  FileUtils.mkdir_p base_path
  File.open(File.join(base_path,'config'), "w+") do |file|
    file.write config.build_config.to_json
  end

  config
end

.load(filename, default = false) ⇒ Object

Load config from file, probably making it the default one



12
13
14
# File 'lib/strokedb/config.rb', line 12

def Config.load(filename, default = false)
  build(JSON.parse(IO.read(filename)).merge(:default => default))
end

Instance Method Details

#[](name) ⇒ Object



85
86
87
# File 'lib/strokedb/config.rb', line 85

def [](name)
  @storages[name] || @indexes[name] || nil
end

#add_index(key, type, storage_key, store_key = nil) ⇒ Object



103
104
105
106
# File 'lib/strokedb/config.rb', line 103

def add_index(key, type, storage_key, store_key = nil)
  @indexes[key] = constantize(:index, type).new(@storages[storage_key])
  @indexes[key].document_store = @stores[store_key] if store_key
end

#add_storage(key, type, *args) ⇒ Object



89
90
91
# File 'lib/strokedb/config.rb', line 89

def add_storage(key, type, *args)
  @storages[key] = constantize(:storage, type).new(*args)
end

#add_store(key, type, options = {}) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/strokedb/config.rb', line 108

def add_store(key, type, options = {})
  
  options[:storage] = @storages[options[:storage] || :default]
  raise "Missing storage for store #{key}" unless options[:storage]
  
  options[:index] ||= @indexes[options[:index] || :default]
  
  store_instance = constantize(:store, type).new(options)
  
  if options[:index]
    options[:index].document_store = store_instance
  end

  @stores[key] = store_instance
end

#chain_storages(*args) ⇒ Object Also known as: chain

Raises:

  • (ArgumentError)


93
94
95
96
97
98
99
# File 'lib/strokedb/config.rb', line 93

def chain_storages(*args)
  raise ArgumentError, "Not enough storages to chain storages" unless args.size >= 2

  args.map {|x| @storages[x] || raise("Missing storage #{x}") }.each_consecutive_pair do |cur, nxt|
    cur.add_chained_storage! nxt
  end
end

#inspectObject



124
125
126
# File 'lib/strokedb/config.rb', line 124

def inspect
  "#<StrokeDB::Config:0x#{object_id.to_s(16)}>"
end