# Data Compression in C++

> Please refer to the Data Compression page for a discussion of the advantages of in-memory and persistent database compression.

> ## In-Memory Database Compression

> For C++ in-memory database applications the APIs for managing data compression are exactly as for C applications. Please see the C API Data Compression page for details.

> ## Persistent Database Compression

> The method for enabling compression for a persistent database is to set the `MCO_FILE_OPEN_COMPRESSED` flag on the database file device. This is done when defining the memory devices in the McoSqlOpenParameters passed to `McoSqlEngine.open()` as shown in the following code snippet:

>         const int n_devices = 4;

>         int main( int argc, char ** argv )
>         {
>             mco_device_t dev[n_devices];
>             McoSqlEngine engine;
>             McoSqlOpenParameters params;

>             dev[0].type       = MCO_MEMORY_CONV;
>             dev[0].assignment = MCO_MEMORY_ASSIGN_DATABASE;
>             dev[0].size       = DATABASE_SIZE;
>             dev[0].dev.conv.ptr = (void*)malloc( DATABASE_SIZE );

>             dev[1].type       = MCO_MEMORY_CONV;
>             dev[1].assignment = MCO_MEMORY_ASSIGN_CACHE;
>             dev[1].size       = CACHE_SIZE;
>             dev[1].dev.conv.ptr = (void*)malloc( CACHE_SIZE );

>             dev[2].type       = MCO_MEMORY_FILE;
>             dev[2].assignment = MCO_MEMORY_ASSIGN_PERSISTENT;
>             strcpy(dev[2].dev.file.name, "persondb.dbs");
>             dev[2].dev.file.flags = MCO_FILE_OPEN_COMPRESSED;

>             dev[3].type       = MCO_MEMORY_FILE;
>             dev[3].assignment = MCO_MEMORY_ASSIGN_LOG;
>             strcpy(dev[3].dev.file.name, "persondb.log");
>             dev[3].dev.file.flags = MCO_FILE_OPEN_DEFAULT;

>             params.databaseName = (char *)db_name;
>             params.dictionary = persondb_get_dictionary();
>             params.mainMemoryDatabaseSize = DATABASE_SIZE;
>             params.mainMemoryPageSize = MEMORY_PAGE_SIZE;
>             params.n_devices = n_devices;
>             params.devices = dev;
>             params.compressionLevel = 9;
>             params.diskDatabaseMaxSize = 100*1024*1024;

>             engine.open(params);
>             ...
>         }

> Note that eXtremeDB implements LZ compression in two special file system libraries: `mcofu98zip` and `mcofu98ziplog` which are only available on Unix systems like Linux, MacOS and Solaris.

> ## Data Compression for IoT Communications

> For C++ IoT applications, the APIs for managing data compression in IoT communications are exactly as for C applications. Please see the C API Data Compression page for details.