Files are read from the filesystem in chunks of Bytes
that are configurable and defaults to GridFS.DEFAULT_CHUNKSIZE
size.
Provided Bytes
internally recycles array of bytes read. The reading of the files is always based on first listing (or querying)
the files in filesystem and then either applying the read operation for all results or only for the first result returned.
Reading single file provides syntax sugar that will result in Process[Task,Bytes]
signature once applied.
Please see examples below:
list withId (fileId) and readFile()
list withId (fileId) and readFile(1024)
list withId (fileId) and readFile()
All the above examples can be later applied to gridfs by following syntax :
filesystem(fileDb) through (list withId (fileId) and readFile()): Process[Task, Bytes]
When reading over multiple files, instead of directly returning the process that reads all files, Mongo Streams return
tuple of (MongoReadFile,Process[Task,Byte])
to eventually separate read content of files from each other.
Following example reads the files starting their name with a
and concatenates their content
(splits chunk reads by new line), that will be separated with name of the file :
def readFiles =
(list files ("filename" regex "a.*") foreach (readFile())) flatMapProcess {
case (file, reader) =>
Process.eval(Task.now("content of: " + file.name + "\n")) ++ reader.map(bytes => new String(bytes.toArray))
}
val output: Seq[String] = (filesystem(fileDb) through readFiles).runLog.run
Total for specification FSReadSpec | |
---|---|
Finished in | 1 second, 980 ms |
Results | 6 examples, 0 failure, 0 error |