Formally recommend one of my open source projects 2-Custom compilers

Source: Internet
Author: User

Sometimes there are scenarios in the project, we need a permission table to control permissions, when the permission table condition is met, the user action is blocked and an error message is returned, the table is structured like this:

Then there is a traditional way, we use a tool such as MyBatis, write a piece of SQL, each time with this SQL check permissions:

SELECT * from Permission_test where ' condition1 ' = #{condition1} and ' condition2 ' = #{condition2}


Another way is that we compile the table into a piece of executable code, and then each time we execute the code, this code looks something like this:

Then the efficiency of these 2 ways is different, the landlord wrote a test code:

As a result, the execution time of the compiledtester is always 1ms, while the Sqltester execution time is around 800ms.

So, how did compiledtester build it? This article will briefly describe this generation mechanism.

This article uses the landlord's Open source project Autogrammer : Http://git.oschina.net/notebook

Its jar package can be downloaded below the published project.

The main code used in this article can be seen here http://www.oschina.net/code/snippet_573815_50916

First there is an interface class Dbdatatester This class has 2 implementations, one is the traditional database implementation Sqldatatester, and the other is compiled into Java Code Dbdatatesterimpl class implementation.

main compiled code :

Our idea is to read all the record from the database and compile the record with the functionality provided by Autogrammer , and eventually generate the target code.

Compiling Java code-related knowledge you can learn from this article:

http://www.oschina.net/code/snippet_573815_50891

The record we read from the database can be seen as the following statement:

Condition (Name: "Condition1", Data: "Condition1-1")

Condition (Name: "Condition2", Data: "Condition1-2")

End (data: "Error 1")

......

First step : Autogrammer to implement a custom compiler, you first need to define the syntax. The code is put in Yp/published/grammer/grammer.txt:

Among them, the third sentence indicates that a condition can be regulated as a conditionlist

The second sentence indicates that a conditionlist and condition can be regulated as a conditionlist

So our grammar supports condition's wireless list.

The first sentence indicates that when an end is encountered, it indicates the end of the current sentence (similar to the one in the programming statement;) The entire sentence is compiled, generating a piece of code

Similar:

if ("Condition1-1". Equals (Data.getcondition1 ()) && "condition2-1". Equals (Data.getcondition2 ())) {

Return "error data 1";

}

Step two : with grammar, we need a lexical parser that can read the data from the database and then translate it into the corresponding token stream in the grammar.

The lexical parser needs to inherit and implement the Tokenreader interface

Public class Dbtokenreader implements Tokenreader

Its key code is here:

In the third step , we need to register the handler function for our defined syntax

Public class Conditionhandler extends defaultexecutehandler

Registering the initialization function at the beginning of the compilation: primary initialization Executepram

Register compile-End Function: The final implementation class is compiled using Executeparam Dbdatatesterimpl

Register the handler function for each protocol statement

In the handler function, Tokenwordlist is a list<tokenword>, and the member inside is the value (condition) to the left of

The value (conditionlist) to the right of the Originalword. Each tokenword has an object data member that can be placed into a custom object.

Paramcontext is the parameter of this compilation execution.

finally , when we have the grammar file, the lexical parser, and the related handler, we can compile the process. In the main compiled code , we can see first we generate a statetable based on the grammar file, and then build our own Tokenreader,executehandler, Finally, the process of generating a executecontext with 3 and performing the compilation.

Post the link again:

Main code: http://www.oschina.net/code/snippet_573815_50916

About compiling Java code Related: http://www.oschina.net/code/snippet_573815_50891

Project Address: Http://git.oschina.net/notebook

One of the items used in this project is the Autogrammer,jar package under the published project.





Formally recommend one of my open source projects 2-Custom compilers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.