Mbed OS Reference
Loading...
Searching...
No Matches
TDBStore Class Reference

Tiny Database Storage (TDBStore) is a lightweight module that stores data on flash storage. More...

#include <TDBStore.h>

Inheritance diagram for TDBStore:
KVStore

Public Types

typedef struct mbed::KVStore::info info_t
 Holds key information. More...
 

Public Member Functions

 TDBStore (BlockDevice *bd)
 Class constructor. More...
 
virtual ~TDBStore ()
 Class destructor. More...
 
virtual int init ()
 Initialize TDBStore. More...
 
virtual int deinit ()
 Deinitialize TDBStore, release and free resources. More...
 
virtual int reset ()
 Reset TDBStore contents (clear all keys) and reserved data. More...
 
virtual int set (const char *key, const void *buffer, size_t size, uint32_t create_flags)
 Set one TDBStore item, given key and value. More...
 
virtual int get (const char *key, void *buffer, size_t buffer_size, size_t *actual_size=NULL, size_t offset=0)
 Get one TDBStore item by given key. More...
 
virtual int get_info (const char *key, info_t *info)
 Get information of a given key. More...
 
virtual int remove (const char *key)
 Remove a TDBStore item by given key. More...
 
virtual int set_start (set_handle_t *handle, const char *key, size_t final_data_size, uint32_t create_flags)
 Start an incremental TDBStore set sequence. More...
 
virtual int set_add_data (set_handle_t handle, const void *value_data, size_t data_size)
 Add data to incremental TDBStore set sequence. More...
 
virtual int set_finalize (set_handle_t handle)
 Finalize an incremental KVStore set sequence. More...
 
virtual int iterator_open (iterator_t *it, const char *prefix=NULL)
 Start an iteration over KVStore keys. More...
 
virtual int iterator_next (iterator_t it, char *key, size_t key_size)
 Get next key in iteration. More...
 
virtual int iterator_close (iterator_t it)
 Close iteration. More...
 
virtual int reserved_data_set (const void *reserved_data, size_t reserved_data_buf_size)
 Set data in reserved area, which is a special location for special data, such as ROT. More...
 
virtual int reserved_data_get (void *reserved_data, size_t reserved_data_buf_size, size_t *actual_data_size=0)
 Get data from reserved area, which is a special location for special data, such as ROT. More...
 
bool is_valid_key (const char *key) const
 Convenience function for checking key validity. More...
 

Detailed Description

Tiny Database Storage (TDBStore) is a lightweight module that stores data on flash storage.

It is part of of the KVStore class family, meaning it supports the get/set interface. It is designed to optimize performance (speed of access), reduce wearing of the flash and minimize storage overhead. It is also resilient to power failures.

TDBStore assumes the underlying block device is fully dedicated to it (starting offset 0). If you want to dedicate only a part of the device to TDBStore, use a sliced block device, typically with SlicingBlockDevice.

Definition at line 45 of file TDBStore.h.

Member Typedef Documentation

◆ info_t

typedef struct mbed::KVStore::info info_t
inherited

Holds key information.

Constructor & Destructor Documentation

◆ TDBStore()

TDBStore ( BlockDevice bd)

Class constructor.

Parameters
[in]bdUnderlying block device.

◆ ~TDBStore()

virtual ~TDBStore ( )
virtual

Class destructor.

Member Function Documentation

◆ init()

virtual int init ( )
virtual

Initialize TDBStore.

If data exists, TDBStore will check the data integrity on initialize. If the integrity checks fails, the TDBStore will use GC to collect the available data and clean corrupted and erroneous records.

Returns
MBED_SUCCESS Success.
Negative error code on failure.

Implements KVStore.

◆ deinit()

virtual int deinit ( )
virtual

Deinitialize TDBStore, release and free resources.

Returns
MBED_SUCCESS Success.

Implements KVStore.

◆ reset()

virtual int reset ( )
virtual

Reset TDBStore contents (clear all keys) and reserved data.

Returns
MBED_SUCCESS Success. MBED_ERROR_NOT_READY Not initialized. MBED_ERROR_READ_FAILED Unable to read from media. MBED_ERROR_WRITE_FAILED Unable to write to media.

Implements KVStore.

◆ set()

virtual int set ( const char *  key,
const void *  buffer,
size_t  size,
uint32_t  create_flags 
)
virtual

Set one TDBStore item, given key and value.

Parameters
[in]keyKey - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
[in]bufferValue data buffer.
[in]sizeValue data size.
[in]create_flagsFlag mask.
Returns
MBED_SUCCESS Success. MBED_ERROR_NOT_READY Not initialized. MBED_ERROR_READ_FAILED Unable to read from media. MBED_ERROR_WRITE_FAILED Unable to write to media. MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments. MBED_ERROR_INVALID_SIZE Invalid size given in function arguments. MBED_ERROR_MEDIA_FULL Not enough room on media. MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.

Implements KVStore.

◆ get()

virtual int get ( const char *  key,
void *  buffer,
size_t  buffer_size,
size_t *  actual_size = NULL,
size_t  offset = 0 
)
virtual

Get one TDBStore item by given key.

Parameters
[in]keyKey - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
[in]bufferValue data buffer.
[in]buffer_sizeValue data buffer size.
[out]actual_sizeActual read size.
[in]offsetOffset to read from in data.
Returns
MBED_SUCCESS Success. MBED_ERROR_NOT_READY Not initialized. MBED_ERROR_READ_FAILED Unable to read from media. MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments. MBED_ERROR_INVALID_SIZE Invalid size given in function arguments. MBED_ERROR_INVALID_DATA_DETECTED Data is corrupt. MBED_ERROR_ITEM_NOT_FOUND No such key.

Implements KVStore.

◆ get_info()

virtual int get_info ( const char *  key,
info_t info 
)
virtual

Get information of a given key.

The returned info contains size and flags

Parameters
[in]keyKey - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
[out]infoReturned information structure.
Returns
MBED_SUCCESS Success. MBED_ERROR_NOT_READY Not initialized. MBED_ERROR_READ_FAILED Unable to read from media. MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments. MBED_ERROR_INVALID_DATA_DETECTED Data is corrupt. MBED_ERROR_ITEM_NOT_FOUND No such key.

Implements KVStore.

◆ remove()

virtual int remove ( const char *  key)
virtual

Remove a TDBStore item by given key.

Parameters
[in]keyKey - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
Returns
MBED_SUCCESS Success. MBED_ERROR_NOT_READY Not initialized. MBED_ERROR_READ_FAILED Unable to read from media. MBED_ERROR_WRITE_FAILED Unable to write to media. MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments. MBED_ERROR_MEDIA_FULL Not enough room on media. MBED_ERROR_ITEM_NOT_FOUND No such key. MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.

Implements KVStore.

◆ set_start()

virtual int set_start ( set_handle_t *  handle,
const char *  key,
size_t  final_data_size,
uint32_t  create_flags 
)
virtual

Start an incremental TDBStore set sequence.

This operation is blocking other operations. Any get/set/remove/iterator operation will be blocked until set_finalize is called.

Parameters
[out]handleReturned incremental set handle.
[in]keyKey - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
[in]final_data_sizeFinal value data size.
[in]create_flagsFlag mask.
Returns
MBED_SUCCESS Success. MBED_ERROR_NOT_READY Not initialized. MBED_ERROR_READ_FAILED Unable to read from media. MBED_ERROR_WRITE_FAILED Unable to write to media. MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments. MBED_ERROR_INVALID_SIZE Invalid size given in function arguments. MBED_ERROR_MEDIA_FULL Not enough room on media. MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.

Implements KVStore.

◆ set_add_data()

virtual int set_add_data ( set_handle_t  handle,
const void *  value_data,
size_t  data_size 
)
virtual

Add data to incremental TDBStore set sequence.

This operation is blocking other operations. Any get/set/remove operation will be blocked until set_finalize will be called.

Parameters
[in]handleIncremental set handle.
[in]value_dataValue data to add.
[in]data_sizeValue data size.
Returns
MBED_SUCCESS Success. MBED_ERROR_NOT_READY Not initialized. MBED_ERROR_WRITE_FAILED Unable to write to media. MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments. MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.

Implements KVStore.

◆ set_finalize()

virtual int set_finalize ( set_handle_t  handle)
virtual

Finalize an incremental KVStore set sequence.

Parameters
[in]handleIncremental set handle.
Returns
MBED_SUCCESS Success. MBED_ERROR_NOT_READY Not initialized. MBED_ERROR_WRITE_FAILED Unable to write to media. MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.

Implements KVStore.

◆ iterator_open()

virtual int iterator_open ( iterator_t *  it,
const char *  prefix = NULL 
)
virtual

Start an iteration over KVStore keys.

There are no issues with any other operations while iterator is open.

Parameters
[out]itReturned iterator handle.
[in]prefixKey prefix (null for all keys).
Returns
MBED_SUCCESS Success. MBED_ERROR_NOT_READY Not initialized. MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.

Implements KVStore.

◆ iterator_next()

virtual int iterator_next ( iterator_t  it,
char *  key,
size_t  key_size 
)
virtual

Get next key in iteration.

There are no issues with any other operations while iterator is open.

Parameters
[in]itIterator handle.
[in]keyBuffer for returned key.
[in]key_sizeKey buffer size.
Returns
MBED_SUCCESS Success. MBED_ERROR_NOT_READY Not initialized. MBED_ERROR_READ_FAILED Unable to read from block device. MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments. MBED_ERROR_INVALID_SIZE Invalid size given in function arguments. MBED_ERROR_INVALID_DATA_DETECTED Data is corrupt. MBED_ERROR_ITEM_NOT_FOUND No more keys found.

Implements KVStore.

◆ iterator_close()

virtual int iterator_close ( iterator_t  it)
virtual

Close iteration.

Parameters
[in]itIterator handle.
Returns
MBED_SUCCESS Success. MBED_ERROR_NOT_READY Not initialized. MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.

Implements KVStore.

◆ reserved_data_set()

virtual int reserved_data_set ( const void *  reserved_data,
size_t  reserved_data_buf_size 
)
virtual

Set data in reserved area, which is a special location for special data, such as ROT.

The data written to reserved area can't be overwritten.

Parameters
[in]reserved_dataReserved data buffer.
[in]reserved_data_buf_sizeReserved data buffer size.
Returns
MBED_SUCCESS Success. MBED_ERROR_NOT_READY Not initialized. MBED_ERROR_READ_FAILED Unable to read from media. MBED_ERROR_WRITE_FAILED Unable to write to media. MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments. MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.

◆ reserved_data_get()

virtual int reserved_data_get ( void *  reserved_data,
size_t  reserved_data_buf_size,
size_t *  actual_data_size = 0 
)
virtual

Get data from reserved area, which is a special location for special data, such as ROT.

Parameters
[in]reserved_dataReserved data buffer.
[in]reserved_data_buf_sizeReserved data buffer size.
[in]actual_data_sizeReturn data size.
Returns
MBED_SUCCESS Success. MBED_ERROR_NOT_READY Not initialized. MBED_ERROR_READ_FAILED Unable to read from media. MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments. MBED_ERROR_INVALID_DATA_DETECTED Data is corrupt. MBED_ERROR_ITEM_NOT_FOUND No reserved data was written.

◆ is_valid_key()

bool is_valid_key ( const char *  key) const
inherited

Convenience function for checking key validity.

Key must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.

Parameters
[in]keyKey buffer.
Returns
MBED_SUCCESS on success or an error code on failure

Definition at line 208 of file KVStore.h.