[ English | 简体中文 ]
KVDB API
KVDB provides lightweight key-value persistent storage, built on the UnQLite embedded database, with an API design inspired by the Android properties specification.
Headers: #include <kvdb.h>, #include <cutils/properties.h>
openvela Implementation Notes
- Storage Backend: Built on the UnQLite embedded database
- Value Types: Supports string, boolean, int32, int64, and binary data
- Sync/Async Writes:
property_set()performs synchronous writes;property_set_oneway()performs asynchronous writes (does not wait for persistence to complete) - Monitoring: Supports key-value change monitoring via
property_wait()orproperty_monitor_*interfaces - CLI Tools: Provides
setprop/getpropcommand-line tools for debugging
Read Interfaces
property_get
int property_get(const char *key, char *value, const char *default_value);
Retrieves a string property value. Returns the default value if the key does not exist.
Parameters:
keyProperty key name.valueBuffer to store the property value.default_valueDefault value when the key does not exist; may beNULL.
Returns:
Returns the length of the property value.
property_get_bool
int8_t property_get_bool(const char *key, int8_t default_value);
Retrieves a boolean property value.
Parameters:
keyProperty key name.default_valueDefault value when the key does not exist.
Returns:
Returns the boolean value of the property.
property_get_int32
int32_t property_get_int32(const char *key, int32_t default_value);
Retrieves a 32-bit integer property value.
Parameters:
keyProperty key name.default_valueDefault value when the key does not exist.
Returns:
Returns the int32 value of the property.
property_get_int64
int64_t property_get_int64(const char *key, int64_t default_value);
Retrieves a 64-bit integer property value.
Parameters:
keyProperty key name.default_valueDefault value when the key does not exist.
Returns:
Returns the int64 value of the property.
property_get_buffer
ssize_t property_get_buffer(const char *key, void *value, size_t size);
Retrieves a binary buffer property value.
Parameters:
keyProperty key name.valueBuffer to store the data.sizeBuffer size.
Returns:
Returns the number of bytes read on success, or a negative error code on failure.
property_get_binary
ssize_t property_get_binary(const char *key, void *value, size_t val_len);
Retrieves a binary property value.
Parameters:
keyProperty key name.valueBuffer to store the data.val_lenBuffer size.
Returns:
Returns the number of bytes read on success, or a negative error code on failure.
property_get_with_err
int property_get_with_err(const char *key, char *value);
Retrieves a string property value, distinguishing between "key does not exist" and "value is empty" via the return value.
Parameters:
keyProperty key name.valueBuffer to store the property value.
Returns:
Returns the property value length on success, or a negative error code when the key does not exist.
property_get_bool_with_err
int property_get_bool_with_err(const char *key, int8_t *value);
Reads a boolean value with error detection.
Parameters:
keyProperty key name.valuePointer to store the boolean result.
Returns:
Returns 0 on success, or a negative error code when the key does not exist or the type does not match.
property_get_int32_with_err
int property_get_int32_with_err(const char *key, int32_t *value);
Reads a 32-bit signed integer with error detection.
Parameters:
keyProperty key name.valuePointer to store the int32 result.
Returns:
Returns 0 on success, or a negative error code when the key does not exist or the type does not match.
property_get_int64_with_err
int property_get_int64_with_err(const char *key, int64_t *value);
Reads a 64-bit signed integer with error detection.
Parameters:
keyProperty key name.valuePointer to store the int64 result.
Returns:
Returns 0 on success, or a negative error code when the key does not exist or the type does not match.
Write Interfaces
property_set
int property_set(const char *key, const char *value);
Sets a string property value (synchronous write; waits for persistence to complete).
Parameters:
keyProperty key name.valueProperty value.
Returns:
Returns 0 on success, or a negative error code on failure.
property_set_oneway
int property_set_oneway(const char *key, const char *value);
Sets a string property value (asynchronous write; does not wait for persistence to complete). Offers better performance but does not guarantee immediate persistence.
Parameters:
keyProperty key name.valueProperty value.
Returns:
Returns 0 on success, or a negative error code on failure.
property_set_bool
int property_set_bool(const char *key, int8_t value);
Sets a boolean property value (synchronous write).
Parameters:
keyProperty key name.valueBoolean value (0or non-0).
Returns:
Returns 0 on success, or a negative error code on failure.
property_set_int32
int property_set_int32(const char *key, int32_t value);
Sets a 32-bit signed integer property value (synchronous write).
Parameters:
keyProperty key name.valueint32 value.
Returns:
Returns 0 on success, or a negative error code on failure.
property_set_int64
int property_set_int64(const char *key, int64_t value);
Sets a 64-bit signed integer property value (synchronous write).
Parameters:
keyProperty key name.valueint64 value.
Returns:
Returns 0 on success, or a negative error code on failure.
property_set_bool_oneway
int property_set_bool_oneway(const char *key, int8_t value);
Asynchronously sets a boolean property value (does not wait for persistence to complete).
Parameters:
keyProperty key name.valueBoolean value.
Returns:
Returns 0 on success, or a negative error code on failure.
property_set_int32_oneway
int property_set_int32_oneway(const char *key, int32_t value);
Asynchronously sets an int32 property value (does not wait for persistence to complete).
Parameters:
keyProperty key name.valueint32 value.
Returns:
Returns 0 on success, or a negative error code on failure.
property_set_int64_oneway
int property_set_int64_oneway(const char *key, int64_t value);
Asynchronously sets an int64 property value (does not wait for persistence to complete).
Parameters:
keyProperty key name.valueint64 value.
Returns:
Returns 0 on success, or a negative error code on failure.
property_set_buffer
int property_set_buffer(const char *key, const void *value, size_t size);
Sets a binary buffer property value (synchronous write).
Parameters:
keyProperty key name.valuePointer to the data buffer.sizeBuffer size in bytes.
Returns:
Returns 0 on success, or a negative error code on failure.
property_set_buffer_oneway
int property_set_buffer_oneway(const char *key, const void *value, size_t size);
Asynchronously sets a binary buffer property value (does not wait for persistence to complete).
Parameters:
keyProperty key name.valuePointer to the data buffer.sizeBuffer size in bytes.
Returns:
Returns 0 on success, or a negative error code on failure.
property_set_binary
int property_set_binary(const char *key, const void *value, size_t val_len, bool oneway);
Sets a binary property value.
Parameters:
keyProperty key name.valueBinary data.val_lenData length.onewayWhether to write asynchronously.
property_delete
int property_delete(const char *key);
Deletes the property with the specified key.
Parameters:
keyProperty key name to delete.
Returns:
Returns 0 on success, or a negative error code on failure.
Management Interfaces
property_commit
int property_commit(void);
Forces all pending property writes to be persisted to storage.
Returns:
Returns 0 on success, or a negative error code on failure.
property_load
int property_load(const char *path);
Loads properties from a file into the database.
Parameters:
pathPath to the property file.
Returns:
Returns 0 on success, or a negative error code on failure.
property_exit
int property_exit(void);
Closes KVDB and releases resources.
Returns:
Returns 0 on success, or a negative error code on failure.
property_list
int property_list(void (*propfn)(const char *key, const char *value, void *cookie), void *cookie);
Iterates over all properties, invoking the callback function for each one.
Parameters:
propfnCallback function that receives the key, value, and user data.cookieUser data passed to the callback function.
property_list_binary
int property_list_binary(void (*propfn)(const char *key, const void *value, size_t val_len, void *cookie), void *cookie);
Iterates over all binary properties, invoking the callback function for each one. Unlike property_list, the callback includes a val_len parameter, making it suitable for non-string values.
Parameters:
propfnCallback function with signaturevoid (*)(const char *key, const void *value, size_t val_len, void *cookie), receiving the key, binary value, value length, and user data.cookieUser data passed to the callback function.
Returns:
Returns 0 on success, or a negative error code on failure.
Monitoring Interfaces
property_wait
ssize_t property_wait(const char *key, char *newkey, void *newvalue, size_t val_len, int timeout);
Waits for a property change notification. Blocks until the specified key (or any key) changes or the timeout expires.
Parameters:
keyKey name to monitor;NULLto monitor all keys.newkeyBuffer to store the changed key name.newvalueBuffer to store the new value.val_lenValue buffer size.timeoutTimeout in milliseconds; -1 for infinite wait.
Returns:
Returns the value length on success, 0 on timeout, or a negative error code on failure.
property_monitor_open
int property_monitor_open(const char *key);
Opens a property monitoring file descriptor, which can be used with poll().
Parameters:
keyKey name to monitor;NULLto monitor all keys.
Returns:
Returns a file descriptor on success, or a negative error code on failure.
property_monitor_read
ssize_t property_monitor_read(int fd, char *newkey, void *newvalue, size_t val_len);
Reads a change event from the monitoring file descriptor.
Parameters:
fdFile descriptor returned byproperty_monitor_open().newkeyBuffer to store the changed key name.newvalueBuffer to store the new value.val_lenValue buffer size.
Returns:
Returns the value length on success, or a negative error code on failure.
property_monitor_close
int property_monitor_close(int fd);
Closes a property monitoring file descriptor.
Parameters:
fdFile descriptor to close.
Returns:
Returns 0 on success, or a negative error code on failure.