Introducing the lazy file loader

Suppose that we are developing a file loading facility that supports lazy loading. By lazy, we are talking about not loading a file until the content is required. Let's take a look at the following code:

mutable struct FileContent
path
loaded
contents
end

The FileContent struct contains three fields:

  • path: The location of the file
  • loaded: A Boolean value that indicates whether the file has been loaded into memory
  • contents: A byte array that contains the contents of the file

Here's the constructor for the same struct:

function FileContent(path) 
ss = lstat(path)
return FileContent(path, false, zeros(UInt8, ss.size))
end

As with our current design, we pre-allocate memory for the file but we do not read the file content until later. The size of the file is determined by a call to the lstat function. When creating the FileContent object, we initialize the loaded field with a false value – an indication that the file has not been loaded into memory.

Eventually, we must load the file content, so we just provide a separate function that reads the file into the pre-allocated byte array:

function load_contents!(fc::FileContent)
open(fc.path) do io
readbytes!(io, fc.contents)
fc.loaded = true
end
nothing
end

Let's run a quick test to see how it works:

Here, we have just created a new FileContent object. Clearly, the loaded field contains a false value because we have not read the file yet. The content field is also full of zeros.

Let's load the file content now:

Now, the contents field contains some real data, and the loaded field has the value of true. Of course, we are just babysitting and running the code manually for now. The idea is to implement lazy loading. We need a way to intercept any read operation into the contents field so that the file content can be loaded just in time. Ideally, this should happen whenever someone uses the fc.contents expression. In order to hijack the call to get fc.contents, we must first understand how Julia's dot notation works. Let's take a detour and go over that now.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.191.22