Disclosure of Invention
In view of the above, the present invention has been made to provide a storage method of an embedded system information record that overcomes or at least partially solves the above problems.
In order to solve the technical problem, the embodiment of the application discloses the following technical scheme:
a storage method of embedded system information records comprises the following steps:
s100, initializing a system by mapping CACHE to a correct data block;
s200, writing the data to be written into a data block;
s300, recording the retrieval process of the data;
s400, recording the addition, modification and deletion of data;
further, in S100, a specific method for initializing the system is as follows:
s101, initializing the CACHE into an empty data block, wherein the information serial number is 0, and the information serial number 0 is a reserved value which is smaller than the information serial number in any real data block, so that the initialized CACHE is ensured to have the lowest exchange sequence;
s102, judging whether unread data blocks exist or not, if not, jumping to S105, otherwise, reading the next data block into the BUF;
s103, checking whether the characteristic numerical values and the check values in the BUF are correct or not in sequence, if not, ignoring, and returning to S102;
s104, when the information sequence number in the BUF is larger than the information sequence number of the CACHE, exchanging the BUF and the CACHE pointer to represent that the CACHE is exchanged into data with higher version; continuing to execute S102;
and S105, completing CACHE mapping and completing system initialization.
Further, in S200, the method for writing new data into the data block includes:
s201, preparing new data ready to be written in the BUF;
s202, acquiring a current information serial number CACHE _ K from a CACHE, wherein the CACHE _ K represents the highest information serial number of a current system, and assigning the highest information serial number to the information serial number K in the BUF;
s203, increasing the information sequence number K in the BUF, namely K is K +1, so that new data has a higher exchange sequence;
s204, judging whether the increased information sequence number meets a formula K < CACHE _ K + M, wherein M represents the total number of the data blocks, and if the increased information sequence number meets the formula, executing S305; if the formula is not satisfied, the writing fails, no available data block exists, and the whole process is exited;
s205, after updating the check value of the BUF data block, calculating a write-in position offset S according to a position offset formula, and writing BUF data into a # S block of the system area;
s206, judging whether a bad block is encountered, returning to S303 if the bad block is encountered, and otherwise continuing to S307;
s207, through atom exchange of CACHE and BUF pointers, the CACHE still keeps the complete data with the highest information sequence number, and the writing process is completed.
Further, the position offset S is formulated as:
S=K MOD M;
wherein S is position deviation, K is information sequence number, M is data block total number, and MOD represents the modulus operator.
Further, in S300, the specific method of S300 is:
s301, reading a block information header in the CACHE, acquiring the total number of records as a constraint condition of iteration, initializing a record access pointer P as a first record position, and setting the iteration number R as 0;
s302, checking whether the iteration times R reach the total recorded number or not, and if not, executing S303; if the result is reached, the search is failed, no record of the designated number exists in the CACHE, and the process is exited;
s303, reading the record pointed by the P, and acquiring the record number T and the record length L of the record;
s304, comparing whether T is the information number to be retrieved, if yes, jumping to S306, and if not, continuing to S305;
s305, increasing the iteration times, wherein R is R +1, recording the jump of the pointer to the next position P is P + L, and jumping to S302 to continue the iteration;
s306, recording the retrieved position, returning a pointer and a length for copying.
Further, in S400, the specific method of S400 is:
s401, initializing a BUF into a null data block, setting a recording pointer P to point to a CACHE first record, setting a recording pointer Q to point to a BUF first record, and setting an iterator R to be 0;
s402, checking whether the iterator R is equal to the total number of CACHE records or not, if so, returning a null pointer, and otherwise, returning the record pointed by the P;
s403, checking whether the record pointer returned by the S402 is valid, if yes, continuing to the S404, otherwise, jumping to the S407;
s404, reading the record number T and the record length L pointed by P, checking whether the T is a number which needs to be modified or deleted in the operation, if so, jumping to S406, otherwise, continuing to S405;
s405, copying the record pointed by the P to a position Q, and revising a Q pointer to point to the next record, namely Q is Q + L;
s406, increasing the iterator by one, wherein R is R +1, meanwhile, revising the P pointer to point to the next record, and jumping to S402;
s407, when all the irrelevant records are copied from the CACHE to the BUF, if the operation is 'delete', jumping to S410, and if 'add' or 'modify', continuing to S408;
s408, checking whether the residual space of the data block is enough to write a new record, if so, exiting the process, otherwise, continuing the S409;
s409, writing the new record into a position Q;
and S410, the data block in the BUF is ready.
Furthermore, the data block is composed of a fixed block information head and a plurality of variable information records; the block header data includes a characteristic value, a check value, an information sequence number, and a total number of records.
Further, the characteristic value is a conventional 32-bit unsigned integer used for directly excluding data blocks not belonging to the system; the information sequence number is a 32-bit unsigned integer which is increased by 1 every time the data block is written, and the information sequence number cannot be circulated in the FLASH life cycle and is used for distinguishing the precedence relationship of the information blocks; the total number of records is the total number of data records present in the block; the check value is a value calculated from the header to the CRC32 of all data between the end of the last record.
The technical scheme provided by the embodiment of the invention has the beneficial effects that at least:
the invention discloses a storage method of embedded system information records, which manages all data records in a micro-file form, and is convenient for random size change and add-delete management of a plurality of records; managing a plurality of FLASH blocks through a verification algorithm to achieve the purpose of wear balance; the safe atomic operation ensures the data safety; data redundancy is improved to achieve higher reliability. The invention is especially suitable for the working environment of various embedded environments, specially deals with various trivial information records, has the advantages of less required resources, high reliability and simple and direct use, and provides powerful storage technical support for constructing stable and reliable embedded application environments.
The technical solution of the present invention is further described in detail by the accompanying drawings and embodiments.
Detailed Description
Exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. While exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure may be embodied in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that this disclosure will be thorough and complete, and will fully convey the scope of the disclosure to those skilled in the art.
In order to solve the problems in the prior art, the embodiment of the invention provides a method.
Example 1
The embodiment discloses a storage method of embedded system information record, as shown in fig. 1, including:
s100, initializing a system by mapping CACHE to a correct data block; specifically, in this embodiment, the data block is a basic unit for performing block erasure, a single data block must be completely written into the block device, and only a part of the data block will be considered as invalid by the system. A pair of memory areas dedicated to data block processing are a read map CACHE and a read write buffer BUF, respectively, which have the same capacity for data blocks. The CACHE can greatly improve the access efficiency, and can support multi-thread concurrent access at any time, and any external record reading is to copy data from the CACHE directly; the BUF is used in the internal processing of the system, and all the record writing outside is copied to the BUF.
In this embodiment, a data block is composed of a fixed block header and a number of variable information records. The block header data includes a characteristic value, a check value, an information sequence number, and a total number of records. Wherein the characteristic value is a predetermined 32-bit unsigned integer for directly excluding data blocks not belonging to the system; the information sequence number is a 32-bit unsigned integer which is increased by 1 every time the data block is written, and the information sequence number cannot be circulated in the FLASH life cycle and is used for distinguishing the precedence relationship of the information blocks; the total number of records is the total number of data records present in the block; the check value is a data field in which the check value itself is located when calculating the CRC32 (cyclic redundancy check) calculation value of all data from the header to the last recording end and is filled with 0).
Specifically, as shown in fig. 2 and S100, the specific method for initializing the system is as follows:
s101, initializing the CACHE into an empty data block, wherein the information serial number is 0, and the information serial number 0 is a reserved value which is smaller than the information serial number in any real data block, so that the initialized CACHE is ensured to have the lowest exchange sequence;
s102, judging whether unread data blocks exist or not, if not, jumping to S105, otherwise, reading the next data block into the BUF;
s103, checking whether the characteristic numerical values and the check values in the BUF are correct or not in sequence, if not, ignoring, and returning to S102;
s104, when the information sequence number in the BUF is larger than the information sequence number of the CACHE, exchanging the BUF and the CACHE pointer to represent that the CACHE is exchanged into data with higher version; continuing to execute S102;
and S105, completing CACHE mapping and completing system initialization.
And S200, writing the data to be written into the data block. In this embodiment S200, as shown in fig. 3, the method for writing new data into a data block includes:
s201, preparing new data ready to be written in the BUF;
s202, acquiring a current information serial number CACHE _ K from a CACHE, wherein the CACHE _ K represents the highest information serial number of a current system, and assigning the highest information serial number to the information serial number K in the BUF;
s203, increasing the information sequence number K in the BUF, namely K is K +1, so that new data has a higher exchange sequence;
s204, judging whether the increased information sequence number meets a formula K < CACHE _ K + M, wherein M represents the total number of the data blocks, and if the increased information sequence number meets the formula, executing S305; if the formula is not satisfied, the writing fails, no available data block exists, and the whole process is exited;
s205, after updating the check value of the BUF data block, calculating a write-in position offset S according to a position offset formula, and writing BUF data into a # S block of the system area; the position offset S is formulated as:
S=K MOD M;
wherein S is position deviation, K is information sequence number, M is data block total number, and MOD represents the modulus operator.
S206, judging whether a bad block is encountered, returning to S303 if the bad block is encountered, and otherwise continuing to S307;
s207, through atom exchange of CACHE and BUF pointers, the CACHE still keeps the complete data with the highest information sequence number, and the writing process is completed.
And S300, recording the retrieval process of the data. In this embodiment S300, as shown in fig. 4, the specific method of S300 is:
s301, reading a block information header in the CACHE, acquiring the total number of records as a constraint condition of iteration, initializing a record access pointer P as a first record position, and setting the iteration number R as 0;
s302, checking whether the iteration times R reach the total recorded number or not, and if not, executing S303; if the result is reached, the search is failed, no record of the designated number exists in the CACHE, and the process is exited;
s303, reading the record pointed by the P, and acquiring the record number T and the record length L of the record;
s304, comparing whether T is the information number to be retrieved, if yes, jumping to S306, and if not, continuing to S305;
s305, increasing the iteration times, wherein R is R +1, recording the jump of the pointer to the next position P is P + L, and jumping to S302 to continue the iteration;
s306, recording the retrieved position, returning a pointer and a length for copying.
And S400, recording the addition, modification and deletion of the data. In this embodiment S400, as shown in fig. 5, the specific method of S400 is:
s401, initializing a BUF into a null data block, setting a recording pointer P to point to a CACHE first record, setting a recording pointer Q to point to a BUF first record, and setting an iterator R to be 0;
s402, checking whether the iterator R is equal to the total number of CACHE records or not, if so, returning a null pointer, and otherwise, returning the record pointed by the P;
s403, checking whether the record pointer returned by the S402 is valid, if yes, continuing to the S404, otherwise, jumping to the S407;
s404, reading the record number T and the record length L pointed by P, checking whether the T is a number which needs to be modified or deleted in the operation, if so, jumping to S406, otherwise, continuing to S405;
s405, copying the record pointed by the P to a position Q, and revising a Q pointer to point to the next record, namely Q is Q + L;
s406, increasing the iterator by one, wherein R is R +1, meanwhile, revising the P pointer to point to the next record, and jumping to S402;
s407, when all the irrelevant records are copied from the CACHE to the BUF, if the operation is 'delete', jumping to S410, and if 'add' or 'modify', continuing to S408;
s408, checking whether the residual space of the data block is enough to write a new record, if so, exiting the process, otherwise, continuing the S409;
s409, writing the new record into a position Q;
and S410, the data block in the BUF is ready.
It can be seen from the above specific flow of S400 that the unified flow of adding, modifying and deleting records is to copy the unrelated record from CACHE to BUF, and then determine whether to add a new record to BUF to achieve the goal.
The flow is very simple to implement, does not generate any space debris, and is easy to calculate whether the residual space is abundant. In terms of fault tolerance, it does not have any effect to "delete" or "modify" a record that does not exist at all, nor does it have any errors to "add" an existing record.
The storage method for the information record of the embedded system has the following advantages:
1. the method can not be broken down, and a plurality of data blocks are subjected to checksum comparison, and after fair competition, the most appropriate data is selected and mapped into the CACHE. The election system avoids the permanent crash of the file system caused by the external impact on the central management data structure of the file system;
2. the block operation of the method is atomic, and the history data cannot be destroyed by accidentally interrupted writing, namely, the block operation is completely successful, a new data block is mapped into a CACHE because of having the highest information sequence number and correct verification, or the new data block is directly ignored when being unsuccessful, and the CACHE still maps the last history data;
3. and automatically managing the erasing wear of the physical blocks. The service life of FLASH is prolonged when frequent updating is carried out. In actual use, the number of the physical blocks can be reasonably configured according to application conditions to balance the abrasion times, so that the service life of the physical blocks in the system is longer than the life cycle of equipment;
4. bad block tolerance is high. In the total M data blocks, the maximum can bear M-2 bad blocks without causing abnormal work;
5. natural redundancy. The rest records except the latest record have 1 to M redundancies, and the longer the historical deposit records, the larger the redundancy number of the records, the less chance to lose from the system;
6. the backup is simple. Although the redundancy in the system is safe, the situation of backup is simple, the writing is adjusted once again, and the problem of what bad blocks and where the blocks are written does not need to be concerned at all;
7. migration is facilitated. Directly marking out a new area from the equipment, writing CACHE into any available data block, and reinitializing from the new area;
8. the CACHE-based record access speed is very high, and multithreading concurrency is supported even if new data is being written;
9. the management of records based on numbers is very convenient, resembles a single directory of small file systems, and is completely independent of variations in the type, size and amount of data recorded.
In summary, the storage method for information records of an embedded system disclosed in this embodiment manages all data records in the form of micro files, so as to facilitate any size change and add/delete management of many records; managing a plurality of FLASH blocks through a verification algorithm to achieve the purpose of wear balance; the safe atomic operation ensures the data safety; data redundancy is improved to achieve higher reliability. The invention is especially suitable for the working environment of various embedded environments, specially deals with various trivial information records, has the advantages of less required resources, high reliability and simple and direct use, and provides powerful storage technical support for constructing stable and reliable embedded application environments.
It should be understood that the specific order or hierarchy of steps in the processes disclosed is an example of exemplary approaches. Based upon design preferences, it is understood that the specific order or hierarchy of steps in the processes may be rearranged without departing from the scope of the present disclosure. The accompanying method claims present elements of the various steps in a sample order, and are not intended to be limited to the specific order or hierarchy presented.
In the foregoing detailed description, various features are grouped together in a single embodiment for the purpose of streamlining the disclosure. This method of disclosure is not to be interpreted as reflecting an intention that the claimed embodiments of the subject matter require more features than are expressly recited in each claim. Rather, as the following claims reflect, invention lies in less than all features of a single disclosed embodiment. Thus, the following claims are hereby expressly incorporated into the detailed description, with each claim standing on its own as a separate preferred embodiment of the invention.
Those of skill would further appreciate that the various illustrative logical blocks, modules, circuits, and algorithm steps described in connection with the embodiments disclosed herein may be implemented as electronic hardware, computer software, or combinations of both. To clearly illustrate this interchangeability of hardware and software, various illustrative components, blocks, modules, circuits, and steps have been described above generally in terms of their functionality. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the overall system. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present disclosure.
The steps of a method or algorithm described in connection with the embodiments disclosed herein may be embodied directly in hardware, in a software module executed by a processor, or in a combination of the two. A software module may reside in RAM memory, flash memory, ROM memory, EPROM memory, EEPROM memory, registers, hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art. An exemplary storage medium is coupled to the processor such the processor can read information from, and write information to, the storage medium. Of course, the storage medium may also be integral to the processor. The processor and the storage medium may reside in an ASIC. The ASIC may reside in a user terminal. Of course, the processor and the storage medium may reside as discrete components in a user terminal.
For a software implementation, the techniques described herein may be implemented with modules (e.g., procedures, functions, and so on) that perform the functions described herein. The software codes may be stored in memory units and executed by processors. The memory unit may be implemented within the processor or external to the processor, in which case it can be communicatively coupled to the processor via various means as is known in the art.
What has been described above includes examples of one or more embodiments. It is, of course, not possible to describe every conceivable combination of components or methodologies for purposes of describing the aforementioned embodiments, but one of ordinary skill in the art may recognize that many further combinations and permutations of various embodiments are possible. Accordingly, the embodiments described herein are intended to embrace all such alterations, modifications and variations that fall within the scope of the appended claims. Furthermore, to the extent that the term "includes" is used in either the detailed description or the claims, such term is intended to be inclusive in a manner similar to the term "comprising" as "comprising" is interpreted when employed as a transitional word in a claim. Furthermore, any use of the term "or" in the specification of the claims is intended to mean a "non-exclusive or".