Data storage method and device, computer equipment and storage medium
By dividing the data storage area into static and dynamic areas and using a circular arrangement to store data during sector switching, the problem of data loss at the MCU end of the vehicle controller is solved, and the security of data storage and sector reorganization efficiency are improved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- FOSS (HANGZHOU) INTELLIGENT TECH CO LTD
- Filing Date
- 2025-12-04
- Publication Date
- 2026-05-12
AI Technical Summary
In existing technologies, the data storage on the MCU side of the vehicle controller suffers from data loss due to abnormal power failure, and the sector reassembly efficiency is low, making it difficult to achieve data sharing between the bootloader and the application.
The data storage area is divided into two parts: static and dynamic. The static area is used to store global data and static variables, while the dynamic area is used to store local variables and function parameters. The appropriate storage area is selected by the data block index, and the data is stored in a circular arrangement when switching sectors to ensure data security and stability.
It improves data storage security and sector reassembly efficiency, avoids data loss, reduces unnecessary operation code, and enhances data storage stability.
Smart Images

Figure CN122018778A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data storage technology, and in particular to a data storage method, apparatus, computer equipment, and storage medium. Background Technology
[0002] In automotive controllers, the Microcontroller Unit (MCU) needs to store EDR (Event Data Recorder) information, configuration information, and diagnostic information. The MCU's bootloader and application need to share information, thus requiring a shared storage protocol stack. The NVM (non-volatile memory) and FEE (Flash EEPROM) code in automotive open system architectures are large and consume significant memory resources, while the bootloader consumes less. Currently, data storage on the MCU often uses dynamic storage, dividing the data storage area into at least two sectors and storing data cyclically between these two sectors. This data storage method is prone to abnormal power outages and data loss during sector switching, hindering data sharing between the bootloader and application. Furthermore, sector reassembly requires reassembling all data blocks within each sector, resulting in low reassembly efficiency. Therefore, ensuring data security during data storage and improving the efficiency of data block reassembly within sectors are problems that need to be addressed. Summary of the Invention
[0003] Therefore, it is necessary to provide a data storage method, apparatus, computer equipment, and storage medium that can ensure data security during data storage and improve the reorganization efficiency of data blocks in sectors, in order to address the aforementioned technical problems.
[0004] In a first aspect, this application provides a data storage method, the method comprising:
[0005] A data storage request is obtained, and a target storage region and a target data block index are determined from the candidate storage regions of the memory based on the data storage request; the data storage request includes the data to be stored, the data block index of the target data block, and the storage region identifier of the target storage region; the candidate storage regions include static data storage regions and dynamic data storage regions;
[0006] If the target storage area is a static data storage area, the physical address of the target data block is determined according to the data block index of the target data block, and the data to be stored is stored into the target data block based on the physical address of the target data block.
[0007] If the target storage area is a dynamic data storage area, then based on the data block index, the starting write address in the dynamic data storage area is determined, and based on the starting write address, the data to be stored is stored in the target data block; the starting write address is the nearest storage address of the most recently activated sector of the last stored data in the dynamic data storage area.
[0008] In one embodiment, the static data storage area includes two static data storage sectors; each static data storage sector includes a static target sector and a static spare sector; each static data storage sector includes static data blocks, and the configuration information of the static data blocks includes: a data block index, a universally unique identifier, a data block length, a data block offset address, a memory address pointer, and an information table; the step of determining the physical address of the target data block based on its data block index, and storing the data to be stored into the target data block based on its physical address, includes:
[0009] The configuration information of the target data block is determined based on the data block index of the target data block, and the data block address offset and data block length of the target data block are determined based on the configuration information of the target data block.
[0010] The base address of the target data block is determined based on the sector base address of the static target sector in the static data storage area, the total length of the status information of the static target sector, and the data block address offset.
[0011] Determine if stored data exists in the target data block;
[0012] If no data has been stored in the target data block, the data to be stored will be stored in the target data block based on the base address of the data block.
[0013] In one embodiment, after determining whether stored data exists in the target data block, the method further includes:
[0014] If the target data block contains existing data, determine whether the existing data and the data to be stored are consistent.
[0015] If the stored data and the data to be stored are consistent, then it is determined that the data to be stored has been stored in the target data block.
[0016] In one embodiment, if the target data block contains already stored data, after determining whether the already stored data and the data to be stored are consistent, the method further includes:
[0017] If the stored data and the data to be stored are inconsistent, then the other data blocks in the static target sector, except for the target data block, are stored in the static spare sector in the static data storage area, and the target data block and other data blocks in the static target sector are deleted.
[0018] Based on the configuration information of the target data block, the target data block is constructed in the static spare sector, and the data to be stored is stored in the target data block in the static spare sector;
[0019] The static spare sector containing the data to be stored is used as the static target sector, and the static target sector that deletes the target data block and other data blocks in the static target sector is used as the static spare sector.
[0020] In one embodiment, the dynamic data storage area includes at least four dynamic data storage sectors, each containing a dynamic data block. The configuration information of the dynamic data block includes: a data block index, a universally unique identifier, a data block length, a data block offset address, a memory address pointer, and an information table. The step of determining the starting write address in the dynamic data storage area based on the data block index, and storing the data to be stored into the target data block based on the starting write address, includes:
[0021] Read the dynamic data blocks in the dynamic data storage area and determine the most recent storage address of the most recently activated sector;
[0022] The configuration information of the target data block is determined based on the data block index of the target data block, and the remaining length of the sector of the most recently activated sector is determined according to the sector length, sector base address and most recently stored address of the most recently activated sector, and it is determined whether the remaining length of the sector of the most recently activated sector is greater than the data block length of the target data block.
[0023] If the remaining length of the most recently activated sector is greater than the length of the target data block, then the most recently activated storage address of the most recently activated sector is used as the starting write address in the dynamic data storage area, and the data to be stored is stored into the target data block based on the starting write address and the configuration information of the target data block.
[0024] In one embodiment, after determining the configuration information of the target data block based on the data block index, and determining the remaining sector length of the most recently activated sector according to the sector length, sector base address, and most recently stored address, and after determining whether the remaining sector length of the most recently activated sector is greater than the data block length of the target data block, the method further includes:
[0025] If the remaining sector length of the most recently activated sector is less than or equal to the data block length of the target data block, then the sector index of the next sector of the most recently activated sector in the dynamic data storage area is set to the switching state, and the sector index of the most recently activated sector is set to the data full state.
[0026] Traverse the dynamic data storage sectors in the dynamic data storage area and store the target data block in the sector following the most recently activated sector;
[0027] After traversing the dynamic data storage sectors in the dynamic data storage area, delete the dynamic data blocks in the two sectors below the most recently activated sector, update the sector status of the two sectors below the most recently activated sector to the erased state, and update the sector status of the sector next to the most recently activated sector to the in-use state.
[0028] In one embodiment, reading dynamic data blocks in the dynamic data storage area includes:
[0029] Based on the sector index of the most recently activated sector, dynamic data blocks are read starting from the most recently activated sector. The data block address of the data block being read in the dynamic data storage area is determined according to the sector base address and sector storage capacity of the dynamic data storage sector in the dynamic data storage area.
[0030] If the data block status information of the data block being read is not empty, then the data block status information of the data block being read is read based on the data block address;
[0031] Based on the data block status information of the read data block, determine whether the fixed fields of the read data block are consistent with the preset field values;
[0032] If the fixed field of the read data block is consistent with the preset field value, then the data block identity and data block length of the read data block are determined according to the data block status information of the read data block;
[0033] The data block identity and data block length of the data block being read are verified based on the configuration information of the data block being read;
[0034] If the verification passes, the data of the read data block is read, and a cyclic redundancy check is performed on the data of the read data block.
[0035] If the verification passes, the sector index of the most recently activated sector and the data block storage address of the read data block are recorded, the data block storage status of the read data block is determined to be valid, and the data in the read data block is copied to the random access memory address space of the read data block.
[0036] Determine whether the data block address of the read data block is less than the highest sector address of the most recently activated sector in the dynamic data storage area;
[0037] If so, it is determined that the dynamic data block in the most recently activated sector has been read. Based on the sector index of the most recently activated sector and the number of sectors of the dynamic data storage sector in the dynamic data storage area, the sector index of the next sector of the most recently activated sector is determined, and based on the sector index of the next sector, it is determined whether the dynamic data block in the next sector has been read.
[0038] If so, then it is determined that the reading of the dynamic data block in the dynamic data storage area has been completed.
[0039] Secondly, this application also provides a data storage device, the device comprising:
[0040] The data storage request acquisition module is used to acquire data storage requests and determine the target storage area and the data block index of the target data block from the candidate storage areas of the memory according to the data storage request; the data storage request includes the data to be stored, the data block index of the target data block, and the storage area identifier of the target storage area; the candidate storage areas include static data storage areas and dynamic data storage areas;
[0041] A static data storage module is used to determine the physical address of the target data block according to the data block index of the target data block if the target storage area is a static data storage area, and to store the data to be stored into the target data block based on the physical address of the target data block.
[0042] The dynamic data storage module is used to determine the starting write address in the dynamic data storage area based on the data block index if the target storage area is a dynamic data storage area, and to store the data to be stored into the target data block based on the starting write address; the starting write address is the nearest storage address of the most recently activated sector of the last stored data in the dynamic data storage area.
[0043] Thirdly, this application also provides a computer device, the computer device including a memory and a processor, the memory storing a computer program, and the processor executing the computer program to perform the following steps:
[0044] A data storage request is obtained, and a target storage region and a target data block index are determined from the candidate storage regions of the memory based on the data storage request; the data storage request includes the data to be stored, the data block index of the target data block, and the storage region identifier of the target storage region; the candidate storage regions include static data storage regions and dynamic data storage regions;
[0045] If the target storage area is a static data storage area, the physical address of the target data block is determined according to the data block index of the target data block, and the data to be stored is stored into the target data block based on the physical address of the target data block.
[0046] If the target storage area is a dynamic data storage area, then based on the data block index of the target data block, the starting write address in the dynamic data storage area is determined, and based on the starting write address, the data to be stored is stored in the target data block; the starting write address is the nearest storage address of the most recently activated sector of the last stored data in the dynamic data storage area.
[0047] Fourthly, this application also provides a computer-readable storage medium having a computer program stored thereon, the computer program performing the following steps when executed by a processor:
[0048] A data storage request is obtained, and a target storage region and a target data block index are determined from the candidate storage regions of the memory based on the data storage request; the data storage request includes the data to be stored, the data block index of the target data block, and the storage region identifier of the target storage region; the candidate storage regions include static data storage regions and dynamic data storage regions;
[0049] If the target storage area is a static data storage area, the physical address of the target data block is determined according to the data block index of the target data block, and the data to be stored is stored into the target data block based on the physical address of the target data block.
[0050] If the target storage area is a dynamic data storage area, then based on the data block index of the target data block, the starting write address in the dynamic data storage area is determined, and based on the starting write address, the data to be stored is stored in the target data block; the starting write address is the nearest storage address of the most recently activated sector of the last stored data in the dynamic data storage area.
[0051] The aforementioned data storage method, apparatus, computer equipment, and storage medium acquire a data storage request and determine a target storage region and a target data block's data block index from candidate storage regions of the memory based on the data storage request. The data storage request includes data to be stored, the data block index of the target data block, and a storage region identifier for the target storage region. The candidate storage regions include static data storage regions and dynamic data storage regions. If the target storage region is a static data storage region, the physical address of the target data block is determined based on its data block index, and the data to be stored is stored in the target data block based on its physical address. If the target storage region is a dynamic data storage region, the starting write address in the dynamic data storage region is determined based on its data block index, and the data to be stored is stored in the target data block based on its starting write address. The starting write address is the nearest storage address of the most recently activated sector where the last stored data was stored in the dynamic data storage region. This solves the problem of data loss that easily occurs during sector switching when storing data on the MCU side through multiple sectors in a cyclical manner using dynamic storage. The above scheme divides the data storage area into a dynamic storage area and a static storage area. The static storage area is mainly used to store global data, static variables, and constants. The dynamic storage area is mainly used to store temporary data such as local variables and function parameters. The target storage area is selected according to the data storage requirements, and the data to be stored is stored into the target data block of the target storage area according to the data block index. This ensures the security of data storage. At the same time, both dynamic and static storage reduce unnecessary operation code and improve the stability of data storage. Attached Figure Description
[0052] Figure 1 This is a flowchart illustrating a data storage method in one embodiment;
[0053] Figure 2 This is a schematic diagram of the architecture of the static data storage area in one embodiment;
[0054] Figure 3 This is a schematic diagram of the architecture of the dynamic data storage area in one embodiment;
[0055] Figure 4 This is a schematic diagram of sector status information in one embodiment;
[0056] Figure 5 This is a schematic diagram of data block status information in one embodiment;
[0057] Figure 6 This is a schematic diagram of the sector layout in one embodiment;
[0058] Figure 7This is an example diagram showing the layout of sector status information in one embodiment;
[0059] Figure 8 This is an example diagram of the sector layout corresponding to different sector status information in one embodiment;
[0060] Figure 9 This is a flowchart illustrating the data storage method in another embodiment;
[0061] Figure 10 This is a flowchart illustrating the data storage method in another embodiment;
[0062] Figure 11 This is a structural block diagram of a data storage device in one embodiment;
[0063] Figure 12 This is an internal structural diagram of a computer device in one embodiment. Detailed Implementation
[0064] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application. Detailed Implementation
[0065] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0066] The data storage method provided in this application embodiment can be applied to a domain controller on a terminal vehicle. The domain controller on the terminal vehicle can obtain a data storage request and determine a target storage region and a target data block's data block index from candidate storage regions of the memory according to the data storage request. The data storage request includes data to be stored, the data block index of the target data block, and a storage region identifier of the target storage region. The candidate storage regions include static data storage regions and dynamic data storage regions. If the target storage region is a static data storage region, the physical address of the target data block is determined according to the data block index of the target data block, and the data to be stored is stored into the target data block based on the physical address of the target data block. If the target storage region is a dynamic data storage region, the starting write address in the dynamic data storage region is determined according to the data block index of the target data block, and the data to be stored is stored into the target data block based on the starting write address. The starting write address is the nearest storage address of the most recently activated sector of the last stored data in the dynamic data storage region.
[0067] In one embodiment, such as Figure 1 As shown, a data storage method is provided. This embodiment illustrates the method's application to a domain controller on a terminal vehicle. It is understood that this method can also be applied to terminals or servers, and can also be applied to systems including terminals and servers, implemented through interaction between the terminal and the server. In this embodiment, the method includes the following steps:
[0068] S110. Obtain the data storage request, and determine the target storage area and the data block index of the target data block from the candidate storage areas of the memory according to the data storage request.
[0069] The data storage request includes the data to be stored, the data block index of the target data block, and the storage area identifier of the target storage area; the candidate storage areas include static data storage areas and dynamic data storage areas.
[0070] It should be noted that the memory can be Flash memory, a non-volatile memory device that can retain data for a long time after power is off. The architecture of the static data storage area is as follows: Figure 2 As shown. There are two static data storage sectors; each static data storage sector includes static data blocks. The configuration information of the static data blocks includes: the data block index, the universally unique identifier (UUID), the data block length, the data block offset address, the memory address pointer (RamADressPtr), and an information table. The data block offset address is a concept in memory segmentation management, referring to the offset of a certain location within the data block relative to the starting address of the data block. The information table is used to record the table information stored in the data block. The static data storage area is used for data sharing between the bootloader and the application. During configuration, shared data blocks are allocated to the low address segment, and non-shared data blocks are allocated to the high address segment. This allows for data sharing and updates to data unrelated to the bootloader without updating the bootloader's configuration information. The architecture of the dynamic data storage area is as follows. Figure 3 As shown. The dynamic data storage area includes at least four dynamic data storage sectors, arranged in a circular pattern. Each dynamic data storage sector contains dynamic data blocks. The configuration information for each dynamic data block includes: the data block index, a universally unique identifier, the data block length, the data block offset address, the memory address pointer RamADressPtr, and an information table. The sector status information corresponding to the static and dynamic storage areas is as follows: Figure 4As shown. Fixed is a fixed value; Status 0x00 indicates UNDEF (undefined), Status 0x01 indicates ERASED (eraseed), Status 0x2 indicates SWITCHSECTOR (sector switching), Status 0x3 indicates USED (in use), Status 0x4 indicates FULL (full), Status 0x5 indicates REQUESTERASED (requested erasure); Reserved indicates a reserved field; CRC (Cyclic Redundancy Check) is CRC16. A sector is a storage structure unit. CRC16 is a technique that detects errors during data transmission or storage by calculating a 16-bit checksum. Its core function is to ensure that data remains intact and accurate after transmission or storage. Sectors in static and dynamic storage areas each contain several data blocks, and the data block status information is as follows: Figure 5 As shown in the diagram. Fixed refers to a fixed value; Status 0x1 indicates that the APP can read and write, Status 0x2 indicates that the BOOT can read and write, and Status 0x80 indicates write protection, meaning it can only be written once; Counter indicates the number of writes, excluding sector switching; UID refers to the data block index, which is unique and cannot be repeated; Offset refers to the offset of the data block relative to the sector base address; Length indicates the length of the data block; HdrCrc is CRC16; BlkCrc is CRC32. CRC32 is a 32-bit algorithm for cyclic redundancy check, a commonly used error detection technique in data communication, which verifies data integrity through a generator polynomial. Furthermore, the sector layout is as follows... Figure 6 As shown in the diagram. The sector base address is a fixed sector memory address. For example, the sector status information layout is as follows: Figure 7 As shown. In a computer storage system, the base address of sector status information refers to the starting reference address in a disk partition or storage device used to locate the status of a specific sector. For example, the sector layout corresponding to different sector status information is as follows: Figure 8 As shown. The static storage area is memory space allocated during program compilation and persists throughout the entire program's execution. It is primarily used to store global data, static variables, and constants. The dynamic storage area is memory space that is dynamically allocated and released as needed during program runtime. It is primarily used to store temporary data such as local variables and function parameters. Static data storage sectors are initialized in the static storage area, and dynamic data storage sectors are initialized in the dynamic storage area.
[0071] Specifically, the system obtains the data storage request sent by the user, determines the data to be stored, the data block index of the target data block, and the storage area identifier of the target storage area based on the data storage request, and determines the target storage area from the candidate storage areas based on the storage area identifier of the target storage area.
[0072] S120. If the target storage area is a static data storage area, the physical address of the target data block is determined according to the data block index of the target data block, and the data to be stored is stored into the target data block based on the physical address of the target data block.
[0073] The physical address refers to the actual location of data in memory, the address that is directly recognized and accessed by the hardware.
[0074] Specifically, if the target storage area is a static data storage area, the physical address of the target data block is determined according to the data block index of the target data block, and the data to be stored is stored into the target data block in the static data storage sector based on the physical address of the target data block.
[0075] S130. If the target storage area is a dynamic data storage area, then based on the data block index of the target data block, determine the starting write address in the dynamic data storage area, and based on the starting write address, store the data to be stored into the target data block.
[0076] The starting write address is the most recent storage address of the most recently active sector in the dynamic data storage area where the last data was stored.
[0077] The most recently active sector is a dynamic data storage sector that stores recently stored data.
[0078] For example, a dynamic data storage sector with a status of USED is selected from the dynamic data storage sectors in the dynamic data storage area as the target data storage sector, i.e., the most recently active sector. Based on the data block index of the target data block, the starting write address in the dynamic data storage area is determined, and based on the starting write address, the data to be stored is stored into the target data block in the most recently active sector.
[0079] In the above data storage method, a data storage request is obtained, and the target storage region and the data block index of the target data block are determined from the candidate storage regions of the memory according to the data storage request. If the target storage region is a static data storage region, the physical address of the target data block is determined according to the data block index, and the data to be stored is stored into the target data block based on the physical address of the target data block. If the target storage region is a dynamic data storage region, the starting write address in the dynamic data storage region is determined according to the data block index, and the data to be stored is stored into the target data block based on the starting write address. This solves the problem of data loss that easily occurs during sector switching when storing data on the MCU side through multiple sectors in a loop when storing data in dynamic storage mode. The above scheme divides the data storage region into a dynamic storage region and a static storage region. The static storage region is mainly used to store global data, static variables, and constants. Dynamic storage areas are mainly used to store temporary data such as local variables and function parameters. The target storage area is selected according to the data storage requirements, and the data to be stored is stored into the target data block of the target storage area according to the data block index. This can ensure the security of data storage. At the same time, both dynamic and static storage reduce unnecessary operation code and improve the stability of data storage.
[0080] In one embodiment, such as Figure 9 As shown, the physical address of the target data block is determined based on the data block index, and the data to be stored is stored into the target data block based on the physical address of the target data block, including:
[0081] S210. Determine the configuration information of the target data block based on the data block index of the target data block, and determine the data block address offset and data block length of the target data block based on the configuration information of the target data block.
[0082] The static data storage area includes two static data storage sectors; the static data storage sector includes a static target sector and a static spare sector; the static data storage sector includes static data blocks, and the configuration information of the static data blocks includes: the data block index, universal unique identifier, data block length, data block offset address, memory address pointer, and information table of the static data block.
[0083] It is understood that if the target storage area is a static data storage area, then the target data block is a static data block. The configuration information of the target data block includes the data block index, universally unique identifier, data block length, data block offset address, memory address pointer, and information table. The data block index is a special structure in the database used to accelerate data retrieval. By maintaining the physical address of the data block, it enables efficient querying of the data block. The data block address offset refers to the method of generating a new address by adding or subtracting an offset from the original address of the data block. The data block length refers to the length of continuous data that serves as the basic unit of operation during data storage, transmission, or processing.
[0084] S220. Determine the data block base address of the target data block based on the sector base address of the static target sector in the static data storage area, the total length of the status information of the static target sector, and the data block address offset.
[0085] The data block base address is a fixed memory address used in the database management system to identify the starting position of a data block.
[0086] Specifically, the base address of the target data block is the sum of the base address of the static target sector in the static data storage area, the total length of the status information of the static target sector, and the data block address offset.
[0087] S230. Determine whether there is already stored data in the target data block.
[0088] Specifically, it determines whether the target data block region in the memory is empty. If the target data block region in the memory is empty, it is determined that there is no stored data in the target data block; if the target data block region in the memory is not empty, it is determined that there is stored data in the target data block.
[0089] For example, after determining whether stored data exists in the target data block, the method further includes:
[0090] If there is already stored data in the target data block, then determine whether the stored data and the data to be stored are consistent; if the stored data and the data to be stored are consistent, then determine that the data to be stored has been stored in the target data block.
[0091] The above scheme, when the stored data and the data to be stored are the same, directly determines that the data to be stored has been stored in the target data block, which can avoid the secondary storage of the data to be stored and improve data storage efficiency.
[0092] For example, if the target data block contains already stored data, after determining whether the stored data and the data to be stored are consistent, the process further includes:
[0093] If the stored data and the data to be stored are inconsistent, the other data blocks in the static target sector, except for the target data block, are stored in the static spare sector in the static data storage area, and the target data block and other data blocks in the static target sector are deleted. Based on the configuration information of the target data block, the target data block is constructed in the static spare sector, and the data to be stored is stored in the target data block in the static spare sector. The static spare sector in which the data to be stored is used as the static target sector, and the static target sector in which the target data block and other data blocks in the static target sector are deleted is used as the static spare sector.
[0094] The above scheme can use one of the two static target sectors as a static spare sector. The static spare sector is empty. If new data needs to be written to the target data block in the static target sector, it is determined whether other data is already stored in the target data block. If so, the other data blocks in the static target sector are moved to the static spare sector, and all data blocks in the static target sector are deleted. Then, the data to be stored is written to the target data block in the static spare sector. This realizes sector switching between the static target sector and the static spare sector, and avoids data loss during the sector switching process, thus improving data security.
[0095] S240. If there is no stored data in the target data block, the data to be stored is stored in the target data block based on the data block base address.
[0096] The above method divides the static data storage sector into two fixed sectors, reducing the static data storage sector's operation on the flash memory.
[0097] In one embodiment, such as Figure 10 As shown, the dynamic data storage area includes at least four dynamic data storage sectors, each containing a dynamic data block. The configuration information of the dynamic data block includes: a data block index, a universally unique identifier, a data block length, a data block offset address, a memory address pointer, and an information table. Based on the data block index, the starting write address in the dynamic data storage area is determined, and based on the starting write address, the data to be stored is stored into the target data block, including:
[0098] S310. Read the dynamic data blocks in the dynamic data storage area and determine the most recent storage address of the most recently activated sector.
[0099] The most recently activated sector's most recently stored address refers to the most recently stored data address in the dynamic data storage sector that stores the most recently stored data.
[0100] For example, reading dynamic data blocks in the dynamic data storage area includes:
[0101] Based on the sector index of the most recently activated sector, dynamic data blocks are read starting from the most recently activated sector. The data block address of the data block to be read in the dynamic data storage area is determined according to the sector base address and sector storage capacity of the dynamic data storage sector in the dynamic data storage area. If the data block status information of the data block to be read is not empty, the data block status information of the data block to be read is read based on the data block address. Based on the data block status information of the data block to be read, it is determined whether the fixed fields of the data block to be read are consistent with the preset field values. If the fixed fields of the data block to be read are consistent with the preset field values, the data block identity and data block length of the data block to be read are determined based on the data block status information of the data block to be read. The data block identity and data block length of the data block to be read are verified based on the configuration information of the data block to be read. If the verification is successful, the data of the data block to be read is read, and... The data in the read data block undergoes cyclic redundancy check (CRC). If the check passes, the sector index of the most recently activated sector and the data block storage address of the read data block are recorded, and the data block storage status of the read data block is determined to be valid. The data in the read data block is copied to the random access memory address space of the read data block. It is determined whether the data block address of the read data block is less than the highest sector address of the most recently activated sector in the dynamic data storage area. If so, it is determined that the reading of the dynamic data block in the most recently activated sector is complete. Based on the sector index of the most recently activated sector and the number of sectors in the dynamic data storage area, the sector index of the next sector of the most recently activated sector is determined, and based on the sector index of the next sector, it is determined whether the dynamic data block in the next sector has been read. If so, it is determined that the reading of the dynamic data block in the dynamic data storage area is complete.
[0102] Data block status information can include the actual length and actual offset address of the target data block. The data block address is equal to the data block base address. Based on the configuration information of the data block being read, the data block identity and length are verified. Specifically, it is determined whether the data block identity and length of the read data block match those recorded in the configuration information. If they match, the verification passes; otherwise, it fails. Cyclic redundancy check (CRC) is used. The random access memory address space, or RAM address space, refers to the memory region in random access memory that can be accessed by programs and is used to store running data and instructions. Each program is allocated an independent virtual address space during runtime, which is mapped to a specific area of physical RAM through the operating system's memory management mechanism.
[0103] The above solution provides a way to read dynamic data blocks in a dynamic data storage area, ensuring the integrity of the data block reading.
[0104] S320. Determine the configuration information of the target data block based on the data block index of the target data block, and determine the remaining sector length of the most recently activated sector according to the sector length, sector base address and most recently stored address of the most recently activated sector, and determine whether the remaining sector length of the most recently activated sector is greater than the data block length of the target data block.
[0105] Specifically, the remaining sector length of the most recently activated sector = the sector length of the most recently activated sector + the sector base address - the most recently stored address. The block length of the target data block can be determined based on its configuration information, and it can be checked whether the remaining sector length of the most recently activated sector is greater than the block length of the target data block. The block length of the target data block can also be determined from the database based on its block index.
[0106] S330. If the remaining length of the most recently activated sector is greater than the length of the target data block, then the most recently activated storage address in the most recently activated sector is used as the starting write address in the dynamic data storage area, and the data to be stored is stored into the target data block based on the starting write address and the configuration information of the target data block.
[0107] Specifically, if the remaining length of the most recently activated sector is greater than the length of the target data block, the target data block can be constructed in the remaining storage area of the most recently activated sector. That is, the target data block is constructed in the most recently activated sector based on the configuration information of the target data block. The most recently activated storage address in the most recently activated sector is used as the starting write address in the dynamic data storage area, and the data to be stored is stored into the target data block based on the starting write address.
[0108] The above scheme can store the data to be stored into the target data block of the most recently activated sector when the remaining length of the most recently activated sector is greater than the length of the target data block. This avoids data storage failure due to insufficient storage space in the most recently activated sector and improves data storage stability.
[0109] For example, after determining the configuration information of the target data block based on the data block index, and determining the remaining sector length of the most recently activated sector according to the sector length, sector base address, and most recently stored address of the most recently activated sector, and determining whether the remaining sector length of the most recently activated sector is greater than the data block length of the target data block, the method further includes:
[0110] If the remaining length of the most recently activated sector is less than or equal to the length of the target data block, then the sector index of the next sector of the most recently activated sector in the dynamic data storage area is set to the switching state, and the sector index of the most recently activated sector is set to the data full state. The dynamic data storage sectors in the dynamic data storage area are traversed, and the target data block is stored in the next sector of the most recently activated sector. After the dynamic data storage sectors in the dynamic data storage area are traversed, the dynamic data block in the next two sectors of the most recently activated sector is deleted, and the sector status of the next two sectors of the most recently activated sector is updated to the erased state, and the sector status of the next sector of the most recently activated sector is updated to the in use state.
[0111] The next sector after the most recently activated sector refers to the next adjacent dynamic data storage sector of the most recently activated sector, and the second sector after the most recently activated sector refers to the next adjacent dynamic data storage sector of the next adjacent dynamic data storage sector of the most recently activated sector.
[0112] The above scheme switches sectors to store data when the remaining length of the most recently activated sector is less than or equal to the length of the target data block, thereby ensuring the stability and integrity of data storage and preventing data loss.
[0113] For example, based on the above embodiments, the data storage method includes:
[0114] Initialize the static data storage sectors in the static data storage area and initialize the dynamic data storage sectors in the dynamic data storage area.
[0115] For example, normal sector states include: 1) UNDEF, UNDEF; 2) ERASED, ERASED; and 3) ERASED, USED. Intermediate sector states include: 1) USED, SWITCHSECTOR; 2) REQUESTERASED, SWITCHSECTOR; 3) REQUESTERASED, USED; and 4) UNDEF, USED. Any other combination of states besides the normal and intermediate sector states described above constitutes an error state.
[0116] The steps for initializing the static data storage sector in the static data storage area include: (1) initializing the sector index = 0; (2) initializing the sector data block index = 0; (3) reading the information block data from the target sector address; the target sector address = sector base address + sector data block index * sector status information length; (4) determining the CRC value Val1 of the information block, the CRC value Val2 of the information block data, determining whether Val1 and Val2 are equal, and if so, recording the sector status information; (5) (2) Increment the sector data block index by 1 and determine whether the sector data block index is greater than the total number of sectors. If the sector data block index is less than or equal to the total number of sectors, return to step (2). If the sector data block index is greater than the total number of sectors, proceed to step (6). (6) Increment the sector index by 1 and determine whether the sector index is greater than the total number of sectors. If the sector index is less than or equal to the total number of sectors, return to step (2). If the sector index is greater than the total number of sectors, proceed to step (7). (7) If the sector status is intermediate... (8) If the sector state is intermediate state 2), then the sector in the SWITCHSECTOR state is written as USED, the sector in the REQUESTERASED state is erased, and the sector in the REQUESTERASED state is written as ERASED; (9) If the sector state is intermediate state 3), then the sector in the REQUESTERASED state is erased, and the sector in the REQUESTERASED state is written as ERASED; (10) If the sector state is intermediate state 4), then the sector in the UNDEF state is erased, and the sector in the REQUESTERASED state is written as EREASED; (11) If the sector state is any other state, erase all sectors and write the sector in the ERASED state; (12) If the sector state is normal state 1), then erase the entire sector and write the sector in the ERASED state; (13) If the sector state is normal state 2), then write the first sector in the USED state; (14) If the sector state is normal state 3), then record the index value of the sector in the USED state as ACTIVE. By performing the above steps, the initialization of the static data storage sector can be completed.
[0117] After initializing the static data storage sector in the static data storage area, read the static data block in the static data storage sector. The steps for reading the static data block include: (1) Initialize the data block index = 0; (2) Obtain the data block address offset, data length and data block UID of the static data block from the data block configuration information; (3) Determine the data block base address of the static data block, the data block base address = sector base address + data block address offset; (4) Read the data status information from the data block address, the data block address is equal to the data block base address; (5) Determine whether the fixed fields of the static data block are consistent. If the fixed fields are inconsistent, execute step (10). If the fixed fields are consistent with the pre-set field values, execute step (6); (6) Calculate the CRC of the data block status information. The CRC of the data block status information is the bytes other than the status information CRC16 and CRC32. Determine whether the CRC and CRC16 of the data block status information are consistent. If the CRC and CRC16 of the data block status information are consistent, execute step (6). If they are inconsistent, proceed to step (10). If the CRC and CRC16 of the data block status information are consistent, proceed to step (7). (7) Verify the UID and LENGTH in the data block status information based on the data block configuration information. If the verification fails, proceed to step (10). If the verification passes, proceed to step (8). (8) Read the data block data and calculate the CRC of the data block data. Determine whether the CRC and CRC32 of the data block data are consistent. If the CRC and CRC32 of the data block data are inconsistent, proceed to step (10). If the CRC and CRC32 of the data block data are consistent, proceed to step (9). (9) Record that the static data block has been read and copy the data to the RAM address space of the static data block. (10) Increment the data block index by 1 and determine whether the data block index is less than the total number of data blocks. If the data block index is less than the total number of data blocks, proceed to step (2). If the data block index is greater than or equal to the total number of data blocks, determine that the reading of the static data block is complete.
[0118] If the target storage area is a static data storage area, the steps for storing the data to be stored into the target data block in the static data storage area include: (1) determining the data block address offset and data block length of the target data block based on the index value of the target data block; (2) calculating the data block base address of the target data block, where the data block base address = the base address of the current active sector + the length of the sector status information + the data block address offset, and the current active sector is the static target sector; (3) determining whether the area corresponding to the data block base address in the flash memory is empty, if it is empty, then execute step (10), if it is not empty, then execute step (4); (4) comparing whether the data to be stored and the data already stored in the target data block are consistent, if they are consistent, then execute step (10), if they are inconsistent, then execute step (5); (5) writing the status of the next sector into SWITCHSECTOR, judging all data blocks, and determining whether it is necessary to copy the static data block in the current active sector to the next sector, where the next sector is the static spare sector; (6) if the sector index value is not equal to the data block index of the target data block. If the index value is equal to the data block index value of the target data block, then calculate the static data block address of the current active sector and the static data block address of the next sector, and execute step (7); the static data block address of the current active sector = the base address of the current active sector + the offset of the data block; the static data block address of the next sector = the base address of the next sector + the offset of the data block; if the sector index value is equal to the data block index value of the target data block, then execute step (8); (7) determine whether the address of the target data block of the current active sector is empty, if not, then read the target data block from the current active sector, and Write the target data block to the next sector; (8) Copy the data block index value +1. If the copied data block index value is less than the total number of data blocks, then execute step (6). If the copied data block index value is greater than or equal to the total number of data blocks, then determine that the static data block in the currently active sector has been copied to the next sector, and execute step (9); (9) Calculate the data block status information crc16 and crc32, and fill the data block status information; (10) Write the data block status information; (11) Write the data to be stored to the target data block.
[0119] For example, the sector management information of the dynamic data storage sector includes the total number of sectors in the dynamic data storage sector (SectorNum), the sector index Active of the most recently activated sector, the sector index NextActive of the next sector after the most recently activated sector, the sector index NextNextActive of the second sector after the most recently activated sector, and the sector status information of the dynamic data storage sector. Here, SectorNum is the total number of sectors in the dynamic data storage sector, and SectorNum is greater than or equal to 4. The dynamic data storage sectors are arranged in a ring, with Active pointing to the most recently activated sector. NextActive and NextNextActive are calculated as follows: NextActive = (Active + 1) % SectorNum; NextNextActive = (Active + 2) % SectorNum.
[0120] It should be noted that the dynamic data storage sector pointed to by NextActive must be in the ERASED state. If the remaining space in the dynamic data storage sector pointed to by Active is insufficient to write the target data block, the dynamic data storage sector pointed to by NextNextActive is checked. If the sector state of the dynamic data storage sector pointed to by NextNextActive is not ERASED, then if the data in the dynamic data block in the dynamic data storage sector pointed to by NextNextActive is recently stored, the dynamic data block is moved to the dynamic data storage sector pointed to by NextActive. After the data is moved and stored, the dynamic data block in the dynamic data storage sector pointed to by NextNextActive is deleted, and the sector state of the dynamic data storage sector pointed to by NextNextActive is written to ERASED. The sector state of the dynamic data storage sector pointed to by Active is written to FULL, the sector state of the dynamic data storage sector pointed to by NextActive is written to USED, and the target data block is written to the dynamic data storage sector pointed to by NextActive. Then let Active=(Active+1)%SectorNum, NextActive=(Active+1)%SectorNum, NextNextActive=(Active+2)%SectorNum.
[0121] The data block management information for dynamic data blocks in a dynamic data storage sector includes: sector index (SectorIndex), storage address (BlockADress), and storage status (BlockStatus). SectorIndex represents the sector index of the dynamic data block, BlockADress represents the address of the dynamic data block, and BlockStatus represents the storage status of the dynamic data block, which can be either Valid or Invalid.
[0122] The normal state of a dynamic data storage sector includes:
[0123] 1)UNDEF,UNDEF,UNDEF,UNDEF;
[0124] 2) ERASED, ERASED, ERASED, ERASED;
[0125] 3) USED, ERASED, ERASED, ERASED;
[0126] 4) FULL,USED,ERASED,ERASED;
[0127] 5) FULL, FULL, USED, ERASED.
[0128] The intermediate states of a dynamic data storage sector include:
[0129] 1) FULL, FULL, USED, SWITCHSETOR;
[0130] 2) FULL, FULL, FULL, SWITCHSETOR;
[0131] 3) REQUESTERASED,FULL,FULL,SWITCHSETOR;
[0132] 4) REQUESTERASED,FULL,FULL,USED.
[0133] Any other combination of states besides the normal and intermediate states mentioned above represents an error state for the dynamic data storage sector.
[0134] The steps for initializing the dynamic data storage sector in the dynamic data storage area include: (1) initializing the sector index of the dynamic data storage sector to 0; (2) initializing the sector information status block index of the dynamic data storage sector to 0; (3) reading the sector information block data from the target address; target address = sector base address + sector information status block index * sector information status block size; (4) determining the CRC value Val3 of the sector information status block and the CRC value Val4 of the data in the sector information status block. If Val3 equals Val4, then record the sector status information according to the CRC value of the sector information status block. (5) Increment the sector status information block index by 1, and determine whether the sector status information block index is greater than the total number of sector status information blocks. If yes, proceed to step (6); otherwise, return to step (3). (6) Increment the sector index of the dynamic data storage sector by 1, and determine whether the sector index of the dynamic data storage sector is greater than the total number of sectors of the dynamic data storage sector. If yes, proceed to step (7); otherwise, return to step (2). (7) If the sector status of the dynamic data storage sector is intermediate state 1), then the sector index written to the dynamic data storage sector is Active, NextActive. The dynamic data storage sector in question is in the state of SWITCHSECTOR. Erase the dynamic data storage sector pointed to by NextActive and write the state to ERASED. (8) If the sector state of the dynamic data storage sector is in intermediate state 2), then the sector index of the switching sector is NextActive. (9) If the sector state of the dynamic data storage sector is in intermediate state 3), then the sector index of the switching sector is Active, the dynamic data storage sector in which NextActive is located is REQUESTERASED, erase the dynamic data storage sector pointed to by NextActive and write the state to ERASED. (10) If the sector state of the dynamic data storage sector is in intermediate state 4), then the sector index of the dynamic data storage sector is Active, the dynamic data storage sector in which NextActive is located is REQUESTERASED, erase the dynamic data storage sector pointed to by NextActive and write the state to ERASED. (11) Erase the dynamic data storage sector in which the sector state is in error state and write ERASED. The dynamic data storage sector in which the sector state is in error state is Active=0.
[0135] After initializing the static data storage sector in the dynamic data storage area, the dynamic data block in the dynamic data storage sector is read. The steps for reading the dynamic data block include: (1) initializing the data block index of the dynamic data block to 0; (2) starting to read the dynamic data block from the dynamic data storage sector with the index value of Active; (3) determining the data block base address = sector base address + sector information block size; the sector information block size is the sector information block storage capacity; (4) determining that the data block address is equal to the data block base address; (5) determining whether the data block status information is empty. If it is empty, step (15) is executed; if it is not empty, step (6) is executed; (6) reading the dynamic data block based on the data block address. (7) Determine whether the fixed fields of the dynamic data block are consistent based on the data block status information of the dynamic data block. If they are inconsistent, proceed to step (11). If they are consistent, proceed to step (8). (8) Calculate the CRC of the data block status information of the dynamic data block and determine whether the CRC is consistent with the CRC16 of the dynamic data block status information. If they are inconsistent, proceed to step (13). If they are consistent, proceed to step (9). (9) Determine whether the dynamic data block being read has been read. If yes, proceed to step (13). If no, proceed to step (13). (10) Based on the configuration information of the dynamic data block, determine the UID and data block length in the data block status information of the dynamic data block. Verify the dynamic data block. If the verification fails, proceed to step (13). If the verification passes, proceed to step (11). (11) Read the data in the dynamic data block and calculate the CRC of the dynamic data block. Determine whether the CRC of the dynamic data block is consistent with the CRC32 in the dynamic data block status information. If they are inconsistent, proceed to step (13). If they are consistent, proceed to step (12). (12) Record that the sector index of the dynamic data storage sector where the read dynamic data block is located = the sector index of the most recently activated sector, the data block storage address of the read dynamic data block is equal to the data block address of the read dynamic data block, the data block storage status of the read dynamic data block is valid, and copy the data to the read dynamic data block. (13) Set the address of the dynamic data block to the address of the data block = the address of the data block + the data status information, and record the address of the most recently activated sector = the address of the data block. If the address of the data block is less than the highest address of the most recently activated sector, then execute step (4). If the address of the data block is greater than or equal to the highest address of the most recently activated sector, then execute step (14). (14) Set the sector index to (sector index - 1)% total number of sectors. If the sector status pointed to by the sector index is FULL, then determine that the data block index = 0 and execute step (15). If the sector status pointed to by the sector index is not FULL, then return to execute step (3). (15) Determine that the dynamic data block reading is complete.
[0136] The steps for switching partitions of dynamic data storage sectors include: (1) writing the sector pointed to by NextActive to the state SWITCHSECTOR; (2) writing the sector pointed to by Active to the state FULL; (3) traversing all dynamic data storage sectors, and ignoring the dynamic data storage sector where the dynamic data block is located if the index of the dynamic data storage sector is not equal to NextNexActive; (4) moving the data block to the dynamic data storage sector pointed to by NextActive and updating the data block management information; (5) after all sectors have been traversed, writing the sector state of the dynamic data storage sector pointed to by NextNextActive to REQUESTERASED and updating the sector address of the dynamic data storage sector; (6) writing the sector state of the dynamic data storage sector pointed to by NextActive to USED; (7) erasing the dynamic data storage sector pointed to by NextNextActive and writing the sector state of the dynamic data storage sector to ERASED; (8) updating Active, NextActive and NextNextActive, and setting Active = (Active + 1)% SectorNum, NextActive=(Active+1)%SectorNum, NextNextActive=(Active+2)%SectorNum; (9) Confirm that the sector switching is complete.
[0137] If the target storage area is a dynamic data storage area, the steps for storing the data to be stored into the target data block in the dynamic data storage area include: (1) determining whether the data to be stored and the data already stored in the target data block are consistent. If they are consistent, then execute step (8); if they are inconsistent, then execute step (2); (2) determining the remaining sector length of the most recently activated sector. The remaining sector length = total sector length + sector base address - sector address of the most recently activated sector. If the remaining sector length is less than or equal to the data block length of the target data block, then switch sectors for data storage. If the remaining sector length is greater than the data block length of the target data block, then execute step (3); (3) calculating the data block status information CRC16 and CRC32 of the target data block and filling the data block status information of the target data block; (4) Write partial data block status information and determine whether an error is returned. If yes, execute step (7); otherwise, execute step (5). (5) Write the data to be stored in the target data block and determine whether an error is returned. If yes, execute step (7); otherwise, execute step (6). (6) Write the status information of the remaining data blocks and determine whether an error is returned. If yes, execute step (7); otherwise, execute step (8). (7) Update the data block storage address = current sector address, the data block storage sector index = sector index of the most recently activated sector, the data block storage status is valid, update the sector address of the most recently activated sector, that is, make the sector address of the most recently activated sector = the address of the most recently activated sector + the data block length of the target data block. (8) Complete the data storage of the data to be stored.
[0138] In the above data storage method, a data storage request is obtained, and the target storage region and the data block index of the target data block are determined from the candidate storage regions of the memory according to the data storage request. If the target storage region is a static data storage region, the physical address of the target data block is determined according to the data block index, and the data to be stored is stored into the target data block based on the physical address of the target data block. If the target storage region is a dynamic data storage region, the starting write address in the dynamic data storage region is determined according to the data block index, and the data to be stored is stored into the target data block based on the starting write address. This solves the problem of data loss that easily occurs during sector switching when storing data on the MCU side through multiple sectors in a loop when storing data in dynamic storage mode. The above scheme divides the data storage region into a dynamic storage region and a static storage region. The static storage region is mainly used to store global data, static variables, and constants. Dynamic storage areas are mainly used to store temporary data such as local variables and function parameters. The target storage area is selected according to the data storage requirements, and the data to be stored is stored into the target data block of the target storage area according to the data block index. This can ensure the security of data storage. At the same time, both dynamic and static storage reduce unnecessary operation code and improve the stability of data storage.
[0139] It should be understood that although the steps in the flowcharts of the embodiments described above are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the embodiments described above may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages of other steps.
[0140] Based on the same inventive concept, this application also provides a data storage device for implementing the data storage method described above. The solution provided by this device is similar to the solution described in the above method; therefore, the specific limitations in one or more data storage device embodiments provided below can be found in the limitations of the data storage method described above, and will not be repeated here.
[0141] In one embodiment, such as Figure 11 As shown, a data storage device is provided, including: a data storage request acquisition module 101, a static data storage module 102, and a dynamic data storage module 103, wherein:
[0142] The data storage request acquisition module 101 is used to acquire a data storage request and determine the target storage area and the data block index of the target data block from the candidate storage areas of the memory according to the data storage request; the data storage request includes the data to be stored, the data block index of the target data block, and the storage area identifier of the target storage area; the candidate storage areas include static data storage areas and dynamic data storage areas;
[0143] The static data storage module 102 is used to determine the physical address of the target data block according to the data block index of the target data block if the target storage area is a static data storage area, and to store the data to be stored into the target data block based on the physical address of the target data block.
[0144] The dynamic data storage module 103 is used to determine the starting write address in the dynamic data storage area based on the data block index of the target data block if the target storage area is a dynamic data storage area, and to store the data to be stored into the target data block based on the starting write address; the starting write address is the nearest storage address of the most recently activated sector of the last stored data in the dynamic data storage area.
[0145] For example, the static data storage area includes two static data storage sectors; the static data storage sector includes a static target sector and a static spare sector; the static data storage sector includes static data blocks, and the configuration information of the static data blocks includes: the data block index, a universally unique identifier, the data block length, the data block offset address, the memory address pointer, and an information table; further, the static data storage module 102 is specifically used for:
[0146] The configuration information of the target data block is determined based on the data block index of the target data block, and the data block address offset and data block length of the target data block are determined based on the configuration information of the target data block.
[0147] The base address of the target data block is determined based on the sector base address of the static target sector in the static data storage area, the total length of the status information of the static target sector, and the data block address offset.
[0148] Determine if stored data exists in the target data block;
[0149] If no data has been stored in the target data block, the data to be stored will be stored in the target data block based on the base address of the data block.
[0150] For example, the static data storage module 102 is also specifically used for:
[0151] If the target data block contains existing data, determine whether the existing data and the data to be stored are consistent.
[0152] If the stored data and the data to be stored are consistent, then it is determined that the data to be stored has been stored in the target data block.
[0153] Furthermore, the static data storage module 102 is also specifically used for:
[0154] If the stored data and the data to be stored are inconsistent, then the other data blocks in the static target sector, except for the target data block, are stored in the static spare sector in the static data storage area, and the target data block and other data blocks in the static target sector are deleted.
[0155] Based on the configuration information of the target data block, the target data block is constructed in the static spare sector, and the data to be stored is stored in the target data block in the static spare sector;
[0156] The static spare sector containing the data to be stored is used as the static target sector, and the static target sector that deletes the target data block and other data blocks in the static target sector is used as the static spare sector.
[0157] For example, the dynamic data storage area includes at least four dynamic data storage sectors, each containing a dynamic data block. The configuration information of the dynamic data block includes: a data block index, a universally unique identifier, a data block length, a data block offset address, a memory address pointer, and an information table. Further, the dynamic data storage module 103 is specifically used for:
[0158] Read the dynamic data blocks in the dynamic data storage area and determine the most recent storage address of the most recently activated sector;
[0159] The configuration information of the target data block is determined based on the data block index of the target data block, and the remaining length of the sector of the most recently activated sector is determined according to the sector length, sector base address and most recently stored address of the most recently activated sector, and it is determined whether the remaining length of the sector of the most recently activated sector is greater than the data block length of the target data block.
[0160] If the remaining length of the most recently activated sector is greater than the length of the target data block, then the most recently activated storage address of the most recently activated sector is used as the starting write address in the dynamic data storage area, and the data to be stored is stored into the target data block based on the starting write address and the configuration information of the target data block.
[0161] For example, the dynamic data storage module 103 is also specifically used for:
[0162] If the remaining sector length of the most recently activated sector is less than or equal to the data block length of the target data block, then the sector index of the next sector of the most recently activated sector in the dynamic data storage area is set to the switching state, and the sector index of the most recently activated sector is set to the data full state.
[0163] Traverse the dynamic data storage sectors in the dynamic data storage area and store the target data block in the sector following the most recently activated sector;
[0164] After traversing the dynamic data storage sectors in the dynamic data storage area, delete the dynamic data blocks in the two sectors below the most recently activated sector, update the sector status of the two sectors below the most recently activated sector to the erased state, and update the sector status of the sector next to the most recently activated sector to the in-use state.
[0165] For example, the dynamic data storage module 103 is also specifically used for:
[0166] Based on the sector index of the most recently activated sector, dynamic data blocks are read starting from the most recently activated sector. The data block address of the data block being read in the dynamic data storage area is determined according to the sector base address and sector storage capacity of the dynamic data storage sector in the dynamic data storage area.
[0167] If the data block status information of the data block being read is not empty, then the data block status information of the data block being read is read based on the data block address;
[0168] Based on the data block status information of the read data block, determine whether the fixed fields of the read data block are consistent with the preset field values;
[0169] If the fixed field of the read data block is consistent with the preset field value, then the data block identity and data block length of the read data block are determined according to the data block status information of the read data block;
[0170] The data block identity and data block length of the data block being read are verified based on the configuration information of the data block being read;
[0171] If the verification passes, the data of the read data block is read, and a cyclic redundancy check is performed on the data of the read data block.
[0172] If the verification passes, the sector index of the most recently activated sector and the data block storage address of the read data block are recorded, the data block storage status of the read data block is determined to be valid, and the data in the read data block is copied to the random access memory address space of the read data block.
[0173] Determine whether the data block address of the read data block is less than the highest sector address of the most recently activated sector in the dynamic data storage area;
[0174] If so, it is determined that the dynamic data block in the most recently activated sector has been read. Based on the sector index of the most recently activated sector and the number of sectors of the dynamic data storage sector in the dynamic data storage area, the sector index of the next sector of the most recently activated sector is determined, and based on the sector index of the next sector, it is determined whether the dynamic data block in the next sector has been read.
[0175] If so, then it is determined that the reading of the dynamic data block in the dynamic data storage area has been completed.
[0176] Each module in the aforementioned data storage device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in or independent of the processor in a computer device, or stored in the memory of a computer device as software, so that the processor can call and execute the operations corresponding to each module.
[0177] In one embodiment, a computer device is provided, which may be a terminal, and its internal structure diagram may be as follows: Figure 12As shown, the computer device includes a processor, memory, input / output interface, communication interface, display unit, and input device. The processor, memory, and input / output interface are connected via a system bus, and the communication interface, display unit, and input device are also connected to the system bus via the input / output interface. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs in the non-volatile storage media. The input / output interface is used for exchanging information between the processor and external devices. The communication interface is used for wired or wireless communication with external terminals; wireless communication can be achieved through Wi-Fi, mobile cellular networks, NFC (Near Field Communication), or other technologies. When the computer program is executed by the processor, it implements a data storage method. The display unit is used to form a visually visible image and can be a display screen, a projection device, or a virtual reality imaging device. The display screen can be an LCD screen or an e-ink screen. The input device of the computer device can be a touch layer covering the display screen, or buttons, trackballs, or touchpads set on the casing of the computer device, or external keyboards, touchpads, or mice, etc.
[0178] Those skilled in the art will understand that Figure 12 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0179] In one embodiment, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to perform the following steps:
[0180] Step 1: Obtain a data storage request, and determine the target storage region and the data block index of the target data block from the candidate storage regions of the memory according to the data storage request; the data storage request includes the data to be stored, the data block index of the target data block, and the storage region identifier of the target storage region; the candidate storage regions include static data storage regions and dynamic data storage regions;
[0181] Step 2: If the target storage area is a static data storage area, then determine the physical address of the target data block according to the data block index of the target data block, and store the data to be stored into the target data block based on the physical address of the target data block.
[0182] Step 3: If the target storage area is a dynamic data storage area, then based on the data block index of the target data block, determine the starting write address in the dynamic data storage area, and based on the starting write address, store the data to be stored into the target data block; the starting write address is the nearest storage address of the most recently activated sector of the last stored data in the dynamic data storage area.
[0183] In one embodiment, a computer-readable storage medium is provided having a computer program stored thereon, the computer program performing the following steps when executed by a processor:
[0184] Step 1: Obtain a data storage request, and determine the target storage region and the data block index of the target data block from the candidate storage regions of the memory according to the data storage request; the data storage request includes the data to be stored, the data block index of the target data block, and the storage region identifier of the target storage region; the candidate storage regions include static data storage regions and dynamic data storage regions;
[0185] Step 2: If the target storage area is a static data storage area, then determine the physical address of the target data block according to the data block index of the target data block, and store the data to be stored into the target data block based on the physical address of the target data block.
[0186] Step 3: If the target storage area is a dynamic data storage area, then based on the data block index of the target data block, determine the starting write address in the dynamic data storage area, and based on the starting write address, store the data to be stored into the target data block; the starting write address is the nearest storage address of the most recently activated sector of the last stored data in the dynamic data storage area.
[0187] In one embodiment, a computer program product is provided, including a computer program that, when executed by a processor, performs the following steps:
[0188] Step 1: Obtain a data storage request, and determine the target storage region and the data block index of the target data block from the candidate storage regions of the memory according to the data storage request; the data storage request includes the data to be stored, the data block index of the target data block, and the storage region identifier of the target storage region; the candidate storage regions include static data storage regions and dynamic data storage regions;
[0189] Step 2: If the target storage area is a static data storage area, then determine the physical address of the target data block according to the data block index of the target data block, and store the data to be stored into the target data block based on the physical address of the target data block.
[0190] Step 3: If the target storage area is a dynamic data storage area, then based on the data block index of the target data block, determine the starting write address in the dynamic data storage area, and based on the starting write address, store the data to be stored into the target data block; the starting write address is the nearest storage address of the most recently activated sector of the last stored data in the dynamic data storage area.
[0191] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of related data must comply with the relevant laws, regulations and standards of the relevant countries and regions.
[0192] Those skilled in the art will understand that all or part of the processes in the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium. When executed, the computer program can include the processes of the embodiments described above. Any references to memory, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take many forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM). The databases involved in the embodiments provided in this application may include at least one type of relational database and non-relational database. Non-relational databases may include, but are not limited to, blockchain-based distributed databases. The processors involved in the embodiments provided in this application may be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, etc., and are not limited to these.
[0193] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0194] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of this patent application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this application should be determined by the appended claims.
Claims
1. A data storage method, characterized in that, include: Obtain the data storage request, and determine the target storage region and the data block index of the target data block from the candidate storage regions of the memory based on the data storage request; The data storage request includes the data to be stored, the data block index of the target data block, and the storage area identifier of the target storage area; the candidate storage areas include static data storage areas and dynamic data storage areas. If the target storage area is a static data storage area, the physical address of the target data block is determined according to the data block index of the target data block, and the data to be stored is stored into the target data block based on the physical address of the target data block. If the target storage area is a dynamic data storage area, then based on the data block index of the target data block, the starting write address in the dynamic data storage area is determined, and based on the starting write address, the data to be stored is stored in the target data block; the starting write address is the nearest storage address of the most recently activated sector of the last stored data in the dynamic data storage area.
2. The method according to claim 1, characterized in that, The static data storage area includes two static data storage sectors; the static data storage sectors include a static target sector and a static spare sector; The static data storage sector includes static data blocks. The configuration information of the static data blocks includes: a data block index, a universally unique identifier, a data block length, a data block offset address, a memory address pointer, and an information table. The step of determining the physical address of the target data block based on its data block index, and storing the data to be stored into the target data block based on its physical address, includes: The configuration information of the target data block is determined based on the data block index of the target data block, and the data block address offset and data block length of the target data block are determined based on the configuration information of the target data block. The base address of the target data block is determined based on the sector base address of the static target sector in the static data storage area, the total length of the status information of the static target sector, and the data block address offset. Determine if stored data exists in the target data block; If no data has been stored in the target data block, the data to be stored will be stored in the target data block based on the base address of the data block.
3. The method according to claim 2, characterized in that, After determining whether stored data exists in the target data block, the process also includes: If there is already stored data in the target data block, determine whether the already stored data and the data to be stored are consistent; If the stored data and the data to be stored are consistent, then it is determined that the data to be stored has been stored in the target data block.
4. The method according to claim 3, characterized in that, If the target data block contains already stored data, after determining whether the stored data and the data to be stored are consistent, the process also includes: If the stored data and the data to be stored are inconsistent, then the other data blocks in the static target sector, except for the target data block, are stored in the static spare sector in the static data storage area, and the target data block and other data blocks in the static target sector are deleted. Based on the configuration information of the target data block, the target data block is constructed in the static spare sector, and the data to be stored is stored in the target data block in the static spare sector; The static spare sector containing the data to be stored is used as the static target sector, and the static target sector that deletes the target data block and other data blocks in the static target sector is used as the static spare sector.
5. The method according to claim 1, characterized in that, The dynamic data storage area includes at least four dynamic data storage sectors, each containing a dynamic data block. The configuration information of the dynamic data block includes: a data block index, a universally unique identifier, a data block length, a data block offset address, a memory address pointer, and an information table. The step of determining the starting write address in the dynamic data storage area based on the data block index, and storing the data to be stored into the target data block based on the starting write address, includes: Read the dynamic data blocks in the dynamic data storage area and determine the most recent storage address of the most recently activated sector; The configuration information of the target data block is determined based on the data block index of the target data block, and the remaining length of the sector of the most recently activated sector is determined according to the sector length, sector base address and most recently stored address of the most recently activated sector, and it is determined whether the remaining length of the sector of the most recently activated sector is greater than the data block length of the target data block. If the remaining length of the most recently activated sector is greater than the length of the target data block, then the most recently activated storage address in the most recently activated sector is used as the starting write address in the dynamic data storage area, and the data to be stored is stored into the target data block based on the starting write address and the configuration information of the target data block.
6. The method according to claim 5, characterized in that, Based on the data block index, the configuration information of the target data block is determined. Then, based on the sector length, sector base address, and most recent storage address of the most recently activated sector, the remaining sector length of the most recently activated sector is determined. After determining whether the remaining sector length of the most recently activated sector is greater than the data block length of the target data block, the process further includes: If the remaining sector length of the most recently activated sector is less than or equal to the data block length of the target data block, then the sector index of the next sector of the most recently activated sector in the dynamic data storage area is set to the switching state, and the sector index of the most recently activated sector is set to the data full state. Traverse the dynamic data storage sectors in the dynamic data storage area and store the target data block in the sector following the most recently activated sector; After traversing the dynamic data storage sectors in the dynamic data storage area, delete the dynamic data blocks in the two sectors below the most recently activated sector, update the sector status of the two sectors below the most recently activated sector to the erased state, and update the sector status of the sector next to the most recently activated sector to the in-use state.
7. The method according to claim 5, characterized in that, Reading dynamic data blocks in the dynamic data storage area includes: Based on the sector index of the most recently activated sector, dynamic data blocks are read starting from the most recently activated sector. The data block address of the data block being read in the dynamic data storage area is determined according to the sector base address and sector storage capacity of the dynamic data storage sector in the dynamic data storage area. If the data block status information of the data block being read is not empty, then the data block status information of the data block being read is read based on the data block address; Based on the data block status information of the read data block, determine whether the fixed fields of the read data block are consistent with the preset field values; If the fixed field of the read data block is consistent with the pre-set field value, then the data block identity and data block length of the read data block are determined according to the data block status information of the read data block; The data block identity and data block length of the data block being read are verified based on the configuration information of the data block being read; If the verification passes, the data of the read data block is read, and a cyclic redundancy check is performed on the data of the read data block. If the verification passes, the sector index of the most recently activated sector and the data block storage address of the read data block are recorded, the data block storage status of the read data block is determined to be valid, and the data in the read data block is copied to the random access memory address space of the read data block. Determine whether the data block address of the read data block is less than the highest sector address of the most recently activated sector in the dynamic data storage area; If so, it is determined that the dynamic data block in the most recently activated sector has been read. Based on the sector index of the most recently activated sector and the number of sectors of the dynamic data storage sector in the dynamic data storage area, the sector index of the next sector of the most recently activated sector is determined, and based on the sector index of the next sector, it is determined whether the dynamic data block in the next sector has been read. If so, then it is determined that the reading of the dynamic data block in the dynamic data storage area has been completed.
8. A data storage device, characterized in that, The data storage device includes: The data storage request acquisition module is used to acquire data storage requests and determine the target storage area and the data block index of the target data block from the candidate storage areas of the memory according to the data storage request; the data storage request includes the data to be stored, the data block index of the target data block, and the storage area identifier of the target storage area; the candidate storage areas include static data storage areas and dynamic data storage areas; A static data storage module is used to determine the physical address of the target data block according to the data block index of the target data block if the target storage area is a static data storage area, and to store the data to be stored into the target data block based on the physical address of the target data block. The dynamic data storage module is used to determine the starting write address in the dynamic data storage area based on the data block index of the target data block if the target storage area is a dynamic data storage area, and to store the data to be stored into the target data block based on the starting write address; the starting write address is the nearest storage address of the most recently activated sector of the last stored data in the dynamic data storage area.
9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 7.