Author: Yijian, terminal OS distributed file system expert; Gongashi, terminal OS distributed data management specialist \

As a distributed operating system, HarmonyOS ‘distributed data management capabilities are important. We also continue to enhance HarmonyOS ‘distributed data management capabilities with the goal of continuously providing developers with a “one piece” data development experience. In this installment, we’ll detail the new capabilities of distributed data management.

First, the challenges of distributed data management platform

Let’s start with a typical distributed scenario:

The mobile phone and smart screen work together for document presentation. The document presentation status (such as page turning, page enlargement, page zooming, and doodle) on the mobile phone needs to be synchronized to the smart screen, and the document presentation status on the smart screen needs to be synchronized to the mobile phone.To achieve the above scenario, developers face two challenges:

Challenge 1: How to achieve “multi-end collaboration” of data more easily?

The document presentation status on the mobile phone is synchronized to the smart screen, and the document presentation status on the smart screen is synchronized to the mobile phone, which is actually “multi-terminal collaboration” of data. In order to realize the “multi-terminal collaboration” of data, the developer needs to complete the message processing logic, including establishing communication links, message sending and receiving processing, error handling and performance tuning, etc., which is very heavy workload. In addition, the complexity of debugging increases exponentially with more devices. * * * *

Challenge 2: How can we more efficiently “migrate” data across different ends?* * * *

When the mobile phone and smart screen cooperate to perform a document presentation, the document on the mobile phone must be sent to the smart screen for operation, which involves “cross-terminal migration” of data. In the past, a communication connection was established between the two devices. The mobile phone sent a document to the smart screen, which opened and modified the document after receiving it, and then sent the document back to the mobile phone. This implementation is very complicated, and file transfer back and forth also greatly affects the user’s operation efficiency.

Second, distributed data management platform

HarmonyOS ‘solution to these two challenges is to:Global access framework and sharing framework are provided in distributed data management platform to support developers’ business innovation in distributed scenarios such as access, migration, collaboration and sharing. Figure 2 distributed data management platform

In Figure 2, the global Access framework and the database in the Sharing framework were released in HarmonyOS 2, which we’ve covered in previous tweets and won’t cover in this issue. The Global Access framework and the Sharing framework are highlighted as “variables” and “files” in HarmonyOS, which will be released in the next release.

Global access to variables Challenge 1: HarmonyOS provides a framework for global access to variables in order to facilitate “multi-endpoint collaboration” of data. In the distributed document presentation scenario above, document presentation states (such as page flipping, page zooming, page zooming, and doodle) are actually variables. If these variables support “global” access, then developers can access them across devices as if they were local variables, and data can be automatically synchronized efficiently and easily across multiple ends. To achieve “global” access to variables, HarmonyOS introduced the concept of “distributed data objects”, which encapsulate variables as distributed data objects.

1. Design concept of “distributed data object”

The design concept for distributed data objects is shown in Figure 3. Building on the capabilities of a distributed soft bus and cross-device synchronization of a distributed in-memory database, HarmonyOS built an interface based on JS objects (i.e., distributed data objects). Developers can directly use this interface to achieve data synchronization across devices, without paying attention to the underlying communication process, and can focus more on the development of business logic.

Figure 3 “Distributed data Object” design concept

2. Three steps to use “Distributed **** type data objects”

The Distributed Data Object interface is extremely simple and easy to use in three steps:

(1) Inherit the DistributedDataObject base class when defining the JS object

(2) Set objectID and sessionID

(3) Subscribe to changes in distributed data objects (optional)

In addition, everything else is exactly the same as local object operations.

The base classes, methods, and parameters of the Distributed Data Object interface are described as follows:

Figure 4 Distributed Data Object interface description

Let’s look at sample code used by the Distributed Data Object interface. Figure 5 is a sample code for the initiator of multi-endpoint collaboration, and Figure 6 is a sample code for the receiver of multi-endpoint collaboration.

Figure 5 example code for the multi-endpoint collaborative initiator

FIG. 6 Example code for multi-terminal cooperative receiver iv. Global file access and sharing \

For challenge 2, HarmonyOS provides a global access and sharing framework for files in order to facilitate more efficient “cross-migration” of data. (For ease of understanding, Figure 7 shows only the file portion of the distributed data management platform.) \

Figure 7 Distributed data Management platform (file part)

As shown in Figure 7, file access frameworks are divided into application file access frameworks and public file access frameworks.

HarmonyOS 2 already provides an application file access framework. In the upcoming release of HarmonyOS, a public file access framework and a file sharing framework will be added to enhance global file access and sharing capabilities. This enables applications to access remote files as if they were accessing local files.

Based on the new common file access framework and file sharing framework, let’s look at how to implement two typical file usage scenarios:

1. Directly open the remote file

Scenario Description: Open the public file on device B on device A. \

Developers will access the distributed File system File Picker (File Picker), the application can directly open and edit remote files through the distributed File Picker, do not need to send files across devices.

The implementation code is as follows:

Var parameter = {" want ": {" action" : "action.com.huawei.filecenter.PICKER_ACITON"},}; FeatureAbility. StartAbilityForResult (parameter, (err, data) = > {...... // Open and edit remote files})Copy the code

The result is shown in Figure 8. The user can select the remote device on the local computer, see the list of files on the remote device, and directly open and edit remote files.

Figure 8. Open the remote file directly

2. Cross-end migration of files

Scenario Description: Device A shares A public file with device B and opens the file on device B.

Implementation Description In this scenario, device A does not send files to device B. Instead, A file mapping relationship is established between device A and device B. Device B remotely accesses files on device A through this mapping relationship. The specific implementation process is shown in Figure 9.

Figure 9 “Cross-end migration” of files

The implementation steps and sample code are as follows:

(1) Device A shares the file handle to device B.

HandoffHelper. Creator (networkID, fd);Copy the code

(2) Device A and device B establish file mapping.

(3) After receiving the file sharing message, device B responds to the message and obtains the file URI.

let helper1 = DataAbilityHelper.creator(getContext());
Copy the code

(4) The application on device B opens the remote file through the URI.

OpenFile (Uri, "rw", function(fd) {………… })Copy the code

V. Concluding remarks

Distributed data management platform added global access framework of variables and files, as well as file sharing framework, to help developers more convenient to achieve “multi-terminal collaboration” of data, more efficient to achieve “cross-terminal migration” of data. These new capabilities will be reflected in the new release of HarmonyOS, so stay tuned!

HarmonyOS is growing and we look forward to welcoming more developers to join us and witness the growth of HarmonyOS!