class Google::Cloud::Env::FileSystem
Ruby process.
large file, its contents will stay in memory for the lifetime of the
This class does not provide any controls for data size. If you read a
is, overrides are “all or nothing”.
If overrides are present, actual file system access is disabled; that
You can also “mock” the file system by providing a hash of overrides.
the file system.
system, caching data so that subsequent accesses do not need to reread
This is a simple class that reads the contents of objects in the file
Access to file system contents.
#
def initialize
Create a file system access object with no overrides.
#
def initialize @overrides = nil @cache = LazyDict.new do |path, binary| if binary File.binread path else File.read path end rescue IOError, SystemCallError nil end # This mutex protects the overrides variable. Its setting (i.e. # whether nil or an overrides hash) will not change within a # synchronize block. @mutex = Thread::Mutex.new end
def overrides= new_overrides
-
new_overrides(Hash{String => String}, nil) --
def overrides= new_overrides @mutex.synchronize do @overrides = new_overrides end end
def read path, binary: false
-
(nil)- if the file does not exist. -
(String)- if the file exists.
Parameters:
-
binary(boolean) -- Whether to read in binary mode. Defaults to -
path(String) -- The path to the file.
def read path, binary: false result = false @mutex.synchronize do result = @overrides[path] if @overrides end result = @cache.get(path, binary) if result == false if result && binary != (result.encoding == Encoding::ASCII_8BIT) raise IOError, "binary encoding flag mismatch" end result end
def with_overrides temp_overrides
-
temp_overrides(nil, Hash{String => String}) --
def with_overrides temp_overrides old_overrides = @overrides begin @mutex.synchronize do @overrides = temp_overrides end yield ensure @mutex.synchronize do @overrides = old_overrides end end end