kusano 7d535a
# LZ4 Streaming API Example : Double Buffer
kusano 7d535a
by *Takayuki Matsuoka*
kusano 7d535a
kusano 7d535a
`blockStreaming_doubleBuffer.c` is LZ4 Straming API example which implements double buffer (de)compression.
kusano 7d535a
kusano 7d535a
Please note :
kusano 7d535a
kusano 7d535a
 - Firstly, read "LZ4 Streaming API Basics".
kusano 7d535a
 - This is relatively advanced application example.
kusano 7d535a
 - Output file is not compatible with lz4frame and platform dependent.
kusano 7d535a
kusano 7d535a
kusano 7d535a
## What's the point of this example ?
kusano 7d535a
kusano 7d535a
 - Handle huge file in small amount of memory
kusano 7d535a
 - Always better compression ratio than Block API
kusano 7d535a
 - Uniform block size
kusano 7d535a
kusano 7d535a
kusano 7d535a
## How the compression works
kusano 7d535a
kusano 7d535a
First of all, allocate "Double Buffer" for input and LZ4 compressed data buffer for output.
kusano 7d535a
Double buffer has two pages, "first" page (Page#1) and "second" page (Page#2).
kusano 7d535a
kusano 7d535a
```
kusano 7d535a
        Double Buffer
kusano 7d535a
kusano 7d535a
      Page#1    Page#2
kusano 7d535a
    +---------+---------+
kusano 7d535a
    | Block#1 |         |
kusano 7d535a
    +----+----+---------+
kusano 7d535a
         |
kusano 7d535a
         v
kusano 7d535a
      {Out#1}
kusano 7d535a
kusano 7d535a
kusano 7d535a
      Prefix Dependency
kusano 7d535a
         +---------+
kusano 7d535a
         |         |
kusano 7d535a
         v         |
kusano 7d535a
    +---------+----+----+
kusano 7d535a
    | Block#1 | Block#2 |
kusano 7d535a
    +---------+----+----+
kusano 7d535a
                   |
kusano 7d535a
                   v
kusano 7d535a
                {Out#2}
kusano 7d535a
kusano 7d535a
kusano 7d535a
   External Dictionary Mode
kusano 7d535a
         +---------+
kusano 7d535a
         |         |
kusano 7d535a
         |         v
kusano 7d535a
    +----+----+---------+
kusano 7d535a
    | Block#3 | Block#2 |
kusano 7d535a
    +----+----+---------+
kusano 7d535a
         |
kusano 7d535a
         v
kusano 7d535a
      {Out#3}
kusano 7d535a
kusano 7d535a
kusano 7d535a
      Prefix Dependency
kusano 7d535a
         +---------+
kusano 7d535a
         |         |
kusano 7d535a
         v         |
kusano 7d535a
    +---------+----+----+
kusano 7d535a
    | Block#3 | Block#4 |
kusano 7d535a
    +---------+----+----+
kusano 7d535a
                   |
kusano 7d535a
                   v
kusano 7d535a
                {Out#4}
kusano 7d535a
```
kusano 7d535a
kusano 7d535a
Next, read first block to double buffer's first page. And compress it by `LZ4_compress_continue()`.
kusano 7d535a
For the first time, LZ4 doesn't know any previous dependencies,
kusano 7d535a
so it just compress the line without dependencies and generates compressed block {Out#1} to LZ4 compressed data buffer.
kusano 7d535a
After that, write {Out#1} to the file.
kusano 7d535a
kusano 7d535a
Next, read second block to double buffer's second page. And compress it.
kusano 7d535a
In this time, LZ4 can use dependency to Block#1 to improve compression ratio.
kusano 7d535a
This dependency is called "Prefix mode".
kusano 7d535a
kusano 7d535a
Next, read third block to double buffer's *first* page. And compress it.
kusano 7d535a
Also this time, LZ4 can use dependency to Block#2.
kusano 7d535a
This dependency is called "External Dictonaly mode".
kusano 7d535a
kusano 7d535a
Continue these procedure to the end of the file.
kusano 7d535a
kusano 7d535a
kusano 7d535a
## How the decompression works
kusano 7d535a
kusano 7d535a
Decompression will do reverse order.
kusano 7d535a
kusano 7d535a
 - Read first compressed block.
kusano 7d535a
 - Decompress it to the first page and write that page to the file.
kusano 7d535a
 - Read second compressed block.
kusano 7d535a
 - Decompress it to the second page and write that page to the file.
kusano 7d535a
 - Read third compressed block.
kusano 7d535a
 - Decompress it to the *first* page and write that page to the file.
kusano 7d535a
kusano 7d535a
Continue these procedure to the end of the compressed file.