---
layout: manual
Content-Style: 'text/css'
title: _MP(PMEMKV_ITERATOR, 3)
collection: libpmemkv
header: PMEMKV_ITERATOR
secondary_title: pmemkv
...

[comment]: <> (SPDX-License-Identifier: BSD-3-Clause)
[comment]: <> (Copyright 2020-2021, Intel Corporation)

[comment]: <> (libpmemkv_iterator.3 -- man page for libpmemkv iterators API)

[NAME](#name)<br />
[SYNOPSIS](#synopsis)<br />
[DESCRIPTION](#description)<br />
[ERRORS](#errors)<br />
[EXAMPLE](#example)<br />
[SEE ALSO](#see-also)<br />


# NAME #

**pmemkv_iterator** - Iterator API for libpmemkv

This API is EXPERIMENTAL and might change.

# SYNOPSIS #

```c
#include <libpmemkv.h>

int pmemkv_iterator_new(pmemkv_db *db, pmemkv_iterator **it);
int pmemkv_write_iterator_new(pmemkv_db *db, pmemkv_write_iterator **it);

void pmemkv_iterator_delete(pmemkv_iterator *it);
void pmemkv_write_iterator_delete(pmemkv_write_iterator *it);

int pmemkv_iterator_seek(pmemkv_iterator *it, const char *k, size_t kb);
int pmemkv_iterator_seek_lower(pmemkv_iterator *it, const char *k, size_t kb);
int pmemkv_iterator_seek_lower_eq(pmemkv_iterator *it, const char *k, size_t kb);
int pmemkv_iterator_seek_higher(pmemkv_iterator *it, const char *k, size_t kb);
int pmemkv_iterator_seek_higher_eq(pmemkv_iterator *it, const char *k, size_t kb);

int pmemkv_iterator_seek_to_first(pmemkv_iterator *it);
int pmemkv_iterator_seek_to_last(pmemkv_iterator *it);

int pmemkv_iterator_is_next(pmemkv_iterator *it);
int pmemkv_iterator_next(pmemkv_iterator *it);
int pmemkv_iterator_prev(pmemkv_iterator *it);

int pmemkv_iterator_key(pmemkv_iterator *it, const char **k, size_t *kb);

int pmemkv_iterator_read_range(pmemkv_iterator *it, size_t pos, size_t n,
			       const char **data, size_t *rb);
int pmemkv_write_iterator_write_range(pmemkv_write_iterator *it, size_t pos, size_t n,
				      char **data, size_t *wb);

int pmemkv_write_iterator_commit(pmemkv_write_iterator *it);
void pmemkv_write_iterator_abort(pmemkv_write_iterator *it);
```

For general description of pmemkv and available engines see **libpmemkv**(7).
For description of pmemkv core API see **libpmemkv**(3).

# DESCRIPTION #

Iterators provide methods to iterate over records in db.

Both iterator types (pmemkv_iterator (read) and pmemkv_write_iterator) allow reading record's
key and value. To use pmemkv_write_iterator as a pmemkv_iterator you need to get its member "iter" (write_it->iter).

Example of calling pmemkv_iterator_seek_to_first() with both iterator types.
```c
// read_it is already created pmemkv_iterator
int status = pmemkv_iterator_seek_to_first(read_it);

// write_it is already created pmemkv_write_iterator
int status = pmemkv_iterator_seek_to_first(write_it->iter);
```

A pmemkv_write_iterator additionally can modify record's value transactionally.

Some of the functions are not guaranteed to be implemented by all engines.
If an engine does not support a certain function, it will return PMEMKV\_STATUS\_NOT\_SUPPORTED.

Holding simultaneously in the same thread more than one iterator is undefined behavior.

`int pmemkv_iterator_new(pmemkv_db *db, pmemkv_iterator **it);`
:	Creates a new pmemkv_iterator instance and stores a pointer to it in `*it`.

`int pmemkv_write_iterator_new(pmemkv_db *db, pmemkv_write_iterator **it);`
:	Creates a new pmemkv_write_iterator instance and stores a pointer to it in `*it`.

`void pmemkv_iterator_delete(pmemkv_iterator *it);`
:	Deletes pmemkv_iterator.

`void pmemkv_write_iterator_delete(pmemkv_write_iterator *it);`
:	Deletes pmemkv_write_iterator

`int pmemkv_iterator_seek(pmemkv_iterator *it, const char *k, size_t kb);`
:	Changes iterator position to the record with given key `k` of length `kb`.
	If the record is present and no errors occurred, returns PMEMKV_STATUS_OK.
	If the record does not exist, PMEMKV_STATUS_NOT_FOUND is returned and the iterator
	position is undefined.
	It internally aborts all changes made to an element previously pointed by the iterator.

`int pmemkv_iterator_seek_lower(pmemkv_iterator *it, const char *k, size_t kb);`
:	Changes iterator position to the record with key lower than given key `k` of length `kb`.
	If the record is present and no errors occurred, returns PMEMKV_STATUS_OK.
	If the record does not exist, PMEMKV_STATUS_NOT_FOUND is returned and the iterator
	position is undefined.
	It internally aborts all changes made to an element previously pointed by the iterator.

`int pmemkv_iterator_seek_lower_eq(pmemkv_iterator *it, const char *k, size_t kb);`
:	Changes iterator position to the record with key equal or lower than given key `k` of length `kb`.
	If the record is present and no errors occurred, returns PMEMKV_STATUS_OK.
	If the record does not exist, PMEMKV_STATUS_NOT_FOUND is returned and the iterator
	position is undefined.
	It internally aborts all changes made to an element previously pointed by the iterator.

`int pmemkv_iterator_seek_higher(pmemkv_iterator *it, const char *k, size_t kb);`
:	Changes iterator position to the record with key higher than given key `k` of length `kb`.
	If the record is present and no errors occurred, returns PMEMKV_STATUS_OK.
	If the record does not exist, PMEMKV_STATUS_NOT_FOUND is returned and the iterator
	position is undefined.
	It internally aborts all changes made to an element previously pointed by the iterator.

`int pmemkv_iterator_seek_higher_eq(pmemkv_iterator *it, const char *k, size_t kb);`
:	Changes iterator position to the record with key equal or higher than given key `k` of length `kb`.
	If the record is present and no errors occurred, returns PMEMKV_STATUS_OK.
	If the record does not exist, PMEMKV_STATUS_NOT_FOUND is returned and the iterator
	position is undefined.
	It internally aborts all changes made to an element previously pointed by the iterator.

`int pmemkv_iterator_seek_to_first(pmemkv_iterator *it);`
:	Changes iterator position to the first record. If db isn't empty, and no errors occurred, returns
	PMEMKV_STATUS_OK. If db is empty, PMEMKV_STATUS_NOT_FOUND is returned and the iterator position is undefined.
	It internally aborts all changes made to an element previously pointed by the iterator.

`int pmemkv_iterator_seek_to_last(pmemkv_iterator *it);`
:	Changes iterator position to the last record. If db isn't empty, and no errors occurred, returns
	PMEMKV_STATUS_OK. If db is empty, PMEMKV_STATUS_NOT_FOUND is returned and the iterator position is undefined.
	It internally aborts all changes made to an element previously pointed by the iterator.

`int pmemkv_iterator_is_next(pmemkv_iterator *it);`
:	Checks if there is a next record available. If true is returned, it is guaranteed that
	pmemkv_iterator_next(it) will return PMEMKV_STATUS_OK, otherwise iterator is already on the last
	element and pmemkv_iterator_next(it) will return PMEMKV_STATUS_NOT_FOUND.

`int pmemkv_iterator_next(pmemkv_iterator *it);`
:	Changes iterator position to the next record.
	If the next record exists, returns PMEMKV_STATUS_OK, otherwise
	PMEMKV_STATUS_NOT_FOUND is returned and the iterator position is undefined.
	It internally aborts all changes made to an element previously pointed by the iterator.

`int pmemkv_iterator_prev(pmemkv_iterator *it);`
:	Changes iterator position to the previous record.
	If the previous record exists, returns PMEMKV_STATUS_OK, otherwise
	PMEMKV_STATUS_NOT_FOUND is returned and the iterator position is undefined.
	It internally aborts all changes made to an element previously pointed by the iterator.

`int pmemkv_iterator_key(pmemkv_iterator *it, const char **k, size_t *kb);`
:	Assigns record's key's address to `k` and key's length to `kb`. If the iterator is on an undefined position,
	calling this method is undefined behaviour.

`int pmemkv_iterator_read_range(pmemkv_iterator *it, size_t pos, size_t n, const char **data, size_t *rb);`
:	Allows getting record's value's range which can be only read.
	You can request for either full value or only value's subrange (`n` elements starting from `pos`).
	Assigns pointer to the beginning of the requested range to `data`, and number of elements in range to `rb`.
	If `n` is bigger than length of a value it's automatically shrinked.
	If the iterator is on an undefined position, calling this method is undefined behaviour.

`int pmemkv_write_iterator_write_range(pmemkv_write_iterator *it, size_t pos, size_t n, char **data, size_t *wb);`
:	Allows getting record's value's range which can be modified.
	You can request for either full value or only value's subrange (`n` elements starting from `pos`).
	Assigns pointer to the beginning of the requested range to `data`, and number of elements in range to `wb`.
	If `n` is bigger than length of a value it's automatically shrinked.
	Changes made on a requested range are not persistent until *pmemkv_write_iterator_commit()* is called.
	If the iterator is on an undefined position, calling this method is undefined behaviour.

`int pmemkv_write_iterator_commit(pmemkv_write_iterator *it);`
:	Commits modifications made on the current record.
	Calling this method is the only way to save modifications made by the iterator on the current
	record. You need to call this method before changing the iterator position, otherwise
	modifications will be automatically aborted.

`void pmemkv_write_iterator_abort(pmemkv_write_iterator *it);`
:	Aborts uncommitted modifications made on the current record.

## ERRORS ##

Each function, except for *pmemkv_iterator_delete()*, *pmemkv_write_iterator_delete()* and *pmemkv_write_iterator_abort()*,
returns one of the pmemkv status codes. To check possible options see **libpmemkv**(3).

# EXAMPLE #

The following example is taken from `examples/pmemkv_iterator_c` directory.

Usage of basic iterator functions to iterate over all records and modify one of them.

```c
@ITERATOR_BASIC_EXAMPLE@
```

# SEE ALSO #

**libpmemkv**(7), **libpmemkv**(3) and **<https://pmem.io>**
