Class: Dis::Layer
Overview
Dis Layer
Represents a layer of storage. Wraps a Fog::Storage connection; any provider supported by Fog should be usable.
Instance Attribute Summary collapse
-
#connection ⇒ Fog::Storage
readonly
The underlying Fog connection.
Instance Method Summary collapse
-
#cache? ⇒ Boolean
Returns true if the layer is a cache layer.
-
#cached_files ⇒ Array<Hash>
Returns cached file entries sorted by mtime ascending (oldest first).
-
#delayed? ⇒ Boolean
Returns true if the layer is a delayed layer.
-
#delete(type, key) ⇒ Boolean
Deletes a file from the store.
-
#existing(type, keys) ⇒ Array<String>
Returns all the given keys that exist in the layer.
-
#exists?(type, key) ⇒ Boolean
Returns true if an object with the given key exists.
-
#file_path(type, key) ⇒ String?
Returns the absolute file path for a locally stored file, or nil if the provider is not local or the file does not exist.
-
#get(type, key) ⇒ Fog::Model?
Retrieves a file from the store.
-
#immediate? ⇒ Boolean
Returns true if the layer isn’t a delayed layer.
-
#initialize(connection, options = {}) ⇒ Layer
constructor
A new instance of Layer.
-
#max_size ⇒ Integer?
Returns the cache size limit in bytes, or nil if not a cache.
-
#name ⇒ String
Returns a name for the layer.
-
#public? ⇒ Boolean
Returns true if the layer is public.
-
#readonly? ⇒ Boolean
Returns true if the layer is read only.
-
#size ⇒ Integer
Returns the total size in bytes of all files stored locally.
-
#store(type, key, file) ⇒ Fog::Model
Stores a file.
-
#stored_keys(type) ⇒ Array<String>
Returns all content hashes stored under the given type.
-
#writeable? ⇒ Boolean
Returns true if the layer is writeable.
Methods included from Logging
Constructor Details
#initialize(connection, options = {}) ⇒ Layer
Returns a new instance of Layer.
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/dis/layer.rb', line 47 def initialize(connection, = {}) = .merge() @connection = connection @delayed = [:delayed] @readonly = [:readonly] @public = [:public] @path = [:path] @cache = [:cache] end |
Instance Attribute Details
#connection ⇒ Fog::Storage (readonly)
Returns the underlying Fog connection.
32 33 34 |
# File 'lib/dis/layer.rb', line 32 def connection @connection end |
Instance Method Details
#cache? ⇒ Boolean
Returns true if the layer is a cache layer.
96 97 98 |
# File 'lib/dis/layer.rb', line 96 def cache? !!@cache end |
#cached_files ⇒ Array<Hash>
Returns cached file entries sorted by mtime ascending (oldest first).
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/dis/layer.rb', line 126 def cached_files return [] unless connection.respond_to?(:local_root) root = local_root_path return [] unless root.exist? entries = root.glob("**/*").select(&:file?) entries.filter_map { |f| cached_file_entry(f, root) } .sort_by { |e| e[:mtime] } end |
#delayed? ⇒ Boolean
Returns true if the layer is a delayed layer.
61 62 63 |
# File 'lib/dis/layer.rb', line 61 def delayed? @delayed end |
#delete(type, key) ⇒ Boolean
Deletes a file from the store.
244 245 246 247 248 249 250 |
# File 'lib/dis/layer.rb', line 244 def delete(type, key) raise Dis::Errors::ReadOnlyError if readonly? debug_log("Delete #{type}/#{key} from #{name}") do delete!(type, key) end end |
#existing(type, keys) ⇒ Array<String>
Returns all the given keys that exist in the layer.
163 164 165 166 167 168 169 170 |
# File 'lib/dis/layer.rb', line 163 def existing(type, keys) return [] if keys.empty? futures = keys.map do |key| Concurrent::Promises.future { key if exists?(type, key) } end futures.filter_map(&:value!) end |
#exists?(type, key) ⇒ Boolean
Returns true if an object with the given key exists.
196 197 198 199 200 201 202 |
# File 'lib/dis/layer.rb', line 196 def exists?(type, key) if directory(type, key)&.files&.head(key_component(type, key)) true else false end end |
#file_path(type, key) ⇒ String?
Returns the absolute file path for a locally stored file, or nil if the provider is not local or the file does not exist.
226 227 228 229 230 231 232 233 234 235 |
# File 'lib/dis/layer.rb', line 226 def file_path(type, key) return unless connection.respond_to?(:local_root) return unless exists?(type, key) File.join( connection.local_root, directory_component(type, key), key_component(type, key) ) end |
#get(type, key) ⇒ Fog::Model?
Retrieves a file from the store.
209 210 211 212 213 214 215 216 217 218 |
# File 'lib/dis/layer.rb', line 209 def get(type, key) dir = directory(type, key) return unless dir result = debug_log("Get #{type}/#{key} from #{name}") do dir.files.get(key_component(type, key)) end touch_file(type, key) if result && cache? result end |
#immediate? ⇒ Boolean
Returns true if the layer isn’t a delayed layer.
68 69 70 |
# File 'lib/dis/layer.rb', line 68 def immediate? !delayed? end |
#max_size ⇒ Integer?
Returns the cache size limit in bytes, or nil if not a cache.
103 104 105 |
# File 'lib/dis/layer.rb', line 103 def max_size @cache if cache? end |
#name ⇒ String
Returns a name for the layer.
258 259 260 |
# File 'lib/dis/layer.rb', line 258 def name "#{connection.class.name}/#{path}" end |
#public? ⇒ Boolean
Returns true if the layer is public.
75 76 77 |
# File 'lib/dis/layer.rb', line 75 def public? @public end |
#readonly? ⇒ Boolean
Returns true if the layer is read only.
82 83 84 |
# File 'lib/dis/layer.rb', line 82 def readonly? @readonly end |
#size ⇒ Integer
Returns the total size in bytes of all files stored locally. Returns 0 for non-local providers.
111 112 113 114 115 116 117 118 |
# File 'lib/dis/layer.rb', line 111 def size return 0 unless connection.respond_to?(:local_root) root = local_root_path return 0 unless root.exist? root.glob("**/*").sum { |f| f.file? ? f.size : 0 } end |
#store(type, key, file) ⇒ Fog::Model
Stores a file. The key must be a hex digest of the file content. If an object with the supplied hash already exists, no action will be performed.
150 151 152 153 154 155 156 |
# File 'lib/dis/layer.rb', line 150 def store(type, key, file) raise Dis::Errors::ReadOnlyError if readonly? debug_log("Store #{type}/#{key} to #{name}") do store!(type, key, file) end end |
#stored_keys(type) ⇒ Array<String>
Returns all content hashes stored under the given type.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/dis/layer.rb', line 176 def stored_keys(type) dir = connection.directories.get(path || "") return [] unless dir prefix = "#{type}/" dir.files.filter_map do |file| next unless file.key.start_with?(prefix) parts = file.key.delete_prefix(prefix).split("/") next unless parts.length == 2 "#{parts[0]}#{parts[1]}" end end |
#writeable? ⇒ Boolean
Returns true if the layer is writeable.
89 90 91 |
# File 'lib/dis/layer.rb', line 89 def writeable? !readonly? end |