Class: FileStore::MultiTenantFileStore

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

Overview

Class implementing a multitenant file store

Instance Attribute Summary collapse

Attributes included from Logger

#logger

Instance Method Summary collapse

Constructor Details

#initialize(root_path = ".", logger = StdoutLogger) ⇒ MultiTenantFileStore

Initializes a new instance of MultiTenantFileStore



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

def initialize(root_path = ".", logger = StdoutLogger)
	@rootPath = root_path
	@stores = {}
	@logger = logger
end

Instance Attribute Details

#rootPathObject (readonly)

Accessors



14
15
16
# File 'lib/filestore/multitenant_store.rb', line 14

def rootPath
  @rootPath
end

#storesObject

Returns the value of attribute stores.



15
16
17
# File 'lib/filestore/multitenant_store.rb', line 15

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

Returns:

The file ID

Raises:



93
94
95
96
97
98
99
100
101
102
# File 'lib/filestore/multitenant_store.rb', line 93

def add_to_tenant(tenant, file, md = {})
	raise FileStoreException, "Tenant #{tenant} not registered. File #{file} can't be added." if not @stores.key?(tenant)
	
	f_id = @stores[tenant].add(file, md)
	
	inform ObserverAction.new(:type => ObserverAction::TYPE_MSTORE_ADD, 
        :objects => [tenant, file, f_id], :msg => "Added file to tenant #{tenant} with ID #{f_id}") if self.is_a?(ObservedSubject)
        
    return f_id
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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/filestore/multitenant_store.rb', line 34

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)
		sfs = SimpleStoreFactory::create path, self.is_a?(ObservedSubject), @logger
	
		@stores[id] = sfs
		
		inform ObserverAction.new(:type => ObserverAction::TYPE_MSTORE_CREATE, 
        :msg => "Created new tenant store") if self.is_a?(ObservedSubject)
	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:



126
127
128
129
130
# File 'lib/filestore/multitenant_store.rb', line 126

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:



77
78
79
80
81
# File 'lib/filestore/multitenant_store.rb', line 77

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)


137
138
139
# File 'lib/filestore/multitenant_store.rb', line 137

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:



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

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:



58
59
60
61
62
63
64
65
66
67
# File 'lib/filestore/multitenant_store.rb', line 58

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

#shutdownObject

Shuts down this multitenant store



143
144
145
146
147
148
149
# File 'lib/filestore/multitenant_store.rb', line 143

def shutdown
	# Shut down any tenant store
	@stores.each do |id, store|
	  @logger.info "Shutting down file store for tenant #{id}"
		store.shutdown
	end
end