Remove places where document is read into memory
There is at least one place where metadata is read into memory. This is done when the doc needs to be written to disk.
This needs to be changed to a buffer. Any other places should be changed as well.