Data storage and data reading method and device
Patent Information
- Application Number
- CN202110450349.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-04-25
- Publication Date
- 2025-10-03
- Estimated Expiration
- 2041-04-25
AI Technical Summary
When multiple processes access the same address range of the same storage medium, data consistency cannot be guaranteed because the virtual cache area of each process is only visible to itself, resulting in data inconsistency between processes.
Sub-areas corresponding to storage media are set in the shared memory of the electronic device. Each sub-area contains multiple memory slices. The data management mechanism in the shared memory ensures the consistency of data during storage and reading, and the data is stored and read by determining the target memory slice.
It achieves data consistency when multiple processes access the same storage medium, ensuring that all processes can access the same data content.
Smart Images

Figure CN115237819B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data storage, and in particular to a data storage and data reading method and device. Background Art
[0002] Typically, electronic devices may be provided with various types of storage media such as disks and SSDs (Solid State Disks), which are used to store data generated by various processes run by the electronic devices, as well as data required when the electronic devices run various processes.
[0003] In the related art, when each process writes data to a storage medium, it usually first writes the data to be stored into a virtual cache area that is accessible only to the process itself, and then writes the data from the virtual cache area to the storage medium.
[0004] However, in the above-mentioned related technologies, since the data in the virtual cache of each process is only visible to the process, when multiple processes need to access the same address range of the same storage medium, the consistency of the data accessed by the multiple processes cannot be guaranteed.
[0005] For example, process 1 is about to write data a to be stored to address range A0 of storage medium A. This data a is required for process 2 to run, and process 2 needs to read the data it needs from address range A0 of storage medium A. Because process 1 must first write data a to a virtual cache area accessible only to itself, and process 2 cannot access process 1's virtual cache area, before data a is written to address range A0 of storage medium A, the data read by process 2 from address range A0 of storage medium A is inconsistent with the data a. Summary of the Invention
[0006] The purpose of the embodiments of the present invention is to provide a data storage and data reading method and device to ensure the consistency of the data accessed by multiple processes when multiple processes need to access the same data. The specific technical solution is as follows:
[0007] In a first aspect, an embodiment of the present invention provides a data storage method, applied to an electronic device, wherein sub-areas corresponding to respective storage media of the electronic device are provided in a shared memory of the electronic device, each sub-area including multiple memory slices; the method comprising:
[0008] Get the data to be stored;
[0009] Determining a first target storage medium for storing the data to be stored, and a target address range of the data to be stored in the first target storage medium;
[0010] Based on the target address range, determining a target memory slice for storing the data to be stored in a first target sub-area corresponding to the first target storage medium;
[0011] The data to be stored is stored in the target memory slice.
[0012] Optionally, in a specific implementation, the step of determining, based on the target address range, in a first target sub-area corresponding to the first target storage medium, a target memory slice for storing the data to be stored includes:
[0013] Determine whether a first shard exists in a first target sub-area corresponding to the first target storage medium; wherein the first shard is a memory shard for which a correspondence has been established with an address range in the first target storage medium, and the corresponding address range in the first target storage medium overlaps with the target address range;
[0014] If so, determining whether the target address range is entirely within the address range corresponding to the first fragment in the first target storage medium;
[0015] If yes, determining the first shard as the target memory shard;
[0016] Otherwise, a second shard for storing the remaining data is determined from the first target sub-area, and the first shard and the second shard are determined as target memory shards; wherein the remaining data is: data in the data to be stored that cannot be stored in the first shard.
[0017] Optionally, in a specific implementation, the step of determining, from the first target sub-region, a second shard for storing the remaining data includes:
[0018] Determining whether the storage space of the available memory slice in the first target sub-region satisfies the amount of remaining data;
[0019] If yes, selecting a second shard for storing the remaining data from the available memory shards;
[0020] Otherwise, changing the state of at least one memory slice in the first target sub-region that is in an unavailable state to an available state, so that after the change is completed, the storage space of the memory slice in the available state in the first target sub-region can meet the data volume of the remaining data;
[0021] After the change is completed, a second shard for storing the remaining data is selected from the available memory shards in the first target sub-area.
[0022] Optionally, in a specific implementation, before the step of changing the state of at least one memory shard in the first target sub-region that is in an unavailable state to an available state, the method further includes:
[0023] Determine a memory slice to be refreshed based on the relationship between the number of updates and the number of disk flushes for each memory slice in the unavailable state in the first target sub-area, and write the data stored in the memory slice to be refreshed to the first target storage medium; wherein the disk flushing includes: writing the stored data to the first target storage medium;
[0024] After the writing is completed, the step of changing the state of at least one memory slice in the first target sub-region that is in an unavailable state to an available state is performed.
[0025] Optionally, in a specific implementation, before the step of determining the first shard as the target memory shard, the method further includes:
[0026] Determining whether the first slice satisfies any of preset write conditions; wherein the preset write conditions include: no data is stored in the first slice; data is stored in the first slice, and there is no gap between an address range of the stored data in the first slice and an address range corresponding to the target address range in the first slice;
[0027] If yes, determining the first shard as the target memory shard;
[0028] Otherwise, the data to be stored is written into the target address range of the first target storage medium.
[0029] Optionally, in a specific implementation, the method further includes:
[0030] If the first slice does not exist in the first target sub-area corresponding to the first target storage medium, a third slice for storing the data to be stored is determined from the first target sub-area.
[0031] Optionally, in a specific implementation, the step of storing the data to be stored in the target memory shard includes:
[0032] If the target memory slice is the first slice, determining a target starting offset address in the first slice, and writing the data to be stored into the first slice starting from the target starting offset address; wherein the storage address corresponding to the target starting offset address in the first target storage medium is the starting address of the target address range;
[0033] If the target memory shards are the first shard and the second shard, determine the specified offset address range in the first shard, write the target data in the data to be stored into the specified offset address range, and write the remaining data in the data to be stored into the second shard; wherein, the address range corresponding to the specified offset address range in the first target storage medium is the address range in the address range corresponding to the first shard in the first target storage medium that overlaps with the target address range, and the target data is: the data whose storage address in the first target storage medium is the storage address corresponding to the specified offset address range; the remaining data is: the data in the data to be stored that cannot be stored in the first shard.
[0034] Optionally, in a specific implementation, each sub-region further includes: a shared memory index region; the shared memory index region includes: shared memory header information and an index of each memory shard in the sub-region; the shared memory header information includes: an IO count; the index of each memory shard includes: a memory update count, a disk flush count, and an elimination sequence; the memory update count is used to indicate the number of times data stored in the memory shard has been updated, the disk flush count is used to indicate the number of times data stored in the memory shard has been written to a corresponding storage medium, and the elimination sequence indicates the order in which the memory shards are changed from an unavailable state to an available state; the method further includes:
[0035] After writing data to each of the memory slices, the IO count in the shared memory header information of the sub-region where the memory slice is located is increased by 1, and the elimination sequence in the index information of the memory slice is updated to the IO count after the increase by 1, and the memory update count in the index information of the memory slice is increased by 1;
[0036] After the data stored in each of the memory slices to be refreshed is written to the first target storage medium, the disk flush count in the index information of the memory slice to be refreshed is increased by 1.
[0037] Optionally, in a specific implementation, the shared memory header information further includes: a memory initialization flag; and before the step of obtaining the data to be stored, the method further includes:
[0038] For each storage medium, determining whether the sub-region corresponding to the storage medium is initialized according to the memory initialization flag in the shared memory header information of the sub-region corresponding to the storage medium;
[0039] If yes, determine that a memory slice to be cleaned has data stored in the sub-area corresponding to the storage medium, write the data stored in the memory slice to be cleaned to the storage medium, and mark the state of the memory slice to be cleaned as available;
[0040] Otherwise, the sub-area corresponding to the storage medium is initialized.
[0041] Optionally, in a specific implementation, the method further includes:
[0042] For each sub-region, perform the following steps:
[0043] For each unavailable memory shard in the sub-region, check whether the memory update count and disk flush count in the index of the memory shard are the same;
[0044] If they are the same, determine whether the elimination sequence of the memory slice is within the preset range;
[0045] If it is, change the state of the memory slice to available state;
[0046] If they are not the same, determining whether the storage allocation satisfies any one of the preset elimination conditions; wherein the preset elimination conditions include: a ratio of the storage space occupied by the data stored in the memory slice to the storage space of the memory slice is greater than a preset ratio, and a ratio of the storage space occupied by the data stored in the memory slice to the storage space of the memory slice is not greater than a preset ratio, and an elimination sequence of the memory slice is within the preset range;
[0047] When the conditions are met, the data stored in the memory slice is written to the storage medium corresponding to the sub-area, the disk flush count in the index of the memory slice is increased by 1, and the state of the memory slice is changed to available state.
[0048] Optionally, in a specific implementation, the method further includes:
[0049] For each sub-region, when the sub-region meets the preset disk flushing condition, the data stored in each memory slice in the sub-region is written to the storage medium corresponding to the sub-region;
[0050] The preset disk flushing conditions include: the amount of stored data exceeds a preset amount of data, or the time since the last disk flush reaches a preset time.
[0051] Optionally, in a specific implementation, the method further includes:
[0052] For each sub-region, when the number of memory slices in an available state in the sub-region is less than a preset number, the state of at least one memory slice in an unavailable state in the sub-region is changed to an available state, so that after the change is completed, the number of memory slices in an available state in the sub-region is not less than the preset number.
[0053] In a second aspect, an embodiment of the present invention provides a data reading method, which is applied to an electronic device, wherein sub-areas corresponding to respective storage media of the electronic device are set in a shared memory of the electronic device, each sub-area including multiple memory slices; the method comprises:
[0054] Obtaining a data read instruction generated by a target process, and determining a second target sub-region corresponding to a second target storage medium for storing the data to be read;
[0055] Searching for the data to be read in each memory slice storing data in the second target sub-area;
[0056] If all the data to be read are found, the data to be read are written into the virtual cache of the target process;
[0057] If partial data of the data to be read is found, the partial data and the missing data read from the second target storage medium are written into the virtual cache of the target process; wherein the missing data is data in the data to be read that is not stored in the second target sub-region;
[0058] If the to-be-read data is not found, the to-be-read data read from the second target storage medium is written into the virtual cache of the target process.
[0059] In a third aspect, an embodiment of the present invention provides a data storage device, applied to an electronic device, wherein sub-areas corresponding to respective storage media of the electronic device are provided in a shared memory of the electronic device, each sub-area including multiple memory slices; the device comprises:
[0060] A data acquisition module, used to acquire data to be stored;
[0061] an information determining module, configured to determine a first target storage medium for storing the data to be stored, and a target address range of the data to be stored in the first target storage medium;
[0062] A shard determination module, configured to determine, based on the target address range, in a first target sub-area corresponding to the first target storage medium, a target memory shard for storing the data to be stored;
[0063] The data storage module is used to store the data to be stored in the target memory slice.
[0064] Optionally, in a specific implementation, the shard determination module includes:
[0065] A first shard determination submodule is configured to determine whether a first shard exists in a first target sub-area corresponding to the first target storage medium; if so, trigger an address range determination submodule; wherein the first shard is a memory shard for which a correspondence has been established with an address range in the first target storage medium, and the corresponding address range in the first target storage medium overlaps with the target address range;
[0066] The address range determination submodule is configured to determine whether the target address range is entirely within the address range corresponding to the first shard in the first target storage medium; if so, trigger the first target shard determination submodule; otherwise, trigger the second shard determination submodule;
[0067] The first target shard determination submodule is configured to determine the first shard as a target memory shard;
[0068] The second shard determination submodule is used to determine a second shard for storing the remaining data from the first target sub-area, and determine the first shard and the second shard as target memory shards; wherein the remaining data is: data in the data to be stored that cannot be stored in the first shard.
[0069] Optionally, in a specific implementation, the second shard determination submodule includes:
[0070] a space determination unit, configured to determine whether the storage space of the memory shard in the first target sub-area that is in an available state satisfies the amount of remaining data; if so, trigger the second shard determination unit; otherwise, trigger the state change unit;
[0071] The second shard determination unit is configured to select a second shard for storing the remaining data from the available memory shards;
[0072] The state changing unit is configured to change the state of at least one memory slice in the first target sub-region that is in an unavailable state to an available state, so that after the change is completed, the storage space of the memory slice in the available state in the first target sub-region meets the data volume of the remaining data;
[0073] The second shard determination unit is further configured to select, after the change is completed, a second shard for storing the remaining data from the available memory shards in the first target sub-region.
[0074] Optionally, in a specific implementation, the device further includes:
[0075] a disk flushing module configured to, before changing the state of at least one memory slice in an unavailable state in the first target sub-region to an available state, determine a memory slice to be refreshed based on the relationship between the number of updates and the number of disk flushes of each memory slice in an unavailable state in the first target sub-region, and write the data stored in the memory slice to be refreshed to the first target storage medium; and trigger the state changing unit after the writing is completed;
[0076] The flashing step includes writing the stored data into the first target storage medium.
[0077] Optionally, in a specific implementation, the device further includes:
[0078] a write condition judgment module, configured to, before determining the first shard as the target memory shard, determine whether the first shard satisfies any of the preset write conditions; if so, trigger the first target shard determination submodule; otherwise, trigger the data write module; wherein the preset write conditions include: no data is stored in the first shard; data is stored in the first shard, and there is no gap between the address range of the stored data in the first shard and the address range corresponding to the target address range in the first shard;
[0079] The data writing module is configured to write the data to be stored into the target address range of the first target storage medium.
[0080] Optionally, in a specific implementation, the device further includes:
[0081] The third shard determination submodule is configured to determine a third shard for storing the data to be stored from the first target subregion if the first shard does not exist in the first target subregion corresponding to the first target storage medium.
[0082] Optionally, in a specific implementation, the data storage module is specifically used to:
[0083] If the target memory slice is the first slice, determining a target starting offset address in the first slice, and writing the data to be stored into the first slice starting from the target starting offset address; wherein the storage address corresponding to the target starting offset address in the first target storage medium is the starting address of the target address range;
[0084] If the target memory shards are the first shard and the second shard, determine the specified offset address range in the first shard, write the target data in the data to be stored into the specified offset address range, and write the remaining data in the data to be stored into the second shard; wherein, the address range corresponding to the specified offset address range in the first target storage medium is the address range in the address range corresponding to the first shard in the first target storage medium that overlaps with the target address range, and the target data is: the data whose storage address in the first target storage medium is the storage address corresponding to the specified offset address range; the remaining data is: the data in the data to be stored that cannot be stored in the first shard.
[0085] Optionally, in a specific implementation, each sub-region further includes: a shared memory index region; the shared memory index region includes: shared memory header information and an index of each memory shard in the sub-region; the shared memory header information includes: an IO count; the index of each memory shard includes: a memory update count, a disk flush count, and an elimination sequence; the memory update count is used to indicate the number of times data stored in the memory shard has been updated, the disk flush count is used to indicate the number of times data stored in the memory shard has been written to the corresponding storage medium, and the elimination sequence indicates the order in which the memory shard has been changed from an unavailable state to an available state; the device further includes:
[0086] A first index update module is configured to, after writing data to each memory slice, increment the IO count in the shared memory header information of the sub-region where the memory slice is located by 1, update the elimination sequence in the index information of the memory slice to the incremented IO count, and increment the memory update count in the index information of the memory slice by 1;
[0087] The second index updating module is configured to add 1 to the disk flush count in the index information of each memory slice to be refreshed after writing the data stored in the memory slice to be refreshed to the first target storage medium.
[0088] Optionally, in a specific implementation, the shared memory header information further includes: a memory initialization flag; and the device further includes:
[0089] an initialization determination module configured to determine, for each storage medium, whether the sub-region corresponding to the storage medium is initialized based on a memory initialization flag in the shared memory header information of the sub-region corresponding to the storage medium before acquiring the data to be stored; if so, trigger the data cleaning module; otherwise, trigger the initialization module;
[0090] The data cleaning module is configured to determine a memory slice to be cleaned that stores data in a sub-area corresponding to the storage medium, write the data stored in the memory slice to be cleaned to the storage medium, and mark the state of the memory slice to be cleaned as available;
[0091] The initialization module is used to initialize the sub-area corresponding to the storage medium.
[0092] Optionally, in a specific implementation, the apparatus further includes an elimination module configured to perform the following steps for each sub-region:
[0093] For each unavailable memory shard in the sub-region, check whether the memory update count and disk flush count in the index of the memory shard are the same;
[0094] If they are the same, determine whether the elimination sequence of the memory slice is within the preset range;
[0095] If it is, change the state of the memory slice to available state;
[0096] If they are not the same, determining whether the storage allocation satisfies any one of the preset elimination conditions; wherein the preset elimination conditions include: a ratio of the storage space occupied by the data stored in the memory slice to the storage space of the memory slice is greater than a preset ratio, and a ratio of the storage space occupied by the data stored in the memory slice to the storage space of the memory slice is not greater than a preset ratio, and an elimination sequence of the memory slice is within the preset range;
[0097] When the conditions are met, the data stored in the memory slice is written to the storage medium corresponding to the sub-area, the disk flush count in the index of the memory slice is increased by 1, and the state of the memory slice is changed to available state.
[0098] Optionally, in a specific implementation, the device further includes:
[0099] The data flushing module is used to write the data stored in each memory slice in each sub-region to the storage medium corresponding to the sub-region when the sub-region meets the preset flushing conditions;
[0100] The preset disk flushing conditions include: the amount of stored data exceeds a preset amount of data, or the time since the last disk flush reaches a preset time.
[0101] Optionally, in a specific implementation, the device further includes:
[0102] The state change module is used to change the state of at least one memory slice in an unavailable state to an available state in each sub-region when the number of memory slices in the available state in the sub-region is less than a preset number, so that after the change is completed, the number of memory slices in the available state in the sub-region is not less than the preset number.
[0103] In a fourth aspect, an embodiment of the present invention provides a data reading device, applied to an electronic device, wherein sub-areas corresponding to respective storage media of the electronic device are provided in a shared memory of the electronic device, each sub-area including a plurality of memory slices; the device comprises:
[0104] an instruction acquisition module, configured to acquire a data read instruction generated by a target process and determine a second target sub-region corresponding to a second target storage medium for storing data to be read;
[0105] a data search module, configured to search for the data to be read in each memory slice storing data in the second target sub-area;
[0106] A first data reading module, configured to write the data to be read into the virtual cache of the target process if all the data to be read is found;
[0107] a second data reading module, configured to, if partial data of the data to be read is found, write the partial data and the missing data read from the second target storage medium into the virtual cache of the target process; wherein the missing data is data in the data to be read that is not stored in the second target sub-region;
[0108] The third data reading module is configured to write the data to be read from the second target storage medium into the virtual cache of the target process if the data to be read is not found.
[0109] In a fifth aspect, an embodiment of the present invention provides an electronic device, including a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other via the communication bus;
[0110] Memory for storing computer programs;
[0111] The processor is configured to implement, when executing a program stored in the memory, the steps of any data storage method provided in the first aspect and / or the steps of any data reading method provided in the second aspect.
[0112] In a sixth aspect, an embodiment of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of any data storage method provided in the first aspect and / or the steps of any data reading method provided in the second aspect are implemented.
[0113] In the seventh aspect, an embodiment of the present invention provides a computer program product comprising instructions, which, when run on a computer, enables the computer to execute the steps of any data storage method provided in the first aspect and / or the steps of any data reading method provided in the second aspect.
[0114] Beneficial effects of the embodiments of the present invention:
[0115] As can be seen from the above, by applying the solution provided by the embodiment of the present invention, sub-areas corresponding to the various storage media of the electronic device are set in the shared memory of the electronic device. Therefore, when the process in the electronic device stores the generated data to be stored in the storage medium, it can first write the data to be stored into the sub-area corresponding to the storage medium; similarly, when the process in the electronic device needs to read the data stored in the storage medium during operation, it can also first check whether the required data is stored in the sub-area corresponding to the storage medium.
[0116] Based on this, since the data in the shared memory is visible to all processes, when multiple processes need to access the same address range of the same storage medium, the data stored in the same address range of the same storage medium can be stored in the sub-area corresponding to the storage medium in the shared memory, so that multiple processes can access the data from the sub-area, thereby ensuring the consistency of the data accessed by multiple processes. BRIEF DESCRIPTION OF THE DRAWINGS
[0117] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0118] Figure 1 A schematic diagram of the structure of each sub-area set in a specific embodiment;
[0119] Figure 2 A schematic diagram of a data storage method according to an embodiment of the present invention;
[0120] Figure 3 for Figure 2A flowchart of a specific implementation of S203;
[0121] Figure 4 for Figure 2 A flowchart of another specific implementation of S203;
[0122] Figure 5 for Figure 3 and Figure 4 A flowchart of a specific implementation method of S2034;
[0123] Figure 6 for Figure 3 and Figure 4 A flowchart of another specific implementation of S2034;
[0124] Figure 7 for Figure 2 A flowchart of another specific implementation of S203;
[0125] FIG8( a ) is a schematic diagram of a first shard in which no data is stored;
[0126] Figure 8(b)-Figure 8(d) Schematic diagram of various storage situations of data to be stored in the first shard when the data to be stored is stored in the first shard;
[0127] 8( e ) to ( f ) are schematic diagrams illustrating the relationship between the address range of the stored data and the address range corresponding to the target address range in the first shard shown in FIG. 8( d );
[0128] Figure 8(g)-Figure 8(h) 8( c ) is a schematic diagram illustrating the relationship between the address range of the stored data and the address range corresponding to the target address range in the first shard;
[0129] Figure 8(i)-Figure 8(m) 8( b ) is a schematic diagram illustrating the relationship between the address range of the stored data and the address range corresponding to the target address range in the first shard;
[0130] Figure 9 A schematic diagram of a flow chart of another data storage method provided by an embodiment of the present invention;
[0131] Figure 10 A flowchart of a data reading method provided by an embodiment of the present invention;
[0132] Figure 11 A schematic structural diagram of a data storage device provided in an embodiment of the present invention;
[0133] Figure 12A schematic structural diagram of a data reading device provided by an embodiment of the present invention;
[0134] Figure 13 A schematic structural diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0135] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of the present invention.
[0136] In related art, when each process writes data to a storage medium, it typically first writes the data to be stored into a virtual cache area accessible only to that process, and then writes the data from this virtual cache area to the storage medium. However, in this related art, because the data in each process's virtual cache area is only visible to that process, when multiple processes need to access the same address range of the same storage medium, the consistency of the data accessed by the multiple processes cannot be guaranteed.
[0137] In order to solve the above technical problems, embodiments of the present invention provide a data storage method and a data reading method.
[0138] The data storage method provided by an embodiment of the present invention can be applied to any application scenario requiring the storage of process-generated data in a storage medium; correspondingly, the data reading method provided by an embodiment of the present invention can be applied to any application scenario requiring the reading of data required for the execution of a process from a storage medium. Furthermore, both the data storage method and the data reading method provided by an embodiment of the present invention can be applied to various types of electronic devices equipped with a storage medium and provided with a shared memory, and the electronic device can be a device in various systems or a standalone device.
[0139] Furthermore, since the same electronic device may need to store data generated by a process in a storage medium and read data required for the process from the storage medium, the data storage method and data reading method provided in the embodiments of the present invention can be applied to the same electronic device. Of course, the data storage method and data reading method provided in the embodiments of the present invention can also be applied to different electronic devices, respectively, which is reasonable.
[0140] In an embodiment of the present invention, an electronic device for performing a data storage method and / or a data reading method provided by an embodiment of the present invention is provided with a shared memory. Furthermore, the shared memory is provided with sub-regions corresponding to respective storage media of the electronic device. Furthermore, each sub-region includes multiple memory slices.
[0141] Among them, when there are multiple storage media in the electronic device, multiple sub-areas are set in the shared memory of the electronic device, and each sub-area corresponds to a storage medium; when there is one storage medium in the electronic device, a sub-area is set in the shared memory of the electronic device, and the sub-area corresponds to the storage medium, and the sub-area can be the entire shared memory or a partial area in the shared memory, which is all reasonable.
[0142] Furthermore, each memory shard can have two states: unavailable and available. When a memory shard is available, data can be written to it, regardless of whether it already has data stored. Thus, the newly written data can overwrite the data originally stored in the memory shard. Furthermore, when a memory shard is unavailable, it can be changed from unavailable to available based on actual data storage needs. This change process can be referred to as eliminating the data stored in the memory shard.
[0143] Optionally, when the state of a memory slice in an unavailable state is changed to an available state, data stored in the memory slice may be cleared.
[0144] Among them, for each electronic device, the structure of the sub-area set for each storage medium in the electronic device can be determined based on the number of storage media in the electronic device, the size of the shared memory, and the actual situation of the running process, for example, the amount of data generated by the running process, the amount of data required for the process to run, etc.
[0145] For example, Figure 1 FIG. 4 is a schematic diagram of the structure of each sub-area set in a specific embodiment.
[0146] like Figure 1 As shown, each subregion includes a shared memory index area and n memory shards. The total size of the n memory shards is 1GB, and each memory shard is 1MB. The area consisting of the n memory shards can be referred to as the shared memory data area of the subregion. Furthermore, the shared memory index area includes shared memory header information and an index for each memory shard.
[0147] Thus, the size of each sub-area = the size of the shared memory header information + the size of the shared memory data area + the size of the index of each memory shard * n.
[0148] Furthermore, the data structure of the shared memory information header includes: device UUID (Universally Unique Identifier), shared memory size, total number of shards, size of a single shard, memory initialization flag, number of free shards, IO count and reserved field; the data structure of the index of each memory shard includes: shard ID (identification), memory start address, memory shard size, valid data offset, valid data size, disk start offset, memory update count, disk flush count, free flag and elimination sequence.
[0149] Table 1 shows the data structure of the shared memory information header:
[0150] Table 1
[0151]
[0152] Table 2 shows the data structure of the index for each memory shard:
[0153]
[0154]
[0155] Among them, optionally, for the above-mentioned memory initialization mark, the numbers "0" and "1" can be used to indicate whether the sub-area has been initialized. For example, when the memory initialization mark is 1, it can indicate that the sub-area has been initialized, and when the memory initialization mark is 0, it can indicate that the sub-area has not been initialized.
[0156] Optionally, after updating the data for each memory shard, the IO count is incremented by 1, and the resulting IO count is used as the elimination sequence for the memory shard and updated into the index information for the memory shard. Furthermore, the smaller the elimination sequence in the index of each memory shard, the sooner the memory shard changes from an unavailable state to an available state.
[0157] For example, the elimination sequence of memory shard 1 is 1, the elimination sequence of memory shard 2 is 2, and the elimination sequence of memory shard 3 is 3, and memory shards 1-3 are all in an unavailable state. When the state of one unavailable memory shard needs to be changed to an available state, memory shard 1 needs to be changed from an unavailable state to an available state; when the state of two unavailable memory shards needs to be changed to an available state, memory shards 1 and 2 need to be changed from an unavailable state to an available state.
[0158] Optionally, for each memory shard, after each time data is written to the memory shard, the memory update count in the index of the memory shard is increased by 1; and after each time the data stored in the memory shard is written to the corresponding storage medium, the disk flush count in the index of the memory shard is increased by 1; that is, the process of writing the data stored in the memory shard to the corresponding storage medium can be called disk flushing.
[0159] Furthermore, optionally, in order to further reduce the number of IO operations during data storage and data reading in actual applications, the memory slices in each sub-region may be aligned.
[0160] Based on the above description, an embodiment of the present invention provides a data storage method, which may include the following steps:
[0161] Get the data to be stored;
[0162] Determining a first target storage medium for storing the data to be stored, and a target address range of the data to be stored in the first target storage medium;
[0163] Based on the target address range, determining a target memory slice for storing the data to be stored in a first target sub-area corresponding to the first target storage medium;
[0164] The data to be stored is stored in the target memory slice.
[0165] Accordingly, a data reading method provided by an embodiment of the present invention may include the following steps:
[0166] Obtaining a data read instruction generated by a target process, and determining a first target sub-region corresponding to a first target storage medium for storing data to be read;
[0167] Searching for the data to be read in each memory slice storing data in the first target sub-area;
[0168] If all the data to be read are found, the data to be read are written into the virtual cache of the target process;
[0169] If partial data of the data to be read is found, the partial data and the missing data read from the first target storage medium are written into the virtual cache of the target process; wherein the missing data is: data in the data to be read that is not stored in the first target sub-region;
[0170] If the to-be-read data is not found, the to-be-read data read from the first target storage medium is written into the virtual cache of the target process.
[0171] As can be seen from the above, by applying the solution provided by the embodiment of the present invention, sub-areas corresponding to the various storage media of the electronic device are set in the shared memory of the electronic device. Therefore, when the process in the electronic device stores the generated data to be stored in the storage medium, it can first write the data to be stored into the sub-area corresponding to the storage medium; similarly, when the process in the electronic device needs to read the data stored in the storage medium during operation, it can also first check whether the required data is stored in the sub-area corresponding to the storage medium.
[0172] Based on this, since the data in the shared memory is visible to all processes, when multiple processes need to access the same address range of the same storage medium, the data stored in the same address range of the same storage medium can be stored in the sub-area corresponding to the storage medium in the shared memory, so that multiple processes can access the data from the sub-area, thereby ensuring the consistency of the data accessed by multiple processes.
[0173] Below, a data storage method provided by an embodiment of the present invention is first described in detail with reference to the accompanying drawings.
[0174] Figure 2 A flow chart of a data storage method provided by an embodiment of the present invention is as follows: Figure 2 As shown, the storage method may include the following steps:
[0175] S201: Acquire data to be stored;
[0176] When storing data, the electronic device may first obtain the data to be stored.
[0177] The data to be stored can be data generated by a process running on the electronic device and need to be stored on the storage medium of the electronic device; or it can be data required by the process running on the electronic device and read from the storage medium of the electronic device. Of course, the data to be stored can also be other data related to the process running on the electronic device, which is reasonable.
[0178] S202: Determine a first target storage medium for storing the data to be stored, and a target address range of the data to be stored in the first target storage medium;
[0179] When the data to be stored is acquired, the electronic device may determine a first target storage medium for storing the data to be stored, and further determine a target address range in the first target storage medium for storing the data to be stored.
[0180] When the data to be stored is data generated by a process running in an electronic device and needs to be stored in a storage medium of the electronic device, the process, when generating the data, can simultaneously determine in which storage medium and in which address range the data needs to be stored. Thus, when the electronic device obtains the data, it can subsequently determine a first target storage medium for storing the data to be stored and a target address range for storing the data to be stored in the first target storage medium.
[0181] When the data to be stored is data required by a process running in the electronic device and is read from the storage medium of the electronic device, when reading the data, the first target storage medium for storing the data to be stored and the target address range of the data to be stored in the first target storage medium can be directly determined.
[0182] S203: Determine, based on the target address range, in a first target sub-area corresponding to the first target storage medium, a target memory slice for storing the data to be stored;
[0183] Since different storage media in the electronic device correspond to different sub-areas, after determining the first target storage medium for storing the data to be stored and the target address range of the data to be stored in the first target storage medium, the first target sub-area corresponding to the first target storage medium can be determined among the multiple sub-areas set in the shared memory.
[0184] Furthermore, since different memory shards in the first target sub-region correspond to different address ranges in the first target storage medium, and the states of different memory shards in the first target sub-region are different, in order to make the address range of the data to be stored in the first target sub-region correspond to the target address range in the first target storage medium, the target memory shard for storing the data to be stored can be determined in the first target sub-region based on the target address range.
[0185] Among them, the target memory slice determined for storing the data to be stored is in an available state. At this time, if there is stored data in the target memory slice, the data to be stored can overwrite the above stored data and then be stored in the target memory slice.
[0186] S204: Storing the data to be stored in the target memory slice.
[0187] After determining the target memory slice for storing the data to be stored, the acquired data to be stored may be stored in the target memory slice.
[0188] Optionally, after the data to be stored is stored in the target memory slice, the index of the target memory slice may be updated, and a corresponding relationship between the target memory slice and the target address range may be established.
[0189] As can be seen from the above, since the data in the shared memory is visible to all processes, when multiple processes need to access the same address range of the same storage medium, the data stored in the same address range of the same storage medium can be stored in the sub-area corresponding to the storage medium in the shared memory, so that multiple processes can access the data from the sub-area, thereby ensuring the consistency of the data accessed by multiple processes.
[0190] Optionally, in a specific implementation, such as Figure 3 As shown, the above step S203, based on the target address range, determines the target memory slice for storing the data to be stored in the first target sub-area corresponding to the first target storage medium, and may include the following steps:
[0191] S2031: Determine whether there is a first slice in the first target sub-area corresponding to the first target storage medium; if so, execute step S2032;
[0192] The first shard is a memory shard that has established a correspondence with an address range in the first target storage medium, and whose corresponding address range in the first target storage medium overlaps with the target address range;
[0193] In this specific implementation, after determining the first target storage medium and the target address range, the electronic device can determine the first target sub-region corresponding to the first target storage medium. Furthermore, the electronic device can determine whether the first shard exists among the multiple memory shards included in the first target sub-region.
[0194] When data is stored in a memory slice in the first target sub-area, the address range occupied by the data stored in the first target storage medium can be determined. Furthermore, the correspondence between the memory slice and the address range in the first target storage medium can be determined based on the size of the memory slice, the size of the data, and the address of the starting storage position of the data in the memory slice in the shared memory.
[0195] The address range in the first target storage medium corresponding to the memory slice refers to the address range occupied by the data in the first target storage medium when the memory slice is fully stored with data.
[0196] Thus, when a memory slice in the first target sub-area has established a correspondence with an address range in the first target storage medium, and the corresponding address range in the first target storage medium overlaps with the target address range, the memory slice is the first slice.
[0197] For example, in the above Figure 1 Based on the structural diagram of the sub-areas shown, for each memory shard, the address range of the valid data in the corresponding storage medium can be determined according to the disk starting offset and valid data size in the index information of the memory shard. Furthermore, the correspondence between the memory shard and the address range in the storage medium can be determined according to the memory starting address, memory shard size and valid data offset of the memory shard.
[0198] Optionally, step S2031 may include the following step 1:
[0199] Step 1: Determine whether data is stored in a first target sub-area corresponding to a first target storage medium, the address range occupied by which in the first target storage medium overlaps with the target address range.
[0200] After determining the first target storage medium and the target address range, the electronic device may first determine a first target sub-region corresponding to the first target storage medium, and then search each memory slice included in the first target sub-region to determine whether any data is stored whose address range in the first target storage medium overlaps with the target address range. The data whose address range in the first target storage medium overlaps with the target address range may be considered data that conflicts with the data to be stored.
[0201] That is to say, the electronic device can determine the various data stored in the various memory slices included in the first target sub-area, and determine the address range occupied by each data in the first target storage medium based on the correspondence established between the memory slices storing the data and the address range in the first target storage medium, thereby judging whether there is an address range in the determined address ranges that overlaps with the target address range.
[0202] Furthermore, if the above judgment result is yes, then the memory shard where the data is located is the first shard.
[0203] S2032: Determine whether the target address range is entirely within the address range corresponding to the first fragment in the first target storage medium; if so, execute step S2033; otherwise, execute step S2034;
[0204] S2033: Determine the first shard as the target memory shard;
[0205] S2034: Determine, from the first target sub-region, a second shard for storing the remaining data, and determine the first shard and the second shard as target memory shards;
[0206] The remaining data is the data to be stored that cannot be stored in the first shard.
[0207] After determining that the first shard exists in the first target sub-area, considering whether the first shard can store all the data to be stored, it is necessary to further determine whether the target address range is entirely within the address range corresponding to the first shard in the first target storage medium.
[0208] Among them, when it is determined that the target address range is entirely within the address range corresponding to the first shard in the first target storage medium, it means that if the data to be stored is completely stored in the first shard, then, based on the correspondence between the first shard and the address range in the first target storage medium, the data to be stored can be stored in the target address range in the first target storage medium. Therefore, when it is determined that the target address range is entirely within the address range corresponding to the first shard in the first target storage medium, it means that the data to be stored can be completely stored in the first shard, and thus, the first shard can be directly determined as the target memory shard.
[0209] Accordingly, when it is determined that the target address range is not entirely within the address range corresponding to the first shard in the first target storage medium, it can be explained that the storage range occupied by part of the data to be stored in the first target storage medium is not within the address range corresponding to the first shard in the first target storage medium, and thus, the part of the data cannot be stored in the first shard. If the part of the data is stored in the first shard, then the part of the data will not be able to be stored in the correct address range in the first target storage medium. Therefore, when it is determined that the target address range is not entirely within the address range corresponding to the first shard in the first target storage medium, while determining the first shard as the target memory shard, it is also necessary to determine a second shard for storing the remaining data from the first target sub-area, and thus, the determined second shard can also be determined as the target storage allocation. The determined second shard is a memory shard that is in an available state.
[0210] In this way, the data to be stored can be stored in the first shard and the second shard. The first shard is used to store data whose address range in the first target storage medium is within the address range corresponding to the first shard in the first target storage medium, and the second shard is used to store the remaining data that cannot be stored in the first shard.
[0211] In the above step S2031, it may be determined that the first fragment does not exist in the first target sub-area corresponding to the first target storage medium.
[0212] Based on this, optionally, in a specific implementation, such as Figure 4 As shown in the above Figure 3 Based on the specific implementation shown, the above step S203 may further include the following steps:
[0213] S2035: If the first slice does not exist in the first target sub-area corresponding to the first target storage medium, determine a third slice for storing the data to be stored from the first target sub-area.
[0214] When it is determined that the first shard does not exist in the first target sub-region, it can be explained that the correspondence between the memory shard and the target address range has not yet been established. Thus, the electronic device can determine the third shard for storing the data to be stored from the first target sub-region.
[0215] The determined third shard is a memory shard in an available state.
[0216] Optionally, in a specific implementation, such as Figure 5 As shown, in the above step S2034, determining the second shard for storing the remaining data from the first target sub-region may include the following steps:
[0217] S2034a: Determine whether the storage space of the available memory slice in the first target sub-region meets the data volume of the remaining data; if so, execute S2034b; otherwise, execute step S2034c;
[0218] S2034b: Selecting a second shard for storing the remaining data from the available memory shards;
[0219] S2034c: Changing the state of at least one memory slice in the first target sub-region that is in an unavailable state to an available state, so that after the change is completed, the storage space of the memory slice in the available state in the first target sub-region can meet the data volume of the remaining data;
[0220] S2034d: After the change is completed, a second shard for storing the remaining data is selected from the available memory shards in the first target sub-region.
[0221] When it is determined that the target address range is not entirely within the address range corresponding to the first shard in the first target storage medium, the remaining data in the data to be stored that cannot be stored in the first shard can be determined based on the overlap between the target address range and the address range corresponding to the first shard in the first target storage medium, and then the data amount of the remaining data can be obtained.
[0222] In this way, it is possible to determine whether the storage space of the available memory slice in the first target sub-region satisfies the amount of remaining data, wherein "satisfying the amount of remaining data" means: not less than the amount of remaining data.
[0223] When it is determined that the storage space of the available memory slices in the first target sub-region meets the amount of the remaining data, the electronic device can directly select a second slice from these available memory slices to store the remaining data. The storage space of the selected second slice is not less than the amount of the remaining data and the second slice is available.
[0224] When it is determined that the storage space of the memory slices in the available state in the first target sub-area does not meet the data amount of the remaining data, that is, only the currently existing memory slices in the available state will not be able to store all the remaining data in the first target sub-area, thus, the status of some memory slices that are currently in the unavailable state can be updated to the available state for storing the remaining data.
[0225] In this way, when it is determined that the storage space of the memory slices in the available state in the first target sub-area does not meet the data volume of the remaining data, the state of at least one memory slice in the unavailable state in the first target sub-area can be changed to an available state, so that after the change is completed, the storage space of the memory slices in the available state in the first target sub-area meets the data volume of the remaining data.
[0226] Then, after the change is completed, a second shard for storing the remaining data can be selected from the available memory shards in the first target sub-region, wherein the storage space of the selected second shard is not less than the amount of the remaining data and the second shard is available.
[0227] Among them, optionally, the number of memory shards that undergo state changes can be: determined based on the amount of remaining data and the size of the memory shards, so that after the change is completed, the storage space of the memory shards in the available state in the first target sub-area meets the amount of remaining data and is the minimum number; it can also be: a specified number, and after one change is completed, determine whether the storage space of the memory shards in the available state in the first target sub-area meets the amount of remaining data. If so, continue to execute the subsequent step S2034d, otherwise, perform another change; this is all reasonable.
[0228] In addition, optionally, the order of each memory shard in the unavailable state may be determined according to the value of the elimination sequence in the index of each memory shard in the unavailable state in ascending order, and starting from the memory shard ranked first, the state of at least one memory shard in the unavailable state may be changed to the available state;
[0229] Optionally, the order of each memory shard in the unavailable state may be determined according to the order of importance of the data stored in each memory shard in the unavailable state from low to high, and starting from the memory shard ranked first, the state of at least one memory shard in the unavailable state may be changed to an available state;
[0230] Of course, other methods may also be used to determine at least one unavailable memory slice that needs to change its state, and this embodiment of the present invention does not specifically limit this.
[0231] When the data to be stored is data generated by a process running in an electronic device and needs to be stored in a storage medium in the electronic device, then the data stored in each memory slice needs to be written to the corresponding storage medium. In this way, for each memory slice, since the number of updates of the memory slice can represent the number of updates of the data stored in the memory slice, that is, it can represent the number of times data is written to the memory slice, and the number of disk flushes of the memory slice can represent the number of times the data stored in the memory slice is written to the corresponding storage medium, therefore, when the data currently stored in the memory slice is written to the storage medium, the number of updates and the number of disk flushes of the memory slice are the same.
[0232] However, in many cases, there may be memory slices in the first target sub-area whose stored data has not been written to the corresponding storage medium. Then, when the status of these memory slices is changed to the available state, the data stored in these memory slices may be lost.
[0233] Based on this, optionally, in a specific implementation, such as Figure 6As shown, in the above step S2034, determining the second shard for storing the remaining data from the first target sub-region may further include the following steps:
[0234] S2034e: Determine a memory slice to be refreshed based on the relationship between the update count and the disk flush count of each unavailable memory slice in the first target sub-region, and write the data stored in the memory slice to be refreshed to the first target storage medium;
[0235] Wherein, flashing the disk is: writing the stored data to the first target storage medium;
[0236] After the writing is completed, the above step S2034c is executed to change the state of at least one memory slice in the first target sub-region that is in an unavailable state to an available state.
[0237] In this specific implementation, before changing the state of at least one memory slice in the first target sub-region that is in an unavailable state to an available state, the relationship between the number of updates and the number of disk flushes for each memory slice in the first target sub-region that is in an unavailable state can be first determined, and then, based on the above-determined relationship, the memory slice to be refreshed can be determined among the memory slices in the first target sub-region that are in an unavailable state. In this way, the data stored in the determined memory slice to be refreshed can be written to the first target storage medium. Furthermore, after the above-mentioned writing is completed, the step of changing the state of at least one memory slice in the first target sub-region that is in an unavailable state to an available state can be continued.
[0238] In addition, the above Figure 4 In step S2035 of the specific implementation method shown, the specific implementation method of determining the third shard for storing the data to be stored from the first target sub-area is similar to the specific implementation method of determining the second shard for storing the remaining data from the first target sub-area in the above step S2034, and will not be repeated here.
[0239] Optionally, in a specific implementation, such as Figure 7 As shown in the above Figure 3 Based on the specific implementation shown, after determining that the target address range is entirely within the address range corresponding to the first shard in the first target storage medium and before determining the first shard as the target memory shard, step S203 of determining, based on the target address range, the target memory shard for storing the data to be stored in the first target sub-area corresponding to the first target storage medium may further include the following steps:
[0240] S2036: Determine whether the first segment meets any of the preset write conditions; if yes, execute step S2033; otherwise, execute step S2037;
[0241] The preset write conditions include: no data is stored in the first slice, and data is stored in the first slice, and there is no gap between the address range of the stored data in the first slice and the address range corresponding to the target address range in the first slice;
[0242] S2037: Writing the data to be stored into the target address range of the first target storage medium.
[0243] After determining that the target address range is entirely within the address range corresponding to the first segment in the first target storage medium, the electronic device may further determine whether the first segment satisfies any of the preset write conditions.
[0244] Wherein, when the judgment result is yes, the electronic device may determine the first slice as the target memory slice, so that the data to be stored may be stored in the first slice;
[0245] That is, when no data is stored in the first shard, the first shard may be determined as the target memory shard;
[0246] For example, as shown in Figure 8(a), it is a schematic diagram of the first shard in which no data is stored, and as shown in Figures (b) to 8(d), it is a schematic diagram of various storage situations of the data to be stored in the first shard when the data to be stored is stored in the first shard; wherein, the idle data is the idle area, indicating that no data is stored in the area, and the valid data is the data to be stored stored in the first shard.
[0247] When data is stored in the first shard, it is further determined whether there is a gap between the address range of the stored data in the first shard and the address range corresponding to the target address range in the first shard. In other words, when data is stored in the first shard, it is necessary to determine whether the intersection of the address range occupied by the stored data and the address range occupied by the data to be stored in the first target storage medium is a continuous address range.
[0248] When determining that data is stored in the first shard, the address range of the stored data in the first shard and the address range corresponding to the target address range in the first shard can be first determined, thereby determining whether the intersection of the two address ranges is a continuous address range. Therefore, when the intersection of the two address ranges is a continuous address range, it can be explained that there is no gap between the address range of the stored data in the first shard and the address range corresponding to the target address range in the first shard; otherwise, it can be explained that there is a gap between the address range of the stored data in the first shard and the address range corresponding to the target address range in the first shard.
[0249] When it is determined that no free area exists, the first slice may be determined as the target memory slice, and the data to be stored may be written into the first slice.
[0250] Correspondingly, when the judgment result is that the first slice does not meet any of the preset writing conditions, the data to be stored may not be stored in the memory slice of the first target sub-area, but directly written to the target address range of the first target storage medium.
[0251] For example, FIG8(d) is a schematic diagram of a first slice when data is stored in the initial portion of the first slice; FIG8(e) to FIG8(f) are schematic diagrams of the relationship between the address range of the stored data in the first slice shown in FIG8(d) and the address range corresponding to the target address range in the first slice; wherein, free data is an idle area, indicating that no data is stored in the area; valid data is data stored in the first slice; and new data is data to be stored;
[0252] As shown in FIG8( c ), this is a schematic diagram of the first shard when data is stored at the tail of the first shard; Figure 8(g)-Figure 8(h) 8(c) shows a schematic diagram of the relationship between the address range of the stored data and the address range corresponding to the target address range in the first slice; wherein, free data refers to an idle area, indicating that no data is stored in the area; valid data refers to the data stored in the first slice; and new data refers to the data to be stored;
[0253] As shown in FIG8( b ), this is a schematic diagram of the first shard when data is stored in the middle portion of the first shard; Figure 8(i)-Figure 8(m) As shown, it is a schematic diagram of the relationship between the address range of the stored data in the first slice shown in Figure 8(b) and the address range corresponding to the target address range in the first slice; wherein, the idle data is the idle area, indicating that no data is stored in the area, the valid data is the data stored in the first slice, and the new data is the data to be stored.
[0254] Optionally, in a specific implementation, in the above Figure 3 and Figure 7 Based on the specific implementation shown, after determining that the target address range is not entirely within the address range corresponding to the first shard in the first target storage medium, and before determining the second shard for storing the remaining data from the first target sub-region, the above step S203 of determining the target memory shard for storing the data to be stored in the first target sub-region corresponding to the first target storage medium based on the target address range may further include the following steps 2-3:
[0255] Step 2: Determine whether the first shard meets any of the preset storage conditions; if so, execute step S2033; otherwise, execute step 3;
[0256] The preset storage conditions include: no data is stored in the first shard, data is stored in the first shard, and there is no gap between the address range of the stored data in the first shard and the specified address range; the specified address range is: the address range in the first shard corresponding to the address range occupied by the data to be stored in the first target storage medium that can be stored in the first shard;
[0257] Step 3: Write the data to be stored into the target address range of the first target storage medium.
[0258] Optionally, in a specific implementation, the above step S204, storing the data to be stored in the target memory slice, may include the following steps 4-5:
[0259] Step 4: If the target memory slice is the first slice, determine the target starting offset address in the first slice, and write the data to be stored into the first slice starting from the target starting offset address;
[0260] The storage address corresponding to the target starting offset address in the first target storage medium is the starting address of the target address range;
[0261] In this specific implementation, if the target memory shard is the first shard, and all the data to be stored can be written into the first shard, then, based on the correspondence between the first shard and the address range in the first target storage medium, the target starting offset address corresponding to the starting address of the target address range in the first shard can be determined. In other words, the storage address corresponding to the target starting offset address in the first target storage medium is the starting address of the target address range. In this way, the data to be stored can be written into the first shard starting from the target starting offset address.
[0262] Step 5: If the target memory slice is the first slice and the second slice, determine the specified offset address range in the first slice, write the target data in the data to be stored into the specified offset address range, and write the remaining data in the data to be stored into the second slice;
[0263] Among them, the address range corresponding to the specified offset address range in the first target storage medium is the address range in the address range corresponding to the first shard in the first target storage medium that overlaps with the target address range, and the target data is: the data whose storage address in the first target storage medium is the storage address corresponding to the specified offset address range; the remaining data is: the data to be stored that cannot be stored in the first shard.
[0264] In this specific implementation, if the target memory shards are the first shard and the second shard, part of the data to be stored is written into the first shard, and the other part of the data is written into the second shard.
[0265] Then, based on the correspondence between the first shard and the address range in the first target storage medium and the target address range, the address range where the address range corresponding to the first shard in the first target storage medium overlaps with the target address range is determined, thereby determining the address range corresponding to the overlapping address range in the first shard, and obtaining the specified offset address range in the first shard. Furthermore, based on the specified offset address range, the target data in the data to be stored can be determined, and the storage address of the target data in the first target storage medium is the storage address corresponding to the specified offset address range in the first target storage medium.
[0266] In this way, the target data in the data to be stored can be written into the specified offset address range, and the remaining data in the data to be stored can be written into the second slice.
[0267] The remaining data may be written to any location in the second shard, which is not specifically limited in the embodiment of the present invention.
[0268] It should be emphasized that: when writing the data to be stored to the target memory slice, if there is previously stored data at the location where the data to be stored is to be written, then the data to be stored can be stored over the location, that is, the data to be stored overwrites the previously stored data.
[0269] Furthermore, optionally, in a specific implementation, the above step S204 of storing the data to be stored in the target memory slice may further include the following step 6:
[0270] Step 6: If the target memory shard is the third shard, write the data to be stored into the third shard;
[0271] In this specific implementation, the data to be stored can be written to any location in the third shard, and this embodiment of the present invention does not impose any specific limitation on this.
[0272] Optionally, in a specific implementation, each sub-region further includes a shared memory index area; the shared memory index area includes shared memory header information and index information of each memory shard in the sub-region; the shared memory header information includes an IO count, and the index information of each memory shard includes a memory update count, a disk flush count, and an elimination sequence;
[0273] The memory update count indicates the number of times the data stored in the memory shard is updated, the disk flush count indicates the number of times the data stored in the memory shard is written to the corresponding storage medium, and the elimination sequence indicates the order in which the memory shard is changed from an unavailable state to an available state.
[0274] For example, Figure 1 The schematic diagram of the structure of each sub-region is shown, where each sub-region includes a shared memory index area and n memory shards. Furthermore, the shared memory index area includes shared memory header information and the index of each memory shard. The data structure of the shared memory information header includes: I / O count; the data structure of the index of each memory shard includes: memory update count, disk flush count, and elimination sequence.
[0275] Furthermore, in this specific implementation, a data storage method provided by an embodiment of the present invention may further include the following steps 7-8:
[0276] Step 7: After writing data to each memory slice, increase the IO count in the shared memory header information of the sub-region where the memory slice is located by 1, update the elimination sequence in the index information of the memory slice to the IO count after the increase by 1, and increase the memory update count in the index information of the memory slice by 1;
[0277] Step 8: After writing the data stored in each memory slice to be refreshed to the first target storage medium, add 1 to the disk flush count in the index information of the memory slice to be refreshed.
[0278] In this specific implementation, after writing data to each memory shard, the IO count in the shared memory header information of the sub-area where the memory shard is located can be increased by 1, and the IO count obtained after adding 1 can be used as the elimination sequence of the memory shard and updated to the index information of the memory shard, that is, the elimination sequence in the index information of the memory shard is updated to the IO count after adding 1, and the memory update count in the index information of the memory shard can also be increased by 1.
[0279] Furthermore, after the data stored in each memory slice to be refreshed is written to the first target storage medium, the disk flush count in the index information of the memory slice to be refreshed may be increased by 1.
[0280] In addition, optionally, after writing data to each memory slice, other information in the shared memory header information of the sub-region where the memory slice is located may be further updated; for example, Figure 1 The number of free shards in the shared memory header information shown;
[0281] Of course, after writing data to each memory slice and writing the data stored in each memory slice to be refreshed to the first target storage medium, other information in the index of the memory slice may be further updated; for example, Figure 1 The valid data offset, disk start time, valid data size, free mark, etc. are shown.
[0282] Optionally, in a specific implementation, the shared memory header information further includes: a memory initialization flag; in this specific implementation, if Figure 9 As shown, a data storage method provided by an embodiment of the present invention may further include the following steps:
[0283] S201a: For each storage medium, determine whether the sub-region corresponding to the storage medium is initialized based on the memory initialization flag in the shared memory header information of the sub-region corresponding to the storage medium; if so, execute step S201b; otherwise, execute step S201c;
[0284] S201b: Determine a memory slice to be cleaned that stores data in the sub-area corresponding to the storage medium, write the data stored in the memory slice to be cleaned to the storage medium, and mark the state of the memory slice to be cleaned as available;
[0285] S201c: Initialize the sub-area corresponding to the storage medium.
[0286] Before executing any of the data storage methods provided by the above embodiments of the present invention, it is necessary to first set sub-areas corresponding to the respective storage media of the electronic device in the shared memory of the electronic device.
[0287] Optionally, the electronic device may obtain identifications of various storage media, and thereby determine sub-areas corresponding to various storage media of the electronic device in the shared memory based on the identifications and requirements in actual applications and user instructions.
[0288] Furthermore, before obtaining the data to be stored, the electronic device needs to first determine whether the sub-area corresponding to each storage medium has been initialized. That is to say, for each storage medium, the electronic device can obtain the memory initialization mark in the shared memory header information of the sub-area corresponding to the storage medium, and thus determine whether the sub-area corresponding to the storage medium is initialized based on the content of the obtained memory initialization mark.
[0289] For example, the numbers "0" and "1" can be used to indicate whether the sub-area has been initialized. For example, when the memory initialization mark is 1, it can indicate that the sub-area has been initialized, and when the memory initialization mark is 0, it can indicate that the sub-area has not been initialized.
[0290] Furthermore, for each storage medium, when it is determined that the sub-region corresponding to the storage medium is not initialized, the electronic device may first initialize the sub-region. Initializing the sub-region means: initially setting the shared memory information header of the sub-region and the indexes of each memory shard of the sub-region.
[0291] Correspondingly, for each storage medium, when it is determined that the sub-area corresponding to the storage medium has been initialized, the memory slice to be cleaned that stores data in the sub-area can be determined, and the data stored in the memory slice to be cleaned can be written to the storage medium. Then, the electronic device can mark the status of the memory slice to be cleaned as available.
[0292] Optionally, in a specific implementation, each sub-region includes: a shared memory index area; the shared memory index area includes an index of each memory shard in the sub-region; and the index of each memory shard includes: a memory update count, a disk flush count, and an elimination sequence. In a data storage method provided by an embodiment of the present invention, the electronic device may further perform the following steps for each sub-region:
[0293] Step 11: For each unavailable memory shard in the sub-region, check whether the memory update count and the disk flush count in the index of the memory shard are the same; if they are the same, go to step 12; otherwise, go to step 13;
[0294] Step 12: Determine whether the elimination sequence of the memory slice is within a preset range; if so, execute step 14;
[0295] Step 14: Change the state of the memory slice to available state;
[0296] Step 13: Determine whether the storage allocation meets any of the preset elimination conditions; if so, proceed to step 15;
[0297] The preset elimination conditions include: a ratio of the storage space occupied by the data stored in the memory slice to the storage space of the memory slice is greater than a preset ratio, and a ratio of the storage space occupied by the data stored in the memory slice to the storage space of the memory slice is not greater than a preset ratio, and the elimination sequence of the memory slice is within a preset range;
[0298] Step 15: Write the data stored in the memory slice to the storage medium corresponding to the sub-area, increase the disk flush count in the index of the memory slice by 1, and change the status of the memory slice to available.
[0299] In this specific implementation, in order to include more memory slices in an available state in each sub-region, the electronic device can determine the state of some memory slices in an unavailable state in each sub-region as an available state.
[0300] Among them, for each sub-area, the electronic device can first determine the storage slices in the sub-area that are in an unavailable state, and then, for each memory slice determined to be in an unavailable state, the electronic device can detect whether the memory update count in the index of the memory slice is the same as the disk flush count.
[0301] If the memory update count in the index of the memory shard is the same as the disk flush count, it means that the data currently stored in the memory shard has been written to the corresponding storage medium. Then, the electronic device can further determine whether the elimination sequence of the memory shard is within a preset range.
[0302] Furthermore, when it is determined that the elimination sequence of the memory slice is within the preset range, the state of the memory slice can be determined to be an available state.
[0303] The preset range may be any integer value not greater than the total number of memory shards included in the sub-region; after determining the arrangement order of the unusable memory shards in the sub-region according to the values of the elimination sequences of the unusable memory shards in the sub-region from small to large, the elimination sequences of the unusable memory shards ranked before the integer value are within the preset range;
[0304] The above-mentioned preset range can also be: any percentage not greater than 1; after determining the arrangement order of each memory slice in an unusable state according to the values of the elimination sequences of each memory slice in an unusable state in the sub-area from small to large, the elimination sequence of the memory slice whose ratio of the number of arrangement bits to the total number of memory slices in an unusable state in the sub-area is less than the percentage is within the preset range.
[0305] Correspondingly, if the memory update count in the index of the memory shard is different from the disk flush count, it means that the data currently stored in the memory shard has not yet been written to the storage medium corresponding to the sub-region. Then, the electronic device can further determine whether the memory shard meets any of the preset elimination conditions.
[0306] Furthermore, when it is determined that the memory slice meets any of the preset elimination conditions, the data stored in the memory slice can be written to the storage medium corresponding to the sub-area, the disk flush count in the index of the memory slice can be increased by 1, and the status of the memory slice can be determined to be available.
[0307] When detecting that the memory update count in the index of the memory slice is different from the disk flush count, the electronic device may further determine whether a ratio of the storage space occupied by the data stored in the memory slice to the storage space of the memory slice is greater than a preset ratio;
[0308] If it is greater than, the data stored in the memory slice can be written to the storage medium corresponding to the sub-region, the disk flush count in the index of the memory slice is increased by 1, and the state of the memory slice is changed to available state;
[0309] If it is not greater than, it can be further determined whether the elimination sequence of the memory slice is within the above preset range;
[0310] If it is located, the data stored in the memory slice can be written to the storage medium corresponding to the sub-area, the disk flush count in the index of the memory slice is increased by 1, and the status of the memory slice is changed to available.
[0311] Optionally, in a specific implementation, a data storage method provided by an embodiment of the present invention may further include the following step 9:
[0312] Step 9: For each sub-region, when the sub-region meets the preset disk flushing condition, write the data stored in each memory slice in the sub-region to the storage medium corresponding to the sub-region;
[0313] The preset disk flushing conditions include: the amount of stored data exceeds the preset amount of data, or the time since the last disk flush reaches the preset time.
[0314] In this specific implementation, for each sub-area, the electronic device can detect whether the sub-area meets the preset disk flushing conditions, and when it is detected that the sub-area meets the preset disk flushing conditions, the data stored in each memory slice in the sub-area is written to the storage medium corresponding to the sub-area.
[0315] For each sub-region, the electronic device can detect whether the amount of data stored in the sub-region exceeds a preset data amount. Thus, when it is detected that the amount of data stored in the sub-region exceeds the preset data amount, the data stored in each memory slice in the sub-region can be written to the storage medium corresponding to the sub-region.
[0316] For each sub-region, the electronic device may also periodically write the data stored in each memory slice in the sub-region to the storage medium corresponding to the sub-region at preset time intervals.
[0317] Optionally, after writing the data stored in each memory slice in the sub-region to the storage medium corresponding to the sub-region, the electronic device may also clear each memory slice storing data in the sub-region.
[0318] Optionally, in a specific implementation, a data storage method provided by an embodiment of the present invention may further include the following step 10:
[0319] Step 10: For each sub-region, when the number of memory slices in an available state in the sub-region is less than a preset number, change the state of at least one memory slice in an unavailable state in the sub-region to an available state, so that after the change is completed, the number of memory slices in an available state in the sub-region is not less than a preset number.
[0320] In this specific implementation, for each sub-area, the electronic device can detect the number of memory slices in an available state included in the sub-area. Thus, when it is detected that the number of memory slices in an available state in the sub-area is less than a preset number, the electronic device can change the state of at least one memory slice in an unavailable state in the sub-area to an available state, so that after the change is completed, the number of memory slices in an available state in the sub-area is not less than the preset number.
[0321] Among them, the number of memory shards to be changed in state can be: after the change is completed, the number of memory shards in the available state in the sub-area is not less than the minimum number of the preset number; it can also be: the number of memory shards that are all in the unavailable state; it can also be a specified number, and after the state is changed once, it is detected whether the number of memory shards in the available state included in the sub-area is less than the preset value. If it is less, the state of the specified number of memory shards in the unavailable state can be changed to the available state again.
[0322] Furthermore, when the number of memory shards to be changed in state is a specified number, and the number of memory shards in an unavailable state is less than the specified number, the states of all memory shards in an unavailable state may be changed to an available state.
[0323] Next, a data reading method provided by an embodiment of the present invention will be described in detail with reference to the accompanying drawings.
[0324] Figure 10 A flow chart of a data reading method provided by an embodiment of the present invention is shown as follows: Figure 10 As shown, the reading method may include the following steps:
[0325] S1001: Obtain a data read instruction generated by a target process, and determine a second target sub-region corresponding to a second target storage medium for storing data to be read;
[0326] When the target process needs to obtain certain data from the storage medium of the electronic device during operation, the target process can generate a data reading instruction regarding the data to be read, which carries the identification of the storage medium storing the data to be read and the target address range of the data to be read in the storage medium.
[0327] In this way, the electronic device can obtain the data reading instruction generated by the target process, and after obtaining the data reading instruction, determine the second target sub-area corresponding to the second target storage medium for storing the data to be read, and the target address range of the data to be read in the storage medium.
[0328] S1002: Searching for data to be read in each memory slice storing data in the second target sub-region;
[0329] Furthermore, after determining the second target sub-region corresponding to the second target storage medium for storing the data to be read, the data to be read can be searched for in each memory slice storing data in the second target sub-region.
[0330] Among them, since each memory slice storing data in the second target sub-area can establish a corresponding relationship with the address range in the second target storage medium, the electronic device can search the above-mentioned target address range in the above-mentioned corresponding relationship. Then, in the above-mentioned corresponding relationship, the memory slice corresponding to the above-mentioned target address range may store data to be read.
[0331] Furthermore, the electronic device can determine the offset address corresponding to the target address range in the memory slice corresponding to the target address range, thereby checking whether data is stored in the offset address and whether the stored data is all the data to be read.
[0332] S1003: If all the data to be read are found, the data to be read are written into the virtual cache of the target process;
[0333] If all the data to be read can be found in each memory slice storing data in the second target sub-area, the electronic device can directly read the found data, thereby writing the read data to be read into the virtual cache of the target process for use by the target process.
[0334] S1004: If partial data of the data to be read is found, the partial data and the missing data read from the second target storage medium are written into the virtual cache of the target process;
[0335] The missing data is: data in the data to be read that is not stored in the second target sub-region;
[0336] If part of the data to be read can be found in each memory slice storing data in the second target sub-area, the electronic device can determine the missing data in the data to be read and the storage address of the missing data in the second target storage medium based on the offset address of the part of the data in the memory slice, the correspondence between the memory slice and the storage range in the second target storage medium, and the target address range. Thus, the electronic device can read the missing data from the second target storage medium based on the storage address determined above. Furthermore, the electronic device can write the part of the data read from the memory slice of the second target sub-area and the missing data read from the second target storage medium to the virtual cache of the target process for use by the target process.
[0337] Optionally, the electronic device may first write part of the data read from the memory slice of the second target sub-area into the virtual cache of the target process, then read the missing data from the second target storage medium, and write the missing data directly into the virtual cache of the target process;
[0338] Optionally, the electronic device may first write part of the data read from the memory slice of the second target sub-area to the virtual cache of the target process, then read the missing data from the second target storage medium, and write the missing data to the memory slice of the second target sub-area, thereby reading the missing data from the memory slice of the second target sub-area and writing the missing data to the virtual cache of the target process;
[0339] Optionally, the electronic device may first write part of the data read from the memory slice of the second target sub-region to the virtual cache of the target process, then read the missing data from the second target storage medium, and write the missing data directly to the virtual cache of the target process, and at the same time, write the missing data to the memory slice of the second target sub-region;
[0340] Optionally, the electronic device can read the missing data from the second target storage medium and write the missing data to the memory slice of the second target sub-area, thereby reading the complete data to be read from the memory slice of the second target sub-area and writing the read data to be read to the virtual cache of the target process.
[0341] S1005: If the data to be read is not found, the data to be read read from the second target storage medium is written into the virtual cache of the target process.
[0342] If any of the data to be read cannot be found in the memory slices storing data in the second target sub-area, the electronic device can directly read the data to be read from the second target storage medium according to the target address range, and write the data to be read from the second target storage medium to the virtual cache of the target process for use by the target process.
[0343] Optionally, the electronic device may read the data to be read from the second target storage medium, and directly write the data to be read into the virtual cache of the target process;
[0344] Optionally, the electronic device may read the data to be read from the second target storage medium and write the data to be read into the memory slice of the second target sub-area, thereby further reading the data to be read from the memory slice of the second target sub-area and writing the data to be read into the virtual cache of the target process;
[0345] Optionally, the electronic device may read the data to be read from the second target storage medium, and directly write the data to be read into the virtual cache of the target process, and at the same time, write the data to be read into the memory slice of the second target sub-area.
[0346] Among them, in a data reading method provided in an embodiment of the present invention, the manner in which missing data or data to be stored read from the second target storage medium is written into the memory shards in the second target sub-area is similar to a data storage method provided in the above-mentioned embodiment of the present invention, and will not be repeated here.
[0347] Corresponding to the data storage method and the data reading method provided by the above-mentioned embodiments of the present invention, the embodiments of the present invention further provide a data storage device and a data reading device.
[0348] Figure 11 Schematic diagram of the structure of a data storage device provided in an embodiment of the present invention, the storage device is applied to an electronic device, wherein sub-areas corresponding to the respective storage media of the electronic device are set in the shared memory of the electronic device, and each sub-area includes multiple memory slices; Figure 11 As shown, the storage device may include the following modules:
[0349] A data acquisition module 1110 is used to acquire data to be stored;
[0350] An information determining module 1120 is configured to determine a first target storage medium for storing the data to be stored, and a target address range of the data to be stored in the first target storage medium;
[0351] A shard determination module 1130 is configured to determine, based on the target address range, a target memory shard for storing the data to be stored in a first target sub-area corresponding to the first target storage medium;
[0352] The data storage module 1140 is configured to store the data to be stored in the target memory slice.
[0353] As can be seen from the above, since the data in the shared memory is visible to all processes, when multiple processes need to access the same address range of the same storage medium, the data stored in the same address range of the same storage medium can be stored in the sub-area corresponding to the storage medium in the shared memory, so that multiple processes can access the data from the sub-area, thereby ensuring the consistency of the data accessed by multiple processes.
[0354] Optionally, in a specific implementation, the shard determination module 1130 includes:
[0355] A first shard determination submodule is configured to determine whether a first shard exists in a first target sub-area corresponding to the first target storage medium; if so, trigger an address range determination submodule; wherein the first shard is a memory shard for which a correspondence has been established with an address range in the first target storage medium, and the corresponding address range in the first target storage medium overlaps with the target address range;
[0356] The address range determination submodule is configured to determine whether the target address range is entirely within the address range corresponding to the first shard in the first target storage medium; if so, trigger the first target shard determination submodule; otherwise, trigger the second shard determination submodule;
[0357] The first target shard determination submodule is configured to determine the first shard as a target memory shard;
[0358] The second shard determination submodule is used to determine a second shard for storing the remaining data from the first target sub-area, and determine the first shard and the second shard as target memory shards; wherein the remaining data is: data in the data to be stored that cannot be stored in the first shard.
[0359] Optionally, in a specific implementation, the second shard determination submodule includes:
[0360] a space determination unit, configured to determine whether the storage space of the memory shard in the first target sub-area that is in an available state satisfies the amount of remaining data; if so, trigger the second shard determination unit; otherwise, trigger the state change unit;
[0361] The second shard determination unit is configured to select a second shard for storing the remaining data from the available memory shards;
[0362] The state changing unit is configured to change the state of at least one memory slice in the first target sub-region that is in an unavailable state to an available state, so that after the change is completed, the storage space of the memory slice in the available state in the first target sub-region meets the data volume of the remaining data;
[0363] The second shard determination unit is further configured to select, after the change is completed, a second shard for storing the remaining data from the available memory shards in the first target sub-region.
[0364] Optionally, in a specific implementation, the device further includes:
[0365] a disk flushing module configured to, before changing the state of at least one memory slice in an unavailable state in the first target sub-region to an available state, determine a memory slice to be refreshed based on the relationship between the number of updates and the number of disk flushes of each memory slice in an unavailable state in the first target sub-region, and write the data stored in the memory slice to be refreshed to the first target storage medium; and trigger the state changing unit after the writing is completed;
[0366] The flashing step includes writing the stored data into the first target storage medium.
[0367] Optionally, in a specific implementation, the device further includes:
[0368] a write condition judgment module, configured to, before determining the first shard as the target memory shard, determine whether the first shard satisfies any of the preset write conditions; if so, trigger the first target shard determination submodule; otherwise, trigger the data write module; wherein the preset write conditions include: no data is stored in the first shard; data is stored in the first shard, and there is no gap between the address range of the stored data in the first shard and the address range corresponding to the target address range in the first shard;
[0369] The data writing module is configured to write the data to be stored into the target address range of the first target storage medium.
[0370] Optionally, in a specific implementation, the device further includes:
[0371] The third shard determination submodule is configured to determine a third shard for storing the data to be stored from the first target subregion if the first shard does not exist in the first target subregion corresponding to the first target storage medium.
[0372] Optionally, in a specific implementation, the data storage module 1140 is specifically configured to:
[0373] If the target memory slice is the first slice, determining a target starting offset address in the first slice, and writing the data to be stored into the first slice starting from the target starting offset address; wherein the storage address corresponding to the target starting offset address in the first target storage medium is the starting address of the target address range;
[0374] If the target memory shards are the first shard and the second shard, determine the specified offset address range in the first shard, write the target data in the data to be stored into the specified offset address range, and write the remaining data in the data to be stored into the second shard; wherein, the address range corresponding to the specified offset address range in the first target storage medium is the address range in the address range corresponding to the first shard in the first target storage medium that overlaps with the target address range, and the target data is: the data whose storage address in the first target storage medium is the storage address corresponding to the specified offset address range; the remaining data is: the data in the data to be stored that cannot be stored in the first shard.
[0375] Optionally, in a specific implementation, each sub-region further includes: a shared memory index region; the shared memory index region includes: shared memory header information and an index of each memory shard in the sub-region; the shared memory header information includes: an IO count; the index of each memory shard includes: a memory update count, a disk flush count, and an elimination sequence; the memory update count is used to indicate the number of times data stored in the memory shard has been updated, the disk flush count is used to indicate the number of times data stored in the memory shard has been written to the corresponding storage medium, and the elimination sequence indicates the order in which the memory shard has been changed from an unavailable state to an available state; the device further includes:
[0376] A first index update module is configured to, after writing data to each memory slice, increment the IO count in the shared memory header information of the sub-region where the memory slice is located by 1, update the elimination sequence in the index information of the memory slice to the incremented IO count, and increment the memory update count in the index information of the memory slice by 1;
[0377] The second index updating module is configured to add 1 to the disk flush count in the index information of each memory slice to be refreshed after writing the data stored in the memory slice to be refreshed to the first target storage medium.
[0378] Optionally, in a specific implementation, the shared memory header information further includes: a memory initialization flag; and the device further includes:
[0379] an initialization determination module configured to determine, for each storage medium, whether the sub-region corresponding to the storage medium is initialized based on a memory initialization flag in the shared memory header information of the sub-region corresponding to the storage medium before acquiring the data to be stored; if so, trigger the data cleaning module; otherwise, trigger the initialization module;
[0380] The data cleaning module is configured to determine a memory slice to be cleaned that stores data in a sub-area corresponding to the storage medium, write the data stored in the memory slice to be cleaned to the storage medium, and mark the state of the memory slice to be cleaned as available;
[0381] The initialization module is used to initialize the sub-area corresponding to the storage medium.
[0382] Optionally, in a specific implementation, the apparatus further includes an elimination module configured to perform the following steps for each sub-region:
[0383] For each unavailable memory shard in the sub-region, check whether the memory update count and disk flush count in the index of the memory shard are the same;
[0384] If they are the same, determine whether the elimination sequence of the memory slice is within the preset range;
[0385] If it is, change the state of the memory slice to available state;
[0386] If they are not the same, determining whether the storage allocation satisfies any one of the preset elimination conditions; wherein the preset elimination conditions include: a ratio of the storage space occupied by the data stored in the memory slice to the storage space of the memory slice is greater than a preset ratio, and a ratio of the storage space occupied by the data stored in the memory slice to the storage space of the memory slice is not greater than a preset ratio, and an elimination sequence of the memory slice is within the preset range;
[0387] When the conditions are met, the data stored in the memory slice is written to the storage medium corresponding to the sub-area, the disk flush count in the index of the memory slice is increased by 1, and the state of the memory slice is changed to available state.
[0388] Optionally, in a specific implementation, the device further includes:
[0389] The data flushing module is used to write the data stored in each memory slice in each sub-region to the storage medium corresponding to the sub-region when the sub-region meets the preset flushing conditions;
[0390] The preset disk flushing conditions include: the amount of stored data exceeds a preset amount of data, or the time since the last disk flush reaches a preset time.
[0391] Optionally, in a specific implementation, the device further includes:
[0392] The state change module is used to change the state of at least one memory slice in an unavailable state to an available state in each sub-region when the number of memory slices in the available state in the sub-region is less than a preset number, so that after the change is completed, the number of memory slices in the available state in the sub-region is not less than the preset number.
[0393] Figure 12 Schematic diagram of the structure of a data reading device provided in an embodiment of the present invention, the reading device is applied to an electronic device, wherein sub-areas corresponding to the respective storage media of the electronic device are set in the shared memory of the electronic device, and each sub-area includes multiple memory slices; Figure 12 As shown, the reading device may include the following modules:
[0394] The instruction acquisition module 1210 is used to acquire a data read instruction generated by a target process and determine a second target sub-region corresponding to a second target storage medium for storing data to be read;
[0395] A data search module 1220 is configured to search for the data to be read in each memory slice storing data in the second target sub-region;
[0396] A first data reading module 1230 is configured to write the data to be read into the virtual cache of the target process if all the data to be read is found;
[0397] The second data reading module 1240 is configured to, if partial data of the data to be read is found, write the partial data and the missing data read from the second target storage medium into the virtual cache of the target process; wherein the missing data is data in the data to be read that is not stored in the second target sub-region;
[0398] The third data reading module 1250 is configured to write the data to be read from the second target storage medium into the virtual cache of the target process if the data to be read is not found.
[0399] As can be seen from the above, since the data in the shared memory is visible to all processes, when multiple processes need to access the same address range of the same storage medium, the data stored in the same address range of the same storage medium can be stored in the sub-area corresponding to the storage medium in the shared memory, so that multiple processes can access the data from the sub-area, thereby ensuring the consistency of the data accessed by multiple processes.
[0400] Corresponding to the above-mentioned embodiment of the present invention, a data storage method and a data reading method are provided. The embodiment of the present invention also provides an electronic device, such as Figure 13 As shown, it includes a processor 1301, a communication interface 1302, a memory 1303 and a communication bus 1304, wherein the processor 1301, the communication interface 1302, and the memory 1303 communicate with each other through the communication bus 1304.
[0401] Memory 1303, used for storing computer programs;
[0402] The processor 1301 is configured to implement the steps of any data storage method provided by the above-mentioned embodiment of the present invention and / or the steps of any data reading method provided by the above-mentioned embodiment of the present invention when executing the program stored in the memory 1303 .
[0403] The communication bus mentioned in the electronic device mentioned above may be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus. This communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, only one thick line is used in the figure, but this does not mean that there is only one bus or only one type of bus.
[0404] The communication interface is used for communication between the above electronic device and other devices.
[0405] The memory may include random access memory (RAM) or non-volatile memory (NVM), such as at least one disk storage. Alternatively, the memory may be at least one storage medium located away from the processor.
[0406] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, and discrete hardware components.
[0407] In another embodiment provided by the present invention, a computer-readable storage medium is also provided, which stores a computer program. When the computer program is executed by a processor, it implements the steps of any data storage method provided by the above-mentioned embodiment of the present invention and / or the steps of any data reading method provided by the above-mentioned embodiment of the present invention.
[0408] In another embodiment provided by the present invention, a computer program product containing instructions is also provided, which, when run on a computer, enables the computer to execute the steps of any data storage method provided by the above-mentioned embodiment of the present invention and / or the steps of any data reading method provided by the above-mentioned embodiment of the present invention.
[0409] In the above embodiments, all or part of the embodiments can be implemented by software, hardware, firmware, or any combination thereof. When implemented using software, all or part of the embodiments can be implemented in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of the present invention are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via a wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) method. The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage medium such as a server or data center that includes one or more available media. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive (SSD)).
[0410] It should be noted that, in this document, relational terms such as first and second, etc., are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply the existence of any such actual relationship or order between these entities or operations. Moreover, the terms "comprises," "comprising," or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article, or device comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or elements inherent to such process, method, article, or device. In the absence of further limitations, an element defined by the phrase "comprising a ..." does not exclude the presence of other identical elements in the process, method, article, or device comprising the element.
[0411] Each embodiment in this specification is described in a related manner. Similar portions between the various embodiments can be referenced to each other. Each embodiment focuses on the differences between the other embodiments. In particular, since the apparatus embodiments, electronic device embodiments, computer-readable storage medium embodiments, and computer program product embodiments are generally similar to the method embodiments, their descriptions are relatively simple. For related portions, reference can be made to the descriptions of the method embodiments.
[0412] The above description is only a preferred embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present invention are included in the scope of protection of the present invention.
Claims
1. A data storage method, characterized in that: Applied to an electronic device, sub-areas corresponding to respective storage media of the electronic device are set in the shared memory of the electronic device, and each sub-area includes multiple memory slices; the method includes: Get the data to be stored; Determining a first target storage medium for storing the data to be stored, and a target address range of the data to be stored in the first target storage medium; Determine whether a first shard exists in a first target sub-area corresponding to the first target storage medium; wherein the first shard is a memory shard for which a correspondence has been established with an address range in the first target storage medium, and the corresponding address range in the first target storage medium overlaps with the target address range; If so, determining whether the target address range is entirely within the address range corresponding to the first fragment in the first target storage medium; If yes, determining the first shard as the target memory shard; Otherwise, determining a second shard for storing the remaining data from the first target sub-region, and determining the first shard and the second shard as target memory shards; wherein the remaining data is: data in the data to be stored that cannot be stored in the first shard; storing the data to be stored in the target memory slice; The step of determining, from the first target sub-region, a second shard for storing the remaining data includes: Determining whether the storage space of the available memory slice in the first target sub-region satisfies the amount of remaining data; If yes, selecting a second shard for storing the remaining data from the available memory shards; Otherwise, changing the state of at least one memory slice in the first target sub-region that is in an unavailable state to an available state, so that after the change is completed, the storage space of the memory slice in the available state in the first target sub-region can meet the data volume of the remaining data; After the change is completed, selecting a second shard for storing the remaining data from the available memory shards in the first target sub-region; Before the step of changing the state of at least one memory slice in the first target sub-region that is in an unavailable state to an available state, the method further includes: Determine a memory slice to be refreshed based on the relationship between the number of updates and the number of disk flushes for each memory slice in the unavailable state in the first target sub-area, and write the data stored in the memory slice to be refreshed to the first target storage medium; wherein the disk flushing includes: writing the stored data to the first target storage medium; After the writing is completed, the step of changing the state of at least one memory slice in the first target sub-region that is in an unavailable state to an available state is performed.
2. The method according to claim 1, characterized in that Before the step of determining the first shard as the target memory shard, the method further includes: Determining whether the first slice satisfies any of preset write conditions; wherein the preset write conditions include: no data is stored in the first slice; data is stored in the first slice, and there is no gap between an address range of the stored data in the first slice and an address range corresponding to the target address range in the first slice; If yes, determining the first shard as the target memory shard; Otherwise, the data to be stored is written into the target address range of the first target storage medium.
3. The method according to claim 1, characterized in that The method further comprises: If the first slice does not exist in the first target sub-area corresponding to the first target storage medium, a third slice for storing the data to be stored is determined from the first target sub-area.
4. The method according to claim 1, wherein The step of storing the data to be stored in the target memory slice includes: If the target memory slice is the first slice, determining a target starting offset address in the first slice, and writing the data to be stored into the first slice starting from the target starting offset address; wherein the storage address corresponding to the target starting offset address in the first target storage medium is the starting address of the target address range; If the target memory shards are the first shard and the second shard, determine the specified offset address range in the first shard, write the target data in the data to be stored into the specified offset address range, and write the remaining data in the data to be stored into the second shard; wherein, the address range corresponding to the specified offset address range in the first target storage medium is the address range in the address range corresponding to the first shard in the first target storage medium that overlaps with the target address range, and the target data is: the data whose storage address in the first target storage medium is the storage address corresponding to the specified offset address range; the remaining data is: the data in the data to be stored that cannot be stored in the first shard.
5. The method according to claim 1, characterized in that: Each sub-region further includes: a shared memory index region; the shared memory index region includes: shared memory header information and an index of each memory shard in the sub-region; the shared memory header information includes: an IO count; the index of each memory shard includes: a memory update count, a disk flush count, and an elimination sequence; the memory update count is used to indicate the number of times data stored in the memory shard has been updated, the disk flush count is used to indicate the number of times data stored in the memory shard has been written to the corresponding storage medium, and the elimination sequence indicates the order in which the memory shard has been changed from an unavailable state to an available state; the method further includes: After writing data to each of the memory slices, the IO count in the shared memory header information of the sub-region where the memory slice is located is increased by 1, and the elimination sequence in the index information of the memory slice is updated to the IO count after the increase by 1, and the memory update count in the index information of the memory slice is increased by 1; After the data stored in each of the memory slices to be refreshed is written to the first target storage medium, the disk flush count in the index information of the memory slice to be refreshed is increased by 1.
6. The method according to claim 5, characterized in that The shared memory header information further includes: a memory initialization flag; and before the step of obtaining the data to be stored, the method further includes: For each storage medium, determining whether the sub-region corresponding to the storage medium is initialized according to the memory initialization flag in the shared memory header information of the sub-region corresponding to the storage medium; If yes, determine that a memory slice to be cleaned has data stored in the sub-area corresponding to the storage medium, write the data stored in the memory slice to be cleaned to the storage medium, and mark the state of the memory slice to be cleaned as available; Otherwise, the sub-area corresponding to the storage medium is initialized.
7. The method according to claim 6, characterized in that The method further comprises: For each sub-region, perform the following steps: For each unavailable memory shard in the sub-region, check whether the memory update count and disk flush count in the index of the memory shard are the same; If they are the same, determine whether the elimination sequence of the memory slice is within the preset range; If it is, change the state of the memory slice to available state; If they are not the same, determining whether the storage allocation satisfies any one of the preset elimination conditions; wherein the preset elimination conditions include: a ratio of the storage space occupied by the data stored in the memory slice to the storage space of the memory slice is greater than a preset ratio, and a ratio of the storage space occupied by the data stored in the memory slice to the storage space of the memory slice is not greater than a preset ratio, and an elimination sequence of the memory slice is within the preset range; When the conditions are met, the data stored in the memory slice is written to the storage medium corresponding to the sub-area, the disk flush count in the index of the memory slice is increased by 1, and the state of the memory slice is changed to available state.
8. The method according to any one of claims 1 to 6, characterized in that The method further comprises: For each sub-region, when the sub-region meets the preset disk flushing condition, the data stored in each memory slice in the sub-region is written to the storage medium corresponding to the sub-region; The preset disk flushing conditions include: the amount of stored data exceeds a preset amount of data, or the time since the last disk flush reaches a preset time.
9. The method according to any one of claims 1 to 6, characterized in that The method further comprises: For each sub-region, when the number of memory slices in an available state in the sub-region is less than a preset number, the state of at least one memory slice in an unavailable state in the sub-region is changed to an available state, so that after the change is completed, the number of memory slices in an available state in the sub-region is not less than the preset number.
10. A data reading method, characterized in that: Applied to an electronic device, sub-areas corresponding to respective storage media of the electronic device are set in the shared memory of the electronic device, and each sub-area includes multiple memory slices; the method includes: Obtaining a data read instruction generated by a target process, and determining a second target sub-region corresponding to a second target storage medium for storing the data to be read; Searching for the data to be read in each memory slice storing data in the second target sub-area; If all the data to be read are found, the data to be read are written into the virtual cache of the target process; If partial data of the data to be read is found, the partial data and the missing data read from the second target storage medium are written into the virtual cache of the target process; wherein the missing data is data in the data to be read that is not stored in the second target sub-region; If the to-be-read data is not found, writing the to-be-read data read from the second target storage medium into the virtual cache of the target process; The method further comprises: According to the method according to any one of claims 1 to 9, the data to be read is written into the memory slice of the second target sub-region.
11. A data storage device, characterized in that: Applied to an electronic device, wherein sub-areas corresponding to respective storage media of the electronic device are set in the shared memory of the electronic device, and each sub-area includes multiple memory slices; the device includes: A data acquisition module, used to acquire data to be stored; an information determining module, configured to determine a first target storage medium for storing the data to be stored, and a target address range of the data to be stored in the first target storage medium; Shard determination module, include: A first shard determination submodule is configured to determine whether a first shard exists in a first target sub-area corresponding to the first target storage medium; if so, trigger an address range determination submodule; wherein the first shard is a memory shard for which a correspondence has been established with an address range in the first target storage medium, and the corresponding address range in the first target storage medium overlaps with the target address range; The address range determination submodule is configured to determine whether the target address range is entirely within the address range corresponding to the first shard in the first target storage medium; if so, trigger the first target shard determination submodule; otherwise, trigger the second shard determination submodule; The first target shard determination submodule is configured to determine the first shard as a target memory shard; The second shard determination submodule is configured to determine, from the first target sub-region, a second shard for storing remaining data, and determine the first shard and the second shard as target memory shards; wherein the remaining data is data of the to-be-stored data that cannot be stored in the first shard; A data storage module, configured to store the data to be stored in the target memory slice; The second fragment determination submodule includes: a space determination unit, configured to determine whether the storage space of the memory shard in the first target sub-area that is in an available state satisfies the amount of remaining data; if so, trigger the second shard determination unit; otherwise, trigger the state change unit; The second shard determination unit is configured to select a second shard for storing the remaining data from the available memory shards; The state changing unit is configured to change the state of at least one memory slice in the first target sub-region that is in an unavailable state to an available state, so that after the change is completed, the storage space of the memory slice in the available state in the first target sub-region meets the data volume of the remaining data; The second shard determination unit is further configured to select, after the change is completed, a second shard for storing the remaining data from the available memory shards in the first target sub-region; The device further comprises: a disk flushing module configured to, before changing the state of at least one memory slice in an unavailable state in the first target sub-region to an available state, determine a memory slice to be refreshed based on the relationship between the number of updates and the number of disk flushes of each memory slice in an unavailable state in the first target sub-region, and write the data stored in the memory slice to be refreshed to the first target storage medium; and trigger the state changing unit after the writing is completed; The flashing step includes writing the stored data into the first target storage medium.
12. A data reading device, characterized in that: Applied to an electronic device, wherein sub-areas corresponding to respective storage media of the electronic device are set in the shared memory of the electronic device, and each sub-area includes multiple memory slices; the device includes: an instruction acquisition module, configured to acquire a data read instruction generated by a target process and determine a second target sub-region corresponding to a second target storage medium for storing data to be read; a data search module, configured to search for the data to be read in each memory slice storing data in the second target sub-area; A first data reading module, configured to write the data to be read into the virtual cache of the target process if all the data to be read is found; a second data reading module, configured to, if partial data of the data to be read is found, write the partial data and the missing data read from the second target storage medium into the virtual cache of the target process; wherein the missing data is data in the data to be read that is not stored in the second target sub-region; a third data reading module, configured to write the data to be read from the second target storage medium into the virtual cache of the target process if the data to be read is not found; The device is also used for: According to the method according to any one of claims 1 to 9, the data to be read is written into the memory slice of the second target sub-region.
Citation Information
Patent Citations
Method and system for efficiently utilizing solid-state disk for caching
CN103678169A
Data writing method and device, data reading method and device and electronic device
CN112000589A