Topic: Rule Engine Development Specification--Rule Configuration Class Specification <serial 4>

2.1. The index processing mechanism of the memory table The
memory table is the storage of multi-row data. Generally, when accessing the memory table, the memory table is accessed by traversing, and then the judgment and processing are performed row by row. This method generally returns and exits after traversing the entire memory table data.
However, in some cases, we may only need to find the value of a certain column and do the corresponding processing. In this case, the traversal consumes too much performance.
Therefore, we can specify a certain column as a key to build an index for this memory table in advance. The next time you visit, you can first find the corresponding row based on the keyword. Then process. The index is implemented with HashMap, which will greatly improve performance.
According to actual needs, we design the index into three types: unique index, linked list multi-index, and key-value multi-index. These three implementations can exist in a memory table at the same time, but only one of the same implementation can exist.
One is the unique index of a single key. The unique index means the primary key of a bit item, and the value of a certain column is unique. HashMap<Object,int> is used to store the index, and int is the row number. To use this type of index, there are mainly the following methods:
resetKeyMap Create a unique index, and the input parameter is the column number where the keyword is located
moveToKey Find and locate the row found according to the unique index: The input parameter is the value to be matched. According to this method, you can locate the row where the keyword is located, and then modify it. In
the following method, if the field is set with a unique keyword, it will be called:
findFirstByKey is equivalent to using a map, find the incoming value in the first column, and return the first The value of the second column. If there is no keyword initialization, traverse the
exitKey to see if there is a row equal to the incoming value in the specified column. If there is no keyword unique index initialization, traversal
getValueByKey is similar to findFirstByKey, except that the second parameter passes in the column where the specified keyword is located and the column where the return value is located.
updateFirstByKey changes the value of the specified column of the row where the specified keyword is the first incoming value to the second incoming value, and the third parameter is the column where the specified keyword is located and the column where the returned value is located. If there is a keyword unique index, take it from the index, otherwise traverse.
The second index implementation is a multi-index with a single key, which can concatenate multiple columns into a string as a single key. When creating an index for this kind of index, you need to use the initKeyListMap method to specify which columns are index items, and specify the splicing characters. The index will convert the values ​​of these columns into strings and then use splicing characters to get a string, which is put into HashMap<String,List> as the key value. The next time you want to use it, you must also use splicing characters to spliced ​​the value to be accessed, and then use the findKeyList method to call it after splicing it into a new string.
initKeyListMap(int): The incoming value is the column number where the index is located, and a multi-index is created based on this column
initKeyListMap(string, string): The incoming value is the column where the key is located, and the connector used for splicing. The column where the keyword is located can be multiple, separated by , you can make the column number or column name
findKeyList(string), according to the incoming value, find the keyword as the incoming value, multiple indexes
nextKeyList(): find according to the index After multiple rows, the next row can be obtained in turn. The
third type of index implements multi-keyword multi-index. The keywords in this way are stored in the form of HashMap<Object,HashMap>, that is, the index
multiKeyReset(int) is found below the index: According to the incoming column number, the index
multiKeyReset(int,int) of this number is established: According to the incoming column number, establish an index of two columns
multiKeyReset(int, int, int): According to the incoming column number, establish an index of three columns
dynamicKeyReset2(int[]): Create indexes of multiple columns according to the incoming column
multiKeyFind(Object): Find the satisfied row according to the set index of a column, corresponding to multiKeyReset(int)
multiKeyFind(Object, Object) : Find the satisfying row according to the set indexes of the two columns, corresponding to multiKeyReset(int, int)
multiKeyFind(Object, Object, Object): Find the satisfying row according to the set indexes of the three columns, and multiKeyReset( int,int,int) corresponds to
dynamicKeyFind2(Object[]): According to the set index of multiple columns, find the satisfied row, corresponding to dynamicKeyReset2(int[])
nextKeyList(): After finding multiple rows according to the index, The next row can be fetched in sequence.
Note that when a memory table is stored in shared memory, if child threads need to access the shared memory by copying or referencing. The memory tables of the child threads are all referenced or indexed in the original shared memory.
2.2. Sharing between the main thread and sub-threads of
the memory table After the main thread creates a memory table, it can be passed to the sub-thread for access. By default, for thread safety, a new memory table is copied and passed to the child thread. However, if you want to save memory costs and access by reference, you need to do some specific operations.
Sheet.setThreadType(int) can specify the sharing method when the current memory table is passed to the child thread.
Among them, when getThreadType() == 1, the reference is directly passed in. At this time, special attention should be paid to the thread sharing problem. Row-by-row read and write operations cannot be performed on the memory table. You can only do batch additions and other operations. Except for the following methods that are set as thread-safe for access, other methods will have thread-safety problems:
initFromSheet: import all the data in {arg1} into the table according to the set column correspondence
appendFromSheet: add the data in {arg1} The data is additionally imported into the table according to the set column correspondence.
loadFromSheet: import all the data in {arg1} into the table
loadFromList: re-import the data in the export list {arg1} into the table
loadFromExcelFile: import data from Excel to the memory table
appendFromResult: append the data in the query result ({arg1}) into the table according to the set column correspondence
loadFromResult: import all the data in the query result ({arg1}) into the table according to the set column correspondence
loadFromResult: import all the data in the query result ({arg1}) into the table according to the set column correspondence
loadFromView: import all the data in the query result ({arg1}) into the table
getThreadType() == 2: use sharing access to the data. At this point there will be a new memory table, but pointing to the same array. At this point when reading is made, it is thread-safe. But when data needs to be written, it is thread-unsafe.
Therefore, getThreadType() == 2 is used if you need to read one by one, read only and not write;
otherwise, use getThreadType() == 1, and you can only write data through batch import.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326627346&siteId=291194637