disk-bytes-0.1.0.0: On-disk storage, but referentially transparent
Safe HaskellSafe-Inferred
LanguageHaskell2010

System.Mem.Disk

Synopsis

Synopsis

This module introduces a data type DiskBytes which represents a sequence of bytes. However, unlike most Haskell data types, values of this type are not stored in volatile RAM, but on the hard disk (e.g. magnetic or SSD). But just like ordinary Haskell data types, this type is referentially transparent — no IO is needed to access the disk!

The purpose of this data type is to allow you to make trade-offs between RAM (scarce but fast) and disk (plenty but slow) simply by switching between the ByteString and DiskBytes types — while keeping the Haskell code pure. In order to test whether this worked out in your favor, you can use the withDiskMemory function.

The on-disk storage space is represented by the Disk type. Typically, this storage is managed through a file on the file system, e.g. using withDiskSqlite.

DiskBytes

data DiskBytes Source #

A sequence of bytes that is stored on disk — if and only if the value is evaluated to WHNF.

The value is subject to normal garbage collection: When the value is no longer referenced, the disk memory will be freed (eventually).

For estimating the memory cost: Even though the bulk of the data is kept on disk, each WHNF of DiskBytes occupies roughly ~100 bytes of RAM; this is due to administrative overhead like weak pointers and finalizers.

toDiskBytes :: Disk -> ByteString -> DiskBytes Source #

Offload a sequence of bytes onto a Disk.

NOTE: The result must be evaluated to WHNF before the data is actually on disk! Also, the original ByteString needs to be garbage collected in for its RAM to become free.

fromDiskBytes :: DiskBytes -> ByteString Source #

Read the sequence of bytes back into RAM.

Disk storage space

data Disk Source #

Represents the on-disk storage where DiskBytes are stored.

getDiskSize :: Disk -> IO Integer Source #

Rough estimate of the current size of the Disk, in bytes.

withDiskSqlite :: FilePath -> (Disk -> IO a) -> IO a Source #

Create a new file and use it for storing DiskBytes.

Throw an error if the file already exists, delete the file after use.

withDiskMemory :: FilePath -> (Disk -> IO a) -> IO a Source #

Create a Disk in memory for the purpose of testing and profiling — by swapping withDiskMemory for withDiskSqlite and looking at the heap profile of your program, you can quickly find out whether the use of DiskBytes really helps.

Ignores the FilePath argument.