Class: FileStore::MultiTenantFileStore

Inherits:
Object
  • Object
show all
Includes:
Logger, OberservedSubject, Singleton
Defined in:
lib/filestore/multitenant_store.rb

Overview

Singleton class implementing a multitenant file store

Instance Attribute Summary collapse

Attributes included from OberservedSubject

#observers

Attributes included from Logger

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OberservedSubject

included, #inform, #initialize_obs, #register, #unregister

Constructor Details

#initializeMultiTenantFileStore

Initializes a new instance of MultiTenantFileStore



23
24
25
26
27
28
# File 'lib/filestore/multitenant_store.rb', line 23

def initialize()
	@rootPath = Dir.getwd
	@stores = {}
	
	self.initialize_obs
end

Instance Attribute Details

#rootPathObject (readonly)

Accessors



19
20
21
# File 'lib/filestore/multitenant_store.rb', line 19

def rootPath
  @rootPath
end

#storesObject (readonly)

Accessors



19
20
21
# File 'lib/filestore/multitenant_store.rb', line 19

def stores
  @stores
end

Class Method Details

.recover(rootPath, logger) ⇒ Object

Recovers a multitenant store

Arguments: rootPath: The base path of the multitenant store

Raises:



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/filestore/multitenant_store.rb', line 171

def self.recover(rootPath, logger)
	raise FileStoreException, "Root path #{rootPath} isn't a valid multitenant store" if not File.directory?(rootPath)
	
	stores = {}
	
	Dir.glob(File.join(rootPath, "*")).each { |e|
		begin
			if File.directory?(e)
				tenant = File.basename(e)
				mm = MemoryMetaManager.new File.join(e, MemoryMetaManager::FILE), logger
				sfs = SimpleFileStore.new mm, e, @logger
		
				stores[tenant] = sfs
			end
		rescue Exception => e
			logger.error "Couldn't create store for tenant #{tenant}.\n#{e.message}"
		end
	}
	
	return stores
end

Instance Method Details

#add_to_tenant(tenant, file, md = {}) ⇒ Object

Adds a file to the tenant’s store

Arguments: tenant: The tenant’s ID file: The file to be added md: Optional meta data

Raises:



109
110
111
112
113
114
115
116
# File 'lib/filestore/multitenant_store.rb', line 109

def add_to_tenant(tenant, file, md = {})
	raise FileStoreException, "Tenant #{tenant} not registered. File #{file} can't be added." if not @stores.key?(tenant)
	
	@stores[tenant].add(file, md)
	
	self.inform ObserverAction.new(:type => ObserverAction::TYPE_MSTORE_ADD, 
        :objects => [tenant, file], :msg => "Added file to tenant")
end

#create_tenant_store(id = '') ⇒ Object

Creates a new file store for a tenant

Arguments: id: The optional ID of the tenant. If omitted, an ID will be created automatically

Returns: The tenants ID



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/filestore/multitenant_store.rb', line 52

def create_tenant_store(id = '')
	id = UUIDTools::UUID.random_create.to_s if id == '' or id.nil?
	
	begin
		path = File.join(@rootPath, id)
		FileUtils.mkdir path if not File.directory?(path)
		mm = MemoryMetaManager.new File.join(path, "meta.yaml"), @logger
		sfs = SimpleFileStore.new mm, path, @logger
	
		@stores[id] = sfs
		
		self.inform ObserverAction.new(:type => ObserverAction::TYPE_MSTORE_CREATE, 
        :msg => "Created new tenant store")
	rescue Exception => e
		raise FileStoreException, "Couldn't create multitenant store.\n#{e.message}"
	end
	
	return id
end

#get_from_tenant(tenant, id) ⇒ Object

Retrieves a file from the tenant’s store

Arguments: tenant: The tenant’s ID file: The file to be retrieved

Returns: A hash containing the file object (:path) and the corresponding meta data (:data)

Raises:



140
141
142
143
144
# File 'lib/filestore/multitenant_store.rb', line 140

def get_from_tenant(tenant, id)
	raise FileStoreException, "Given tenant #{tenant} isn't registered" if not @stores.key?(tenant)
	
	return @stores[tenant].get(id)
end

#get_tenant_store(id) ⇒ Object

Returns the complete file store for a given tenant

Arguments: id: The tenant’s ID

Returns: An instance of FileStore::SimpleFileStore

Raises:



96
97
98
99
100
# File 'lib/filestore/multitenant_store.rb', line 96

def get_tenant_store(id)
	raise FileStoreException, "Tenant #{id} not registered. No file store given." if not @stores.key?(id)
	
	return @stores[id]
end

#has_tenant?(id) ⇒ Boolean

Determines wether a tenant is registered

Arguments: tenant: The tenant’s ID to be tested

Returns:

  • (Boolean)


151
152
153
# File 'lib/filestore/multitenant_store.rb', line 151

def has_tenant?(id)
	return @stores.key?(id)
end

#remove_from_tenant(tenant, id) ⇒ Object

Removes a file from the tenant’s store

Arguments: tenant: The tenant’s ID id: The ID of the file to be removed

Raises:



124
125
126
127
128
# File 'lib/filestore/multitenant_store.rb', line 124

def remove_from_tenant(tenant, id)
	raise FileStoreException, "Tenant #{tenant} not registered. File with ID {id} can't be removed." if not @stores.key?(tenant)
	
	@stores[tenant].remove(id)
end

#remove_tenant_store(id) ⇒ Object

Permanently removes a tenant’s store

Arguments: id: The tenant’s ID

Raises:



77
78
79
80
81
82
83
84
85
86
# File 'lib/filestore/multitenant_store.rb', line 77

def remove_tenant_store(id)
	raise FileStoreException, "Tenant #{id} can't be removed. Not registered." if not @stores.key?(id)
	
	begin
		@stores.delete(id)
		FileUtils.remove_dir File.join(@rootPath, id)
	rescue Exception => e
		raise FileStoreException, "Couldn't delete tenant #{id}.\n#{e.message}"
	end
end

#set_root_path(rootPath) ⇒ Object

Sets the root path of the multitenant store. As FileStore::MultiTenantFileStore is a singleton class, this method must be used before any other

Arguments: rootPath: The path to be used

Raises:



36
37
38
39
40
41
# File 'lib/filestore/multitenant_store.rb', line 36

def set_root_path(rootPath)
	raise FileStoreException, "Root path #{rootPath} doesn't exist" if not File.exists?(rootPath)
	
	@rootPath = rootPath
	@stores = MultiTenantFileStore.recover(rootPath, @logger)
end

#shutdownObject

Shuts down this multitenant store



157
158
159
160
161
162
# File 'lib/filestore/multitenant_store.rb', line 157

def shutdown
	# Shut down any tenant store
	@stores.values.each do |s|
		s.shutdown
	end
end