1# Cross-Device Synchronization of KV Stores 2 3 4## When to Use 5 6KV Stores are suitable for storing service data with simple relationships. It provides higher read and write performance than the SQL database. KV stores are widely used because the simplicity of the KV data model poses fewer database version compatibility issues in distributed scenarios and simplifies conflict handling in data synchronization. 7 8 9## Basic Concepts 10 11Before implementing cross-device synchronization of KV stores, understand the following concepts: 12 13 14### Single KV Store 15 16In a single KV store, data is saved in the unit of a single entry. When data is modified locally, the data entry is updated no matter whether it has been synchronized. Only one copy of data is retained globally for multiple devices. The data of the latest time is kept for the same entry (with the same primary code) of multiple devices. The data in single KV stores is not differentiated by device. If the data modified on multiple devices has the same key, the value will be overwritten. For the data written or modified locally, the data with the latest time is synchronized to other devices. Single KV stores are used to store information, such as the Contacts and weather application data. 17 18![singleKVStore](figures/singleKVStore.jpg) 19 20 21### Device KV Store 22 23In a device KV store, the local device ID is added before the key of the KV pair stored by an application. In this way, the data of different devices is isolated. Data is managed by device and can be queried by device. 24 25The underlying devices manage the data by device. The device KV stores support distributed data query by device, but do not support modification of the data synchronized from peer devices. Device KV stores are used to store the data that needs to be accessed by device, such as the Gallery thumbnails. 26 27![deviceKVStore](figures/deviceKVStore.jpg) 28 29 30## Synchronization Types 31 32The **DatamgrService** provides the following synchronization types: 33 34 35- Manual synchronization: The application calls **sync()** to trigger a synchronization. The list of devices to be synchronized and the synchronization mode must be specified. The synchronization mode can be **PULL_ONLY** (pulling remote data to the local end), **PUSH_ONLY** (pushing local data to the remote end), or **PUSH_PULL** (pushing local data to the remote end and pulling remote data to the local end). You can use the [**sync()** with the **query** parameter](../reference/apis/js-apis-distributedKVStore.md#sync-1) to synchronize the data that meets the specified conditions. The manual synchronization is available only for system applications. 36 37- Automatic synchronization: The distributed database automatically pushes local data to the remote end and pulls remote data to the local end. An automatic synchronization is triggered when a device goes online or an application updates data. 38 39 40## Working Principles 41 42After completing device discovery and authentication, the underlying communication component notifies the application that the device goes online. The **DatamgrService** then establishes an encrypted transmission channel to synchronize data between the two devices. 43 44 45### Cross-Device Data Synchronization Mechanism 46 47![kvStore](figures/kvStore.jpg) 48 49When **put()** or **delete()** is called successfully, an automatic synchronization is triggered. The distributed data is sent to the peer device through the communication adaptation layer for synchronization. 50 51If **sync()** is called successfully, a manual synchronization is triggered to send distributed data to the peer device through the communication adaptation layer. 52 53 54### Data Change Notification Mechanism 55 56When data is added, deleted, or modified, a notification is sent to the subscriber. The notifications can be classified into the following types: 57 58- Local data change notification: subscription of the application data changes on the local device. When the data in the local KV store is added, deleted, or modified in the database, a notification is received. 59 60- Distributed data change notification: subscription of the application data changes of other devices in the network. When the data in the local KV store changes after being synchronized with data from another device in the same network, a notification is received. 61 62 63## Constraints 64 65- For each record in a device KV store, the key cannot exceed 896 bytes and the value cannot exceed 4 MB. 66 67- For each record in a single KV store, the key cannot exceed 1 KB and the value cannot exceed 4 MB. 68 69- The KV stores do not support custom conflict resolution policies for applications. 70 71- A maximum of 16 KV stores can be opened simultaneously for an application. 72 73- Each KV store supports a maximum of eight callbacks for subscription of data change notifications. 74 75- The manual synchronization is available only for system applications. 76 77 78## Available APIs 79 80The following table lists the APIs for cross-device data synchronization of the single KV store. Most of the APIs are executed asynchronously, using a callback or promise to return the result. The following table uses the callback-based APIs as an example. For more information about the APIs, see [Distributed KV Store](../reference/apis/js-apis-distributedKVStore.md). 81 82| API| Description| 83| -------- | -------- | 84| createKVManager(config: KVManagerConfig): KVManager | Creates a **KvManager** instance to manage database objects.| 85| getKVStore<T>(storeId: string, options: Options, callback: AsyncCallback<T>): void | Creates and obtains a KV store of the specified type.| 86| put(key: string, value: Uint8Array\|string\|number\|boolean, callback: AsyncCallback<void>): void | Inserts and updates data.| 87| on(event: 'dataChange', type: SubscribeType, listener: Callback<ChangeNotification>): void | Subscribes to data changes in the KV store.| 88| get(key: string, callback: AsyncCallback<boolean \| string \| number \| Uint8Array>): void | Queries the value of the specified key.| 89| sync(deviceIds: string[], mode: SyncMode, delayMs?: number): void | Triggers a manual synchronization of the KV store.| 90 91 92## How to Develop 93 94The following uses a single KV store as an example to describe how to implement cross-device data synchronization. The following describes the development process. 95 96![kvStore_development_process](figures/kvStore_development_process.png) 97 98> **NOTE** 99> 100> The data on a device can be synchronized only to the devices whose data security labels are not higher than the security level of the device. For details, see [Access Control Mechanism in Cross-Device Synchronization](sync-app-data-across-devices-overview.md#access-control-mechanism-in-cross-device-synchronization). 101 1021. Import the module. 103 104 ```js 105 import distributedKVStore from '@ohos.data.distributedKVStore'; 106 ``` 107 1082. Request permissions. 109 110 1. Request the **ohos.permission.DISTRIBUTED_DATASYNC** permission. For details, see [Declaring Permissions in the Configuration File](../security/accesstoken-guidelines.md#declaring-permissions-in-the-configuration-file). 111 2. Display a dialog box to ask authorization from the user when the application is started for the first time. For details, see [Requesting User Authorization](../security/accesstoken-guidelines.md#requesting-user-authorization). 112 1133. Create a **KvManager** instance based on the specified **KvManagerConfig** object. 114 115 1. Create a **kvManagerConfig** object based on the application context. 116 2. Create a **KvManager** instance. 117 118 119 ```js 120 // Obtain the context of the stage model. 121 import UIAbility from '@ohos.app.ability.UIAbility'; 122 let kvManager; 123 let context = null; 124 125 class EntryAbility extends UIAbility { 126 onWindowStageCreate(windowStage) { 127 context = this.context; 128 } 129 } 130 131 // Obtain the context of the FA model. 132 import featureAbility from '@ohos.ability.featureAbility'; 133 134 let context = featureAbility.getContext(); 135 136 // Construct a kvManager instance. 137 try { 138 const kvManagerConfig = { 139 bundleName: 'com.example.datamanagertest', 140 context: context 141 } 142 kvManager = distributedKVStore.createKVManager(kvManagerConfig); 143 console.info('Succeeded in creating KVManager.'); 144 // Create and obtain the KV store. 145 } catch (e) { 146 console.error(`Failed to create KVManager. Code:${e.code},message:${e.message}`); 147 } 148 ``` 149 1504. Obtain the KV store of the specified type. 151 152 1. Declare the ID of the distributed KV store to create. 153 2. Disable the auto synchronization function (**autoSync:false**) to facilitate subsequent verification of the synchronization function. If synchronization is required, call the **sync()** interface. 154 155 156 ```js 157 try { 158 const options = { 159 createIfMissing: true, 160 encrypt: false, 161 backup: false, 162 autoSync: false, 163 // If kvStoreType is left empty, a device KV store is created by default. 164 kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION, 165 // Device KV store: kvStoreType: distributedKVStore.KVStoreType.DEVICE_COLLABORATION, 166 securityLevel: distributedKVStore.SecurityLevel.S1 167 }; 168 kvManager.getKVStore('storeId', options, (err, kvStore) => { 169 if (err) { 170 console.error(`Failed to get KVStore: Code:${err.code},message:${err.message}`); 171 return; 172 } 173 console.info('Succeeded in getting KVStore.'); 174 // Perform related data operations. 175 }); 176 } catch (e) { 177 console.error(`An unexpected error occurred. Code:${e.code},message:${e.message}`); 178 } 179 ``` 180 1815. Subscribe to changes of distributed data. 182 183 ```js 184 try { 185 kvStore.on('dataChange', distributedKVStore.SubscribeType.SUBSCRIBE_TYPE_ALL, (data) => { 186 console.info(`dataChange callback call data: ${data}`); 187 }); 188 } catch (e) { 189 console.error(`An unexpected error occurred. code:${e.code},message:${e.message}`); 190 } 191 ``` 192 1936. Write data to the single KV store. 194 195 1. Construct the key and value to be written to the single KV store. 196 2. Write KV pairs to the single KV store. 197 198 199 ```js 200 const KEY_TEST_STRING_ELEMENT = 'key_test_string'; 201 const VALUE_TEST_STRING_ELEMENT = 'value_test_string'; 202 try { 203 kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, (err) => { 204 if (err !== undefined) { 205 console.error(`Failed to put data. Code:${err.code},message:${err.message}`); 206 return; 207 } 208 console.info('Succeeded in putting data.'); 209 }); 210 } catch (e) { 211 console.error(`An unexpected error occurred. Code:${e.code},message:${e.message}`); 212 } 213 ``` 214 2157. Query data in the single KV store. 216 217 1. Construct the key to be queried from the single KV store. 218 2. Query data from the single KV store. 219 220 221 ```js 222 const KEY_TEST_STRING_ELEMENT = 'key_test_string'; 223 const VALUE_TEST_STRING_ELEMENT = 'value_test_string'; 224 try { 225 kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, (err) => { 226 if (err !== undefined) { 227 console.error(`Failed to put data. Code:${err.code},message:${err.message}`); 228 return; 229 } 230 console.info('Succeeded in putting data.'); 231 kvStore.get(KEY_TEST_STRING_ELEMENT, (err, data) => { 232 if (err != undefined) { 233 console.error(`Failed to get data. Code:${err.code},message:${err.message}`); 234 return; 235 } 236 console.info(`Succeeded in getting data. Data:${data}`); 237 }); 238 }); 239 } catch (e) { 240 console.error(`Failed to get data. Code:${e.code},message:${e.message}`); 241 } 242 ``` 243 2448. Synchronize data to other devices. 245 246 Select the devices to be synchronized with data and the synchronization mode. The user needs to confirm the synchronization mode when the application is started for the first time. 247 248 > **NOTE** 249 > 250 > If manual synchronization is used, **deviceIds** is obtained by using [devManager.getTrustedDeviceListSync](../reference/apis/js-apis-device-manager.md#gettrusteddevicelistsync). The APIs of the **deviceManager** module are all system interfaces and available only to system applications. 251 252 253 ```js 254 import deviceManager from '@ohos.distributedHardware.deviceManager'; 255 256 let devManager; 257 // create deviceManager 258 deviceManager.createDeviceManager('bundleName', (err, value) => { 259 if (!err) { 260 devManager = value; 261 // deviceIds is obtained by devManager.getTrustedDeviceListSync. 262 let deviceIds = []; 263 if (devManager !== null) { 264 // The ohos.permission.ACCESS_SERVICE_DM permission is required. This permission is available only for system applications. 265 let devices = devManager.getTrustedDeviceListSync(); 266 for (let i = 0; i < devices.length; i++) { 267 deviceIds[i] = devices[i].deviceId; 268 } 269 } 270 try { 271 // 1000 indicates the maximum delay, in ms. 272 kvStore.sync(deviceIds, distributedKVStore.SyncMode.PUSH_ONLY, 1000); 273 } catch (e) { 274 console.error(`An unexpected error occurred. Code:${e.code},message:${e.message}`); 275 } 276 } 277 }); 278 ``` 279