---
name: "mtbl_writer"
section: "3"
platform: "linux"
package: "libmtbl-dev"
version: "1.6.1-1+b1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/mtbl_writer"
---

# mtbl\_writer(3)

create an MTBL file

## NAME

mtbl\_writer - create an MTBL file

## SYNOPSIS

**#include \<mtbl.h\>**

Writer objects:

```
struct mtbl_writer *
mtbl_writer_init(const char *fname, const struct mtbl_writer_options *wopt);
```

```
struct mtbl_writer *
mtbl_writer_init_fd(int fd, const struct mtbl_writer_options *wopt);
```

```
void
mtbl_writer_destroy(struct mtbl_writer **w);
```

```
mtbl_res
mtbl_writer_add(struct mtbl_writer *w,
        const uint8_t *key, size_t len_key,
        const uint8_t *val, size_t len_val);
```

Writer options:

```
struct mtbl_writer_options *
mtbl_writer_options_init(void);
```

```
void
mtbl_writer_options_destroy(struct mtbl_writer_options **wopt);
```

```
void
mtbl_writer_options_set_compression(
        struct mtbl_writer_options *wopt,
        mtbl_compression_type compression_type);
```

```
void
mtbl_writer_options_set_compression_level(
        struct mtbl_writer_options *wopt,
        int compression_level);
```

```
void
mtbl_writer_options_set_block_size(
        struct mtbl_writer_options *wopt,
        size_t block_size);
```

```
void
mtbl_writer_options_set_block_restart_interval(
        struct mtbl_writer_options *wopt,
        size_t block_restart_interval);
```

## DESCRIPTION

MTBL files are written to disk by creating an **mtbl\_writer** object, calling **mtbl\_writer\_add**() for each key-value entry, and then calling **mtbl\_writer\_destroy**().

**mtbl\_writer\_add**() copies key-value pairs from the caller into the **mtbl\_writer** object. Keys are specified as a pointer to a buffer, *key*, and the length of that buffer, *len\_key*. Values are specified as a pointer to a buffer, *val*, and the length of that buffer, *len\_val*.

Keys must be in sorted, lexicographical byte order. The same key may not be added to an **mtbl\_writer** more than once. If the input entries are not sorted or may contain duplicate keys, then the [**mtbl\_sorter**(3)](/mtbl_sorter/3.md) interface should be used instead.

**mtbl\_writer** objects may be created by calling **mtbl\_writer\_init**() with an *fname* argument specifying a filename to be created. The filename must not already exist on the filesystem. Or, **mtbl\_writer\_init\_fd**() may be called with an *fd* argument specifying an open, writable file descriptor. Prior to the call, moving *fd*'s cursor via [**write**(2)](/write/2.md) or [**lseek**(2)](/lseek/2.md) will have the effect of reserving the file’s initial bytes for non-MTBL use. MTBL will only write at, or above the current offset. Thereafter, only **mtbl\_writer** may access this *fd* (including closing it), and no other writes may be made to the underlying file above the first offset of the MTBL data. The MTBL data must be last in the file.

If the *wopt* parameter to **mtbl\_writer\_init**() or **mtbl\_writer\_init\_fd**() is non-NULL, the parameters specified in the **mtbl\_writer\_options** object will be configured into the **mtbl\_writer** object.

### Writer options

**compression**

Specifies the compression algorithm to use on data blocks. Possible values are **MTBL\_COMPRESSION\_NONE**, **MTBL\_COMPRESSION\_SNAPPY**, **MTBL\_COMPRESSION\_LZ4**, **MTBL\_COMPRESSION\_LZ4HC**, **MTBL\_COMPRESSION\_ZSTD**, or **MTBL\_COMPRESSION\_ZLIB** (the default).

**compression\_level**

Specifies the compression level to use on data blocks, if the selected compression algorithm supports different compression levels. The compression algorithms which support different compression levels are **MTBL\_COMPRESSION\_LZ4HC** (0-16), **MTBL\_COMPRESSION\_ZSTD** (1-22), and **MTBL\_COMPRESSION\_ZLIB** (0-9). Other compression algorithms ignore this option, and compression levels outside the ranges supported by the algorithm will be silently clamped to the minimum or maximum supported level. If not specified, a reasonable default will be used.

**block\_size**

The maximum size of uncompressed data blocks, specified in bytes. The default is 8 kilobytes.

**block\_restart\_interval**

How frequently to restart intra-block key prefix compression. The default is every 16 keys.

## RETURN VALUE

**mtbl\_writer\_init**() and **mtbl\_writer\_init\_fd**() return NULL on failure, and non-NULL on success.

**mtbl\_writer\_add**() returns **mtbl\_res\_success** if the key-value entry was successfully copied into the **mtbl\_writer** object, and **mtbl\_res\_failure** if not, for instance if there has been a key-ordering violation.
