Note: To compile and run queries using the Scala backend requires the Scala compiler. Please refer to Installation for more details.
1. Compiling and running a query
DBToaster generates a JAR file for a query when using the -l scala and -c <file> switches:
The command above compiles the query to test.jar, which can be run as follows:
After processing all insertions and deletions, the final result is printed.
Note for Windows users: When running compiled Scala programs under Cygwin or Windows directly, one should use Windows-style classpath separators (i.e., semicolons). For instance:
2. Scala API Guide
In the previous example, we used the standard main function to test the query. However, to use the query in real applications, it has to be run from within an application.
The following listing shows a simple example application that communicates with the query class. The communication between the application and the query class is handled using akka.
This example first creates an ActorSystem and then launches the query actor. The events are sent to the query actor using TupleEvent messages with the following structure:
|op : TupleOp||TupleInsert for insertion, TupleDelete for deletion.|
|stream : String||Name of the stream as it appears in the SQL file.|
|data : List[Any]||The values of the tuple being inserted into/deleted from the stream.|
To retrieve the final result, an EndOfStream message is sent to the query actor. Alternatively the intermediate result of a query can be retrieved using a GetSnapshot message with the following structure:
|view : List[Int]||List of maps that a snapshot is taken of.|
Assuming that the example code has been saved as example.scala, it can be compiled with:
It can then be launched with the following command:
3. Generated Code Reference
The Scala code generator generates a single file containing an object, a base class containing trigger functions, and an actor class for the query. The base name is derived from the query name unless the -n
The code generated for the previous example looks as follows:
3.1. The query object
The query object contains the code used by the standalone binary to execute the query. Its execute method reads from the input streams specified in the query file and sends them to the query actor. The main method calls execute and prints the result when all tuples have been processed.
3.2. The query base class
The actual query processor lives in the query base class. For every stream R, there is an insertion onAddR and a deletion trigger onDelR. These trigger methods are responsible for updating the intermediate result. The map and singleton data structures at the top of the base class hold the intermediate result.
The onSystemReady trigger is responsible of loading static information (CREATE TABLE statements in the query file) before the actual processing begins.
3.3. The query actor
The query actor class dispatches events like tuple insertions and deletions to the base class using the receive method.
The EndOfStream message is sent from the event source when it is exhausted. The query actor replies to this message with the current processing statistics (processing time, number of tuples processed, number of tuples skipped) and one or multiple query results.
The GetSnapshot message can be used by an application to access the intermediate result. The query actor replies to this message with the current processing statistics and the results that the message asks for.
The whole process is guarded by a timeout. If the timeout is reached, the actor will stop to process tuples.