Key-value storage based graph database decimal index encoding and decoding method
By using a key-value storage-based graph database decimal index encoding method, decimal values are decomposed into symbols, weights, and number arrays. A compact binary encoding method is adopted to solve the problems of low storage efficiency, insufficient comparison performance, and negative number sorting logic defects in existing technologies. This achieves efficient data storage and retrieval, and is suitable for analysis tasks in financial transactions and social networks.
Patent Information
- Application Number
- CN202511367845.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-24
- Publication Date
- 2025-12-05
- Estimated Expiration
- 2045-09-24
AI Technical Summary
Existing graph database systems suffer from problems such as low storage efficiency, insufficient comparison performance, flawed negative number sorting logic, and incomplete handling of special values when processing high-precision decimal data, making it difficult to meet the needs of complex analysis tasks.
A key-value-based graph database decimal index encoding method is adopted. By decomposing decimal values into symbols, weights, and numeric arrays, and using a compact binary encoding method, a set of encoding auxiliary parameters is dynamically generated. Escape characters and padding characters are used to avoid conflicts, and pruning operations optimize storage space, ensuring the mathematical correctness of sorting and the handling of special values.
It significantly improves the indexing efficiency and query performance of high-precision decimal data in graph databases, and is suitable for large-scale network data analysis scenarios, such as amount sorting in financial transaction graphs and weight filtering in social network graphs.
Smart Images

Figure CN120849671B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The application relates to the technical field of graph database management, and in particular to a graph database decimal index encoding and decoding method based on key-value storage. BACKGROUND
[0002] In modern database systems, graph databases are widely used in social networks, financial transactions, knowledge graphs and other scenarios due to their efficient modeling capabilities for complex relationship networks. These application scenarios often involve the storage and query of high-precision decimal data (such as amount calculation, geographic coordinate analysis, scientific calculation, etc.), which puts strict requirements on the accuracy of numerical values, storage efficiency and query performance.
[0003] However, existing graph database systems face the following technical bottlenecks when dealing with high-precision decimal data:
[0004] 1. Low storage efficiency. In existing technologies, high-precision decimal data is usually stored in string or binary encoding. Although string storage can intuitively represent numerical values, it has the problem of occupying additional storage space due to redundant characters (such as leading zeros and decimal point symbols); while binary encoding schemes (such as IEEE 754 floating-point numbers) are difficult to meet the high-precision requirements, and may cause precision loss when dealing with values beyond the standard range. The above methods significantly increase the storage overhead in large-scale data set scenarios, limiting the scalability of the system.
[0005] 2. Insufficient comparison performance. In key-value storage systems, string-based decimal data comparison requires parsing the value size character by character, with a time complexity of O(n), where n is the number of numerical digits. In frequent range queries or sorting operations, the efficiency is low. For example, in the financial transaction scenario, it is necessary to quickly filter transactions within a certain range. The existing method is difficult to meet the real-time requirements.
[0006] 3. Defects in negative number sorting logic. Existing encoding schemes do not align the lexicographical order with the numerical order of negative numbers. For example, the string encoding of "-100" is greater than "-2" in lexicographical order, while the actual numerical value is "-100<-2", resulting in incorrect sorting results. Such problems may cause serious errors in social network relationship weight analysis or financial risk assessment.
[0007] 4. Incomplete handling of special values. Existing technologies lack unified standards for supporting special values (such as NaN, positive infinity, and negative infinity). For example, in a knowledge graph, the handling of missing values or abnormal values (such as NaN) may lead to inconsistent query results due to different encoding methods, or even cause system abnormalities. In addition, the sorting logic of special values is not clearly defined, further increasing the complexity of data management.
[0008] In summary, the existing graph database system has significant defects in the storage and query of high-precision decimal data, and an index scheme that can balance storage efficiency, comparison performance, negative sorting logic and special value processing is urgently needed to meet the needs of complex analysis tasks. SUMMARY
[0009] The purpose of the present application is to provide a key-value storage-based graph database decimal index encoding and decoding method to solve the problems of low storage efficiency, insufficient comparison performance, negative sorting logic defects and imperfect special value processing when existing graph database systems handle high-precision decimal data.
[0010] To achieve the above purpose, the following technical solutions are adopted in the present application:
[0011] A key-value storage-based graph database decimal index encoding method of the present application comprises the following steps:
[0012] Receiving the to-be-encoded numerical value and its sorting direction parameter;
[0013] Normalizing the to-be-encoded numerical value and extracting the sign, weight and digit array of the normalized to-be-encoded numerical value, wherein the elements in the digit array represent multi-digit numbers in a predetermined base;
[0014] Based on the sorting direction parameter and the sign, dynamically generating a set of encoding auxiliary parameters, which includes a sorting mask, an escape character and a padding character;
[0015] According to the set of encoding auxiliary parameters, converting the sign, weight and digit array into a binary encoding stream;
[0016] Adding a data termination marker composed of the escape character at the end of the binary encoding stream;
[0017] Outputting the binary encoding stream for ordered storage and retrieval of data.
[0018] As a preferred embodiment, the normalization of the to-be-encoded numerical value comprises:
[0019] Calling a trimming method to remove the leading zeros or trailing zeros of the to-be-encoded numerical value.
[0020] As a preferred embodiment, the method further comprises extracting the number of elements of the normalized to-be-encoded numerical value;
[0021] Before dynamically generating a set of encoding auxiliary parameters based on the sorting direction parameter and the sign, it further comprises:
[0022] In response to the number of elements being zero, adjusting its weight to a preset minimum weight;
[0023] performing a sign inversion operation on the weight in response to the sign representing a negative value.
[0024] As a preference, the dynamically generating an encoding auxiliary parameter set based on the sorting direction parameter and the sign comprises:
[0025] If the sorting direction is ascending, the initial sorting mask, escape character and padding character are a first preset sorting mask, a first preset escape character and a first preset padding character respectively.
[0026] If the sorting direction is descending, the initial sorting mask, escape character and padding character are a second preset mask, a second preset escape character and a second preset padding character respectively.
[0027] If the sign represents a negative value, the determined sorting mask, escape character and padding character are bitwise inverted respectively.
[0028] As a preference, the converting the sign, weight and number array into a binary encoding stream comprises:
[0029] encoding the sign and weight into binary data respectively and performing a high bit inversion to obtain a first byte sequence and a second byte sequence;
[0030] traversing the number array, for each number element, converting it into a big-endian byte sequence with high byte first and low byte last;
[0031] If the number to be encoded is negative, performing an XOR operation between the converted number element and the sorting mask, and splitting the XOR operation result into a high byte and a low byte;
[0032] When the generated high byte or low byte is the same as the escape character, adding the padding character after it.
[0033] As a preference, the data termination marker is a sequence composed of two consecutive escape characters.
[0034] A key-value storage-based graph database decimal index decoding method, comprising:
[0035] receiving an encoded data stream and a sorting direction parameter thereof;
[0036] parsing a sign and a weight from the encoded data stream;
[0037] dynamically generating a decoding auxiliary parameter set based on the sorting direction parameter and the sign, the decoding auxiliary parameter set comprising a sorting mask, an escape character and a padding character;
[0038] locating a data termination marker in the encoded data stream according to the escape character;
[0039] traversing a data segment in the encoded data stream before the data termination marker according to the decoding auxiliary parameter set to restore a digital array, wherein elements in the digital array represent multi-digit numbers in a predetermined base;
[0040] reconstructing the original numerical values based on the symbols, the weights and the restored digital array.
[0041] As a preferred embodiment, the decoding auxiliary parameter set is dynamically generated based on the sorting direction parameter and the symbols, comprising:
[0042] if the sorting direction is ascending, the initial sorting mask, the escape character and the padding character are a first preset mask, a first preset escape character and a first preset padding character respectively;
[0043] if the sorting direction is descending, the initial sorting mask, the escape character and the padding character are a second preset mask, a second preset escape character and a second preset padding character respectively;
[0044] if the symbols represent negative values, the determined sorting mask, the escape character and the padding character are bitwise inverted respectively.
[0045] As a preferred embodiment, the data termination marker in the encoded data stream is located according to the escape character, comprising:
[0046] two consecutive escape characters in the encoded data stream are searched as the data termination marker.
[0047] As a preferred embodiment, the digital array is restored, comprising:
[0048] two bytes are read as a group of processing units in sequence;
[0049] when the read byte is equal to the escape character, the next byte is skipped;
[0050] the two bytes are combined as a big-endian byte sequence;
[0051] if the symbols represent positive values, the big-endian byte sequence is directly converted into original digital elements;
[0052] if the symbols represent negative values, the big-endian byte sequence is XORed with the sorting mask, and the operation result is converted into original digital elements.
[0053] As a preferred embodiment, the method further comprises:
[0054] when the restored digital array is empty, the weight is adjusted to a preset minimum weight value.
[0055] As preferred, after the symbol and weight are parsed, the method further comprises:
[0056] The symbol and weight are respectively subjected to high bit inversion, and the weight subjected to high bit inversion is converted into an original digital element;
[0057] If the symbol subjected to high bit inversion represents a negative value, the converted weight is subjected to a taking inverse operation.
[0058] The present application has the following beneficial effects:
[0059] The present application realizes efficient storage and query by decomposing a decimal value into a symbol, a weight and a digital array, and adopting a compact binary coding mode. Specifically, the negative weight is subjected to inversion processing, which can ensure the mathematical correctness of sorting; the specific symbol mark can support the processing of NaN, positive infinity and negative infinity; the escape character and the null character are used to avoid conflicts, and the storage space is optimized through pruning operation. The index efficiency and query performance of high-precision decimal data in the graph database are significantly improved, and the present application is suitable for large-scale network data analysis scenarios, such as amount sorting in financial transaction graphs or weight filtering in social network graphs. BRIEF DESCRIPTION OF DRAWINGS
[0060] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the drawings needed to be used in the embodiments or prior art description will be briefly introduced. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without creative labor.
[0061] Figure 1 is a flowchart of a decimal index coding method based on a key-value storage graph database provided by an embodiment of the present application;
[0062] Figure 2 is a coding flowchart of a symbol, a weight and a digital array provided by an embodiment of the present application;
[0063] Figure 3 is a flowchart of a decimal index decoding method based on a key-value storage graph database provided by an embodiment of the present application;
[0064] Figure 4 is a flowchart of a restored digital array provided by an embodiment of the present application;
[0065] Figure 5 is a schematic diagram of an electronic device for implementing a decimal index coding and decoding method based on a key-value storage graph database provided by an embodiment of the present application. DETAILED DESCRIPTION
[0066] For the technical solutions of the present application to be more clear, the present application is further described in detail below in combination with the drawings and specific embodiments. The terms "first", "second", etc. in the claims and the description of the present application are used to distinguish similar objects, and do not have to be used to describe a specific order or sequence. It should be understood that the terms used in this way can be interchanged under appropriate circumstances, and this is merely a way of distinguishing the objects with the same attributes in the description of the embodiments of the present application. In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusion, so that a process, method, system, product or apparatus including a series of units does not have to be limited to those units, but can include other units not clearly listed or inherent to the process, method, product or apparatus.
[0067] Noun explanation:
[0068] Decimal data type: A high-precision numerical data type that can represent fractions and decimals with arbitrary precision, used to store decimal values in databases.
[0069] Key-value storage: A data storage paradigm that stores data in the form of key-value pairs, commonly used in NoSQL databases (such as graph databases).
[0070] NumericDigit: A 16-bit unsigned integer that stores a numeric unit (0-9999) in base 10000, corresponding to up to 4 decimal digits.
[0071] Weight: Represents the position of the first non-zero digit relative to the decimal point in a decimal value, used to determine the size and precision of the value. In decimal representation, the weight is defined as the position index of the digit group. Specifically, taking the decimal point as the reference, the integer part (left of the decimal point) is divided into groups of four digits from right to left, and the weight value of the rightmost group (containing the units place) is set to 0, and the weight value of each group to the left increases by 1. The decimal part (right of the decimal point) is divided into groups of four digits from left to right, and the weight value of the leftmost group (containing the tenths place) is set to -1, and the weight value of each group to the right decreases by 1. In this embodiment, the position of the first non-zero digit relative to the decimal point is taken as the weight of the entire value, for example, the first non-zero digit in the value 12345678.9012 is 1 in the ten million place, so the weight of 12345678.9012 is 1.
[0072] Sign: Represents the positivity of a decimal value (such as non-negative number POSITIVE, negative number NEGATIVE) or special state (such as non-number NaN, positive infinity POS_INF, negative infinity NEG_INF).
[0073] Digits: A list of NumericDigit in sequence, which together form a complete high-precision number.
[0074] Ndigits: The number of NumericDigit actually used in digits array, which determines the precision and size of the number.
[0075] Precision: The total number of significant digits in a decimal value, ignoring leading zeros. For example, 0.00456 has 3 significant digits.
[0076] Scale: The number of digits after the decimal point in a decimal value.
[0077] OrderMask: Specifies the sorting field and its direction through the binary pattern of a bitmask.
[0078] Escape Character: A special character used in the encoding process to avoid conflicts.
[0079] Null Character: Represents a specific value or delimiter in the encoding.
[0080] In this application, the high-precision decimal data type is implemented based on the Decimal class to support arbitrary precision numerical operations. To ensure data processing consistency and precision lossless, all external input data, regardless of its source format, whether it is a string, integer or floating point number, must be standardized to a Decimal object before encoding. A Decimal object is a complete data structure, containing properties such as sign, digits, weight, ndigits, etc. For example:
[0081] For the value 123.45, sign=POSITIVE, ndigits=2, weight=0, scale=2, digits=[0123,4500];
[0082] For the value -0.0006789, sign=NEGATIVE, ndigits=2, weight=-1, scale=7, digits=[0006,7890];
[0083] For the value 123456789, sign=POSITIVE, ndigits=3, weight=2, scale=0, digits=[0001,2345,6789];
[0084] For the special value NaN, sign = NaN, ndigits = 0, weight = 0, scale = 0, digits = [] (empty array);
[0085] For the special value positive infinity, sign = POS INF, ndigits = 0, weight = 0, scale = 0, digits = [] (empty array);
[0086] For the special value negative infinity, sign = NEG INF, ndigits = 0, weight = 0, scale = 0, digits = [] (empty array).
[0087] Embodiment 1
[0088] As shown in the following table, the present disclosure provides a method for encoding a decimal index of a graph database based on a key-value storage, which includes steps S110-S160. Figure 1
[0089] S110, receiving a to-be-encoded numerical value and a sorting direction parameter thereof.
[0090] In the encoding method proposed in the present disclosure, the initial step includes that the encoder receives two core parameters input by the calling end: the to-be-encoded numerical value object and the corresponding sorting direction parameter. Among them, the to-be-encoded numerical value is a pre-constructed high-precision decimal object, which contains structured attribute information such as sign, digit array, weight and element quantity; the sorting direction parameter is a Boolean variable isDesc, whose value clearly specifies the sorting rule that should be followed in the encoding process, that is, when the value is false, the ascending (Ascending) rule is adopted, and when the value is true, the descending (Descending) rule is adopted. By receiving and analyzing the above input parameters, the foundation for subsequent numerical conversion and byte sequence processing is laid, so as to ensure that the encoding operation is executed under the constraint of clear logical target and rule.
[0091] S120, normalizing the to-be-encoded numerical value, and extracting the sign, weight and digit array of the normalized to-be-encoded numerical value, wherein the elements in the digit array represent multi-digit numbers in a predetermined base.
[0092] Further, the normalization of the to-be-encoded numerical value includes:
[0093] calling the trimming method to remove the leading zeros or trailing zeros of the to-be-encoded numerical value.
[0094] After obtaining the Decimal value object to be encoded and the sorting direction parameter isDesc, if there is redundant 0 in the Decimal value object, a trim method is called to normalize the to-be-encoded value. The purpose of this step is to eliminate redundant information in the value representation, remove meaningless leading zeros or trailing zeros, ensure the uniformity and simplicity of the data format, and effectively reduce the data volume in the subsequent storage and transmission process.
[0095] After the normalization is completed, the structured attribute information of the to-be-encoded value such as Sign, Digits, Weight and Ndigits is extracted. In the embodiment of the present disclosure, the elements in Digits are decomposed using the base 10000 (myriad system) to the value, that is, each array element corresponds to four decimal digits, so as to realize efficient compression representation of large numbers.
[0096] It should be noted that in the embodiment of the present disclosure, Sign determines the sorting priority, NEG_INF < NEGATIVE < POSITIVE < POS_INF < NAN, which ensures the correct order of special values and positive and negative numbers.
[0097] S130, based on the sorting direction parameter and the symbol, dynamically generating an encoding auxiliary parameter set, the encoding auxiliary parameter set includes a sorting mask, an escape character and a padding character.
[0098] Further, it also includes extracting the element quantity of the normalized to-be-encoded value;
[0099] Before dynamically generating the encoding auxiliary parameter set based on the sorting direction parameter and the symbol, it further includes:
[0100] In response to the element quantity being zero, the weight is adjusted to a preset minimum weight;
[0101] In response to the to-be-encoded value being a negative number, the weight is inverted.
[0102] After extracting the attribute information of the to-be-encoded value, it is first judged whether the element quantity Ndigits is zero. If the value is zero, that is, Ndigits = 0, the weight Weight is adjusted to a preset minimum weight value MIN_WEIGHT. In the embodiment of the present disclosure, the minimum weight value is set to -32768. This operation can ensure that the zero value can be assigned a uniform and identifiable extreme weight in the sorting process, so as to be positioned at the correct position in the sorting sequence.
[0103] Then, it is further determined whether the sign represents a negative value. If the value is negative, the opposite operation is performed on the current weight, i.e., the weight value is multiplied by -1. This step makes the larger negative number (i.e., the smaller absolute value) obtain a smaller weight, and the smaller negative number (i.e., the larger absolute value) obtain a larger weight, so that when the values are sorted by weight, the values in the negative number interval can be correctly arranged from small to large, and the overall order of all values (negative numbers, zeros, and positive numbers) is finally achieved.
[0104] Further, based on the sorting direction parameter and the sign, a set of encoding auxiliary parameters is dynamically generated, including:
[0105] If the sorting direction is ascending, the initial sorting mask, escape character, and padding character are the first preset sorting mask, the first preset escape character, and the first preset padding character, respectively.
[0106] If the sorting direction is descending, the initial sorting mask, escape character, and padding character are the second preset mask, the second preset escape character, and the second preset padding character, respectively.
[0107] If the sign represents a negative value, the determined sorting mask, escape character, and padding character are inverted bit by bit.
[0108] After the normalization processing and weight adjustment of the to-be-encoded values are completed, the corresponding encoding auxiliary parameters are configured according to the values of the sorting direction parameter isDesc and the sign Sign. Specifically, first, the Boolean value of the sorting direction parameter isDesc is determined. If isDesc takes the value false, it indicates that an ascending arrangement is requested, and then the first preset sorting mask ASC_MASK (0x0000) is selected as the initial sorting mask, the first preset escape character ASC_ESCAPE (0x00) is selected as the initial escape character, and the first preset padding character ASC_NULL (0x01) is selected as the initial padding character. It should be noted that in the embodiments of the present disclosure, the null character (i.e., the NULL character) is selected as the padding character, which is used to fill a specific field during encoding to ensure format alignment and correct parsing. If isDesc takes the value true, it indicates that a descending arrangement is requested, and then the second preset sorting mask DESC_MASK (0xFFFF) is selected as the initial sorting mask, the second preset escape character DESC_ESCAPE (0xFF) is selected as the initial escape character, and the second preset padding character DESC_NULL (0xFE) is selected as the initial padding character. Similarly, the padding character here is also a null character. The selection of different sorting masks is the basis for all subsequent encoding operations, which determines the keynote of the byte sequence to ensure that the final lexicographic order meets the expected sorting direction.
[0109] After obtaining the initial set of encoding auxiliary parameters, the sign of the value to be encoded is further checked. If the sign indicates that the value is negative, a bitwise NOT operation is performed on the currently selected sorting mask, escape character and padding character (i.e. all binary bits 0 become 1, and 1 become 0). This step is a key mechanism to ensure that negative numbers obtain a consistent representation opposite to positive numbers in the sorting encoding. By taking the complement, a complementary encoding space is constructed for negative numbers, so that regardless of the sorting direction, the negative numbers can maintain their mathematical size relationship in the final encoding sequence.
[0110] S140, converting the sign, weight and number array into a binary encoding stream according to the set of encoding auxiliary parameters.
[0111] Further, as shown in Figure 2 converting the sign, weight and number array into a binary encoding stream includes:
[0112] S210, encoding the sign and weight into binary data respectively, and performing high-bit NOT to obtain a first byte sequence and a second byte sequence;
[0113] S220, traversing the number array, for each number element, converting it into a big-endian byte sequence with high-bit byte in front and low-bit byte behind;
[0114] S230, if the value to be encoded is negative, performing XOR operation between the converted number element and the sorting mask, and splitting the XOR operation result into high-bit byte and low-bit byte;
[0115] S240, when the generated high-bit byte or low-bit byte is the same as the escape character, adding the padding character after it.
[0116] After determining the set of encoding auxiliary parameters, the encoding is formally performed. First, the sign Sign and the weight Weight are encoded into 16-bit unsigned integers respectively, thereby obtaining a first byte sequence and a second byte sequence, and then the encoding results are sequentially added (first the first byte sequence and then the second byte sequence) to the starting position of the encoding stream, to ensure that the sign byte is compared first in sorting, i.e. the entry with positive sign must be placed after the entry with negative sign, thereby ensuring the requirement that the negative number must appear before the positive number in sorting.
[0117] Then traverse each element in the digital array, and perform the following operations: convert the current digital element into a 16-bit unsigned integer representation in big-endian byte order, where big-endian byte order refers to arranging the bytes of a numerical value in the order of high byte first and low byte last, for example, the hexadecimal representation of the decimal 2 is 0x0002, occupying 2 bytes, according to the rule of big-endian byte order, the high byte 0x00 is first, and the low byte 0x02 is last, resulting in [0x00, 0x02], since 2 is a positive number, it is not necessary to perform a bitwise XOR operation on the big-endian byte order and the determined sorting mask (typical value is 0x0000), but if it is a negative number, the big-endian byte order and the sorting mask are also subjected to a bitwise XOR operation to generate an intermediate value, and then the XOR result, i.e. the intermediate value, is split into high 8-bit byte byte1 and low 8-bit byte byte2. For positive numbers, after converting to big-endian byte order, there are already high 8-bit bytes and low 8-bit bytes, so no splitting is needed. byte1 is sequentially appended to the encoding stream, and if byte1 is equal to the escape character (such as 0x00), a padding character (such as 0xFF, represented as cnull) is added after byte1 to avoid parsing conflicts; byte2 is sequentially appended to the encoding stream, and if byte2 is equal to the escape character, cnull is added after byte2.
[0118] This step can ensure that the encoding byte sequence of each number does not conflict with the escape character in transmission, and the order of appending byte1 and byte2 (low byte first, high byte last) is consistent with the original representation of big-endian byte order, providing a reliable data structure for subsequent sorting.
[0119] S150, a data termination marker composed of escape characters is added at the end of the binary encoding stream.
[0120] After the encoding of the digital array is completed, two consecutive escape characters (such as non-negative number termination marker [0x00, 0x00] or negative number termination marker [0xFF, 0xFF]) are added at the end of the binary encoding stream to form a clear separator sequence. In the embodiments of the present disclosure, the two consecutive escape characters are used as a fixed separator to accurately mark the end position of the digital array encoding part. When parsing, this sequence can be scanned to distinguish between the digital array and subsequent data, avoiding parsing errors caused by data content and boundary confusion.
[0121] S160, output the binary encoding stream for ordered storage and retrieval of data.
[0122] The output binary encoding stream is carefully designed and constructed to support efficient and ordered storage and accurate retrieval. The structured encoding mechanism ensures that the data can be sorted directly by byte sequence after storage, without the need for additional parsing or conversion, and provides clear boundary markers and logical layering for retrieval operations.
[0123] In one embodiment, the encoding process is described with an example of arranging the positive values 12.34 and 123.45 in ascending order.
[0124] Converting 12.34 to a Decimal object, we get: sign = POSITIVE, weight = 0, digits = [12, 3400], ndigits = 2, scale = 2.
[0125] Converting 123.45 to a Decimal object, we get: sign = POSITIVE, weight = 0, digits = [123, 4500], ndigits = 2, scale = 2.
[0126] In this embodiment, isDesc takes the value false, and both 12.34 and 123.45 are positive numbers, so the encoding auxiliary parameter set has orderMask = 0x0000, escape = 0x00, cnull = 0x01.
[0127] For 12.34, the sign POSITIVE = 0x0062 high bit inversion -> [0x80, 0x62], where 0x0062 is the hexadecimal encoding of POSITIVE itself, converting it to binary encoding gets 0000 0000 0110 0010, and through the highest bit inversion, the highest bit 0 is converted to 1 to get 1000 0000 0110 0010, which corresponds to hexadecimal 0x8062 = [0x80, 0x62]; the weight weight = 0 high bit inversion gets [0x80, 0x00]; the digit array digits = [12, 3400] -> [0x00, 0x01, 0x0c, 0x0D, 0x48], here, first generate the big-endian byte sequence of 12 [0x00, 0x0C], then escape 0x00 to get [0x00, 0x01, 0x0c], then encode 3400 in big-endian to get [0x0D, 0x48], and finally combine to get [12, 3400] -> [0x00, 0x01, 0x0c, 0x0D, 0x48]; the separator takes [0x00, 0x00]; combine all the above contents in a fixed order to get the complete binary encoding result [0x80, 0x62, 0x80, 0x00, 0x00, 0x01, 0x0C, 0x0D, 0x48, 0x00, 0x00].
[0128] For 123.45, the sign POSITIVE = 0x0062 high bit complement → [0x80, 0x62]; the weight weight = 0 high bit complement → [0x80, 0x00]; the digit array digits = [123, 4500] → [0x00, 0x01, 0x7B, 0x11, 0x94]; the separator is [0x00, 0x00]; the complete binary encoding result is [0x80, 0x62, 0x80, 0x00, 0x00, 0x01, 0x7B, 0x11, 0x94, 0x00, 0x00]. The calculation logic is the same as that of 12.34, which is not described here again.
[0129] Since the first 4 bytes (sign and weight) are the same, the number part is compared, and 0x0C < 0x7B, so 12.34 < 123.45, which is in ascending order.
[0130] In another embodiment, taking the descending order of negative numbers -12.34 and -123.45 as an example, the above encoding process is described.
[0131] -12.34 is converted into a Decimal object, and the result is: sign = NEGATIVE, weight = 0, digits = [12, 3400], ndigits = 2, scale = 2.
[0132] -123.45 is converted into a Decimal object, and the result is: sign = NEGATIVE, weight = 0, digits = [123, 4500], ndigits = 2, scale = 2.
[0133] For -12.34, its sign NEGATIVE = 0x0020 high bit complement → [0x80, 0x20], where 0x0020 is the encoding of NEGATIVE itself; weight = 0 high bit complement → [0x80, 0x00]; digit array digits = [12, 3400] → [0xFF, 0xFE, 0xF3, 0xF2, 0xB7], first generate 12 in big-endian byte order [0x00, 0x0C], then perform bitwise XOR operation with the ordering mask 0xFFFF to obtain [0xFF, 0xFD], escape 0xFF to obtain [0xFF, 0xFE, 0xFD], then encode 3400 in big-endian to obtain [0x0D, 0x48], then perform bitwise XOR operation with the ordering mask 0xFFFF to obtain [0xF2, 0xB7], and finally combine to obtain [12, 3400] → [0xFF, 0xFE, 0xFD, 0xF2, 0xB7]; the separator is [0xFF, 0xFF]; the complete binary encoding result is [0x80, 0x20, 0x80, 0x00, 0xFF, 0xFE, 0xFD, 0xF2, 0xB7, 0xFF, 0xFF].
[0134] For -123.45, its sign NEGATIVE = 0x0020 high bit complement → [0x80, 0x20]; weight = 0 high bit complement → [0x80, 0x00]; digit array digits = [123, 4500] → [0xFF, 0xFE, 0x84, 0xEE, 0x6B]; the separator is [0xFF, 0xFF]; the complete binary encoding result is [0x80, 0x20, 0x80, 0x00, 0xFF, 0xFE, 0x84, 0xEE, 0x6B, 0xFF, 0xFF]. The calculation logic is the same as that of -12.34, and will not be described here.
[0135] Since the first 4 bytes (sign and weight) are the same, compare the digit part, and 0x84 < 0xF2, so -123.45 < -12.34, which is in ascending order.
[0136] In yet another embodiment, the above encoding process is described by taking the ascending order arrangement of special values NEG_INF, 0 and POS_INF as an example.
[0137] Convert NEG_INF to a Decimal object to obtain: sign = NEG_INF (0x01), digits = [], ndigits = 0, weight = 0.
[0138] Convert 0 to a Decimal object, getting: sign = POSITIVE, digits = [], ndigits = 0, weight = MIN_WEIGHT.
[0139] Convert POS_INF to a Decimal object, getting: sign = POS_INF(0xA3), digits = [], ndigits = 0, weight = 0.
[0140] The complete binary encoding result of NEG_INF is [0x80, 0x01, 0x7F, 0x00, 0x00, 0x00].
[0141] The complete binary encoding result of 0 is [0x80, 0x62, 0x7F, 0x00, 0x00, 0x00].
[0142] The complete binary encoding result of POS_INF is [0x80, 0xA3, 0x7F, 0x00, 0x00, 0x00].
[0143] Compare the signs. Since 0x01 < 0x62 < 0xA3, that is, NEG_INF < 0 < POS_INF, which conforms to ascending order.
[0144] The encoding method provided by the embodiments of the present disclosure decomposes the decimal value into a sign, a weight, and an array of digits, and adopts a compact binary encoding method to achieve efficient storage and query. Specifically, taking the inverse of the negative weight can ensure the mathematical correctness of sorting; the processing of NaN, positive infinity, and negative infinity can be supported through specific sign flags; escape characters and null characters are also used to avoid conflicts, and the storage space is optimized through pruning operations. It significantly improves the indexing efficiency and query performance of high-precision decimal data in the graph database, and is applicable to the analysis scenarios of large-scale network data, such as the amount sorting in the financial transaction graph or the weight filtering in the social network graph.
[0145] Embodiment 2
[0146] As Figure 3 shown, the embodiments of the present disclosure provide a decimal index decoding method for a graph database based on key-value storage, which is used to decode the data generated according to the encoding method described in Embodiment 1, including steps S210 - S260.
[0147] S310. Receive the encoded data stream and its sorting direction parameter.
[0148] In the decoding method proposed in the embodiments of the present disclosure, the initial step includes receiving two core parameters passed in by the calling end: an encoded data stream and its corresponding ordering direction parameter. The encoded data stream is a standardized binary sequence generated after encoding the Decimal object according to the encoding method provided in Embodiment 1, and its structure strictly follows the predefined format, including a symbol sequence, weight information, a data termination marker, and necessary auxiliary parameters. The ordering direction parameter is completely consistent with the rule used in the encoding stage (for example, both specify that the symbols are arranged in ascending order or descending order), which ensures that the decoder can directly reuse the ordering logic in the encoding. Through this parameter synchronization mechanism, the decoder can efficiently restore the original Decimal object without additional calculation or negotiation. This design not only eliminates the risk of parameter inconsistency between the encoding and decoding ends, but also significantly reduces the system implementation complexity and ensures the high reliability of the decoding process.
[0149] S320, parsing the symbols and weights from the encoded data stream.
[0150] In Embodiment 1, the first byte sequence and the second byte sequence generated by Sign and Weight are added to the starting position of the encoded stream in a fixed order (i.e., first the first byte sequence and then the second byte sequence). This design ensures that the decoder can directly obtain the key information through a simple sequential reading mechanism. When the decoder starts parsing from the starting position of the encoded stream, it first reads and parses the pre-sequence byte sequence (i.e., the first byte sequence) to recover the symbol information, and then parses the subsequent byte sequence (i.e., the second byte sequence) to recover the weight information, so that the complete extraction of symbols and weights can be completed without scanning or processing the remaining part of the encoded stream. The parsing process includes inverting the most significant bit of the read symbol and weight, converting the inverted weight to the original digital element, and if the inverted symbol represents a negative value, performing the opposite operation on the converted weight to restore the original weight. This structured sequence arrangement not only simplifies the encoding / decoding logic, but also significantly improves the efficiency and reliability of information acquisition.
[0151] S330, dynamically generating a decoding auxiliary parameter set based on the ordering direction parameter and the symbols, the decoding auxiliary parameter set including an ordering mask, an escape character, and a padding character.
[0152] In the encoding phase described in Embodiment 1, the encoder generates a unique encoding auxiliary parameter set based on the ordering direction and the symbol. In the decoding phase, the determination method of the decoding auxiliary parameter set is completely consistent with the generation logic of the encoding auxiliary parameter set in Embodiment 1. Specifically, the decoder directly calls the same generation logic using the received ordering direction parameter and the parsed symbol, without additional input or parameter negotiation. This deterministic mechanism based on ordering and symbol makes the parameter set generation logic at the encoding and decoding ends completely consistent, without the need for additional synchronization steps, thereby achieving efficient and unambiguous parameter reuse.
[0153] S340, locating a data termination marker in the encoded data stream according to the escape character.
[0154] The data termination marker is composed of two consecutive escape characters, and its core advantage lies in avoiding conflicts with single escape characters in the original data. In the encoded data stream, the decoder starts from the third byte sequence at the starting position and compares byte by byte through a sequential scanning mechanism: when two consecutive escape characters are detected (for example, “0x00, 0x00” appears in the byte sequence), it is determined as a data termination marker. At this time, all bytes before the data termination marker (excluding the data termination marker itself, the first byte sequence, and the second byte sequence) are the complete encoded stream of the original numerical array. This mechanism can efficiently locate the data boundary through simple linear scanning, without additional parsing or predefined length, significantly simplifying the boundary processing logic of the data stream.
[0155] S350, traversing the data segment before the data termination marker in the encoded data stream to restore the numerical array according to the decoding auxiliary parameter set, wherein the elements in the numerical array represent multi-digit numbers in a predetermined base.
[0156] Further, as shown in Figure 4 restoring the numerical array includes:
[0157] S410, reading two bytes as a group of processing units in sequence;
[0158] S420, when the read byte is equal to the escape character, skipping the next byte;
[0159] S430, combining the two bytes into a big-endian byte sequence;
[0160] S440, if the symbol represents a positive value, directly converting the big-endian byte sequence into an original numerical element;
[0161] S450, if the symbol represents a negative value, performing an exclusive OR operation on the big-endian byte sequence and the ordering mask, and converting the operation result into an original numerical element.
[0162] The original digital array can be obtained by restoring the positioned digital array encoding stream. In the embodiments of the present disclosure, the original digital array is restored by gradually performing according to a specific byte sequence parsing rule. First, two bytes are read in sequence as a basic processing unit, and if the current read byte is equal to the preset escape character, the byte immediately following it is automatically ignored to ensure that the escape sequence does not affect the actual data parsing. Then, the two valid bytes are combined in order to form a complete big-endian byte sequence, where the big-endian byte sequence is based on hexadecimal representation. Then, it is judged whether the symbol parsed is a negative value, if not, the big-endian byte sequence is directly converted into the original digital element, if yes, the big-endian byte sequence is subjected to a bitwise XOR operation with the order mask, and then the operation result is converted into the original digital element.
[0163] S360, based on the symbol, the weight and the restored digital array, the original numerical value is reconstructed.
[0164] After the digital array is restored, the original numerical value is gradually reconstructed by integrating the symbol, the weight and the restored digital array. This process needs to strictly ensure that the order and mapping relationship of each data component are correct and accurate, and finally the numerical value result conforming to the original semantics is generated through arithmetic synthesis.
[0165] The embodiments of the present disclosure can realize efficient decimal data indexing in the key-value storage of the graph database, while maintaining the correctness of mathematical ordering and supporting special values.
[0166] Embodiment 3
[0167] As shown in Figure 5 The embodiments of the present disclosure provide an electronic device, including a memory 501 and a processor 502, the memory 501 is used to store one or more computer instructions, wherein the one or more computer instructions are executed by the processor 502 to implement the above-mentioned one kind of key-value storage-based graph database decimal index encoding and decoding method.
[0168] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the electronic device described above can refer to the corresponding process in the foregoing method embodiments, which will not be repeated here.
[0169] A computer readable storage medium storing a computer program, the computer program causes a computer to execute when realizing the above-mentioned one kind of key-value storage-based graph database decimal index encoding and decoding method.
[0170] For example, the computer program can be divided into one or more modules / units, one or more modules / units are stored in the memory 501 and executed by the processor 502, and the I / O interface transmission of data is completed by the input interface 505 and the output interface 506, so as to complete the present application. One or more modules / units can be a series of computer program instruction segments capable of completing a specific function, which are used to describe the execution process of the computer program in the computer device.
[0171] The computer device can be a desktop computer, a notebook computer, a palm computer, a cloud server and the like. The computer device can include, but is not limited to, the memory 501 and the processor 502. Those skilled in the art can understand that the embodiment is only an example of the computer device, and does not constitute a limitation on the computer device, and can include more or fewer components, or combine certain components, or different components. For example, the computer device can also include an inputter 507, a network access device, a bus and the like.
[0172] The processor 502 can be a central processing unit (CPU), and can also be other general-purpose processors 502, digital signal processors 502 (DSP), application specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor 502 can be a microprocessor or can also be any conventional processor 502.
[0173] The memory 501 can be an internal storage unit of the computer device, such as a hard disk or a memory of the computer device. The memory 501 can also be an external storage device of the computer device, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card and the like. Further, the memory 501 can include both the internal storage unit and the external storage device of the computer device. The memory 501 is used to store computer programs and other programs and data required by the computer device. The memory 501 can also be used to temporarily store the outputter 508, and the aforementioned storage medium includes a U disk, a mobile hard disk, a read-only memory (ROM) 503, a random access memory (RAM) 504, a disk or an optical disk and the like. Various storage medium capable of storing program codes.
[0174] The above embodiments only express several implementation manners of the present application, which are described in a more specific and detailed manner, but cannot be understood as a limitation on the patent scope of the present application. It should be noted that, for ordinary skilled persons in the art, several modifications and improvements can be made without departing from the concept of the present application, which all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application should be subject to the appended claims.
Claims
1. A decimal index encoding method for graph databases based on key-value storage, characterized in that, The method comprises the following steps: receiving a to-be-encoded numerical value and a sorting direction parameter thereof; normalizing the to-be-encoded numerical value, and extracting a sign, a weight, and a numerical array of the normalized to-be-encoded numerical value, wherein an element in the numerical array represents a multi-digit number in a predetermined base; dynamically generating an encoding auxiliary parameter set based on the sorting direction parameter and the sign, the encoding auxiliary parameter set comprising a sorting mask, an escape character, and a padding character; converting the sign, the weight, and the numerical array into a binary encoding stream according to the encoding auxiliary parameter set; adding a data termination marker composed of the escape character to the end of the binary encoding stream; and outputting the binary encoding stream for ordered storage and retrieval of data.
2. The method of claim 1, wherein, The normalization of the to-be-encoded numerical value comprises: calling a trimming method to remove leading zeros or trailing zeros of the to-be-encoded numerical value.
3. The method of claim 1, wherein, The method further comprises extracting the number of elements of the normalized to-be-encoded numerical value. Before the dynamic generation of the encoding auxiliary parameter set based on the sorting direction parameter and the sign, the method further comprises: in response to the number of elements being zero, adjusting the weight thereof to a preset minimum weight; and in response to the sign representing a negative value, performing a negation operation on the weight thereof.
4. The method of claim 3, wherein, The dynamic generation of the encoding auxiliary parameter set based on the sorting direction parameter and the sign comprises: if the sorting direction is ascending, the initial sorting mask, the escape character, and the padding character are respectively a first preset sorting mask, a first preset escape character, and a first preset padding character; if the sorting direction is descending, the initial sorting mask, the escape character, and the padding character are respectively a second preset mask, a second preset escape character, and a second preset padding character; and if the sign represents a negative value, the determined sorting mask, the escape character, and the padding character are respectively bitwise inverted.
5. The method of claim 1, wherein, The conversion of the sign, the weight, and the numerical array into the binary encoding stream comprises: encoding the sign and the weight into binary data respectively, and performing a high-bit inversion to obtain a first byte sequence and a second byte sequence; traversing the numerical array, and for each numerical element, converting it into a big-endian byte sequence with a high-bit byte in front and a low-bit byte behind; if the to-be-encoded numerical value is negative, performing an exclusive OR operation on the converted numerical element and the sorting mask, and splitting the exclusive OR operation result into a high-bit byte and a low-bit byte; when the generated high-bit byte or low-bit byte is the same as the escape character, adding the padding character after the high-bit byte or low-bit byte.
6. The method of claim 1, wherein, The data termination marker is a sequence composed of two consecutive escape characters.
7. A method for decoding a decimal index of a graph database based on a key-value store, the method comprising: receiving a decimal index of a graph database; and decoding the decimal index into a binary index using a base-10 to base-2 conversion. The method comprises the following steps: receiving an encoded data stream and a sorting direction parameter thereof; parsing a sign and a weight from the encoded data stream; dynamically generating a decoding auxiliary parameter set based on the sorting direction parameter and the sign, the decoding auxiliary parameter set comprising a sorting mask, an escape character, and a padding character; locating a data termination marker in the encoded data stream according to the escape character; traversing a data segment in the encoded data stream before the data termination marker, and restoring a numerical array according to the decoding auxiliary parameter set, wherein an element in the numerical array represents a multi-digit number in a predetermined base; and outputting the numerical array for ordered storage and retrieval of data. Based on the symbol, the weight and the restored number array, the original numerical value is reconstructed.
8. The method of claim 7, wherein, The decoding auxiliary parameter set is dynamically generated based on the sorting direction parameter and the symbol, and includes: If the sorting direction is ascending, the initial sorting mask, escape character and padding character are respectively a first preset mask, a first preset escape character and a first preset padding character; If the sorting direction is descending, the initial sorting mask, escape character and padding character are respectively a second preset mask, a second preset escape character and a second preset padding character; If the symbol represents a negative value, the determined sorting mask, escape character and padding character are respectively bitwise inverted.
9. The method of claim 7, wherein, The data termination marker in the encoded data stream is located according to the escape character, and includes: Two continuous escape characters in the encoded data stream are searched as the data termination marker.
10. The method of claim 7, wherein, The number array is restored, and includes: Two bytes are read as a group of processing units in sequence; When the read byte is equal to the escape character, the next byte is skipped; The two bytes are combined as a big-endian byte sequence; If the symbol represents a positive value, the big-endian byte sequence is directly converted into an original numerical element; If the symbol represents a negative value, the big-endian byte sequence is XORed with the sorting mask, and the operation result is converted into an original numerical element.
11. The method of claim 7, wherein, The method further includes: When the restored number array is empty, the weight is adjusted to a preset minimum weight value.
12. The method of claim 7, wherein, After the symbol and the weight are parsed, the method further includes: The symbol and the weight are respectively high-bit inverted, and the high-bit inverted weight is converted into an original numerical element; If the high-bit inverted symbol represents a negative value, the converted weight is subjected to a taking opposite operation.
Citation Information
Patent Citations
Indexing method and device based on key value pair KV system, electronic equipment and medium
CN111241108A
Graph data processing method, apparatus and device, and computer readable storage medium
CN120277240A