Flash memory structure, power failure detection method and query method

CN122111343APending Publication Date: 2026-05-29FUJIAN XINGHAI COMM TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202610294040.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-03-11
Publication Date
2026-05-29

Smart Images

  • Figure CN122111343A_ABST
    Figure CN122111343A_ABST
Patent Text Reader

Abstract

The application discloses a FLASH storage structure, a power failure detection method and a query method, and is provided with a plurality of data blocks for storage, at least one data block comprising a first transaction sector, a second transaction sector, a first preset number of backup sectors and a second preset number of data record sectors; other data blocks comprising data record sectors; the first transaction sector comprising a write position pointer for recording current data, index information of timestamps of each data block and address information of backup sectors; the write position pointer is used for controlling the ring writing of the data record sectors; the index information of the timestamps is used for positioning a target data block. Through the above structure design, the application can guarantee the consistency and recoverability of data under abnormal conditions such as power failure, and improve the reliability and stability of the whole storage system.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of data storage technology, and in particular to a FLASH storage structure, a power failure detection method, and a query method. Background Technology

[0002] With the development of embedded systems, IoT devices, and industrial control equipment, a large number of devices need to continuously record operating logs, sensor data, or status information in non-volatile memory. Because FLASH memory has advantages such as large storage capacity, low cost, and data retention even when power is off, it is widely used in various embedded devices for data recording and historical data storage.

[0003] In practical applications, FLASH typically uses sequential writing to record data, for example, writing data to each storage sector in log format. When the system needs to query historical data, it usually needs to retrieve records in the FLASH based on a time range. However, in existing technologies, FLASH log systems often need to scan the entire storage space record by record when querying data. Especially when the storage capacity is large, the query process consumes a lot of time and system resources, making it difficult to meet the needs of fast data retrieval.

[0004] Furthermore, power loss is a relatively common anomaly during the operation of embedded devices. When a sudden power loss occurs during data writing, the current sector data may not be completely written, resulting in data structure corruption or loss of index information. Once transaction information or data structures are damaged, the system may not be able to correctly locate existing data records after power is restored. In severe cases, it may even be necessary to reinitialize the entire FLASH memory, resulting in the loss of existing data.

[0005] While existing technologies mitigate the risk of data corruption through simple verification mechanisms or repeated writes, achieving both data reliability and efficient data retrieval remains challenging in practical applications. For instance, some solutions enhance reliability by increasing data redundancy, but this significantly increases the number of writes to the FLASH memory and reduces storage utilization. Other solutions, while capable of data recovery, still require traversing large amounts of stored data during queries, resulting in low query efficiency. Summary of the Invention

[0006] The technical problem to be solved by this invention is to provide a FLASH storage structure, a power failure detection method, and a query method that can ensure data consistency and recoverability under abnormal conditions such as power failure.

[0007] To solve the above-mentioned technical problems, the technical solution adopted by the present invention is as follows: A FLASH storage structure includes multiple data blocks for storage, at least one of the data blocks including a first transaction sector, a second transaction sector, a first preset number of backup sectors and a second preset number of data record sectors; the other data blocks include data record sectors; The first transaction sector includes a write position pointer for recording the current data, index information of the timestamps of each data block, and address information of the backup sector; the write position pointer is used to control the circular writing of the data recording sector; the index information of the timestamps is used to locate the target data block; After any data update is completed, the data recording sector is copied to the backup sector, and the address information of the backup sector in the first transaction sector is updated; the second transaction sector is used to store the mirror data of the first transaction sector.

[0008] To solve the above-mentioned technical problems, the present invention adopts other technical solutions as follows: A power-loss detection method for FLASH memory, applied to the aforementioned FLASH storage structure, includes the following steps: Read the first verification information of the first transaction sector and perform integrity verification on the first transaction sector based on the first verification information; When the first transaction sector fails to verify, the first transaction sector is restored based on the mirror data of the second transaction sector. When the first transaction sector verification passes, the data record sector running before the power failure is located according to the write position pointer of the first transaction sector, and the integrity of the data record sector is verified according to the second verification information of the data record sector; when the verification of the data record sector fails, the data record sector is restored according to the corresponding data in the backup sector.

[0009] A FLASH query method is applied to a FLASH storage structure, wherein each data record sector includes a data storage area and a metadata area; the metadata area is set after the data storage sector and is used to record the timestamp of the stored data in the corresponding data record sector; The query method includes the following steps: In response to the target query request, extract the start timestamp and end timestamp from the target query request; Based on the index information of the timestamp of the first transaction sector, locate the target data block in all data blocks that is greater than or equal to the start timestamp and less than or equal to the end timestamp; Based on the timestamp of the metadata area, data records that meet the target query requirements are filtered out from the target data block.

[0010] The beneficial effects of this invention are as follows: By setting up a first transaction sector, a second transaction sector, a backup sector, and a data recording sector, reliable management of FLASH log data is achieved. The first transaction sector is used to record the current data write position pointer, the timestamp index information of each data block, and the backup sector address information. The circular writing of the data recording sector is controlled by the write position pointer, allowing the data recording area to be automatically recycled when space is exhausted, thereby avoiding waste of FLASH storage space. Simultaneously, each data block is identified by the timestamp index information, enabling rapid location of the target data block during query execution, thus avoiding scanning the entire FLASH storage space line by line and improving query efficiency.

[0011] Furthermore, by configuring the data recording sector to be copied to the backup sector after an update and updating the backup sector's address information, a data snapshot is created during the FLASH write process. In the event of an abnormal power outage, the complete data in the backup sector can be used for recovery, avoiding data structure corruption caused by write interruptions. Further, a second transaction sector mirrors the first transaction sector. If the first transaction sector experiences an anomaly, the mirrored data can be used for recovery, ensuring the reliability of transaction information. Through this structural design, the FLASH storage system achieves efficient data recording and retrieval while ensuring data consistency and recoverability in abnormal situations such as power outages, improving the overall reliability and stability of the storage system. Attached Figure Description

[0012] Figure 1 This is a schematic diagram of a FLASH storage structure according to an embodiment of the present invention; Figure 2 This is a flowchart of a power-down detection method for FLASH according to an embodiment of the present invention; Figure 3 This is a flowchart illustrating a power-down detection method for FLASH memory according to an embodiment of the present invention. Detailed Implementation To explain in detail the technical content, objectives, and effects of the present invention, the following description is provided in conjunction with the embodiments and accompanying drawings.

[0013] In existing technologies, FLASH memory is widely used in embedded devices, industrial control systems, IoT terminals, and data acquisition equipment due to its advantages such as non-volatility, fast read / write speeds, and low cost. It is used to store device operation logs, historical data records, and status information. In these applications, systems typically need to write data to the FLASH storage space in a continuous recording manner and query historical data based on time range or recording order when needed. However, in existing technologies, FLASH log storage structures mostly use sequential writing and simple indexing for data management. When the system needs to query historical data, it often needs to scan the entire storage space or a large number of data records one by one, resulting in low query efficiency, especially when the data volume is large, the query time problem becomes more pronounced. Furthermore, in actual operation, devices may experience abnormal power outages. If a power outage occurs during FLASH writing, it can easily lead to incomplete data in the currently written sector or corrupted index information, thus affecting subsequent data reading and location. Although existing technologies improve data reliability through simple verification or redundant writing mechanisms, it is still difficult to achieve efficient data querying while ensuring data consistency; therefore, there is still room for further improvement.

[0014] To at least solve the above problems, please refer to Figure 1 This invention provides a FLASH storage structure with multiple data blocks for storage. At least one of the data blocks includes a first transaction sector, a second transaction sector, a first preset number of backup sectors, and a second preset number of data record sectors; the other data blocks include data record sectors. The first transaction sector includes a write position pointer for recording the current data, index information of the timestamps of each data block, and address information of the backup sector; the write position pointer is used to control the circular writing of the data recording sector; the index information of the timestamps is used to locate the target data block; After any data update is completed, the data recording sector is copied to the backup sector, and the address information of the backup sector in the first transaction sector is updated; the second transaction sector is used to store the mirror data of the first transaction sector.

[0015] As described above, the beneficial effects of this invention are as follows: by setting up a first transaction sector, a second transaction sector, a backup sector, and a data recording sector, reliable management of FLASH log data is achieved. The first transaction sector is used to record the current data write position pointer, the timestamp index information of each data block, and the backup sector address information. The circular writing of the data recording sector is controlled by the write position pointer, enabling the data recording area to be automatically recycled when space is exhausted, thereby avoiding waste of FLASH storage space. Simultaneously, by identifying each data block using the timestamp index information, the target data block can be quickly located during query execution, thereby avoiding scanning the entire FLASH storage space line by line and improving query efficiency.

[0016] Furthermore, by configuring the data recording sector to be copied to the backup sector after an update and updating the backup sector's address information, a data snapshot is created during the FLASH write process. In the event of an abnormal power outage, the complete data in the backup sector can be used for recovery, avoiding data structure corruption caused by write interruptions. Further, a second transaction sector mirrors the first transaction sector. If the first transaction sector experiences an anomaly, the mirrored data can be used for recovery, ensuring the reliability of transaction information. Through this structural design, the FLASH storage system achieves efficient data recording and retrieval while ensuring data consistency and recoverability in abnormal situations such as power outages, improving the overall reliability and stability of the storage system.

[0017] In some implementations, the first transaction sector further includes a start sector address and an end sector address, which are used to define the circular write range of the data record sector.

[0018] As described above, a start sector address and an end sector address are added to the first transaction sector to define the circular write range of the data recording sector. This technical feature allows for flexible definition of the boundaries of the data recording area within the FLASH storage space, clearly separating the data recording area from functional areas such as the transaction management area and backup area.

[0019] By setting start and end sector addresses, the system can perform cyclic writing within a specified range during data writing. When the writing position reaches the end sector address, the writing position pointer automatically returns to the start sector address, thus achieving a stable circular writing structure. This structure prevents write operations from overwriting other functional areas and also facilitates flexible configuration of the data recording area capacity according to actual application needs. Therefore, this technical solution not only improves the flexibility of system configuration but also enhances the security of data storage management.

[0020] Please refer to Figure 2 A power-loss detection method for FLASH memory, applied to a FLASH storage structure, includes the following steps: Read the first verification information of the first transaction sector and perform integrity verification on the first transaction sector based on the first verification information; When the first transaction sector fails to verify, the first transaction sector is restored based on the mirror data of the second transaction sector. When the first transaction sector verification passes, the data record sector running before the power failure is located according to the write position pointer of the first transaction sector, and the integrity of the data record sector is verified according to the second verification information of the data record sector; when the verification of the data record sector fails, the data record sector is restored according to the corresponding data in the backup sector.

[0021] As described above, the integrity of stored data is checked by setting verification information in both the first transaction sector and the data record sector. Upon startup, the system first reads the first transaction sector and performs integrity verification based on the first verification information to determine whether the transaction information was completely written before power failure. If the first transaction sector verification fails, the mirrored data stored in the second transaction sector is used to restore the first transaction sector, thus ensuring the consistency of the transaction information.

[0022] When the first transaction sector verification passes, the system locates the data record sector that was running before the power outage based on the write position pointer recorded therein, and performs an integrity check on that data record sector. If the data record sector verification fails, the corresponding complete data is read from the backup sector for recovery. Through the above detection and recovery process, the system can automatically determine the FLASH storage status and recover abnormal data structures after power-on, thereby ensuring data consistency after an abnormal power outage. This technical solution effectively avoids the data corruption problems that may occur in traditional FLASH log systems during power outages, improving system reliability.

[0023] In some implementations, the steps also include: When the integrity checks of both the first transaction sector and the second transaction sector are abnormal, the FLASH storage structure is initialized.

[0024] As described above, it is further stipulated that when the integrity checks of both the first and second transaction sectors fail, the FLASH storage structure is initialized. This technical solution allows the system to be restored to a runnable state by re-initializing the storage structure, even in the extreme case where transaction information is completely corrupted.

[0025] In practical applications, if both the transaction sector and the mirrored transaction sector experience anomalies, it indicates that the system may have undergone severe abnormal writes or initial power-on failure. By performing initialization operations, the transaction management structure and the initial state of the data record area can be re-established, thereby preventing the system from entering an unrecoverable state due to the inability to parse transaction information. This solution ensures that the system can rebuild a workable storage environment under various abnormal conditions, improving the system's robustness.

[0026] In some implementations, the step of restoring the data record sector based on the corresponding data in the backup sector further includes the following steps: Update the write position pointer in the first transaction sector so that the write position pointer points to the position of the last valid record in the recovered data record sector.

[0027] As described above, after performing data record sector recovery, the write position pointer in the first transaction sector is further updated to point to the position of the last valid record in the recovered data record sector. This technical feature ensures that the system's write position remains consistent with the data record state after the recovery operation is completed.

[0028] If the write position pointer is not updated after data recovery, subsequent write operations may overwrite existing valid data, resulting in data loss. By updating the write position pointer to the position of the last valid record, it can be ensured that subsequent write operations continue to execute in the correct position, thereby maintaining the continuity and integrity of data records.

[0029] In some implementations, the step of restoring the data record sector based on the corresponding data in the backup sector specifically includes: Based on the current backup sector address recorded in the first transaction sector, read the complete data of the corresponding backup sector and write it back to the data record sector.

[0030] As described above, the specific method for data record sector recovery is further defined: based on the current backup sector address recorded in the first transaction sector, complete data is read from the corresponding backup sector and written back to the data record sector. This method allows the system to quickly locate the backup data, thereby reducing traversal operations on the FLASH storage space during recovery and improving recovery efficiency.

[0031] Because the backup sector saves a complete snapshot after the data recording sector is updated, the abnormal sector can be recovered simply by reading the corresponding data directly from the backup address during the recovery process, thus ensuring the accuracy and efficiency of the recovery operation.

[0032] A FLASH query method is applied to a FLASH storage structure, wherein each data record sector includes a data storage area and a metadata area; the metadata area is set after the data storage sector and is used to record the timestamp of the stored data in the corresponding data record sector; The query method includes the following steps: In response to the target query request, extract the start timestamp and end timestamp from the target query request; Based on the index information of the timestamp of the first transaction sector, locate the target data block in all data blocks that is greater than or equal to the start timestamp and less than or equal to the end timestamp; Based on the timestamp of the metadata area, data records that meet the target query requirements are filtered out from the target data block.

[0033] As described above, by setting up a metadata area in the data record sector to record timestamp information and combining it with the timestamp index information in the first transaction sector, the system can quickly locate the target data block. The system first extracts the starting timestamp based on the query requirements, and then uses the timestamp index information stored in the transaction sector to locate the target data block containing that timestamp among all data blocks, thereby significantly narrowing the search range.

[0034] Subsequently, by reading the timestamp information of the metadata area in the data record sector, the records in the target data block are filtered to obtain the data records that meet the query conditions. This technical solution avoids the traditional method of scanning FLASH storage space record by record, ensuring that the query operation is performed only within a limited scope, thus improving query efficiency.

[0035] In some implementations, data records that meet the target query requirements are filtered from the data block based on the timestamp of the metadata area, specifically including: Read the metadata area in the data record sector corresponding to the target data block; Traverse the timestamps in the metadata area and compare them with the starting timestamp to locate the first target stored data in the data storage area that is greater than or equal to the starting timestamp; Starting from the timestamp of the target stored data and ending from the timestamp, a target interval is formed. Stored data that satisfies the target interval in the data storage area is extracted and output as the query result.

[0036] As described above, the method for locating data records during the query process is further defined. By traversing the timestamps in the metadata area and comparing them with the start timestamp, the first data record in the data storage area that meets the query conditions can be located. Subsequently, the end timestamp is extracted, and a query interval is constructed with the target stored data's timestamp as the start and the end timestamp as the end, thereby retrieving all data records within the target interval.

[0037] This method enables data retrieval based on time intervals, allowing for quick location of the query starting point and continuous extraction of records within the target time range, thereby improving query efficiency.

[0038] In some implementations, reading the metadata area in the data record sector corresponding to the target data block specifically includes: Read the timestamp of the metadata area in the data record sector of the target data block one by one. If the timestamp of the metadata area in the current data record sector cannot meet the starting timestamp, switch to the next data record sector in the target data block for reading until a data record sector that meets the starting timestamp is located.

[0039] As described above, the method for locating data record sectors within a data block is further defined. This is achieved by reading the timestamp information from the metadata area of ​​each data record sector one by one, and switching to the next data record sector when the current sector cannot meet the starting timestamp condition. This sector-by-sector scanning method avoids scanning records one by one within a single sector, thereby further improving positioning efficiency.

[0040] Meanwhile, this technical solution enables rapid identification of data sectors through timestamp information in the metadata area, making the query process more efficient.

[0041] In some implementations, the steps also include: Select the first data record sector in the target data block, and read the timestamp of the last stored data in the first data record sector as the last timestamp; The last timestamp is compared with the starting timestamp. When the last timestamp is greater than the starting timestamp, the current data record sector is determined as the target data record sector, and the position of the current data record sector is returned. When the last timestamp does not meet the starting timestamp condition, the last timestamp of the next data record sector is read and the comparison continues until the target data record sector is determined.

[0042] As described above, a method for locating data record sectors based on the last timestamp is proposed. The system first reads the last timestamp of the first data record sector in the target data block and compares it with the start timestamp. If the last timestamp is greater than the start timestamp, it can be determined that the sector contains the target data record; otherwise, the system continues to read the last timestamp of the next sector for comparison.

[0043] Since the last timestamp directly reflects the maximum time value recorded in the current sector, comparing the last timestamp can quickly determine whether the target data is likely to exist in that sector, thereby reducing the number of accesses to irrelevant sectors. Compared to the positioning method based on the first timestamp, this method can determine the location of the target sector faster, thus improving query efficiency.

[0044] Please refer to Figure 1 Embodiment 1 of the present invention is as follows: This invention provides a FLASH storage structure, which includes multiple data blocks for storing data. At least one of the data blocks includes a first transaction sector, a second transaction sector, a first preset number of backup sectors, and a second preset number of data record sectors, while the remaining data blocks include data record sectors.

[0045] The first transaction sector is used to record the write position pointer of the current data, the timestamp index information of each data block, and the address information of the backup sector. The write position pointer is used to control the circular writing of the data record sector; the timestamp index information is used to time-identify the data records in each data block to locate the target data block when performing a query operation.

[0046] In this embodiment, after the data in the data recording sector is updated, the updated data recording sector is copied to the backup sector, and the backup sector address information recorded in the first transaction sector is updated simultaneously, thereby forming a backup snapshot of the data recording sector. In this way, in the event of an abnormal power outage or write interruption, the data in the backup sector can be used to restore the data recording sector, thus ensuring the integrity of the data structure.

[0047] Furthermore, the second transaction sector is used to store mirror data of the first transaction sector. When the first transaction sector encounters an anomaly or data corruption, it can be recovered using the mirror data in the second transaction sector, thereby ensuring the reliability of transaction information.

[0048] The above structural design enables the FLASH storage system to reliably manage log data. Specifically, by using the write position pointer recorded in the first transaction sector, the data recording sectors can be written in a circular manner, allowing the data recording area to be reused after space is exhausted, thus avoiding waste of FLASH storage space. Simultaneously, by identifying each data block with timestamp index information, the target data block can be quickly located during query operations, reducing the need for scanning the entire FLASH storage space line by line and improving query efficiency.

[0049] Furthermore, by setting up a backup mechanism for data record sectors and a mirroring mechanism for transaction sectors, the system can still recover the data structure in the event of an abnormal power outage, thereby ensuring the data consistency and reliability of the FLASH storage system.

[0050] In some implementations, the first transaction sector further includes a start sector address and an end sector address to define the circular write range of the data recording sector. By setting the start sector address and the end sector address, the data recording area in the FLASH storage space can be boundary-defined, clearly separating the data recording area from functional areas such as the transaction management area and the backup area.

[0051] In this implementation, when the data write position reaches the end sector address, the write position pointer will automatically return to the start sector address, thus forming a stable circular write structure. This structure prevents write operations from overwriting other functional areas and allows for flexible configuration of the data recording area according to actual application needs, thereby improving system configuration flexibility and data storage management security.

[0052] In this embodiment, the specific application of the above storage structure is as follows: The FLASH memory is divided into fixed sector sizes, with each sector having a capacity of 4KB. Every 16 sectors constitute a data block, and the entire FLASH contains a total of 4096 sectors. By functionally dividing these sectors, the FLASH simultaneously possesses functions such as transaction management, data recording, and data backup.

[0053] In this embodiment, at least one data block includes a first transaction sector, a second transaction sector, a backup sector, and multiple data record sectors. The first transaction sector records the current data write position pointer, the timestamp index information of each data block, and the address information of the backup sector; the second transaction sector stores the mirror data of the first transaction sector.

[0054] The following information can be recorded in the first transaction sector: The write position pointer of the current data is recorded at the starting offset position of the first transaction sector, which is used to indicate the position of the data block and data record sector where the current data record is located; The timestamp index information of each data block is recorded in the first transaction sector to identify the time range of the data in each data block, so as to quickly locate the target data block when performing a query operation; The first transaction sector can also record the number of FLASH cycle writes, the number of FLASH reads and writes, and FLASH health status information, which can be used to monitor the operating status of the FLASH storage device. The first transaction sector also records the start address, end address, and current backup address of the backup sector, which is used to manage the usage of the backup sector. The first transaction sector also records the starting and ending sector addresses of the data recording sector, which are used to define the circular write range of the data recording sector.

[0055] In this embodiment, the backup sector is used to store a backup snapshot of the data recording sector. After any data recording sector completes a data update, the system copies that data recording sector to the backup sector and updates the backup sector address information in the first transaction sector, thereby forming backup data for the data recording sector. If an abnormal power failure occurs during the writing process, the data in the backup sector can be used to restore the data recording sector, thus ensuring the integrity of the data records.

[0056] In this embodiment, multiple data recording sectors are used to store target data. Each data recording sector includes a data storage area and a metadata area. The data storage area is used to sequentially store data records and supports variable-length data record formats; the metadata area is located at the end of the data recording sector and is used to record the recording information of that data recording sector.

[0057] The following information can be recorded in the metadata area: Record the address information of the first data record in the data record sector; Record the address index information of each data record in the data record sector; Record the current number of data records in this data record sector; Record the health status information of this data sector; Record the timestamp of the first data record in this data record sector and the timestamp of the latest data record; Record the remaining available storage space information of this data record sector; Record the verification information of the data record sector to ensure the integrity of the metadata area data.

[0058] The aforementioned metadata area allows for quick location of target data records within data record sectors during query operations, eliminating the need to scan the entire data storage area line by line, thereby improving data query efficiency.

[0059] In this embodiment, the remaining data blocks are all set as data recording sectors, with the same structure as the aforementioned data recording sectors, and are organized according to a unified data structure. In this way, a unified data recording structure can be formed throughout the entire FLASH storage space.

[0060] During system operation, once any data record sector has been written, the system copies that data record sector to a backup sector and updates the backup address information in the first transaction sector. Simultaneously, when transaction information is updated, the data in the first transaction sector is copied to the second transaction sector, thus ensuring the reliability of transaction information.

[0061] The following technical effects can be achieved using the above-described FLASH storage structure: By using the write position pointer and timestamp index information recorded in the first transaction sector, the target data block can be quickly located, thereby reducing the traversal of the entire FLASH storage space during the query process; By using the metadata area in the data record sector, the location of the data record can be quickly located, thereby improving data query efficiency; By backing up the data recording sector using backup sectors, the data structure of the data recording sector can be restored in the event of an abnormal power outage. By mirroring the first transaction sector using the second transaction sector, transaction information can be recovered in case of abnormalities.

[0062] Therefore, the above-mentioned FLASH storage structure can ensure data reliability while achieving efficient data recording and fast data retrieval, thereby improving the operating efficiency and stability of the entire FLASH storage system.

[0063] Please refer to Figure 2 Embodiment two of the present invention is as follows: In this embodiment, a power-loss detection method for FLASH memory is provided, which is applied to the FLASH storage structure described in Embodiment 1. The FLASH storage structure includes a first transaction sector, a second transaction sector, a backup sector, and multiple data recording sectors. The first transaction sector is used to record write position pointers and timestamp index information, the second transaction sector is used to store mirror data of the first transaction sector, and the backup sector is used to store backup data of the data recording sectors.

[0064] In this embodiment, to ensure that the FLASH storage system can still recover its normal data structure in the event of an abnormal power outage, verification information is set in both the first transaction sector and the data record sector. Specifically, the first transaction sector contains first verification information to verify the data integrity of the first transaction sector; the data record sector contains second verification information to verify the data integrity of the data record sector.

[0065] When the system powers on, it first reads the first verification information from the first transaction sector and performs an integrity check on the first transaction sector based on the first verification information. If the first transaction sector verification fails, it indicates that the first transaction sector may have been in the process of being written before the power failure. In this case, the mirror data stored in the second transaction sector is read and used to restore the first transaction sector, thereby ensuring the consistency of transaction information.

[0066] If the first transaction sector verification passes, the data record sector being written before the power failure is determined based on the write position pointer recorded in the first transaction sector, and the integrity of the data record sector is verified based on the second verification information in that data record sector. If an abnormal data record sector verification is detected, it indicates that the data record sector may not have been fully written before the power failure. In this case, the data record sector is restored based on the corresponding backup data stored in the backup sector.

[0067] Through the above detection and recovery process, the operating status of the FLASH storage structure can be automatically determined after the system is powered on, and abnormal data can be recovered. This ensures that the FLASH storage system can maintain the integrity of the data structure in the event of an abnormal power failure, thereby improving the reliability of system operation.

[0068] In some implementations, when the integrity checks of both the first and second transaction sectors fail, an initialization process is performed on the FLASH storage structure. This method allows for the reconstruction of the FLASH storage structure even in extreme cases where transaction information is completely corrupted, restoring the system to a runnable state. In practical applications, when both the transaction sector and its mirrored counterpart fail, it may indicate that the system has experienced a severe write failure or is undergoing its first power-on operation. By performing the initialization operation, the initial state of the transaction management structure and data record area can be re-established, thereby preventing the system from entering an unrecoverable state due to the inability to parse transaction information and improving system robustness.

[0069] In some implementations, after restoring the data record sectors based on the data in the backup sectors, the process further includes updating the write position pointer in the first transaction sector so that the write position pointer points to the position of the last valid record in the restored data record sector. This ensures that the system's write position remains consistent with the current data record state after the data recovery operation is complete. If the write position pointer is not updated after data recovery, subsequent write operations may overwrite existing valid data, leading to data loss. By updating the write position pointer to the position of the last valid record, it ensures that subsequent write operations continue to execute at the correct position, thereby maintaining the continuity and integrity of the data records.

[0070] In some implementations, the process of restoring a data recording sector based on data in the backup sector includes: reading the complete data in the corresponding backup sector according to the current backup sector address recorded in the first transaction sector, and writing the data back to the corresponding data recording sector. In this way, the system can directly locate the backup data, thereby reducing traversal operations on the FLASH storage space during the recovery process and improving recovery efficiency. Simultaneously, since the backup sector saves a complete snapshot after the data recording sector is updated, the abnormal sector can be restored simply by reading the corresponding data directly from the backup address during the recovery process, thus ensuring the accuracy and efficiency of the recovery operation.

[0071] Specifically, to further illustrate the implementation process of the FLASH power failure detection method described in Embodiment 2.

[0072] Please refer to Figure 3 When the system powers on, the microcontroller (MCU) first reads the data from the first transaction sector (corresponding to SECTOR0 in this embodiment) and performs an integrity check on the sector based on the first verification information recorded in the first transaction sector (CRC check can be used). For example, the MCU calculates a check value based on the data in the first transaction sector and compares the calculated check value with a checksum pre-stored in the first transaction sector. When the check values ​​match, it indicates that the data in the first transaction sector is not abnormal.

[0073] When the first transaction sector verification passes, the system determines the position of the currently being written data record sector based on the write position pointer recorded in the first transaction sector, and reads the data from the corresponding data record sector. Then, it performs an integrity check on the data record sector based on the second verification information in that data record sector. If the verification result passes, it indicates that the data record sector has not experienced any abnormalities, and no power-down recovery operation is required. The system then loads the relevant parameters from the first transaction sector into the MCU's random access memory (RAM) for subsequent data recording and query operations, and then ends the FLASH detection process.

[0074] When a verification anomaly is detected in the current data record sector, it indicates that the data record sector may have been in the process of being written before the power failure. At this time, the system reads the corresponding backup sector data based on the backup sector address information recorded in the first transaction sector, and uses the data in the backup sector to restore the current data record sector. After the recovery operation is completed, the relevant parameters in the first transaction sector are loaded into the MCU's RAM, enabling the system to continue to operate normally.

[0075] When the system detects an anomaly in the first transaction sector during the initial detection process, it further reads the data from the second transaction sector (corresponding to SECTOR01 in this embodiment) and performs integrity verification based on the verification information in the second transaction sector. If the second transaction sector verification passes, it indicates that the first transaction sector may have been abnormally written before the power failure. In this case, the system copies the data from the second transaction sector to the first transaction sector, thereby restoring the data structure of the first transaction sector. After restoration, the integrity detection of the data record sector is performed again according to the above process.

[0076] If both the verification of the first and second transaction sectors fails, it indicates that the FLASH memory may be in its first running state or the transaction data structure may be completely corrupted. In this case, the system performs initialization processing on the FLASH storage structure. The initialization process includes clearing the first and second transaction sectors and writing default initialization parameters and verification information, while also initializing the metadata structure of the data record sectors, such as initializing the metadata area of ​​the data record sectors and the corresponding verification information. After initialization is complete, the system terminates the FLASH detection process.

[0077] Reference Figure 3 The power failure detection method first checks the status of the first transaction sector. If the first transaction sector is abnormal, it uses the second transaction sector for recovery. When the transaction information is normal, it further checks the data integrity of the current data record sector and uses the backup sector for recovery when an anomaly is detected. In extreme cases, when all transaction information is damaged, the FLASH storage structure is re-established through initialization.

[0078] Through the above detection and recovery process, the system can automatically detect whether there are any abnormalities in the FLASH storage structure when the system is powered on, and perform transaction recovery or data recovery operations when necessary, thereby ensuring the data consistency and structural integrity of the FLASH storage system after an abnormal power failure, and improving the stability and reliability of the system operation.

[0079] Embodiment 3 of the present invention is as follows: A FLASH query method is disclosed, applied to the FLASH storage structure described in Embodiment 1. The FLASH storage structure includes a first transaction sector, a second transaction sector, a backup sector, and multiple data record sectors, wherein each data record sector includes a data storage area and a metadata area. The metadata area is located at the end of the data record sector and is used to record the timestamp information and related record information of each stored data in the corresponding data record sector.

[0080] In this embodiment, when the system receives a target query request, it first extracts the start and end timestamps from the request. Then, based on the timestamp index information recorded in the first transaction sector, it locates the target data block among all data blocks whose time range is greater than or equal to the start timestamp. By utilizing the timestamp index information recorded in the first transaction sector, data blocks that may contain the target data can be quickly located among multiple data blocks, thereby significantly reducing the query scope and avoiding a line-by-line scan of the entire FLASH storage space.

[0081] After identifying the target data block, the system reads the data record sectors within that block and obtains the timestamp information corresponding to each stored data item through the metadata area within those sectors. Then, based on the end timestamp, the system filters the timestamps in the metadata area to determine the data records that meet the target query requirements.

[0082] In some implementations, to further improve query efficiency, the system first reads the metadata area in the data record sector corresponding to the target data block and traverses the timestamp information in the metadata area. By comparing the timestamp in the metadata area with the starting timestamp, the first data record in the data storage area whose timestamp is greater than or equal to the starting timestamp can be located. Subsequently, using the timestamp of the target data record as the starting point and the ending timestamp as the ending point, a target query interval is constructed, thereby extracting all data records in the data storage area that are located within this time interval and outputting them as the query result.

[0083] In some implementations, when locating a target data record sector, the timestamp information in the metadata area of ​​each data record sector in the target data block can be read one by one. If the timestamp information in the current data record sector does not meet the starting timestamp condition, the system automatically switches to the next data record sector in the target data block to continue reading until a data record sector that meets the starting timestamp condition is located. This sector-by-sector scanning method avoids scanning records one by one within a single data record sector, thereby improving query and location efficiency.

[0084] In some implementations, to further improve positioning efficiency, the timestamp of the last stored data in a data recording sector can be used for rapid determination. Specifically, the system first selects the first data recording sector in the target data block and reads the timestamp of the last data record in that sector as the last timestamp. Then, this last timestamp is compared with the starting timestamp. When the last timestamp is greater than the starting timestamp, it can be determined that the current data recording sector may contain the target data record, thus identifying that sector as the target data recording sector. When the last timestamp does not meet the starting timestamp condition, the system continues to read and compare the last timestamp of the next data recording sector until a data recording sector containing the target data record is determined.

[0085] Since the last timestamp directly reflects the maximum time value recorded in the current data record sector, comparing the last timestamp allows for a quick determination of whether the target data might exist in that data record sector, thereby reducing the number of accesses to irrelevant data record sectors. Compared to locating based on the first timestamp, this method can determine the location of the target data record sector faster, further improving query efficiency.

[0086] Using the above query method, data blocks can be quickly located using the timestamp index information in the first transaction sector, and data records can be filtered by combining the metadata area in the data record sector, thereby achieving fast data query based on time intervals. Compared with the traditional query method that requires scanning the entire FLASH storage space record by record, this embodiment significantly narrows the query range through a multi-level time index structure, thereby greatly improving query efficiency and enabling high-speed retrieval of FLASH log data.

[0087] The above method is applied in the following specific ways: When the system enters the FLASH query process, it first obtains the start and end timestamps based on the query requirements. Then, the system reads the timestamp index information from the first transaction sector and, based on this index, locates data blocks with timestamps greater than or equal to the start timestamp among multiple data blocks, thereby determining the starting position of the target data block. By utilizing the data block time indexes recorded in the first transaction sector, data blocks potentially containing the target data can be quickly located throughout the entire FLASH storage space, significantly reducing the query scope.

[0088] After identifying the target data block, the system reads the first data record sector within that block and obtains the timestamp of the first data record in that sector through the metadata area. This timestamp is then compared to the query start timestamp. If the timestamp is greater than or equal to the start timestamp, it indicates that the data record sector may contain data records that meet the query conditions. In this case, the system reads the corresponding data storage area and outputs the data.

[0089] If the timestamp of the currently read data record is less than the starting timestamp, the system continues reading the timestamp of the next record in the same data record sector and comparing it with the starting timestamp again. This process of reading timestamp information from the metadata area record by record continues until the first data record that meets the starting timestamp condition is located.

[0090] After the system outputs a data record that meets the conditions, it will further determine whether there are still unread data records in the current data record sector. If there are still subsequent records in the current data record sector, it will continue to read the timestamp of the next record and process it; if all records in the current data record sector have been read, it will switch to the next data record sector and read the timestamp of the first data record in that data record sector.

[0091] After locating the first data record that meets the start timestamp condition, the system begins filtering subsequent data based on the end timestamp. Specifically, the system reads the timestamp of the next data record and compares it with the end timestamp. If the timestamp of the data record is less than or equal to the end timestamp, the data record is output, and the system continues reading subsequent data records; if the timestamp of the data record is greater than the end timestamp, it indicates that the query time interval has ended, and data output stops.

[0092] During the entire query process, if the records in the current data record sector have been read completely but the end timestamp has not yet been reached, the system will switch to the next data record sector and read the timestamp of the first data record in that sector to continue the query operation.

[0093] The method described above first determines the target data block range using the timestamp index information in the first transaction sector. Then, it reads the metadata area information in the data record sector of the target data block to locate the first data record that meets the start timestamp condition. After locating the target record, it continuously reads subsequent data records based on the end timestamp until the query termination condition is met.

[0094] The above query process significantly reduces the scope of FLASH storage space traversal while ensuring query accuracy. Compared to traditional data query methods that require scanning the entire FLASH storage space record by record, this embodiment achieves multi-level time index positioning by combining transaction sector time index and data record sector metadata index, thereby significantly improving query efficiency and enabling high-speed retrieval of FLASH log data.

[0095] In practical applications, when a single data record is about 200 bytes in size and the FLASH capacity is 16MB, the traditional method of scanning one record at a time requires traversing a large number of data records. However, this embodiment can directly locate the target data block and data record sector through timestamp indexing, which significantly improves the average query speed compared to the traditional method, thereby improving the data query performance of the entire system.

[0096] Preferably, to further improve query efficiency, the data record sector can be quickly located by using the last timestamp recorded in the data record sector, thereby determining the starting data record sector containing the target data record.

[0097] Specifically, after identifying the target data block, the system first determines whether the current data block is the first data block. In this embodiment, a portion of the sectors in the first data block is used for transaction information management, therefore the starting position of its data record sectors differs from other data blocks. When the current data block is the first data block, the system reads the metadata area information starting from the preset starting data record sector of that data block; when the current data block is another data block, it reads the metadata area information starting from the first data record sector of that data block.

[0098] Subsequently, the system reads the timestamp of the last data record in the metadata area of ​​the current data record sector and compares it with the starting timestamp in the query request. When the last timestamp is greater than the starting timestamp, it indicates that the data record sector may contain data records that meet the query conditions. Therefore, the current data record sector can be identified as the target data record sector, and the location of the data record sector is returned. Then, the data query operation is performed starting from that data record sector.

[0099] When the last timestamp is less than or equal to the start timestamp, it means that all data records in the data record sector are earlier than the query start time. At this time, the system reads the last timestamp in the metadata area of ​​the next data record sector and continues to compare until the data record sector containing the target data record is determined.

[0100] In practical applications, when the system detects that the data in the next data record sector has not yet been written, it can be assumed that the current data record sector has reached the end of the data record area. If no data record sector that meets the conditions is found at this time, it means that there is no data record that meets the query conditions in the current FLASH storage structure.

[0101] Using the above method, the maximum time range of records in the current data record sector can be quickly determined by using the last timestamp in the data record sector, thereby determining whether the data record sector may contain the target data record. Compared with the traditional method of judging by the first timestamp, the last timestamp can directly reflect the upper bound of the time of records in the current data record sector, thus allowing for a faster determination of whether to continue traversing subsequent data record sectors, thereby reducing unnecessary sector accesses.

[0102] The above description is merely an embodiment of the present invention and does not limit the patent scope of the present invention. Any equivalent modifications made based on the content of the present invention specification and drawings, or direct or indirect applications in related technical fields, are similarly included within the patent protection scope of the present invention.

Claims

1. A FLASH storage structure, comprising multiple data blocks for storage, characterized in that, At least one of the data blocks includes a first transaction sector, a second transaction sector, a first preset number of backup sectors, and a second preset number of data record sectors; the other data blocks include data record sectors; The first transaction sector includes a write position pointer for recording the current data, index information of the timestamps of each data block, and address information of the backup sector; the write position pointer is used to control the circular writing of the data recording sector; the index information of the timestamps is used to locate the target data block; After any data update is completed, the data recording sector is copied to the backup sector, and the address information of the backup sector in the first transaction sector is updated. The second transaction sector is used to store mirror data of the first transaction sector.

2. The FLASH storage structure according to claim 1, characterized in that, The first transaction sector also includes a start sector address and an end sector address, which are used to define the circular write range of the data record sector.

3. A method for detecting power failure in FLASH memory, characterized in that, Applied to a FLASH storage structure as described in claim 1 or 2, comprising the following steps: Read the first verification information of the first transaction sector and perform integrity verification on the first transaction sector based on the first verification information; When the first transaction sector fails to verify, the first transaction sector is restored based on the mirror data of the second transaction sector. When the first transaction sector verification passes, the data record sector running before the power failure is located according to the write position pointer of the first transaction sector, and the integrity of the data record sector is verified according to the second verification information of the data record sector; when the verification of the data record sector fails, the data record sector is restored according to the corresponding data in the backup sector.

4. The power-down detection method for FLASH memory according to claim 3, characterized in that, It also includes the following steps: When the integrity checks of both the first transaction sector and the second transaction sector are abnormal, the FLASH storage structure is initialized.

5. The power-down detection method for FLASH memory according to claim 3, characterized in that, The process of restoring the data record sector based on the corresponding data in the backup sector further includes the following steps: Update the write position pointer in the first transaction sector so that the write position pointer points to the position of the last valid record in the recovered data record sector.

6. The power-down detection method for FLASH memory according to claim 3, characterized in that, The step of restoring the data record sector based on the corresponding data in the backup sector specifically includes: Based on the current backup sector address recorded in the first transaction sector, read the complete data of the corresponding backup sector and write it back to the data record sector.

7. A FLASH query method, characterized in that, Applied to a FLASH storage structure as described in claim 1 or 2, each data record sector includes a data storage area and a metadata area; the metadata area is located after the data storage sector and is used to record the timestamp of the stored data in the corresponding data record sector; The query method includes the following steps: In response to the target query request, extract the start timestamp and end timestamp from the target query request; Based on the index information of the timestamp of the first transaction sector, locate the target data block in all data blocks that is greater than or equal to the start timestamp and less than or equal to the end timestamp; Based on the timestamp of the metadata area, data records that meet the target query requirements are filtered out from the target data block.

8. A FLASH query method according to claim 7, characterized in that, Based on the timestamp of the metadata area, data records that meet the target query requirements are filtered out from the data block, specifically including: Read the metadata area in the data record sector corresponding to the target data block; Traverse the timestamps in the metadata area and compare them with the starting timestamp to locate the first target stored data in the data storage area that is greater than or equal to the starting timestamp; Starting from the timestamp of the target stored data and ending from the timestamp, a target interval is formed. Stored data that satisfies the target interval in the data storage area is extracted and output as the query result.

9. A FLASH query method according to claim 8, characterized in that, The step of reading the metadata area in the data record sector corresponding to the target data block specifically includes: Read the timestamp of the metadata area in the data record sector of the target data block one by one. If the timestamp of the metadata area in the current data record sector cannot meet the starting timestamp, switch to the next data record sector in the target data block for reading until a data record sector that meets the starting timestamp is located.

10. A FLASH query method according to claim 7, characterized in that, It also includes the following steps: Select the first data record sector in the target data block, and read the timestamp of the last stored data in the first data record sector as the last timestamp; The last timestamp is compared with the starting timestamp. When the last timestamp is greater than the starting timestamp, the current data record sector is determined as the target data record sector, and the position of the current data record sector is returned. When the last timestamp does not meet the starting timestamp condition, the last timestamp of the next data record sector is read and the comparison continues until the target data record sector is determined.