Mapped Files

From J Wiki
Jump to navigation Jump to search

Limitations of Mapped Files

The "Mapped Files" studio outlines the basic use of mapped files but fails to explicitly mention a number of limitations.

Size Should Not Change

The examples in the studio show how the file can be changed by manipulating the mapped variable but uses an example that does not change the size of the variable or the file. So, this works as advertised:

   '1234567' fappend flnm=. 'NewFile.txt'
   require 'jmf'
   JCHAR map_jmf_ 'var';flnm
   var=: 3|.var
   fread flnm
4567123

But this fails:

   var=: var,'8910'
|allocation error
|   var    =:var,'8910'

because it attempts to increase the size of the variable. Attempting to decrease the size does not give an error but the result may be unexpected:

   var=: 2}.var
   var
67123
   fread flnm
6712323

It looks like the new, shorter value of the variable simply overwrites the initial part of the file.

Similarly, changing the size of the file is not reflected in the mapped variable:

   'XXXX' fappend flnm
4
   var
23
   fread flnm
2312323XXXX

Once the variable is re-mapped to the file, it will reflect the changes:

   unmap_jmf_ 'var'
0
   JCHAR map_jmf_ 'var';flnm
   var
2312323XXXX

Temporarily Decreasing Size

A further complication is that the variable may be shortened, then lengthened back to the original size:

   var=: '12345',~5}.var
   var
23XXXX12345
   fread flnm
23XXXX12345

This does not have to be done all at once:

   var=: 3}.var
   var=: var,'foo'
   fread flnm
XXX12345foo