Class: FileStore::SimpleFileStore

Inherits:
Object
  • Object
show all
Includes:
Logger
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

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

Attributes included from Logger

#logger

Instance Method Summary collapse

Constructor Details

#initialize(root_path, logger = StdoutLogger) ⇒ SimpleFileStore

Initializes a new instance of SimpleFileStore

Arguments: root_path: The path where the file store resides

logger: The logging facility

Raises:



40
41
42
43
44
45
46
47
48
49
# File 'lib/filestore/simple_store.rb', line 40

def initialize(root_path, logger = StdoutLogger)
  raise FileStoreException, "FileStore root path #{root_path} doesn't exist" if not File.directory?(root_path)
  raise FileStoreException, "FileStore root path #{root_path} isn't writable" if not File.writable?(root_path)
  
  @root_path = root_path
    @store_path = File.join(@root_path, STORE_ROOT)
    @deleted_path = File.join(@root_path, DELETED_ROOT)
    @rollback_path = File.join(@root_path, ROLLBACK_ROOT)
    @meta_file = File.join(@root_path, SimpleFileStore::META_FILE)
end

Instance Attribute Details

#deleted_pathObject (readonly)

Returns the value of attribute deleted_path.



28
29
30
# File 'lib/filestore/simple_store.rb', line 28

def deleted_path
  @deleted_path
end

#meta_fileObject (readonly)

Returns the value of attribute meta_file.



28
29
30
# File 'lib/filestore/simple_store.rb', line 28

def meta_file
  @meta_file
end

#meta_managerObject

Accessors for important properties



27
28
29
# File 'lib/filestore/simple_store.rb', line 27

def meta_manager
  @meta_manager
end

#rollback_pathObject (readonly)

Rollback path will be used in future versions



32
33
34
# File 'lib/filestore/simple_store.rb', line 32

def rollback_path
  @rollback_path
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



28
29
30
# File 'lib/filestore/simple_store.rb', line 28

def root_path
  @root_path
end

#store_pathObject (readonly)

Returns the value of attribute store_path.



28
29
30
# File 'lib/filestore/simple_store.rb', line 28

def store_path
  @store_path
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

Raises:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/filestore/simple_store.rb', line 61

def add(file, meta = {}, 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)
  
  meta = {} if meta.nil?
  id = ""
  
  begin
    dir = SimpleFileStore.get_daily_directory(@store_path)
    @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))
    
    inform ObserverAction.new(:type => ObserverAction::TYPE_STORE_ADD, 
        :objects => { :file => file, :meta => meta }, :msg => "Added file to file store") if self.is_a?(ObservedSubject)
  rescue Exception => e
    raise FileStoreException, "Couldn't add file #{file} to store.", e.backtrace
  end
  
  meta[:path] = dstPath
  @meta_manager.add_or_update(id, meta)
  
  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

Raises:



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/filestore/simple_store.rb', line 101

def get(id)
  raise FileStoreException, "No ID given" if id.nil? or id == ''
  raise FileStoreException, "No file for ID #{id} found" if not @meta_manager.has_id?(id)
  
  md = @meta_manager.get_data(id)
  path = md[:path]

  raise FileStoreException, "No valid meta data found for ID #{id}" if md.nil? or not File.exists?(path)
  
  inform ObserverAction.new(:type => ObserverAction::TYPE_STORE_GET, 
      :objects => [id], :msg => "Returning file from file store")  if self.is_a?(ObservedSubject)
  
  return { :path => File.new(path), :data => md }
end

#remove(id) ⇒ Object

Moves a file from the current to the deleted store

Arguments: id: The ID identifying the file to be moved

Raises:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/filestore/simple_store.rb', line 121

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 @meta_manager.has_id?(id)
  
  file = @meta_manager.get_data(id)[:path]
  
  begin
    @meta_manager.remove(id)
    
    dir = SimpleFileStore.get_daily_directory(@deleted_path)
    dstPath = File.join(dir, id)
    
    FileUtils.move(file, dstPath)
    
    inform ObserverAction.new(:type => ObserverAction::TYPE_STORE_REMOVE, 
        :objects => [id], :msg => "Deleted file from store") if self.is_a?(ObservedSubject)
  rescue Exception => e
    raise FileStoreException, "Couldn't move file #{file} to deleted store.\n#{e.message}"
  end
end

#restore(id) ⇒ Object

Restores a file identified by it’s id

Arguments: id: The file ID

Raises:



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/filestore/simple_store.rb', line 147

def restore(id)
  raise FileStoreException, "No file ID given for restore" if id == '' or id.nil?
  
  begin
    md = @meta_manager.restore id
      @logger.debug "Restoring meta data #{md}"
      file = md[:path]
      
    dir = SimpleFileStore.get_daily_directory(@store_path)
    dstPath = File.join(dir, id)
    
    FileUtils.move(file, dstPath)
    
    inform ObserverAction.new(:type => ObserverAction::TYPE_STORE_RESTORE, 
        :objects => [id], :msg => "Restored file from store") if self.is_a?(ObservedSubject)
  rescue Exception => e
    raise FileStoreException, "Couldn't restore file #{file} from deleted store.\n#{e.message}"
    #
    # Delete restored entry from metaManager
    #
    @meta_manager.delete(id)
  end
end

#shutdownObject

Shuts down the file store



173
174
175
176
177
178
# File 'lib/filestore/simple_store.rb', line 173

def shutdown
  @meta_manager.shutdown
  
  inform ObserverAction.new(:type => ObserverAction::TYPE_STORE_SHUTDOWN, 
      :msg => "File store shutdown")  if self.is_a?(ObservedSubject)
end