Class: FileStore::SimpleFileStore
- Inherits:
-
Object
- Object
- FileStore::SimpleFileStore
- Includes:
- Logger, OberservedSubject
- Defined in:
- lib/filestore/simple_store.rb
Overview
Main library class implementing a simple file store used for storing and managing arbitrary files
Constant Summary collapse
- STORE_LOCK_FILE =
Name of the lock file
".locked"- META_FILE =
Name of the meta file describing the current file store
"filestore.yaml"- STORE_ROOT =
The base name of the file store directory
'filestore'- DELETED_ROOT =
The base name of the directory for storing deleted files
'deleted'- ROLLBACK_ROOT =
The base name of the directory storing files extracted from file store by a rollback action
'rollback'
Instance Attribute Summary collapse
-
#deletedPath ⇒ Object
readonly
Accessors for important properties.
-
#metaFile ⇒ Object
readonly
Accessors for important properties.
-
#metaManager ⇒ Object
readonly
Accessors for important properties.
-
#rollbackPath ⇒ Object
readonly
Accessors for important properties.
-
#rootPath ⇒ Object
readonly
Accessors for important properties.
-
#storePath ⇒ Object
readonly
Accessors for important properties.
Attributes included from OberservedSubject
Attributes included from Logger
Instance Method Summary collapse
-
#add(file, meta = {}, shouldMove = true) ⇒ Object
Adds a file to the store.
-
#get(id) ⇒ Object
Retrieves a file identified by it’s ID.
-
#initialize(metaManager, rootPath = '.', logger) ⇒ SimpleFileStore
constructor
Initializes a new instance of SimpleFileStore.
-
#locked? ⇒ Boolean
Determines wether this store is locked.
-
#remove(id) ⇒ Object
Moves a file from the current to the deleted store.
-
#restore(id) ⇒ Object
Restores a file identified by it’s id.
-
#shutdown ⇒ Object
Shuts down the file store.
Methods included from OberservedSubject
included, #inform, #initialize_obs, #register, #unregister
Constructor Details
#initialize(metaManager, rootPath = '.', logger) ⇒ SimpleFileStore
Initializes a new instance of SimpleFileStore
Arguments: metaManager: The meta data manager instance to be used by this store rootPath: The path where the file store resides
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 |
# File 'lib/filestore/simple_store.rb', line 43 def initialize(, rootPath = '.', logger) raise FileStoreException, "Root path already locked" if SimpleFileStore.is_directory_locked?(rootPath) raise FileStoreException, "FileStore root path #{rootPath} doesn't exist" if not File.directory?(rootPath) raise FileStoreException, "FileStore root path #{rootPath} isn't writable" if not File.writable?(rootPath) raise FileStoreException, "No meta data manager given" if .nil? raise FileStoreException, "Meta data manager must be of type FileStore::MetaManager" if not .is_a?(MetaManager) @metaManager = @rootPath = rootPath @storePath = File.join(@rootPath, STORE_ROOT) @deletedPath = File.join(@rootPath, DELETED_ROOT) @rollbackPath = File.join(@rootPath, ROLLBACK_ROOT) @metaFile = File.join(@rootPath, META_FILE) @locked = false @logger = logger self.initialize_obs begin # Try to recover existing store SimpleFileStore.recover_store(self) rescue FileStoreException => e # Recovery failed, trying to create the store SimpleFileStore.create_store(self) end lock end |
Instance Attribute Details
#deletedPath ⇒ Object (readonly)
Accessors for important properties
35 36 37 |
# File 'lib/filestore/simple_store.rb', line 35 def deletedPath @deletedPath end |
#metaFile ⇒ Object (readonly)
Accessors for important properties
35 36 37 |
# File 'lib/filestore/simple_store.rb', line 35 def @metaFile end |
#metaManager ⇒ Object (readonly)
Accessors for important properties
35 36 37 |
# File 'lib/filestore/simple_store.rb', line 35 def @metaManager end |
#rollbackPath ⇒ Object (readonly)
Accessors for important properties
35 36 37 |
# File 'lib/filestore/simple_store.rb', line 35 def rollbackPath @rollbackPath end |
#rootPath ⇒ Object (readonly)
Accessors for important properties
35 36 37 |
# File 'lib/filestore/simple_store.rb', line 35 def rootPath @rootPath end |
#storePath ⇒ Object (readonly)
Accessors for important properties
35 36 37 |
# File 'lib/filestore/simple_store.rb', line 35 def storePath @storePath end |
Instance Method Details
#add(file, meta = {}, shouldMove = true) ⇒ Object
Adds a file to the store
Arguments: file: The file to be stored meta: Optional meta data to be stored along with the physical file shouldMove: Determines wether to original file should be deleted
Returns:
The newly created ID for the file
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/filestore/simple_store.rb', line 82 def add(file, = {}, shouldMove = true) raise FileStoreException, "File #{file} not found" if not File.exists?(file) raise FileStoreException, "File #{file} isn't readable" if not File.readable?(file) raise FileStoreException, "File #{file} can't be removed" if not File.writable?(file) = {} if .nil? id = "" begin dir = SimpleFileStore.get_daily_directory(@storePath) @logger.info "Adding file #{file} to directory #{dir}" id = SimpleFileStore.get_id(self) @logger.info "Using file id #{id}" dstPath = File.join(dir, id) @logger.info "Created destination path #{dstPath}" shouldMove ? (@logger.info("Moving file"); FileUtils.mv(file, dstPath)) : (@logger.info("Copying file"); FileUtils.copy_file(file, dstPath)) self.inform ObserverAction.new(:type => ObserverAction::TYPE_STORE_ADD, :objects => [file, ], :msg => "Added file to file store") rescue Exception => e raise FileStoreException, "Couldn't add file #{file} to store.", e.backtrace end [:path] = dstPath @metaManager.add_or_update(id, ) return id end |
#get(id) ⇒ Object
Retrieves a file identified by it’s ID
Arguments: id: The files ID to retrieve
Returns: A hash of file object (:path) and corresponding meta data (:data) representing the file in the store
122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/filestore/simple_store.rb', line 122 def get(id) raise FileStoreException, "No ID given" if id.nil? or id == '' raise FileStoreException, "No file for ID #{id} found" if not @metaManager.has_id?(id) md = @metaManager.get_data(id) path = md[:path] raise FileStoreException, "No valid meta data found for ID #{id}" if md.nil? or not File.exists?(path) self.inform ObserverAction.new :type => ObserverAction::TYPE_STORE_GET, :objects => [id], :msg => "Returning file from file store" return { :path => File.new(path), :data => md } end |
#locked? ⇒ Boolean
Determines wether this store is locked
204 205 206 |
# File 'lib/filestore/simple_store.rb', line 204 def locked? return @locked end |
#remove(id) ⇒ Object
Moves a file from the current to the deleted store
Arguments: id: The ID identifying the file to be moved
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/filestore/simple_store.rb', line 142 def remove(id) raise FileStoreException, "No file ID given for removal" if id == '' or id.nil? raise FileStoreException, "File ID for removal not found in store" if not @metaManager.has_id?(id) file = @metaManager.get_data(id)[:path] begin @metaManager.remove(id) dir = SimpleFileStore.get_daily_directory(@deletedPath) dstPath = File.join(dir, id) FileUtils.move(file, dstPath) self.inform ObserverAction.new :type => ObserverAction::TYPE_STORE_REMOVE, :objects => [id], :msg => "Deleted file from store" rescue Exception => e raise FileStoreException, "Couldn't move file #{file} to deleted store.\n#{e.}" end end |
#restore(id) ⇒ Object
Restores a file identified by it’s id
Arguments: id: The file ID
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/filestore/simple_store.rb', line 168 def restore(id) raise FileStoreException, "No file ID given for restore" if id == '' or id.nil? begin md = @metaManager.restore id @logger.debug "Restoring meta data #{md}" file = md[:path] dir = SimpleFileStore.get_daily_directory(@storePath) dstPath = File.join(dir, id) FileUtils.move(file, dstPath) self.inform ObserverAction.new :type => ObserverAction::TYPE_STORE_RESTORE, :objects => [id], :msg => "Restored file from store" rescue Exception => e raise FileStoreException, "Couldn't restore file #{file} from deleted store.\n#{e.}" # # Delete restored entry from metaManager @metaManager.delete(id) end end |
#shutdown ⇒ Object
Shuts down the file store
193 194 195 196 197 198 199 200 |
# File 'lib/filestore/simple_store.rb', line 193 def shutdown @metaManager.shutdown release_lock self.inform ObserverAction.new :type => ObserverAction::TYPE_STORE_SHUTDOWN, :msg => "File store shutdown" end |