Class: Lutaml::Store::Adapter::FileSystem
- Inherits:
-
Base
- Object
- Base
- Lutaml::Store::Adapter::FileSystem
show all
- Defined in:
- lib/lutaml/store/adapter/filesystem.rb
Constant Summary
collapse
- DEFAULT_EXTENSION =
".data"
- METADATA_EXTENSION =
".meta"
Instance Method Summary
collapse
Methods inherited from Base
#bulk_delete, #bulk_get, #bulk_set
Constructor Details
#initialize(config = {}) ⇒ FileSystem
Returns a new instance of FileSystem.
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/lutaml/store/adapter/filesystem.rb', line 14
def initialize(config = {})
super
@root_path = @config[:path] || raise(ConfigurationError, "FileSystem adapter requires :path config")
@extension = @config[:extension] || DEFAULT_EXTENSION
@create_directories = @config.fetch(:create_directories, true)
@integrity_enabled = @config.fetch(:integrity_checks, true)
@integrity_algorithm = @config.fetch(:integrity_algorithm, "sha256")
setup_directory_structure
end
|
Instance Method Details
#all ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/lutaml/store/adapter/filesystem.rb', line 81
def all
result = {}
Dir.glob(File.join(@root_path, "**", "*#{@extension}")).each do |file_path|
next unless File.file?(file_path)
key = key_from_path(file_path)
value = get(key)
result[key] = value if value
end
result
end
|
#clear ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/lutaml/store/adapter/filesystem.rb', line 95
def clear
return 0 unless Dir.exist?(@root_path)
count = 0
Dir.glob(File.join(@root_path, "**", "*#{@extension}")).each do |file_path|
if File.file?(file_path)
File.delete(file_path)
count += 1
end
end
Dir.glob(File.join(@root_path, "**", "*#{METADATA_EXTENSION}")).each do |file_path|
File.delete(file_path) if File.file?(file_path)
end
cleanup_empty_directories(@root_path, preserve_root: true)
count
end
|
#close ⇒ Object
121
|
# File 'lib/lutaml/store/adapter/filesystem.rb', line 121
def close; end
|
#delete(key) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/lutaml/store/adapter/filesystem.rb', line 54
def delete(key)
file_path = path_for_key(key)
metadata_path = metadata_path_for_key(key)
return nil unless File.exist?(file_path)
value = file_safe_read(file_path)
File.delete(file_path)
File.delete(metadata_path) if File.exist?(metadata_path)
cleanup_empty_directories(File.dirname(file_path))
value
end
|
#exists?(key) ⇒ Boolean
69
70
71
|
# File 'lib/lutaml/store/adapter/filesystem.rb', line 69
def exists?(key)
File.exist?(path_for_key(key))
end
|
#get(key) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/lutaml/store/adapter/filesystem.rb', line 25
def get(key)
file_path = path_for_key(key)
return nil unless File.exist?(file_path)
data = file_safe_read(file_path)
metadata = read_metadata(key)
begin
verify_data_integrity(data, metadata)
rescue Integrity::IntegrityError => e
repaired_data = repair_corruption(key)
return repaired_data if repaired_data
raise e
end
data
end
|
#keys ⇒ Object
73
74
75
76
77
78
79
|
# File 'lib/lutaml/store/adapter/filesystem.rb', line 73
def keys
return [] unless Dir.exist?(@root_path)
Dir.glob(File.join(@root_path, "**", "*#{@extension}")).filter_map do |file_path|
key_from_path(file_path) if File.file?(file_path)
end
end
|
#repair_corruption(key, backup_data = nil) ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/lutaml/store/adapter/filesystem.rb', line 139
def repair_corruption(key, backup_data = nil)
file_path = path_for_key(key)
return nil unless File.exist?(file_path)
corrupted_data = file_safe_read(file_path)
repaired_data = Integrity.repair_data(corrupted_data, backup_data)
if Integrity.valid_data?(repaired_data)
set(key, repaired_data)
return repaired_data
end
nil
end
|
#set(key, value) ⇒ Object
44
45
46
47
48
49
50
51
52
|
# File 'lib/lutaml/store/adapter/filesystem.rb', line 44
def set(key, value)
file_path = path_for_key(key)
ensure_directory_exists(File.dirname(file_path))
full_metadata = wrap_with_integrity(value)
file_safe_write(file_path, value)
write_metadata(key, full_metadata) if full_metadata.any?
value
end
|
#size ⇒ Object
115
116
117
118
119
|
# File 'lib/lutaml/store/adapter/filesystem.rb', line 115
def size
return 0 unless Dir.exist?(@root_path)
Dir.glob(File.join(@root_path, "**", "*#{@extension}")).count { |path| File.file?(path) }
end
|
#stats ⇒ Object
154
155
156
157
158
159
160
|
# File 'lib/lutaml/store/adapter/filesystem.rb', line 154
def stats
super.merge(
root_path: @root_path,
extension: @extension,
disk_usage: calculate_disk_usage
)
end
|
#verify_integrity ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/lutaml/store/adapter/filesystem.rb', line 123
def verify_integrity
corrupted_keys = []
keys.each do |key|
get(key)
rescue Integrity::IntegrityError
corrupted_keys << key
end
{
total_keys: keys.size,
corrupted_keys: corrupted_keys,
integrity_ok: corrupted_keys.empty?
}
end
|