filestore 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/filestore.rb +36 -16
- data/lib/log.rb +4 -4
- data/lib/memory_meta.rb +14 -12
- data/lib/meta_manager.rb +22 -10
- data/lib/multitenant_filestore.rb +48 -23
- data/test/testfile.txt +0 -0
- metadata +5 -3
data/lib/filestore.rb
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
#
|
|
2
2
|
# filestore.rb
|
|
3
|
-
#
|
|
4
|
-
#
|
|
5
|
-
#
|
|
3
|
+
#
|
|
4
|
+
# author: Thomas Stätter
|
|
5
|
+
# date: 2012/11/07
|
|
6
6
|
#
|
|
7
7
|
|
|
8
8
|
require 'meta_manager.rb'
|
|
@@ -40,8 +40,10 @@ module FileStore
|
|
|
40
40
|
attr_reader :metaManager, :rootPath, :storePath, :deletedPath, :rollbackPath, :metaFile
|
|
41
41
|
#
|
|
42
42
|
# Initializes a new instance of SimpleFileStore
|
|
43
|
-
#
|
|
44
|
-
#
|
|
43
|
+
#
|
|
44
|
+
# Arguments:
|
|
45
|
+
# metaManager: The meta data manager instance to be used by this store
|
|
46
|
+
# rootPath: The path where the file store resides
|
|
45
47
|
#
|
|
46
48
|
def initialize(metaManager, rootPath = '.')
|
|
47
49
|
raise FileStoreException, "Root path already locked" if SimpleFileStore.is_directory_locked?(rootPath)
|
|
@@ -70,10 +72,14 @@ module FileStore
|
|
|
70
72
|
end
|
|
71
73
|
#
|
|
72
74
|
# Adds a file to the store
|
|
73
|
-
#
|
|
74
|
-
#
|
|
75
|
-
#
|
|
76
|
-
#
|
|
75
|
+
#
|
|
76
|
+
# Arguments:
|
|
77
|
+
# file: The file to be stored
|
|
78
|
+
# meta: Optional meta data to be stored along with the physical file
|
|
79
|
+
# shouldMove: Determines wether to original file should be deleted
|
|
80
|
+
#
|
|
81
|
+
# Returns:
|
|
82
|
+
# The newly created ID for the file
|
|
77
83
|
#
|
|
78
84
|
def add(file, meta = {}, shouldMove = true)
|
|
79
85
|
raise FileStoreException, "File #{file} not found" if not File.exists?(file)
|
|
@@ -104,8 +110,12 @@ module FileStore
|
|
|
104
110
|
end
|
|
105
111
|
#
|
|
106
112
|
# Retrieves a file identified by it's ID
|
|
107
|
-
#
|
|
108
|
-
#
|
|
113
|
+
#
|
|
114
|
+
# Arguments:
|
|
115
|
+
# id: The files ID to retrieve
|
|
116
|
+
#
|
|
117
|
+
# Returns:
|
|
118
|
+
# A hash of file object (:path) and corresponding meta data (:data)
|
|
109
119
|
# representing the file in the store
|
|
110
120
|
#
|
|
111
121
|
def get(id)
|
|
@@ -121,7 +131,9 @@ module FileStore
|
|
|
121
131
|
end
|
|
122
132
|
#
|
|
123
133
|
# Moves a file from the current to the deleted store
|
|
124
|
-
#
|
|
134
|
+
#
|
|
135
|
+
# Arguments:
|
|
136
|
+
# id: The ID identifying the file to be moved
|
|
125
137
|
#
|
|
126
138
|
def remove(id)
|
|
127
139
|
raise FileStoreException, "No file ID given for removal" if id == '' or id.nil?
|
|
@@ -142,7 +154,9 @@ module FileStore
|
|
|
142
154
|
end
|
|
143
155
|
#
|
|
144
156
|
# Restores a file identified by it's id
|
|
145
|
-
#
|
|
157
|
+
#
|
|
158
|
+
# Arguments:
|
|
159
|
+
# id: The file ID
|
|
146
160
|
#
|
|
147
161
|
def restore(id)
|
|
148
162
|
raise FileStoreException, "No file ID given for restore" if id == '' or id.nil?
|
|
@@ -211,7 +225,9 @@ module FileStore
|
|
|
211
225
|
end
|
|
212
226
|
#
|
|
213
227
|
# Creates a new file ID
|
|
214
|
-
#
|
|
228
|
+
#
|
|
229
|
+
# Returns:
|
|
230
|
+
# A string representing the file's ID
|
|
215
231
|
#
|
|
216
232
|
def self.get_id(store)
|
|
217
233
|
for i in 0..2 do
|
|
@@ -240,7 +256,9 @@ module FileStore
|
|
|
240
256
|
end
|
|
241
257
|
#
|
|
242
258
|
# Setup for a new file store directory
|
|
243
|
-
#
|
|
259
|
+
#
|
|
260
|
+
# Arguments:
|
|
261
|
+
# store: The file store instance to set up
|
|
244
262
|
#
|
|
245
263
|
def self.create_store(store)
|
|
246
264
|
Logger.instance.logger.info "Trying to create store in #{store.storePath}"
|
|
@@ -272,7 +290,9 @@ module FileStore
|
|
|
272
290
|
end
|
|
273
291
|
#
|
|
274
292
|
# Recover an existing file store
|
|
275
|
-
#
|
|
293
|
+
#
|
|
294
|
+
# Arguments:
|
|
295
|
+
# store: The file store instance to recover
|
|
276
296
|
#
|
|
277
297
|
def self.recover_store(store)
|
|
278
298
|
# trying to recover existing file store
|
data/lib/log.rb
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
#
|
|
2
2
|
# log.rb
|
|
3
|
-
#
|
|
4
|
-
#
|
|
5
|
-
#
|
|
3
|
+
#
|
|
4
|
+
# author: Thomas Stätter
|
|
5
|
+
# date: 2012/11/07
|
|
6
6
|
#
|
|
7
7
|
require 'date'
|
|
8
8
|
require 'log4r'
|
|
@@ -10,7 +10,7 @@ require 'singleton'
|
|
|
10
10
|
|
|
11
11
|
module FileStore
|
|
12
12
|
#
|
|
13
|
-
#
|
|
13
|
+
# Singleton logging facility class
|
|
14
14
|
#
|
|
15
15
|
class Logger
|
|
16
16
|
include Singleton
|
data/lib/memory_meta.rb
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
#
|
|
2
2
|
# memory_meta.rb
|
|
3
|
-
#
|
|
4
|
-
#
|
|
5
|
-
#
|
|
3
|
+
#
|
|
4
|
+
# author: Thomas Stätter
|
|
5
|
+
# date: 2012/11/08
|
|
6
6
|
#
|
|
7
7
|
require 'yaml'
|
|
8
8
|
require 'filestore.rb'
|
|
@@ -15,17 +15,19 @@ module FileStore
|
|
|
15
15
|
#
|
|
16
16
|
class MemoryMetaManager < MetaManager
|
|
17
17
|
# Constant defining the default file path
|
|
18
|
-
|
|
18
|
+
FILE = 'meta.yaml'
|
|
19
19
|
# Accessor for the file to store data to
|
|
20
20
|
attr_reader :file
|
|
21
21
|
#
|
|
22
22
|
# Creates a new instance of MemoryMetaManager
|
|
23
|
-
#
|
|
23
|
+
#
|
|
24
|
+
# Arguments:
|
|
25
|
+
# persistentFile: The file where the manager class is persisted to
|
|
24
26
|
#
|
|
25
27
|
def initialize(persistentFile = '')
|
|
26
28
|
@data = Hash.new
|
|
27
29
|
@removed = Hash.new
|
|
28
|
-
@file = (persistentFile.nil? or persistentFile == '')?
|
|
30
|
+
@file = (persistentFile.nil? or persistentFile == '')? MemoryMetaManager::FILE : persistentFile
|
|
29
31
|
|
|
30
32
|
begin
|
|
31
33
|
if File.exists?(@file)
|
|
@@ -43,7 +45,7 @@ module FileStore
|
|
|
43
45
|
|
|
44
46
|
end
|
|
45
47
|
#
|
|
46
|
-
#
|
|
48
|
+
# see: MetaManager::get_data
|
|
47
49
|
#
|
|
48
50
|
def get_data(id)
|
|
49
51
|
raise FileStoreException, "No meta data available for ID #{id}" if not @data.key?(id)
|
|
@@ -51,7 +53,7 @@ module FileStore
|
|
|
51
53
|
return @data[id]
|
|
52
54
|
end
|
|
53
55
|
#
|
|
54
|
-
#
|
|
56
|
+
# see: MetaManager::add_or_update
|
|
55
57
|
#
|
|
56
58
|
def add_or_update(id, metaData)
|
|
57
59
|
raise FileStoreException, "Only hashsets can be added" if not metaData.is_a?(Hash)
|
|
@@ -60,7 +62,7 @@ module FileStore
|
|
|
60
62
|
@data[id] = (@data.key?(id) ? @data[id].merge!(metaData) : @data[id] = metaData)
|
|
61
63
|
end
|
|
62
64
|
#
|
|
63
|
-
#
|
|
65
|
+
# see: MetaManager::remove
|
|
64
66
|
#
|
|
65
67
|
def remove(id)
|
|
66
68
|
raise FileStoreException, "Only Strings can be used as keys" if not id.is_a?(String)
|
|
@@ -70,7 +72,7 @@ module FileStore
|
|
|
70
72
|
@data.delete(id)
|
|
71
73
|
end
|
|
72
74
|
#
|
|
73
|
-
#
|
|
75
|
+
# see: MetaManager::restore
|
|
74
76
|
#
|
|
75
77
|
def restore(id)
|
|
76
78
|
raise FileStoreException, "Only Strings can be used as keys" if not id.is_a?(String)
|
|
@@ -80,7 +82,7 @@ module FileStore
|
|
|
80
82
|
@removed.delete(id)
|
|
81
83
|
end
|
|
82
84
|
#
|
|
83
|
-
# see MetaManager::shutdown
|
|
85
|
+
# see: MetaManager::shutdown
|
|
84
86
|
#
|
|
85
87
|
def shutdown
|
|
86
88
|
begin
|
|
@@ -94,7 +96,7 @@ module FileStore
|
|
|
94
96
|
end
|
|
95
97
|
end
|
|
96
98
|
#
|
|
97
|
-
#
|
|
99
|
+
# see: MetaManager::has_id?
|
|
98
100
|
#
|
|
99
101
|
def has_id?(id)
|
|
100
102
|
@data.key?(id)
|
data/lib/meta_manager.rb
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
#
|
|
2
2
|
# meta_manager.rb
|
|
3
|
-
#
|
|
4
|
-
#
|
|
5
|
-
#
|
|
3
|
+
#
|
|
4
|
+
# author: Thomas Stätter
|
|
5
|
+
# date: 2012/11/08
|
|
6
6
|
#
|
|
7
7
|
|
|
8
8
|
module FileStore
|
|
@@ -13,27 +13,37 @@ module FileStore
|
|
|
13
13
|
class MetaManager
|
|
14
14
|
#
|
|
15
15
|
# Returns the data set identified by the given id
|
|
16
|
-
#
|
|
17
|
-
#
|
|
16
|
+
#
|
|
17
|
+
# Arguments:
|
|
18
|
+
# id: The ID to be looked for
|
|
19
|
+
#
|
|
20
|
+
# Returns:
|
|
21
|
+
# A hashset containing all stored meta data
|
|
18
22
|
#
|
|
19
23
|
def get_data(id)
|
|
20
24
|
end
|
|
21
25
|
#
|
|
22
26
|
# Removes a dataset from the collection
|
|
23
|
-
#
|
|
27
|
+
#
|
|
28
|
+
# Arguments:
|
|
29
|
+
# id: The key to identify the data to be deleted
|
|
24
30
|
#
|
|
25
31
|
def remove(id)
|
|
26
32
|
end
|
|
27
33
|
#
|
|
28
34
|
# Restores a previously deleted meta data set
|
|
29
|
-
#
|
|
35
|
+
#
|
|
36
|
+
# Arguments:
|
|
37
|
+
# id: The key to identify the data to be deleted
|
|
30
38
|
#
|
|
31
39
|
def restore(id)
|
|
32
40
|
end
|
|
33
41
|
#
|
|
34
42
|
# Adds/updates a dataset to/in the collection
|
|
35
|
-
#
|
|
36
|
-
#
|
|
43
|
+
#
|
|
44
|
+
# Arguments:
|
|
45
|
+
# id: The key to identify the data to be deleted
|
|
46
|
+
# metaData: The actual meta data to store
|
|
37
47
|
#
|
|
38
48
|
def add_or_update(id, metaData)
|
|
39
49
|
end
|
|
@@ -44,7 +54,9 @@ module FileStore
|
|
|
44
54
|
end
|
|
45
55
|
#
|
|
46
56
|
# Determines wether a given ID is already in use
|
|
47
|
-
#
|
|
57
|
+
#
|
|
58
|
+
# Arguments:
|
|
59
|
+
# id: The ID to be tested
|
|
48
60
|
#
|
|
49
61
|
def has_id?(id)
|
|
50
62
|
end
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
#
|
|
2
2
|
# multitenant_filestore.rb
|
|
3
|
-
#
|
|
4
|
-
#
|
|
5
|
-
#
|
|
3
|
+
#
|
|
4
|
+
# author: Thomas Stätter
|
|
5
|
+
# date: 2012/11/21
|
|
6
6
|
#
|
|
7
7
|
|
|
8
8
|
require 'filestore.rb'
|
|
@@ -32,7 +32,9 @@ module FileStore
|
|
|
32
32
|
#
|
|
33
33
|
# Sets the root path of the multitenant store. As FileStore::MultiTenantFileStore
|
|
34
34
|
# is a singleton class, this method must be used before any other
|
|
35
|
-
#
|
|
35
|
+
#
|
|
36
|
+
# Arguments:
|
|
37
|
+
# rootPath: The path to be used
|
|
36
38
|
#
|
|
37
39
|
def set_root_path(rootPath)
|
|
38
40
|
raise FileStoreException, "Root path #{rootPath} doesn't exist" if not File.exists?(rootPath)
|
|
@@ -42,16 +44,20 @@ module FileStore
|
|
|
42
44
|
end
|
|
43
45
|
#
|
|
44
46
|
# Creates a new file store for a tenant
|
|
45
|
-
#
|
|
47
|
+
#
|
|
48
|
+
# Arguments:
|
|
49
|
+
# id: The optional ID of the tenant. If omitted, an ID will be created
|
|
46
50
|
# automatically
|
|
47
|
-
#
|
|
51
|
+
#
|
|
52
|
+
# Returns:
|
|
53
|
+
# The tenants ID
|
|
48
54
|
#
|
|
49
55
|
def create_tenant_store(id = '')
|
|
50
56
|
id = UUIDTools::UUID.random_create.to_s if id == '' or id.nil?
|
|
51
57
|
|
|
52
58
|
begin
|
|
53
59
|
path = File.join(@rootPath, id)
|
|
54
|
-
FileUtils.mkdir path
|
|
60
|
+
FileUtils.mkdir path if not File.directory?(path)
|
|
55
61
|
mm = MemoryMetaManager.new(File.join(path, "meta.yaml"))
|
|
56
62
|
sfs = SimpleFileStore.new(mm, path)
|
|
57
63
|
|
|
@@ -64,7 +70,9 @@ module FileStore
|
|
|
64
70
|
end
|
|
65
71
|
#
|
|
66
72
|
# Permanently removes a tenant's store
|
|
67
|
-
#
|
|
73
|
+
#
|
|
74
|
+
# Arguments:
|
|
75
|
+
# id: The tenant's ID
|
|
68
76
|
#
|
|
69
77
|
def remove_tenant_store(id)
|
|
70
78
|
raise FileStoreException, "Tenant #{id} can't be removed. Not registered." if not @stores.key?(id)
|
|
@@ -78,8 +86,12 @@ module FileStore
|
|
|
78
86
|
end
|
|
79
87
|
#
|
|
80
88
|
# Returns the complete file store for a given tenant
|
|
81
|
-
#
|
|
82
|
-
#
|
|
89
|
+
#
|
|
90
|
+
# Arguments:
|
|
91
|
+
# id: The tenant's ID
|
|
92
|
+
#
|
|
93
|
+
# Returns:
|
|
94
|
+
# An instance of FileStore::SimpleFileStore
|
|
83
95
|
#
|
|
84
96
|
def get_tenant_store(id)
|
|
85
97
|
raise FileStoreException, "Tenant #{id} not registered. No file store given." if not @stores.key?(id)
|
|
@@ -88,30 +100,38 @@ module FileStore
|
|
|
88
100
|
end
|
|
89
101
|
#
|
|
90
102
|
# Adds a file to the tenant's store
|
|
91
|
-
#
|
|
92
|
-
#
|
|
93
|
-
#
|
|
103
|
+
#
|
|
104
|
+
# Arguments:
|
|
105
|
+
# tenant: The tenant's ID
|
|
106
|
+
# file: The file to be added
|
|
107
|
+
# md: Optional meta data
|
|
94
108
|
#
|
|
95
109
|
def add_to_tenant(tenant, file, md = {})
|
|
96
|
-
raise FileStoreException, "Tenant #{
|
|
110
|
+
raise FileStoreException, "Tenant #{tenant} not registered. File #{file} can't be added." if not @stores.key?(tenant)
|
|
97
111
|
|
|
98
112
|
@stores[tenant].add(file, md)
|
|
99
113
|
end
|
|
100
114
|
#
|
|
101
115
|
# Removes a file from the tenant's store
|
|
102
|
-
#
|
|
103
|
-
#
|
|
116
|
+
#
|
|
117
|
+
# Arguments:
|
|
118
|
+
# tenant: The tenant's ID
|
|
119
|
+
# id: The ID of the file to be removed
|
|
104
120
|
#
|
|
105
121
|
def remove_from_tenant(tenant, id)
|
|
106
|
-
raise FileStoreException, "Tenant #{
|
|
122
|
+
raise FileStoreException, "Tenant #{tenant} not registered. File with ID {id} can't be removed." if not @stores.key?(tenant)
|
|
107
123
|
|
|
108
124
|
@stores[tenant].remove(id)
|
|
109
125
|
end
|
|
110
126
|
#
|
|
111
127
|
# Retrieves a file from the tenant's store
|
|
112
|
-
#
|
|
113
|
-
#
|
|
114
|
-
#
|
|
128
|
+
#
|
|
129
|
+
# Arguments:
|
|
130
|
+
# tenant: The tenant's ID
|
|
131
|
+
# file: The file to be retrieved
|
|
132
|
+
#
|
|
133
|
+
# Returns:
|
|
134
|
+
# A hash containing the file object (:path) and the corresponding meta
|
|
115
135
|
# data (:data)
|
|
116
136
|
#
|
|
117
137
|
def get_from_tenant(tenant, id)
|
|
@@ -121,9 +141,12 @@ module FileStore
|
|
|
121
141
|
end
|
|
122
142
|
#
|
|
123
143
|
# Determines wether a tenant is registered
|
|
124
|
-
#
|
|
144
|
+
#
|
|
145
|
+
# Arguments:
|
|
146
|
+
# tenant: The tenant's ID to be tested
|
|
125
147
|
#
|
|
126
148
|
def has_tenant?(id)
|
|
149
|
+
return @stores.key?(id)
|
|
127
150
|
end
|
|
128
151
|
#
|
|
129
152
|
# Shuts down this multitenant store
|
|
@@ -138,7 +161,9 @@ module FileStore
|
|
|
138
161
|
private
|
|
139
162
|
#
|
|
140
163
|
# Recovers a multitenant store
|
|
141
|
-
#
|
|
164
|
+
#
|
|
165
|
+
# Arguments:
|
|
166
|
+
# rootPath: The base path of the multitenant store
|
|
142
167
|
#
|
|
143
168
|
def self.recover(rootPath)
|
|
144
169
|
raise FileStoreException, "Root path #{rootPath} isn't a valid multitenant store" if not File.directory?(rootPath)
|
|
@@ -149,7 +174,7 @@ module FileStore
|
|
|
149
174
|
begin
|
|
150
175
|
if File.directory?(e)
|
|
151
176
|
tenant = File.basename(e)
|
|
152
|
-
mm = MemoryMetaManager.new(File.join(e,
|
|
177
|
+
mm = MemoryMetaManager.new(File.join(e, MemoryMetaManager::FILE))
|
|
153
178
|
sfs = SimpleFileStore.new(mm, e)
|
|
154
179
|
|
|
155
180
|
stores[tenant] = sfs
|
data/test/testfile.txt
ADDED
|
File without changes
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: filestore
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.6
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -9,7 +9,7 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2012-11-
|
|
12
|
+
date: 2012-11-27 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: uuidtools
|
|
@@ -69,7 +69,8 @@ files:
|
|
|
69
69
|
- lib/multitenant_filestore.rb
|
|
70
70
|
- test/tc_filestore.rb
|
|
71
71
|
- test/tc_multitenant.rb
|
|
72
|
-
|
|
72
|
+
- test/testfile.txt
|
|
73
|
+
homepage: https://github.com/tstaetter/filestore-gem
|
|
73
74
|
licenses: []
|
|
74
75
|
post_install_message:
|
|
75
76
|
rdoc_options: []
|
|
@@ -97,3 +98,4 @@ summary: Simple file storage
|
|
|
97
98
|
test_files:
|
|
98
99
|
- test/tc_filestore.rb
|
|
99
100
|
- test/tc_multitenant.rb
|
|
101
|
+
- test/testfile.txt
|