Class: FileStore::SimpleStoreFactory

Inherits:
BaseFactory show all
Defined in:
lib/filestore/factory.rb

Overview

SimpleFileStore factory class

Class Method Summary collapse

Class Method Details

.create(base_path, observable = false, logger = StdoutLogger) ⇒ Object

Creates a simple file-store instance

Arguments:

base_path: The base path directory
observable (optional): Determines wether this instance should support observation
logger (optional): The logging facility

Returns:

A new instance of SimpleFileStore


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/filestore/factory.rb', line 138

def self.create(base_path, observable = false, logger = StdoutLogger)
  store = super(SimpleFileStore, base_path, observable, logger)
  mm = MemoryMetaFactory.create File.join(base_path, MemoryMetaManager::FILE), observable, logger
  
  store.meta_manager = mm
  
  begin
    SimpleStoreFactory.recover_store(store)
  rescue FileStoreException => e
    logger.debug "Couldn't recover store.\nReason: #{e.message}\nTrying to create the store"
    SimpleStoreFactory.create_store(store)
  end
  
  return store
end

.create_store(store) ⇒ Object

Setup for a new file store directory

Arguments:

store: The file store instance to set up


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/filestore/factory.rb', line 160

def self.create_store(store)
  # Try to create needed directories
  begin
    FileUtils.mkdir [store.store_path, store.deleted_path, store.rollback_path]
  rescue Errno::ENOENT => e
    raise FileStoreException, "One ore more system directories couldn't be created.\n#{e.message}"
  end
  #
  # Try to create hidden meta file
  #
  begin
    meta = { 
      :created_at => Date.today.strftime('%d.%m.%Y %H:%M:%S:%L'), 
      :store_path => store.store_path,
      :deleted_path => store.deleted_path,
      :rollback_path => store.rollback_path,
      :created_by => Etc.getlogin
    }
    
    File.open(store.meta_file, "wb+") do |fh|
      YAML.dump(meta, fh)
    end
    #
    # Creation was successful
    #
  rescue Exception => e
    raise FileStoreException, "Store meta file #{store.meta_file} couldn't be created.\n#{e.message}"
  end
end

.recover_store(store) ⇒ Object

Recover an existing file store

Arguments:

store: The file store instance to recover


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/filestore/factory.rb', line 195

def self.recover_store(store)
  # trying to recover existing file store
  begin
    meta = YAML.load_file(store.meta_file)
    
    raise FileStoreException, "Store directory not found" if not File.directory?(meta[:store_path])
    raise FileStoreException, "Deleted directory not found" if not File.directory?(meta[:deleted_path])
    raise FileStoreException, "Rollback directory not found" if not File.directory?(meta[:rollback_path])
    #
    # Recovery was successful
    #
  rescue Exception => e
    raise FileStoreException, "Unable to recover file store from path #{store.root_path}.\n#{e.message}"
  end
end