• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# UDMF Development (C/C++)
2
3
4## Introduction
5
6The Unified Data Management Framework (UDMF) defines the language and data standards for cross-application and cross-device data interaction, improving data interaction efficiency. It also provides secure and standard data transmission channels and supports different levels of data access permissions and lifecycle management policies. It helps implement efficient data sharing across applications and devices.
7
8
9## Basic Concepts
10
11- UTD<br>A Uniform Type Descriptor (UTD) defines data of the same type to eliminate data type ambiguity. It contains the data type ID and types to which the current data type belongs. The UTD is generally used to filter or identify the data type in scenarios, such as file preview and file sharing.
12
13- UDS<br> A uniform data struct (UDS) defines the data of a certain type (specified by a UTD). Uniform data structs allow unified parsing standards to be used in data interaction, which minimizes the adaptation workload. Uniform data structs are used for data interaction across applications and devices, such as, the drag-and-drop operations.
14
15- Unified record<br>A unified record is abstract definition of a piece of data supported by the UDMF, for example, a text record or an image record.
16
17- Unified data<br>A unified data object encapsulates multiple unified records.
18
19- Unified data provider<br>The unified data provider is configured in a unified record to provide UDS data. It is usually used in delayed data transmission, in which the UDS data is transferred only when the data consumer obtains data from the unified data record.
20
21
22## Constraints
23
24- The UDMF supports management of batch data records. Each unified data object cannot exceed 200 MB. The maximum size of a single property in **PlainText**, **Hyperlink**, or **HTML** is 20 MB.
25- The size of customized data to be added to a unified record cannot exceed 100 MB.
26- When data is written to the UDMF database, the memory size of the unique identifier (key) must be greater than or equal to 512 bytes.
27
28## Available APIs
29
30For details about the APIs, see [UDMF](../reference/apis-arkdata/capi-udmf.md).
31
32| API                                                    | Description                                                       |
33| ------------------------------------------------------------ | ----------------------------------------------------------- |
34| OH_Utd* OH_Utd_Create(const char* typeId)                    | Creates a pointer to an **OH_Utd** instance.               |
35| void OH_Utd_Destroy(OH_Utd* pThis)                           | Destroys the pointer to an **OH_Utd** instance.                   |
36| const char** OH_Utd_GetTypesByFilenameExtension(const char* extension, unsigned int* count) | Obtains the uniform data types by file name extension.                       |
37| const char** OH_Utd_GetTypesByMimeType(const char* mimeType, unsigned int* count) | Obtains the uniform data types by MIME type.                         |
38| bool OH_Utd_Equals(OH_Utd* utd1, OH_Utd* utd2)               | Checks whether two UTDs are the same.                           |
39| void OH_Utd_DestroyStringList(const char** list, unsigned int count) | Destroys a UTD list.                                       |
40| OH_UdsHyperlink* OH_UdsHyperlink_Create()                    | Creates a pointer to an **OH_UdsHyperlink** instance.|
41| void OH_UdsHyperlink_Destroy(OH_UdsHyperlink* pThis)         | Destroys the pointer to an **OH_UdsHyperlink** instance.    |
42| const char* OH_UdsHyperlink_GetType(OH_UdsHyperlink* pThis)  | Obtains the UTD ID from an **OH_UdsHyperlink** instance.                  |
43| const char* OH_UdsHyperlink_GetUrl(OH_UdsHyperlink* pThis)   | Obtains the URL from an **OH_UdsHyperlink** instance.                           |
44| const char* OH_UdsHyperlink_GetDescription(OH_UdsHyperlink* pThis) | Obtains the link description from an **OH_UdsHyperlink** instance.                      |
45| int OH_UdsHyperlink_SetUrl(OH_UdsHyperlink* pThis, const char* url) | Sets the URL for an **OH_UdsHyperlink** instance.                           |
46| int OH_UdsHyperlink_SetDescription(OH_UdsHyperlink* pThis, const char* description) | Sets the link description for an **OH_UdsHyperlink** instance.                      |
47| OH_UdmfData* OH_UdmfData_Create()                            | Creates a pointer to an **OH_UdmfData** instance.                |
48| void OH_UdmfData_Destroy(OH_UdmfData* pThis)                 | Destroys the pointer to an **OH_UdmfData** instance.                    |
49| int OH_UdmfData_AddRecord(OH_UdmfData* pThis, OH_UdmfRecord* record) | Add an **OH_UdmfRecord** to an **OH_UdmfData** instance.             |
50| bool OH_UdmfData_HasType(OH_UdmfData* pThis, const char* type) | Checks whether the specified type exists in an **OH_UdmfData** instance.              |
51| OH_UdmfRecord** OH_UdmfData_GetRecords(OH_UdmfData* pThis, unsigned int* count) | Obtains all data records from an **OH_UdmfData** instance.                          |
52| OH_UdmfRecord* OH_UdmfRecord_Create()                        | Creates a pointer to an **OH_UdmfRecord** instance.              |
53| void OH_UdmfRecord_Destroy(OH_UdmfRecord* pThis)             | Destroys the pointer to an **OH_UdmfRecord** instance.                  |
54| int OH_UdmfRecord_AddHyperlink(OH_UdmfRecord* pThis, OH_UdsHyperlink* hyperlink) | Adds hyperlink data to an **OH_UdmfRecord** instance.                        |
55| char** OH_UdmfRecord_GetTypes(OH_UdmfRecord* pThis, unsigned int* count) | Obtains all data types in an **OH_UdmfRecord** instance.                        |
56| int OH_UdmfRecord_GetHyperlink(OH_UdmfRecord* pThis, OH_UdsHyperlink* hyperlink) | Obtains the hyperlink data from an **OH_UdmfRecord** instance.                        |
57| int OH_Udmf_GetUnifiedData(const char* key, Udmf_Intention intention, OH_UdmfData* unifiedData) | Obtains data from the UDMF database.                                   |
58| int OH_Udmf_SetUnifiedData(Udmf_Intention intention, OH_UdmfData* unifiedData, char* key, unsigned int keyLen) | Sets data in the UDMF database.                                   |
59| OH_UdmfRecordProvider* OH_UdmfRecordProvider_Create()        | Creates a pointer to the unified data provider instance.                         |
60| int OH_UdmfRecordProvider_SetData(OH_UdmfRecordProvider* provider, void* context, const OH_UdmfRecordProvider_GetData callback, const UdmfData_Finalize finalize) | Sets a callback for the unified data provider.                             |
61| int OH_UdmfRecord_SetProvider(OH_UdmfRecord* pThis, const char* const* types, unsigned int count, OH_UdmfRecordProvider* provider) | Sets the unified data provider in an **OH_UdmfRecord** instance.                    |
62
63
64## Adding Dynamic Link Libraries
65
66Add the following library to **CMakeLists.txt**.
67
68```txt
69libudmf.so
70```
71
72## Including Header Files
73
74```c
75#include <cstdio>
76#include <cstring>
77#include <database/udmf/utd.h>
78#include <database/udmf/uds.h>
79#include <database/udmf/udmf.h>
80#include <database/udmf/udmf_meta.h>
81#include <database/udmf/udmf_err_code.h>
82```
83## Obtaining Plaintext Data in Different Ways
84
85The following walks you through on how to use the UTD to obtain plaintext data.
861. Obtain **typeId** of the UTD based on the file name extension **.txt**.
872. Obtain **typeId** of the UTD based on the MIME type **text/plain**.
883. Use the **typeId**s obtained to create two UTD instances.
894. Check whether the two UTD instances are the same.
905. Destroy the pointers created.
91
92```c
93// 1. Obtain typeId of the UTD based on the file name extension .txt.
94unsigned int typeIds1Count = 0;
95const char** typeIds1 = OH_Utd_GetTypesByFilenameExtension(".txt", &typeIds1Count);
96printf("the count of typeIds1 is %u", typeIds1Count);
97// 2. Obtain typeId based on the MIME type.
98unsigned int typeIds2Count = 0;
99const char** typeIds2 = OH_Utd_GetTypesByMimeType("text/plain", &typeIds2Count);
100printf("the count of typeIds2 is %u", typeIds2Count);
101// 3. Use the typeIds obtained to create two UTD instances.
102OH_Utd* utd1 = OH_Utd_Create(typeIds1[0]);
103OH_Utd* utd2 = OH_Utd_Create(typeIds2[0]);
104// 4. Check whether the two UTD instances are the same.
105bool isEquals = OH_Utd_Equals(utd1, utd2);
106if (isEquals) {
107    printf("utd1 == utd2");
108} else {
109    printf("utd1 != utd2");
110}
111// 5. Destroy the pointers obtained by OH_Utd_GetTypesByFilenameExtension and OH_Utd_GetFilenameExtensions and the UTD pointers.
112OH_Utd_DestroyStringList(typeIds1, typeIds1Count);
113OH_Utd_DestroyStringList(typeIds2, typeIds2Count);
114OH_Utd_Destroy(utd1);
115OH_Utd_Destroy(utd2);
116```
117
118## Sending UDS Data
119
120To send hyperlink data, perform the following steps:
1211. Create a UDS for hyperlink data.
1222. Set the URL and description for the hyperlink.
1233. Write the hyperlink data into an **OH_UdmfRecord** instance.
1244. Add the **OH_UdmfRecord** instance to an **OH_UdmfData** instance.
1255. Save the data to the database. The key of the data saved to the database is returned.
1266. Destroy the pointers created.
127
128```c
129// 1. Create a UDS for hyperlink data.
130OH_UdsHyperlink* hyperlink = OH_UdsHyperlink_Create();
131// 2. Set the URL and description for the hyperlink.
132if (OH_UdsHyperlink_SetUrl(hyperlink, "www.demo.com") != Udmf_ErrCode::UDMF_E_OK) {
133    printf("Hyperlink set url error!");
134}
135if (OH_UdsHyperlink_SetDescription(hyperlink, "This is the description.") != Udmf_ErrCode::UDMF_E_OK) {
136    printf("Hyperlink set description error!");
137}
138// 3. Create an OH_UdmfRecord object and add the hyperlink type data to the OH_UdmfRecord object.
139OH_UdmfRecord* record = OH_UdmfRecord_Create();
140if (OH_UdmfRecord_AddHyperlink(record, hyperlink) != Udmf_ErrCode::UDMF_E_OK) {
141    printf("Add hyperlink to record error!");
142}
143// 4. Create an OH_UdmfData object and add OH_UdmfRecord to it.
144OH_UdmfData* data = OH_UdmfData_Create();
145if (OH_UdmfData_AddRecord(data, record) != Udmf_ErrCode::UDMF_E_OK) {
146    printf("Add record to data error!");
147}
148// 5. Construct and write data to the database. The key of the data is returned.
149char key[UDMF_KEY_BUFFER_LEN] = {0};
150if (OH_Udmf_SetUnifiedData(Udmf_Intention::UDMF_INTENTION_DRAG, data, key, sizeof(key)) != Udmf_ErrCode::UDMF_E_OK) {
151    printf("Set data error!");
152}
153printf("key = %s", key);
154// 6. Destroy all the pointers created.
155OH_UdsHyperlink_Destroy(hyperlink);
156OH_UdmfRecord_Destroy(record);
157OH_UdmfData_Destroy(data);
158```
159
160## Obtaining UDS Data
161
162To obtain the UDS from a hyperlink instance, perform the following steps:
1631. Create an **OH_UdmfData** instance to hold the data read from the database.
1642. Obtain data from the database based on the key.
1653. Check whether data of the hyperlink type exists.
1664. Obtain a record from **OH_UdmfData**, and then obtain hyperlink data from the data record.
1675. Obtain information in the hyperlink data.
1686. Destroy the pointers created.
169
170```c
171// 1. Create an OH_UdmfData instance.
172OH_UdmfData* readData = OH_UdmfData_Create();
173// The key here is an example and cannot be directly used. The key must be the same as that returned by OH_Udmf_SetUnifiedData.
174char key[] = {"udmf://Drag/com.ohos.test/0123456789"};
175// 2. Obtain data from the database based on the key.
176if (OH_Udmf_GetUnifiedData(key, Udmf_Intention::UDMF_INTENTION_DRAG, readData) != Udmf_ErrCode::UDMF_E_OK) {
177    printf("Failed to get data.");
178    return;
179}
180// 3. Check whether OH_UdmfData has hyperlink data.
181if (!OH_UdmfData_HasType(readData, UDMF_META_HYPERLINK)) {
182    printf("There is no hyperlink type in data.");
183    return;
184}
185// 4. Obtain the data record and hyperlink data.
186unsigned int recordsCount = 0;
187OH_UdmfRecord** records = OH_UdmfData_GetRecords(readData, &recordsCount);
188printf("the count of records count is %u", recordsCount);
189// Create a UDS to hold the hyperlink data read from the unified record.
190OH_UdsHyperlink* hyperlink = OH_UdsHyperlink_Create();
191// Obtain elements in records.
192for (int i = 0; i < recordsCount; i++) {
193    // Obtain the OH_UdmfRecord type list.
194    unsigned int recordTypeIdCount = 0;
195    char** typeIdsFromRecord = OH_UdmfRecord_GetTypes(records[i], &recordTypeIdCount);
196    for (unsigned int j = 0; j < recordTypeIdCount; j++) {
197        // Obtain the hyperlink type data from OH_UdmfRecord.
198        if (strcmp(typeIdsFromRecord[j], UDMF_META_HYPERLINK) == 0) {
199            // Obtain the hyperlink data.
200            if (OH_UdmfRecord_GetHyperlink(records[i], hyperlink) != Udmf_ErrCode::UDMF_E_OK) {
201                printf("Fail get hyperlink from record!");
202            }
203        }
204    }
205}
206// 5. Read information in OH_UdsHyperlink.
207printf("The hyperlink type id is : %s", OH_UdsHyperlink_GetType(hyperlink));
208printf("The hyperlink url is : %s", OH_UdsHyperlink_GetUrl(hyperlink));
209printf("The hyperlink description is : %s", OH_UdsHyperlink_GetDescription(hyperlink));
210// 6. Destroy the pointers.
211OH_UdsHyperlink_Destroy(hyperlink);
212OH_UdmfData_Destroy(readData);
213```
214
215## Delaying UDS Data Sending
216
217### Defining a Function for Providing UDS Data
218
219The following uses hyperlink data as an example to describe how to define a callback function that provides UDS data.
2201. Define a data providing function for **OH_UdmfRecordProvider**.
2212. Create a UDS of the hyperlink type in the data providing function.
2223. Set the URL and description for the hyperlink.
2234. Define a callback function for destroying the **OH_UdmfRecordProvider** instance.
224
225```c
226// For better code readability, the operation result verification of each step is omitted in the following code. In actual development, you need to check whether each call is successful.
227// 1. Define a callback to be invoked to return the UDS data obtained.
228static void* GetDataCallback(void* context, const char* type) {
229    if (strcmp(type, UDMF_META_PLAIN_TEXT)) {
230        // 2. Create a UDS for hyperlink data.
231        OH_UdsHyperlink* hyperlink = OH_UdsHyperlink_Create();
232        // 3. Set the URL and description for the hyperlink.
233        OH_UdsHyperlink_SetUrl(hyperlink, "www.demo.com");
234        OH_UdsHyperlink_SetDescription(hyperlink, "This is the description.");
235        return hyperlink;
236    }
237    return nullptr;
238}
239// 4. Define a callback to be triggered when OH_UdmfRecordProvider is destroyed.
240static void ProviderFinalizeCallback(void* context) { printf("OH_UdmfRecordProvider finalize."); }
241```
242
243### Delaying UDS Data Sending
244
245To delay the sending of the hyperlink data, perform the following steps: The **GetDataCallback** function is triggered to obtain data only when the data consumer obtains **OH_UdsHyperlink** from **OH_UdmfRecord**.
246
2471. Create an **OH_UdmfRecordProvider** instance and set a data providing function and a callback function for destroying it.
2482. Create an **OH_UdmfRecord** object and configure **OH_UdmfRecordProvider** in this object.
2493. Create an **OH_UdmfData** instance and add **OH_UdmfRecord** to it.
2504. Construct and write data to the database. The key of the data is returned.
2515. Destroy the pointers created.
252
253```c
254// For better code readability, the operation result verification of each step is omitted in the following code. In actual development, you need to check whether each call is successful.
255// 1. Create a OH_UdmfRecordProvider instance and configure two callback functions for providing data and destroying the instance.
256OH_UdmfRecordProvider* provider = OH_UdmfRecordProvider_Create();
257OH_UdmfRecordProvider_SetData(provider, (void* )provider, GetDataCallback, ProviderFinalizeCallback);
258
259// 2. Create an OH_UdmfRecord object and configure OH_UdmfRecordProvider in this object.
260OH_UdmfRecord* record = OH_UdmfRecord_Create();
261const char* types[1] = {UDMF_META_HYPERLINK};
262OH_UdmfRecord_SetProvider(record, types, 1, provider);
263
264// 3. Create an OH_UdmfData object and add OH_UdmfRecord to it.
265OH_UdmfData* data = OH_UdmfData_Create();
266OH_UdmfData_AddRecord(data, record);
267
268// 4. Construct and write data to the database. The key of the data is returned.
269char key[UDMF_KEY_BUFFER_LEN] = {0};
270OH_Udmf_SetUnifiedData(Udmf_Intention::UDMF_INTENTION_DRAG, data, key, sizeof(key));
271printf("key = %s", key);
272
273// 5. Destroy all the pointers created.
274OH_UdmfRecord_Destroy(record);
275OH_UdmfData_Destroy(data);
276```
277