Reading files in filesystem

Reading files in filesystem

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

Reading single file provides syntax sugar that will result in Process[Task,Bytes] signature once applied.
Please see examples below:

  • Reading single file with default buffer size :
 list withId (fileId) and readFile()
  • Reading single file with custom buffer size :
 list withId (fileId) and readFile(1024)
  • Reading first file from list of returned files :
 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]

Reading multiple files

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

Additional verifications

 * Large file is correctly split into chunks
 * Empty file is correctly read
 * Reads multiple files

Total for specification FSReadSpec
Finished in1 second, 980 ms
Results6 examples, 0 failure, 0 error