Class: StrokeDB::Config
Instance Attribute Summary collapse
-
#build_config ⇒ Object
Returns the value of attribute build_config.
-
#indexes ⇒ Object
readonly
Returns the value of attribute indexes.
-
#storages ⇒ Object
readonly
Returns the value of attribute storages.
-
#stores ⇒ Object
readonly
Returns the value of attribute stores.
Class Method Summary collapse
-
.build(opts = {}) ⇒ Object
Build the config from given options.
-
.load(filename, default = false) ⇒ Object
Load config from file, probably making it the default one.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #add_index(key, type, storage_key, store_key = nil) ⇒ Object
- #add_storage(key, type, *args) ⇒ Object
- #add_store(key, type, options = {}) ⇒ Object
- #chain_storages(*args) ⇒ Object (also: #chain)
-
#initialize(default = false) ⇒ Config
constructor
A new instance of Config.
- #inspect ⇒ Object
Constructor Details
Instance Attribute Details
#build_config ⇒ Object
Returns the value of attribute build_config.
76 77 78 |
# File 'lib/strokedb/config.rb', line 76 def build_config @build_config end |
#indexes ⇒ Object (readonly)
Returns the value of attribute indexes.
77 78 79 |
# File 'lib/strokedb/config.rb', line 77 def indexes @indexes end |
#storages ⇒ Object (readonly)
Returns the value of attribute storages.
77 78 79 |
# File 'lib/strokedb/config.rb', line 77 def storages @storages end |
#stores ⇒ Object (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. = 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 |
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, = {}) [:storage] = @storages[[:storage] || :default] raise "Missing storage for store #{key}" unless [:storage] [:index] ||= @indexes[[:index] || :default] store_instance = constantize(:store, type).new() if [:index] [:index].document_store = store_instance end @stores[key] = store_instance end |
#chain_storages(*args) ⇒ Object Also known as: chain
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 |
#inspect ⇒ Object
124 125 126 |
# File 'lib/strokedb/config.rb', line 124 def inspect "#<StrokeDB::Config:0x#{object_id.to_s(16)}>" end |