DeviceKvStore, SingleKvStorepublic interface KvStore
You can create distributed databases of different types by KvManager#getKvStore
with input parameter Options. Distributed database types are defined in KvStoreType,
including SingleKvStore, and DeviceKvStore.
SystemCapability.Data#DATA_DISTRIBUTEDDATAMGR| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
MAX_BATCH_SIZE |
max batch operation size.
|
static int |
MAX_KEY_LENGTH |
max key length.
|
static int |
MAX_KEY_LENGTH_DEVICE |
max device coordinate key length.
|
static int |
MAX_QUERY_LENGTH |
max query length.
|
static int |
MAX_STORE_ID_LENGTH |
max store id length.
|
static int |
MAX_VALUE_LENGTH |
max value length.
|
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
commit() |
Submits a transaction operation in the
KvStore database. |
void |
delete(String key) |
Deletes the key-value pair based on a specified key.
|
void |
deleteBatch(List<String> keys) |
Deletes key-value pairs in batches from the
KvStore database. |
void |
enableSync(boolean enabled) |
Sets whether to enable synchronization.
|
String |
getStoreId() |
Obtains the store ID of the current
KvStore database instance. |
void |
putBatch(List<Entry> entries) |
Inserts key-value pairs into the
KvStore database in batches. |
void |
putBoolean(String key,
boolean value) |
Writes a key-value pair of the boolean type into the
KvStore database. |
void |
putByteArray(String key,
byte[] value) |
Writes a key-value pair of the byte array type into the
KvStore database. |
void |
putDouble(String key,
double value) |
Writes a key-value pair of the double type into the
KvStore database. |
void |
putFloat(String key,
float value) |
Writes a key-value pair of the float type into the
KvStore database. |
void |
putInt(String key,
int value) |
Writes a key-value pair of the int type into the
KvStore database. |
void |
putString(String key,
String value) |
Writes a key-value pair of the string type into the
KvStore database. |
void |
rollback() |
Rolls back a transaction operation in the
KvStore database. |
void |
setSyncRange(List<String> localLabels,
List<String> remoteSupportLabels) |
Sets synchronization range labels.
|
void |
startTransaction() |
Starts a transaction operation in the
KvStore database. |
void |
subscribe(SubscribeType subscribeType,
KvStoreObserver observer) |
Registers a
KvStoreObserver for the database. |
void |
unSubscribe(KvStoreObserver observer) |
Unsubscribes from the
KvStore database based on the specified
subscribeType and KvStoreObserver. |
static final int MAX_BATCH_SIZE
static final int MAX_KEY_LENGTH
static final int MAX_KEY_LENGTH_DEVICE
static final int MAX_QUERY_LENGTH
static final int MAX_STORE_ID_LENGTH
static final int MAX_VALUE_LENGTH
String getStoreId() throws KvStoreException
KvStore database instance.KvStoreException - Throws this exception if any of the following errors occurs:
SERVER_UNAVAILABLE, IPC_ERROR, and DB_ERROR.void putBoolean(String key, boolean value) throws KvStoreException
KvStore database.key - Indicates the key. The length must be less than MAX_KEY_LENGTH.
Spaces before and after the key will be cleared.value - Indicates the boolean value.KvStoreException - Throws this exception if any of the following errors occurs:
SERVER_UNAVAILABLE, IPC_ERROR, and DB_ERROR.void putInt(String key, int value) throws KvStoreException
KvStore database.key - Indicates the key. The length must be less than MAX_KEY_LENGTH.
Spaces before and after the key will be cleared.value - Indicates the int value.KvStoreException - Throws this exception if any of the following errors occurs:
SERVER_UNAVAILABLE, IPC_ERROR, and DB_ERROR.void putFloat(String key, float value) throws KvStoreException
KvStore database.key - Indicates the key. The length must be less than MAX_KEY_LENGTH.
Spaces before and after the key will be cleared.value - Indicates the float value.KvStoreException - Throws this exception if any of the following errors occurs:
SERVER_UNAVAILABLE, IPC_ERROR, and DB_ERROR.void putDouble(String key, double value) throws KvStoreException
KvStore database.key - Indicates the key. The length must be less than MAX_KEY_LENGTH.
Spaces before and after the key will be cleared.value - Indicates the double value.KvStoreException - Throws this exception if any of the following errors occurs:
SERVER_UNAVAILABLE, IPC_ERROR, and DB_ERROR.void putString(String key, String value) throws KvStoreException
KvStore database.
If you do not want to synchronize this key-value pair to other devices, set the write option in the local database.
key - Indicates the key. The length must be less than MAX_KEY_LENGTH.
Spaces before and after the key will be cleared.value - Indicates the string value, which must be less than 4 MB as a UTF-8 byte array.KvStoreException - Throws this exception if any of the following errors occurs:
SERVER_UNAVAILABLE, IPC_ERROR, and DB_ERROR.void putByteArray(String key, byte[] value) throws KvStoreException
KvStore database.key - Indicates the key. The length must be less than MAX_KEY_LENGTH.
Spaces before and after the key will be cleared.value - Indicates the byte array, which must be less than 4 MB.KvStoreException - Throws this exception if any of the following errors occurs:
SERVER_UNAVAILABLE, IPC_ERROR, and DB_ERROR.void delete(String key) throws KvStoreException
key - Indicates the key. The length must be less than MAX_KEY_LENGTH.
Spaces before and after the key will be cleared.KvStoreException - Throws this exception if any of the following errors occurs:
SERVER_UNAVAILABLE, IPC_ERROR, DB_ERROR, and KEY_NOT_FOUND.void subscribe(SubscribeType subscribeType, KvStoreObserver observer) throws KvStoreException
KvStoreObserver for the database. When data in the distributed database changes,
the callback in KvStoreObserver will be invoked.subscribeType - Indicates the subscription type, which is defined in SubscribeType.observer - Indicates the observer of data change events in the distributed database.KvStoreException - Throws this exception if any of the following errors occurs:
SERVER_UNAVAILABLE, IPC_ERROR, DB_ERROR, and STORE_ALREADY_SUBSCRIBE.void unSubscribe(KvStoreObserver observer) throws KvStoreException
KvStore database based on the specified
subscribeType and KvStoreObserver.observer - Indicates the data change observer registered
by #subscribe(SubscribeType, KvStoreObserver).KvStoreException - Throws this exception if any of the following errors occurs:
SERVER_UNAVAILABLE, IPC_ERROR, DB_ERROR, and STORE_NOT_SUBSCRIBE.void putBatch(List<Entry> entries) throws KvStoreException
KvStore database in batches.entries - Indicates the key-value pairs to be inserted in batches.KvStoreException - Throws this exception if a database error occurs.void deleteBatch(List<String> keys) throws KvStoreException
KvStore database.keys - Indicates the key-value pairs to be deleted in batches.KvStoreException - Throws this exception if a database error occurs.void startTransaction()
throws KvStoreException
KvStore database.
After the database transaction is started, you can submit or roll back the operation.
KvStoreException - Throws this exception if a database error occurs.void commit()
throws KvStoreException
KvStore database.KvStoreException - Throws this exception if a database error occurs.void rollback()
throws KvStoreException
KvStore database.KvStoreException - Throws this exception if a database error occurs.void enableSync(boolean enabled)
throws KvStoreException
enabled - Specifies whether to enable synchronization.
The value true means to enable synchronization, and false means the opposite.KvStoreException - Throws this exception if an internal service error occurs.void setSyncRange(List<String> localLabels, List<String> remoteSupportLabels) throws KvStoreException
The labels determine the devices with which data will be synchronized.
localLabels - Indicates the synchronization labels of the local device.remoteSupportLabels - Indicates the labels of the devices with which data will be synchronized.KvStoreException - Throws this exception if an internal service error occurs.